Skip to content

Commit

Permalink
shift injected declarations to the top (#5837)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored Jan 2, 2021
1 parent 2d5d6b0 commit a41c764
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ export default function dom(

body.push(b`
function ${definition}(${args}) {
${injected.map(name => b`let ${name};`)}
${rest}
${reactive_store_declarations}
Expand All @@ -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`
Expand Down
5 changes: 1 addition & 4 deletions src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ export default function ssr(
${reactive_store_values}
${injected.map(name => b`let ${name};`)}
${reactive_declarations}
$$rendered = ${literal};
Expand All @@ -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 }) => {
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/capture-inject-state/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test/runtime/samples/reactive-values-uninitialised/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: '<p>aca</p>'
};
12 changes: 12 additions & 0 deletions test/runtime/samples/reactive-values-uninitialised/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
export let a = 'a';
let b;
$: c = a;
function foo() {
b = c === 'a' ? 'b' : 'c';
}
foo();
</script>

<p>{a}{b}{c}</p>

0 comments on commit a41c764

Please sign in to comment.