From 0d9d8dccc80f9a8eaf47b0137eca7b9513452d67 Mon Sep 17 00:00:00 2001 From: daszgfz Date: Tue, 26 Nov 2019 17:00:20 -0500 Subject: [PATCH] try 2 --- src/compiler/compile/Component.ts | 42 +++++++++---------- src/compiler/interfaces.ts | 1 + .../action-custom-event-handler/expected.js | 14 +++---- .../samples/dont-invalidate-this/expected.js | 12 ++++-- .../each-block-keyed-animated/expected.js | 40 +++++++++--------- test/js/samples/event-modifiers/expected.js | 24 ++++++----- test/js/samples/hoisted-const/expected.js | 12 ++++-- test/js/samples/hoisted-let/expected.js | 12 ++++-- test/js/samples/setup-method/expected.js | 10 ++--- .../samples/ssr-no-oncreate-etc/expected.js | 16 +++---- test/js/samples/transition-local/expected.js | 12 +++--- 11 files changed, 106 insertions(+), 89 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 63945cf9b0..7a52e78cd8 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -63,8 +63,6 @@ export default class Component { imports: ImportDeclaration[] = []; - function_args: Set = new Set(); - hoistable_nodes: Set = new Set(); node_for_declaration: Map = new Map(); partly_hoisted: Array<(Node | Node[])> = []; @@ -701,7 +699,7 @@ export default class Component { const component = this; const { content } = script; - const { function_args, instance_scope, instance_scope_map: map } = this; + const { instance_scope, instance_scope_map: map } = this; let scope = instance_scope; @@ -718,14 +716,6 @@ export default class Component { scope = map.get(node); } - if (node.type === 'CallExpression') { - node.arguments.forEach(arg => { - if (arg.type == 'Identifier') { - function_args.add(arg.name); - } - }); - } - if (node.type === 'ImportDeclaration') { component.extract_imports(node); // TODO: to use actual remove @@ -806,6 +796,17 @@ export default class Component { scope = map.get(node); } + // @ts-ignore + if (node.type === 'ExpressionStatement' && node.expression && node.expression.arguments) { + // @ts-ignore + node.expression.arguments.forEach(({ name }) => { + if (scope.find_owner(name) === instance_scope) { + const variable = component.var_lookup.get(name); + variable['argument'] = true; + } + }); + } + if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') { const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument; const names = extract_names(assignee); @@ -1064,7 +1065,6 @@ export default class Component { const instance_scope = this.instance_scope; let scope = this.instance_scope; const map = this.instance_scope_map; - const function_args = this.function_args; let hoistable = true; @@ -1082,10 +1082,8 @@ export default class Component { if (is_reference(node as Node, parent as Node)) { const { name } = flatten_reference(node); const owner = scope.find_owner(name); - - if (function_args.has(name)) { - hoistable = false; - } else if (injected_reactive_declaration_vars.has(name)) { + + if (injected_reactive_declaration_vars.has(name)) { hoistable = false; } else if (name[0] === '$' && !owner) { hoistable = false; @@ -1138,12 +1136,14 @@ export default class Component { for (const [name, node] of top_level_function_declarations) { if (is_hoistable(node)) { const variable = this.var_lookup.get(name); - variable.hoistable = true; - hoistable_nodes.add(node); + if ('argument' in variable && !variable.argument) { + variable.hoistable = true; + hoistable_nodes.add(node); - const i = body.indexOf(node); - body.splice(i, 1); - this.fully_hoisted.push(node); + const i = body.indexOf(node); + body.splice(i, 1); + this.fully_hoisted.push(node); + } } } } diff --git a/src/compiler/interfaces.ts b/src/compiler/interfaces.ts index e7362b9313..78ff9fa449 100644 --- a/src/compiler/interfaces.ts +++ b/src/compiler/interfaces.ts @@ -160,4 +160,5 @@ export interface Var { hoistable?: boolean; subscribable?: boolean; is_reactive_dependency?: boolean; + argument?: boolean; } \ No newline at end of file diff --git a/test/js/samples/action-custom-event-handler/expected.js b/test/js/samples/action-custom-event-handler/expected.js index 031bb3fcb9..1754599143 100644 --- a/test/js/samples/action-custom-event-handler/expected.js +++ b/test/js/samples/action-custom-event-handler/expected.js @@ -21,10 +21,10 @@ function create_fragment(ctx) { }, m(target, anchor) { insert(target, button, anchor); - foo_action = foo.call(null, button, /*foo_function*/ ctx[2]) || ({}); + foo_action = /*foo*/ ctx[2].call(null, button, /*foo_function*/ ctx[3]) || ({}); }, p(ctx, [dirty]) { - if (is_function(foo_action.update) && dirty & /*bar*/ 1) foo_action.update.call(null, /*foo_function*/ ctx[2]); + if (is_function(foo_action.update) && dirty & /*bar*/ 1) foo_action.update.call(null, /*foo_function*/ ctx[3]); }, i: noop, o: noop, @@ -35,10 +35,6 @@ function create_fragment(ctx) { }; } -function foo(node, callback) { - -} - function instance($$self, $$props, $$invalidate) { let { bar } = $$props; @@ -46,13 +42,17 @@ function instance($$self, $$props, $$invalidate) { console.log(bar); } + function foo(node, callback) { + + } + const foo_function = () => handleFoo(bar); $$self.$set = $$props => { if ("bar" in $$props) $$invalidate(0, bar = $$props.bar); }; - return [bar, handleFoo, foo_function]; + return [bar, handleFoo, foo, foo_function]; } class Component extends SvelteComponent { diff --git a/test/js/samples/dont-invalidate-this/expected.js b/test/js/samples/dont-invalidate-this/expected.js index 98f638dfcf..a9776e2b36 100644 --- a/test/js/samples/dont-invalidate-this/expected.js +++ b/test/js/samples/dont-invalidate-this/expected.js @@ -17,7 +17,7 @@ function create_fragment(ctx) { return { c() { input = element("input"); - dispose = listen(input, "input", make_uppercase); + dispose = listen(input, "input", /*make_uppercase*/ ctx[0]); }, m(target, anchor) { insert(target, input, anchor); @@ -32,14 +32,18 @@ function create_fragment(ctx) { }; } -function make_uppercase() { - this.value = this.value.toUpperCase(); +function instance($$self) { + function make_uppercase() { + this.value = this.value.toUpperCase(); + } + + return [make_uppercase]; } class Component extends SvelteComponent { constructor(options) { super(); - init(this, options, null, create_fragment, safe_not_equal, {}); + init(this, options, instance, create_fragment, safe_not_equal, {}); } } diff --git a/test/js/samples/each-block-keyed-animated/expected.js b/test/js/samples/each-block-keyed-animated/expected.js index 3b697d6860..4218b141de 100644 --- a/test/js/samples/each-block-keyed-animated/expected.js +++ b/test/js/samples/each-block-keyed-animated/expected.js @@ -19,14 +19,14 @@ import { function get_each_context(ctx, list, i) { const child_ctx = ctx.slice(); - child_ctx[1] = list[i]; + child_ctx[2] = list[i]; return child_ctx; } // (19:0) {#each things as thing (thing.id)} function create_each_block(key_1, ctx) { let div; - let t_value = /*thing*/ ctx[1].name + ""; + let t_value = /*thing*/ ctx[2].name + ""; let t; let rect; let stop_animation = noop; @@ -44,7 +44,7 @@ function create_each_block(key_1, ctx) { append(div, t); }, p(ctx, dirty) { - if (dirty & /*things*/ 1 && t_value !== (t_value = /*thing*/ ctx[1].name + "")) set_data(t, t_value); + if (dirty & /*things*/ 1 && t_value !== (t_value = /*thing*/ ctx[2].name + "")) set_data(t, t_value); }, r() { rect = div.getBoundingClientRect(); @@ -55,7 +55,7 @@ function create_each_block(key_1, ctx) { }, a() { stop_animation(); - stop_animation = create_animation(div, rect, foo, {}); + stop_animation = create_animation(div, rect, /*foo*/ ctx[1], {}); }, d(detaching) { if (detaching) detach(div); @@ -68,7 +68,7 @@ function create_fragment(ctx) { let each_1_lookup = new Map(); let each_1_anchor; let each_value = /*things*/ ctx[0]; - const get_key = ctx => /*thing*/ ctx[1].id; + const get_key = ctx => /*thing*/ ctx[2].id; for (let i = 0; i < each_value.length; i += 1) { let child_ctx = get_each_context(ctx, each_value, i); @@ -109,28 +109,28 @@ function create_fragment(ctx) { }; } -function foo(node, animation, params) { - const dx = animation.from.left - animation.to.left; - const dy = animation.from.top - animation.to.top; - - return { - delay: params.delay, - duration: 100, - tick: (t, u) => { - node.dx = u * dx; - node.dy = u * dy; - } - }; -} - function instance($$self, $$props, $$invalidate) { let { things } = $$props; + function foo(node, animation, params) { + const dx = animation.from.left - animation.to.left; + const dy = animation.from.top - animation.to.top; + + return { + delay: params.delay, + duration: 100, + tick: (t, u) => { + node.dx = u * dx; + node.dy = u * dy; + } + }; + } + $$self.$set = $$props => { if ("things" in $$props) $$invalidate(0, things = $$props.things); }; - return [things]; + return [things, foo]; } class Component extends SvelteComponent { diff --git a/test/js/samples/event-modifiers/expected.js b/test/js/samples/event-modifiers/expected.js index 3f324bb76d..fbce1d49a5 100644 --- a/test/js/samples/event-modifiers/expected.js +++ b/test/js/samples/event-modifiers/expected.js @@ -37,10 +37,10 @@ function create_fragment(ctx) { button2.textContent = "or me!"; dispose = [ - listen(button0, "click", stop_propagation(prevent_default(handleClick))), - listen(button1, "click", handleClick, { once: true, capture: true }), - listen(button2, "click", handleClick, true), - listen(div, "touchstart", handleTouchstart, { passive: true }) + listen(button0, "click", stop_propagation(prevent_default(/*handleClick*/ ctx[1]))), + listen(button1, "click", /*handleClick*/ ctx[1], { once: true, capture: true }), + listen(button2, "click", /*handleClick*/ ctx[1], true), + listen(div, "touchstart", /*handleTouchstart*/ ctx[0], { passive: true }) ]; }, m(target, anchor) { @@ -61,18 +61,22 @@ function create_fragment(ctx) { }; } -function handleTouchstart() { - -} +function instance($$self) { + function handleTouchstart() { + + } + + function handleClick() { + + } -function handleClick() { - + return [handleTouchstart, handleClick]; } class Component extends SvelteComponent { constructor(options) { super(); - init(this, options, null, create_fragment, safe_not_equal, {}); + init(this, options, instance, create_fragment, safe_not_equal, {}); } } diff --git a/test/js/samples/hoisted-const/expected.js b/test/js/samples/hoisted-const/expected.js index 2842b54751..496e562d56 100644 --- a/test/js/samples/hoisted-const/expected.js +++ b/test/js/samples/hoisted-const/expected.js @@ -15,7 +15,7 @@ function create_fragment(ctx) { return { c() { b = element("b"); - b.textContent = `${get_answer()}`; + b.textContent = `${/*get_answer*/ ctx[0]()}`; }, m(target, anchor) { insert(target, b, anchor); @@ -31,14 +31,18 @@ function create_fragment(ctx) { const ANSWER = 42; -function get_answer() { - return ANSWER; +function instance($$self) { + function get_answer() { + return ANSWER; + } + + return [get_answer]; } class Component extends SvelteComponent { constructor(options) { super(); - init(this, options, null, create_fragment, safe_not_equal, {}); + init(this, options, instance, create_fragment, safe_not_equal, {}); } } diff --git a/test/js/samples/hoisted-let/expected.js b/test/js/samples/hoisted-let/expected.js index 285b124118..9a7c552230 100644 --- a/test/js/samples/hoisted-let/expected.js +++ b/test/js/samples/hoisted-let/expected.js @@ -15,7 +15,7 @@ function create_fragment(ctx) { return { c() { b = element("b"); - b.textContent = `${get_answer()}`; + b.textContent = `${/*get_answer*/ ctx[0]()}`; }, m(target, anchor) { insert(target, b, anchor); @@ -31,14 +31,18 @@ function create_fragment(ctx) { let ANSWER = 42; -function get_answer() { - return ANSWER; +function instance($$self) { + function get_answer() { + return ANSWER; + } + + return [get_answer]; } class Component extends SvelteComponent { constructor(options) { super(); - init(this, options, null, create_fragment, safe_not_equal, {}); + init(this, options, instance, create_fragment, safe_not_equal, {}); } } diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index 8e30a03a7d..6970c3538f 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -3,11 +3,11 @@ import { SvelteComponent, init, safe_not_equal } from "svelte/internal"; const SOME_CONSTANT = 42; -function foo(bar) { - console.log(bar); -} - function instance($$self, $$props, $$invalidate) { + function foo(bar) { + console.log(bar); + } + return [foo]; } @@ -18,7 +18,7 @@ class Component extends SvelteComponent { } get foo() { - return foo; + return this.$$.ctx[0]; } } diff --git a/test/js/samples/ssr-no-oncreate-etc/expected.js b/test/js/samples/ssr-no-oncreate-etc/expected.js index 276587eeca..a1d8d4c88b 100644 --- a/test/js/samples/ssr-no-oncreate-etc/expected.js +++ b/test/js/samples/ssr-no-oncreate-etc/expected.js @@ -7,14 +7,6 @@ function preload(input) { return output; } -function foo() { - console.log("foo"); -} - -function swipe(node, callback) { - -} - const Component = create_ssr_component(($$result, $$props, $$bindings, $$slots) => { onMount(() => { console.log("onMount"); @@ -24,6 +16,14 @@ const Component = create_ssr_component(($$result, $$props, $$bindings, $$slots) console.log("onDestroy"); }); + function foo() { + console.log("foo"); + } + + function swipe(node, callback) { + + } + return ``; }); diff --git a/test/js/samples/transition-local/expected.js b/test/js/samples/transition-local/expected.js index a5d3b6318f..c41f32c96b 100644 --- a/test/js/samples/transition-local/expected.js +++ b/test/js/samples/transition-local/expected.js @@ -65,7 +65,7 @@ function create_if_block_1(ctx) { if (local) { if (!div_intro) { add_render_callback(() => { - div_intro = create_in_transition(div, foo, {}); + div_intro = create_in_transition(div, /*foo*/ ctx[2], {}); div_intro.start(); }); } @@ -114,20 +114,20 @@ function create_fragment(ctx) { }; } -function foo() { - -} - function instance($$self, $$props, $$invalidate) { let { x } = $$props; let { y } = $$props; + function foo() { + + } + $$self.$set = $$props => { if ("x" in $$props) $$invalidate(0, x = $$props.x); if ("y" in $$props) $$invalidate(1, y = $$props.y); }; - return [x, y]; + return [x, y, foo]; } class Component extends SvelteComponent {