Merge pull request #1991 from sveltejs/gh-1952-b

hoist vars and lets that don't change
pull/1993/head
Rich Harris 6 years ago committed by GitHub
commit 5be480d7ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 <script> or template
if (node.declarations.every(d => d.init.type === 'Literal')) {
if (node.type === 'VariableDeclaration') {
if (node.declarations.every(d => d.init && d.init.type === 'Literal' && !this.mutable_props.has(d.id.name))) {
node.declarations.forEach(d => {
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) {
if_block.d(detach);
if (detach) {
detachNode(if_block_anchor);
}

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

@ -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 {

@ -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);
}
}

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

Loading…
Cancel
Save