Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hoist vars and lets that don't change #1991

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
41 changes: 41 additions & 0 deletions test/js/samples/hoisted-let/expected.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions test/js/samples/hoisted-let/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
let ANSWER = 42;
function get_answer() { return ANSWER; }
</script>

<b>{get_answer()}</b>
2 changes: 1 addition & 1 deletion test/js/samples/if-block-no-update/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function create_fragment($$, ctx) {

d(detach) {
if_block.d(detach);

if (detach) {
detachNode(if_block_anchor);
}
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-simple/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function create_fragment($$, ctx) {

d(detach) {
if (if_block) if_block.d(detach);

if (detach) {
detachNode(if_block_anchor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

Expand All @@ -25,7 +25,7 @@ function create_fragment($$, ctx) {

p(changed, ctx) {
if (changed.x) {
setData(text3, ctx.x);
setData(text3, x);
}
},

Expand All @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions test/js/samples/non-mutable-reference/expected.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,7 +8,7 @@ function create_fragment($$, ctx) {
c() {
h1 = createElement("h1");
text0 = createText("Hello ");
text1 = createText(ctx.name);
text1 = createText(name);
text2 = createText("!");
},

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

Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/use-elements-as-anchors/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function create_fragment($$, ctx) {
}

if (if_block4) if_block4.d(detach);

if (detach) {
detachNode(if_block4_anchor);
}
Expand Down