in spread, distinguish never-updating and always-updating props (#4487)

pull/4498/head
Tan Li Hau 5 years ago committed by GitHub
parent 3a37de364b
commit d802c3b266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@
## Unreleased
* In `vars` array, correctly indicate whether `module` variables are `mutated` or `reassigned` ([#3215](https://github.com/sveltejs/svelte/issues/3215))
* Fix spread props not updating in certain situations ([#3521](https://github.com/sveltejs/svelte/issues/3521), [#4480](https://github.com/sveltejs/svelte/issues/4480))
* In `dev` mode, check for unknown props even if the component has no writable props ([#4323](https://github.com/sveltejs/svelte/issues/4323))
* Exclude global variables from `$capture_state` ([#4463](https://github.com/sveltejs/svelte/issues/4463))
* Fix bitmask overflow for slots ([#4481](https://github.com/sveltejs/svelte/issues/4481))

@ -224,7 +224,9 @@ export default class InlineComponentWrapper extends Wrapper {
const condition = dependencies.size > 0 && (dependencies.size !== all_dependencies.size)
? renderer.dirty(Array.from(dependencies))
: null;
const unchanged = dependencies.size === 0;
let change_object;
if (attr.is_spread) {
const value = attr.expression.manipulate(block);
initial_props.push(value);
@ -233,13 +235,20 @@ export default class InlineComponentWrapper extends Wrapper {
if (attr.expression.node.type !== 'ObjectExpression') {
value_object = x`@get_spread_object(${value})`;
}
changes.push(condition ? x`${condition} && ${value_object}` : value_object);
change_object = value_object;
} else {
const obj = x`{ ${name}: ${attr.get_value(block)} }`;
initial_props.push(obj);
changes.push(condition ? x`${condition} && ${obj}` : x`${levels}[${i}]`);
change_object = obj;
}
changes.push(
unchanged
? x`${levels}[${i}]`
: condition
? x`${condition} && ${change_object}`
: change_object
);
});
block.chunks.init.push(b`

@ -0,0 +1,13 @@
<script>
export let foo;
export let baz;
export let qux;
export let quux;
export let selected;
</script>
<p>foo: {foo}</p>
<p>baz: {baz} ({typeof baz})</p>
<p>qux: {qux}</p>
<p>quux: {quux}</p>
<p>selected: {selected}</p>

@ -0,0 +1,76 @@
export default {
props: {
list: [{
foo: 'lol',
baz: 40 + 2,
qux: 5,
quux: 'core'
}, {
foo: 'lolzz',
baz: 50 + 2,
qux: 1,
quux: 'quuxx'
}],
},
html: `
<div>
<p>foo: lol</p>
<p>baz: 42 (number)</p>
<p>qux: 0</p>
<p>quux: core</p>
<p>selected: true</p>
<p>foo: lolzz</p>
<p>baz: 52 (number)</p>
<p>qux: 0</p>
<p>quux: quuxx</p>
<p>selected: false</p>
</div>
`,
test({ assert, component, target }) {
component.list = [{
foo: 'lol',
baz: 40 + 3,
qux: 8,
quux: 'heart'
}, {
foo: 'lolzz',
baz: 50 + 3,
qux: 8,
quux: 'heartxx'
}];
assert.htmlEqual(target.innerHTML, `
<div>
<p>foo: lol</p>
<p>baz: 43 (number)</p>
<p>qux: 0</p>
<p>quux: heart</p>
<p>selected: true</p>
<p>foo: lolzz</p>
<p>baz: 53 (number)</p>
<p>qux: 0</p>
<p>quux: heartxx</p>
<p>selected: false</p>
</div>
`);
component.qux = 1;
assert.htmlEqual(target.innerHTML, `
<div>
<p>foo: lol</p>
<p>baz: 43 (number)</p>
<p>qux: 1</p>
<p>quux: heart</p>
<p>selected: false</p>
<p>foo: lolzz</p>
<p>baz: 53 (number)</p>
<p>qux: 1</p>
<p>quux: heartxx</p>
<p>selected: true</p>
</div>
`);
}
};

@ -0,0 +1,12 @@
<script>
import Widget from './Widget.svelte';
export let list;
export let qux = 0;
</script>
<div>
{#each list as item, index (item.foo)}
<Widget {...item} qux={qux} selected={qux === index} />
{/each}
</div>
Loading…
Cancel
Save