From 4c458c4787688c8eaeb8579565c4a88b1531bbed Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Wed, 30 Dec 2020 18:21:09 +0800 Subject: [PATCH 1/3] shift initialisation to the top --- src/compiler/compile/render_dom/index.ts | 4 ++-- src/compiler/compile/render_ssr/index.ts | 5 +---- test/js/samples/capture-inject-state/expected.js | 4 ++-- .../unreferenced-state-not-invalidated/expected.js | 5 ++--- .../samples/reactive-values-uninitialised/_config.js | 3 +++ .../reactive-values-uninitialised/main.svelte | 12 ++++++++++++ 6 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 test/runtime/samples/reactive-values-uninitialised/_config.js create mode 100644 test/runtime/samples/reactive-values-uninitialised/main.svelte diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index fe1ee368fcd8..91ad6f9aedab 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -414,6 +414,8 @@ export default function dom( body.push(b` function ${definition}(${args}) { + ${injected.map(name => b`let ${name};`)} + ${rest} ${reactive_store_declarations} @@ -440,8 +442,6 @@ export default function dom( ${inject_state && b`$$self.$inject_state = ${inject_state};`} - ${injected.map(name => b`let ${name};`)} - ${/* before reactive declarations */ props_inject} ${reactive_declarations.length > 0 && b` diff --git a/src/compiler/compile/render_ssr/index.ts b/src/compiler/compile/render_ssr/index.ts index ff45abd78b05..a156a4fe6a4b 100644 --- a/src/compiler/compile/render_ssr/index.ts +++ b/src/compiler/compile/render_ssr/index.ts @@ -101,8 +101,6 @@ export default function ssr( ${reactive_store_values} - ${injected.map(name => b`let ${name};`)} - ${reactive_declarations} $$rendered = ${literal}; @@ -113,13 +111,12 @@ export default function ssr( : b` ${reactive_store_values} - ${injected.map(name => b`let ${name};`)} - ${reactive_declarations} return ${literal};`; const blocks = [ + ...injected.map(name => b`let ${name};`), rest, slots, ...reactive_stores.map(({ name }) => { diff --git a/test/js/samples/capture-inject-state/expected.js b/test/js/samples/capture-inject-state/expected.js index 6a0351ae449e..854e0b32652c 100644 --- a/test/js/samples/capture-inject-state/expected.js +++ b/test/js/samples/capture-inject-state/expected.js @@ -98,6 +98,8 @@ let shadowedByModule; const priv = "priv"; function instance($$self, $$props, $$invalidate) { + let computed; + let $prop, $$unsubscribe_prop = noop, $$subscribe_prop = () => ($$unsubscribe_prop(), $$unsubscribe_prop = subscribe(prop, $$value => $$invalidate(2, $prop = $$value)), prop); @@ -145,8 +147,6 @@ function instance($$self, $$props, $$invalidate) { if ("computed" in $$props) computed = $$props.computed; }; - let computed; - if ($$props && "$$inject" in $$props) { $$self.$inject_state($$props.$$inject); } diff --git a/test/js/samples/unreferenced-state-not-invalidated/expected.js b/test/js/samples/unreferenced-state-not-invalidated/expected.js index 599d18f11494..53044c7ed6d8 100644 --- a/test/js/samples/unreferenced-state-not-invalidated/expected.js +++ b/test/js/samples/unreferenced-state-not-invalidated/expected.js @@ -39,6 +39,8 @@ function create_fragment(ctx) { } function instance($$self, $$props, $$invalidate) { + let x; + let y; let a = 1, b = 2, c = 3; onMount(() => { @@ -54,9 +56,6 @@ function instance($$self, $$props, $$invalidate) { return () => clearInterval(interval); }); - let x; - let y; - $$self.$$.update = () => { if ($$self.$$.dirty & /*b*/ 2) { $: $$invalidate(0, y = b * 2); diff --git a/test/runtime/samples/reactive-values-uninitialised/_config.js b/test/runtime/samples/reactive-values-uninitialised/_config.js new file mode 100644 index 000000000000..4b27d710742a --- /dev/null +++ b/test/runtime/samples/reactive-values-uninitialised/_config.js @@ -0,0 +1,3 @@ +export default { + html: `

aca

` +}; diff --git a/test/runtime/samples/reactive-values-uninitialised/main.svelte b/test/runtime/samples/reactive-values-uninitialised/main.svelte new file mode 100644 index 000000000000..2cacc411d236 --- /dev/null +++ b/test/runtime/samples/reactive-values-uninitialised/main.svelte @@ -0,0 +1,12 @@ + + +

{a}{b}{c}

From 20d942b1e11b266e9d3b3ce87d6d6d10437ec67d Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Thu, 31 Dec 2020 08:22:14 +0800 Subject: [PATCH 2/3] fix lint --- test/runtime/samples/reactive-values-uninitialised/_config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtime/samples/reactive-values-uninitialised/_config.js b/test/runtime/samples/reactive-values-uninitialised/_config.js index 4b27d710742a..ef72d7065217 100644 --- a/test/runtime/samples/reactive-values-uninitialised/_config.js +++ b/test/runtime/samples/reactive-values-uninitialised/_config.js @@ -1,3 +1,3 @@ export default { - html: `

aca

` + html: '

aca

' }; From ff1210bed6baa03c38a7dcf5044830bb7625ab45 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 1 Jan 2021 19:55:21 -0500 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 961502491aa8..58472baf5734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* Fix location of automatically declared reactive variables ([#5749](https://github.com/sveltejs/svelte/issues/5749)) * Warn when using `className` or `htmlFor` attributes ([#5777](https://github.com/sveltejs/svelte/issues/5777)) * Fix checkbox `bind:group` in nested `{#each}` contexts ([#5811](https://github.com/sveltejs/svelte/issues/5811)) * Add graphics roles as known ARIA roles ([#5822](https://github.com/sveltejs/svelte/pull/5822))