diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 52006e4d8a..4895c97748 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -248,6 +248,11 @@ export default function dom( code.appendLeft(node.end, `${code.original[node.end - 1] === ';' ? '' : ';'} ${insert};`); } + pending_assignments = new Set(); + } else if (parent && parent.type !== 'ForStatement' && node.type === 'VariableDeclaration') { + const insert = Array.from(pending_assignments).map(name => component.invalidate(name)).join('; '); + + code.appendLeft(node.end, `${code.original[node.end - 1] === ';' ? '' : ';'} ${insert};`); pending_assignments = new Set(); } } diff --git a/test/js/samples/declaration-assignment-emersion/expected.js b/test/js/samples/declaration-assignment-emersion/expected.js new file mode 100644 index 0000000000..f3ad073ec6 --- /dev/null +++ b/test/js/samples/declaration-assignment-emersion/expected.js @@ -0,0 +1,72 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent, + append, + detach, + element, + init, + insert, + noop, + safe_not_equal, + set_data, + space, + text +} from "svelte/internal"; + +function create_fragment(ctx) { + var h1, t0, t1, t2; + + return { + c() { + h1 = element("h1"); + t0 = text(ctx.foo); + t1 = space(); + t2 = text(ctx.eid); + }, + + m(target, anchor) { + insert(target, h1, anchor); + append(h1, t0); + append(h1, t1); + append(h1, t2); + }, + + p(changed, ctx) { + if (changed.foo) { + set_data(t0, ctx.foo); + } + + if (changed.eid) { + set_data(t2, ctx.eid); + } + }, + + i: noop, + o: noop, + + d(detaching) { + if (detaching) { + detach(h1); + } + } + }; +} + +function instance($$self, $$props, $$invalidate) { + let eid = 1; + let foo; + let employees = [ + {id: eid = (foo = 2), name: 'xxx'}, + ]; $$invalidate('foo', foo); $$invalidate('eid', eid); + + return { eid, foo }; +} + +class Component extends SvelteComponent { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, []); + } +} + +export default Component; diff --git a/test/js/samples/declaration-assignment-emersion/input.svelte b/test/js/samples/declaration-assignment-emersion/input.svelte new file mode 100644 index 0000000000..dec1a490af --- /dev/null +++ b/test/js/samples/declaration-assignment-emersion/input.svelte @@ -0,0 +1,9 @@ + + +

{foo} {eid}

diff --git a/test/js/samples/declaration-assignment-for/expected.js b/test/js/samples/declaration-assignment-for/expected.js new file mode 100644 index 0000000000..11719d7cb6 --- /dev/null +++ b/test/js/samples/declaration-assignment-for/expected.js @@ -0,0 +1,68 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent, + append, + detach, + element, + init, + insert, + noop, + safe_not_equal, + set_data, + space, + text +} from "svelte/internal"; + +function create_fragment(ctx) { + var h1, t0, t1, t2; + + return { + c() { + h1 = element("h1"); + t0 = text(ctx.foo1); + t1 = space(); + t2 = text(ctx.foo2); + }, + + m(target, anchor) { + insert(target, h1, anchor); + append(h1, t0); + append(h1, t1); + append(h1, t2); + }, + + p(changed, ctx) { + if (changed.foo1) { + set_data(t0, ctx.foo1); + } + }, + + i: noop, + o: noop, + + d(detaching) { + if (detaching) { + detach(h1); + } + } + }; +} + +function instance($$self, $$props, $$invalidate) { + let foo1; + let foo2; + for (let bar = (foo1 = 0); bar < 5; bar += 1) { + $$invalidate('foo1', foo1 = foo2); $$invalidate('foo1', foo1); + } + + return { foo1, foo2 }; +} + +class Component extends SvelteComponent { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, []); + } +} + +export default Component; diff --git a/test/js/samples/declaration-assignment-for/input.svelte b/test/js/samples/declaration-assignment-for/input.svelte new file mode 100644 index 0000000000..273489c966 --- /dev/null +++ b/test/js/samples/declaration-assignment-for/input.svelte @@ -0,0 +1,9 @@ + + +

{foo1} {foo2}

diff --git a/test/js/samples/declaration-assignment/expected.js b/test/js/samples/declaration-assignment/expected.js new file mode 100644 index 0000000000..61e649bad0 --- /dev/null +++ b/test/js/samples/declaration-assignment/expected.js @@ -0,0 +1,65 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent, + append, + detach, + element, + init, + insert, + noop, + safe_not_equal, + set_data, + space, + text +} from "svelte/internal"; + +function create_fragment(ctx) { + var h1, t0, t1, t2; + + return { + c() { + h1 = element("h1"); + t0 = text(ctx.foo); + t1 = space(); + t2 = text(ctx.bar); + }, + + m(target, anchor) { + insert(target, h1, anchor); + append(h1, t0); + append(h1, t1); + append(h1, t2); + }, + + p(changed, ctx) { + if (changed.foo) { + set_data(t0, ctx.foo); + } + }, + + i: noop, + o: noop, + + d(detaching) { + if (detaching) { + detach(h1); + } + } + }; +} + +function instance($$self, $$props, $$invalidate) { + let foo; + let bar = (foo = 1); $$invalidate('foo', foo); + + return { foo, bar }; +} + +class Component extends SvelteComponent { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, []); + } +} + +export default Component; diff --git a/test/js/samples/declaration-assignment/input.svelte b/test/js/samples/declaration-assignment/input.svelte new file mode 100644 index 0000000000..8aa05964dd --- /dev/null +++ b/test/js/samples/declaration-assignment/input.svelte @@ -0,0 +1,6 @@ + + +

{foo} {bar}

diff --git a/test/js/samples/declaration-no-assignment/expected.js b/test/js/samples/declaration-no-assignment/expected.js new file mode 100644 index 0000000000..f96cf01829 --- /dev/null +++ b/test/js/samples/declaration-no-assignment/expected.js @@ -0,0 +1,70 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent, + append, + detach, + element, + init, + insert, + noop, + safe_not_equal, + set_data, + space, + text +} from "svelte/internal"; + +function create_fragment(ctx) { + var h1, t0, t1, t2; + + return { + c() { + h1 = element("h1"); + t0 = text(ctx.foo); + t1 = space(); + t2 = text(ctx.bar); + }, + + m(target, anchor) { + insert(target, h1, anchor); + append(h1, t0); + append(h1, t1); + append(h1, t2); + }, + + p(changed, ctx) { + if (changed.foo) { + set_data(t0, ctx.foo); + } + + if (changed.bar) { + set_data(t2, ctx.bar); + } + }, + + i: noop, + o: noop, + + d(detaching) { + if (detaching) { + detach(h1); + } + } + }; +} + +function instance($$self, $$props, $$invalidate) { + let foo; + let bar; + $$invalidate('bar', bar = (foo = 1)); $$invalidate('foo', foo); + + return { foo, bar }; +} + +class Component extends SvelteComponent { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, []); + } +} + +export default Component; diff --git a/test/js/samples/declaration-no-assignment/input.svelte b/test/js/samples/declaration-no-assignment/input.svelte new file mode 100644 index 0000000000..2f83d54a56 --- /dev/null +++ b/test/js/samples/declaration-no-assignment/input.svelte @@ -0,0 +1,7 @@ + + +

{foo} {bar}