-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: object destructuring picks up computed properties (#8386)
- Loading branch information
Showing
19 changed files
with
330 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 24 additions & 15 deletions
39
src/compiler/compile/render_dom/wrappers/shared/add_const_tags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
import ConstTag from '../../../nodes/ConstTag'; | ||
import Block from '../../Block'; | ||
import { b, x } from 'code-red'; | ||
import { b, Node, x } from 'code-red'; | ||
import Renderer from '../../Renderer'; | ||
import Expression from '../../../nodes/shared/Expression'; | ||
|
||
export function add_const_tags(block: Block, const_tags: ConstTag[], ctx: string) { | ||
const const_tags_props = []; | ||
const_tags.forEach((const_tag, i) => { | ||
const name = `#constants_${i}`; | ||
const_tags_props.push(b`const ${name} = ${const_tag.expression.manipulate(block, ctx)}`); | ||
const_tag.contexts.forEach(context => { | ||
const_tags_props.push(b`${ctx}[${block.renderer.context_lookup.get(context.key.name).index}] = ${context.default_modifier(context.modifier({ type: 'Identifier', name }), name => block.renderer.context_lookup.has(name) ? x`${ctx}[${block.renderer.context_lookup.get(name).index}]` : { type: 'Identifier', name })};`); | ||
}); | ||
}); | ||
return const_tags_props; | ||
const const_tags_props = []; | ||
const_tags.forEach((const_tag, i) => { | ||
const name = `#constants_${i}`; | ||
const_tags_props.push(b`const ${name} = ${const_tag.expression.manipulate(block, ctx)}`); | ||
const to_ctx = (name: string) => block.renderer.context_lookup.has(name) ? x`${ctx}[${block.renderer.context_lookup.get(name).index}]` : { type: 'Identifier', name } as Node; | ||
|
||
const_tag.contexts.forEach(context => { | ||
if (context.type === 'DestructuredVariable') { | ||
const_tags_props.push(b`${ctx}[${block.renderer.context_lookup.get(context.key.name).index}] = ${context.default_modifier(context.modifier({ type: 'Identifier', name }), to_ctx)}`); | ||
} else { | ||
const expression = new Expression(block.renderer.component, const_tag, const_tag.scope, context.key); | ||
const_tags_props.push(b`const ${context.property_name} = ${expression.manipulate(block, ctx)}`); | ||
} | ||
}); | ||
}); | ||
return const_tags_props; | ||
} | ||
|
||
export function add_const_tags_context(renderer: Renderer, const_tags: ConstTag[]) { | ||
const_tags.forEach(const_tag => { | ||
const_tag.contexts.forEach(context => { | ||
renderer.add_to_context(context.key.name, true); | ||
}); | ||
}); | ||
const_tags.forEach(const_tag => { | ||
const_tag.contexts.forEach(context => { | ||
if (context.type !== 'DestructuredVariable') return; | ||
renderer.add_to_context(context.key.name, true); | ||
}); | ||
}); | ||
} |
32 changes: 32 additions & 0 deletions
32
test/runtime/samples/await-then-destruct-computed-props/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export default { | ||
async test({ assert, component, target }) { | ||
await Promise.resolve(); | ||
|
||
assert.htmlEqual( | ||
target.innerHTML, | ||
` | ||
<p>propA: 3</p> | ||
<p>propB: 7</p> | ||
<p>num: 3</p> | ||
<p>rest: {"prop3":{"prop9":9,"prop10":10}}</p> | ||
<p>propZ: 5</p> | ||
<p>propY: 6</p> | ||
<p>rest: {"propX":7,"propW":8}</p> | ||
` | ||
); | ||
|
||
await (component.object = Promise.resolve({ prop1: 'one', prop2: 'two', prop3: { prop7: 'seven' }, prop4: { prop10: 'ten' }})); | ||
assert.htmlEqual( | ||
target.innerHTML, | ||
` | ||
<p>propA: seven</p> | ||
<p>propB: ten</p> | ||
<p>num: 5</p> | ||
<p>rest: {"prop1":"one","prop2":"two"}</p> | ||
<p>propZ: 5</p> | ||
<p>propY: 6</p> | ||
<p>rest: {"propX":7,"propW":8}</p> | ||
` | ||
); | ||
} | ||
}; |
23 changes: 23 additions & 0 deletions
23
test/runtime/samples/await-then-destruct-computed-props/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script> | ||
export let object = Promise.resolve({ prop1: { prop4: 2, prop5: 3 }, prop2: { prop6: 5, prop7: 6, prop8: 7 }, prop3: { prop9: 9, prop10: 10 } }); | ||
const objectReject = Promise.reject({ propZ: 5, propY: 6, propX: 7, propW: 8 }); | ||
let num = 1; | ||
const prop = 'prop'; | ||
</script> | ||
|
||
{#await object then { [`prop${num++}`]: { [`prop${num + 3}`]: propA }, [`prop${num++}`]: { [`prop${num + 5}`]: propB }, ...rest }} | ||
<p>propA: {propA}</p> | ||
<p>propB: {propB}</p> | ||
<p>num: {num}</p> | ||
<p>rest: {JSON.stringify(rest)}</p> | ||
{/await} | ||
|
||
{#await objectReject then value} | ||
resolved | ||
{:catch { [`${prop}Z`]: propZ, [`${prop}Y`]: propY, ...rest }} | ||
<p>propZ: {propZ}</p> | ||
<p>propY: {propY}</p> | ||
<p>rest: {JSON.stringify(rest)}</p> | ||
{/await} | ||
|
Oops, something went wrong.