From 81d964751738bd0e6484a71a564a467f7a5b188c Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Fri, 18 Jan 2019 21:40:08 -0500 Subject: [PATCH] hoist vars and lets - closes #1952 --- src/compile/Component.ts | 8 ++-- test/js/samples/hoisted-let/expected.js | 41 +++++++++++++++++++ test/js/samples/hoisted-let/input.html | 6 +++ .../js/samples/if-block-no-update/expected.js | 2 +- test/js/samples/if-block-simple/expected.js | 2 +- .../expected.js | 9 ++-- .../samples/non-mutable-reference/expected.js | 12 ++---- .../use-elements-as-anchors/expected.js | 2 +- 8 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 test/js/samples/hoisted-let/expected.js create mode 100644 test/js/samples/hoisted-let/input.html diff --git a/src/compile/Component.ts b/src/compile/Component.ts index ca3ed818cb45..b07af70a646a 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -750,18 +750,18 @@ export default class Component { } hoist_instance_declarations() { - // we can safely hoist `const` declarations that are + // we can safely hoist variable declarations that are // initialised to literals, and functions that don't // reference instance variables other than other // hoistable functions. TODO others? - const { hoistable_names, hoistable_nodes, imported_declarations } = this; + const { hoistable_names, hoistable_nodes, imported_declarations, instance_scope: scope } = this; const top_level_function_declarations = new Map(); this.instance_script.content.body.forEach(node => { - if (node.kind === 'const') { // TODO or let or var, if never reassigned in + +{get_answer()} \ No newline at end of file diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index 9c1db56bd7ff..d8763aed51b1 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -83,7 +83,7 @@ function create_fragment($$, ctx) { d(detach) { if_block.d(detach); - + if (detach) { detachNode(if_block_anchor); } diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index 43c99c1d5336..bd6528b11c90 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -57,7 +57,7 @@ function create_fragment($$, ctx) { d(detach) { if (if_block) if_block.d(detach); - + if (detach) { detachNode(if_block_anchor); } diff --git a/test/js/samples/instrumentation-template-if-no-block/expected.js b/test/js/samples/instrumentation-template-if-no-block/expected.js index d403b12fb124..dd1f72bc0428 100644 --- a/test/js/samples/instrumentation-template-if-no-block/expected.js +++ b/test/js/samples/instrumentation-template-if-no-block/expected.js @@ -11,7 +11,7 @@ function create_fragment($$, ctx) { text1 = createText("\n\n"); p = createElement("p"); text2 = createText("x: "); - text3 = createText(ctx.x); + text3 = createText(x); dispose = addListener(button, "click", ctx.click_handler); }, @@ -25,7 +25,7 @@ function create_fragment($$, ctx) { p(changed, ctx) { if (changed.x) { - setData(text3, ctx.x); + setData(text3, x); } }, @@ -44,14 +44,15 @@ function create_fragment($$, ctx) { }; } +let x = 0; + function instance($$self, $$props, $$invalidate) { - let x = 0; function click_handler() { if (true) { x += 1; $$invalidate('x', x); } } - return { x, click_handler }; + return { click_handler }; } class SvelteComponent extends SvelteComponent_1 { diff --git a/test/js/samples/non-mutable-reference/expected.js b/test/js/samples/non-mutable-reference/expected.js index 7e83850226b2..b7f6dc60a9c5 100644 --- a/test/js/samples/non-mutable-reference/expected.js +++ b/test/js/samples/non-mutable-reference/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { SvelteComponent as SvelteComponent_1, append, createElement, createText, detachNode, init, insert, noop, safe_not_equal } from "svelte/internal"; +import { SvelteComponent as SvelteComponent_1, append, createElement, createText, detachNode, identity, init, insert, noop, safe_not_equal } from "svelte/internal"; function create_fragment($$, ctx) { var h1, text0, text1, text2; @@ -8,7 +8,7 @@ function create_fragment($$, ctx) { c() { h1 = createElement("h1"); text0 = createText("Hello "); - text1 = createText(ctx.name); + text1 = createText(name); text2 = createText("!"); }, @@ -31,16 +31,12 @@ function create_fragment($$, ctx) { }; } -function instance($$self) { - let name = 'world'; - - return { name }; -} +let name = 'world'; class SvelteComponent extends SvelteComponent_1 { constructor(options) { super(); - init(this, options, instance, create_fragment, safe_not_equal); + init(this, options, identity, create_fragment, safe_not_equal); } } diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index e42ab8a35bf3..eda6f1f721ed 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -238,7 +238,7 @@ function create_fragment($$, ctx) { } if (if_block4) if_block4.d(detach); - + if (detach) { detachNode(if_block4_anchor); }