hoist vars and lets - closes #1952

pull/1991/head
Richard Harris 6 years ago
parent 4d262c4d96
commit 81d9647517

@ -750,18 +750,18 @@ export default class Component {
} }
hoist_instance_declarations() { 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 // initialised to literals, and functions that don't
// reference instance variables other than other // reference instance variables other than other
// hoistable functions. TODO others? // 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(); const top_level_function_declarations = new Map();
this.instance_script.content.body.forEach(node => { this.instance_script.content.body.forEach(node => {
if (node.kind === 'const') { // TODO or let or var, if never reassigned in <script> or template if (node.type === 'VariableDeclaration') {
if (node.declarations.every(d => d.init.type === 'Literal')) { if (node.declarations.every(d => d.init && d.init.type === 'Literal' && !this.mutable_props.has(d.id.name))) {
node.declarations.forEach(d => { node.declarations.forEach(d => {
hoistable_names.add(d.id.name); hoistable_names.add(d.id.name);
}); });

@ -0,0 +1,41 @@
/* generated by Svelte vX.Y.Z */
import { SvelteComponent as SvelteComponent_1, append, createElement, createText, detachNode, identity, init, insert, noop, safe_not_equal } from "svelte/internal";
function create_fragment($$, ctx) {
var b, text_value = get_answer(), text;
return {
c() {
b = createElement("b");
text = createText(text_value);
},
m(target, anchor) {
insert(target, b, anchor);
append(b, text);
},
p: noop,
i: noop,
o: noop,
d(detach) {
if (detach) {
detachNode(b);
}
}
};
}
let ANSWER = 42;
function get_answer() { return ANSWER; }
class SvelteComponent extends SvelteComponent_1 {
constructor(options) {
super();
init(this, options, identity, create_fragment, safe_not_equal);
}
}
export default SvelteComponent;

@ -0,0 +1,6 @@
<script>
let ANSWER = 42;
function get_answer() { return ANSWER; }
</script>
<b>{get_answer()}</b>

@ -83,7 +83,7 @@ function create_fragment($$, ctx) {
d(detach) { d(detach) {
if_block.d(detach); if_block.d(detach);
if (detach) { if (detach) {
detachNode(if_block_anchor); detachNode(if_block_anchor);
} }

@ -57,7 +57,7 @@ function create_fragment($$, ctx) {
d(detach) { d(detach) {
if (if_block) if_block.d(detach); if (if_block) if_block.d(detach);
if (detach) { if (detach) {
detachNode(if_block_anchor); detachNode(if_block_anchor);
} }

@ -11,7 +11,7 @@ function create_fragment($$, ctx) {
text1 = createText("\n\n"); text1 = createText("\n\n");
p = createElement("p"); p = createElement("p");
text2 = createText("x: "); text2 = createText("x: ");
text3 = createText(ctx.x); text3 = createText(x);
dispose = addListener(button, "click", ctx.click_handler); dispose = addListener(button, "click", ctx.click_handler);
}, },
@ -25,7 +25,7 @@ function create_fragment($$, ctx) {
p(changed, ctx) { p(changed, ctx) {
if (changed.x) { 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) { function instance($$self, $$props, $$invalidate) {
let x = 0;
function click_handler() { function click_handler() {
if (true) { x += 1; $$invalidate('x', x); } if (true) { x += 1; $$invalidate('x', x); }
} }
return { x, click_handler }; return { click_handler };
} }
class SvelteComponent extends SvelteComponent_1 { class SvelteComponent extends SvelteComponent_1 {

@ -1,5 +1,5 @@
/* generated by Svelte vX.Y.Z */ /* 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) { function create_fragment($$, ctx) {
var h1, text0, text1, text2; var h1, text0, text1, text2;
@ -8,7 +8,7 @@ function create_fragment($$, ctx) {
c() { c() {
h1 = createElement("h1"); h1 = createElement("h1");
text0 = createText("Hello "); text0 = createText("Hello ");
text1 = createText(ctx.name); text1 = createText(name);
text2 = createText("!"); text2 = createText("!");
}, },
@ -31,16 +31,12 @@ function create_fragment($$, ctx) {
}; };
} }
function instance($$self) { let name = 'world';
let name = 'world';
return { name };
}
class SvelteComponent extends SvelteComponent_1 { class SvelteComponent extends SvelteComponent_1 {
constructor(options) { constructor(options) {
super(); super();
init(this, options, instance, create_fragment, safe_not_equal); init(this, options, identity, create_fragment, safe_not_equal);
} }
} }

@ -238,7 +238,7 @@ function create_fragment($$, ctx) {
} }
if (if_block4) if_block4.d(detach); if (if_block4) if_block4.d(detach);
if (detach) { if (detach) {
detachNode(if_block4_anchor); detachNode(if_block4_anchor);
} }

Loading…
Cancel
Save