pull/4000/head
daszgfz 6 years ago
parent afe06059bf
commit 697e7335d1

@ -63,6 +63,8 @@ export default class Component {
imports: ImportDeclaration[] = []; imports: ImportDeclaration[] = [];
function_args: Set<string> = new Set();
hoistable_nodes: Set<Node> = new Set(); hoistable_nodes: Set<Node> = new Set();
node_for_declaration: Map<string, Node> = new Map(); node_for_declaration: Map<string, Node> = new Map();
partly_hoisted: Array<(Node | Node[])> = []; partly_hoisted: Array<(Node | Node[])> = [];
@ -699,7 +701,7 @@ export default class Component {
const component = this; const component = this;
const { content } = script; const { content } = script;
const { instance_scope, instance_scope_map: map } = this; const { function_args, instance_scope, instance_scope_map: map } = this;
let scope = instance_scope; let scope = instance_scope;
@ -716,6 +718,14 @@ export default class Component {
scope = map.get(node); 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') { if (node.type === 'ImportDeclaration') {
component.extract_imports(node); component.extract_imports(node);
// TODO: to use actual remove // TODO: to use actual remove
@ -1054,6 +1064,7 @@ export default class Component {
const instance_scope = this.instance_scope; const instance_scope = this.instance_scope;
let scope = this.instance_scope; let scope = this.instance_scope;
const map = this.instance_scope_map; const map = this.instance_scope_map;
const function_args = this.function_args;
let hoistable = true; let hoistable = true;
@ -1072,7 +1083,9 @@ export default class Component {
const { name } = flatten_reference(node); const { name } = flatten_reference(node);
const owner = scope.find_owner(name); const owner = scope.find_owner(name);
if (injected_reactive_declaration_vars.has(name)) { if (function_args.has(name)) {
hoistable = false;
} else if (injected_reactive_declaration_vars.has(name)) {
hoistable = false; hoistable = false;
} else if (name[0] === '$' && !owner) { } else if (name[0] === '$' && !owner) {
hoistable = false; hoistable = false;

@ -21,10 +21,10 @@ function create_fragment(ctx) {
}, },
m(target, anchor) { m(target, anchor) {
insert(target, button, anchor); insert(target, button, anchor);
foo_action = foo.call(null, button, /*foo_function*/ ctx[1]) || ({}); foo_action = foo.call(null, button, /*foo_function*/ ctx[2]) || ({});
}, },
p(ctx, [dirty]) { p(ctx, [dirty]) {
if (is_function(foo_action.update) && dirty & /*bar*/ 1) foo_action.update.call(null, /*foo_function*/ ctx[1]); if (is_function(foo_action.update) && dirty & /*bar*/ 1) foo_action.update.call(null, /*foo_function*/ ctx[2]);
}, },
i: noop, i: noop,
o: noop, o: noop,
@ -35,23 +35,24 @@ function create_fragment(ctx) {
}; };
} }
function handleFoo(bar) {
console.log(bar);
}
function foo(node, callback) { function foo(node, callback) {
} }
function instance($$self, $$props, $$invalidate) { function instance($$self, $$props, $$invalidate) {
let { bar } = $$props; let { bar } = $$props;
function handleFoo(bar) {
console.log(bar);
}
const foo_function = () => handleFoo(bar); const foo_function = () => handleFoo(bar);
$$self.$set = $$props => { $$self.$set = $$props => {
if ("bar" in $$props) $$invalidate(0, bar = $$props.bar); if ("bar" in $$props) $$invalidate(0, bar = $$props.bar);
}; };
return [bar, foo_function]; return [bar, handleFoo, foo_function];
} }
class Component extends SvelteComponent { class Component extends SvelteComponent {

@ -23,7 +23,7 @@ function create_fragment(ctx) {
}, },
m(target, anchor) { m(target, anchor) {
insert(target, a, anchor); insert(target, a, anchor);
link_action = link.call(null, a) || ({}); link_action = /*link*/ ctx[0].call(null, a) || ({});
}, },
p: noop, p: noop,
i: noop, i: noop,
@ -35,7 +35,8 @@ function create_fragment(ctx) {
}; };
} }
function link(node) { function instance($$self) {
function link(node) {
function onClick(event) { function onClick(event) {
event.preventDefault(); event.preventDefault();
history.pushState(null, null, event.target.href); history.pushState(null, null, event.target.href);
@ -48,12 +49,15 @@ function link(node) {
node.removeEventListener("click", onClick); node.removeEventListener("click", onClick);
} }
}; };
}
return [link];
} }
class Component extends SvelteComponent { class Component extends SvelteComponent {
constructor(options) { constructor(options) {
super(); super();
init(this, options, null, create_fragment, safe_not_equal, {}); init(this, options, instance, create_fragment, safe_not_equal, {});
} }
} }

@ -0,0 +1,50 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
detach,
element,
init,
insert,
noop,
safe_not_equal
} from "svelte/internal";
import { bar } from "./utils.js";
function create_fragment(ctx) {
let p;
return {
c() {
p = element("p");
p.textContent = `You'll be pleased to know the answer is ${/*foo*/ ctx[0]()}.`;
},
m(target, anchor) {
insert(target, p, anchor);
},
p: noop,
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(p);
}
};
}
function instance($$self) {
function foo() {
return 42;
}
bar(foo);
return [foo];
}
class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, {});
}
}
export default Component;

@ -0,0 +1,11 @@
<script>
import { bar } from './utils.js';
function foo() {
return 42;
}
bar(foo);
</script>
<p>You'll be pleased to know the answer is {foo()}.</p>

@ -0,0 +1,3 @@
export function bar(fn) {
console.log(`I'm safe but you'd never know, ${fn}!`);
}
Loading…
Cancel
Save