From 5b7dc635040a5096fca259c37e7686aaac060620 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Dec 2018 11:14:24 -0500 Subject: [PATCH] various fixes --- src/compile/Component.ts | 2 ++ src/compile/nodes/EventHandler.ts | 15 ++++----------- src/compile/nodes/shared/Expression.ts | 2 +- src/compile/render-dom/wrappers/Element/index.ts | 9 +++++---- .../render-dom/wrappers/InlineComponent/index.ts | 5 +++-- .../render-dom/wrappers/shared/addActions.ts | 2 ++ src/internal/utils.js | 2 +- 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/compile/Component.ts b/src/compile/Component.ts index 61c7ec2721..dfaf3588f2 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -764,6 +764,8 @@ export default class Component { if (this.hoistable_names.has(name)) return name; if (this.imported_declarations.has(name)) return name; if (this.declarations.indexOf(name) === -1) return name; + + this.template_references.add(name); // TODO we can probably remove most other occurrences of this return `ctx.${name}`; } diff --git a/src/compile/nodes/EventHandler.ts b/src/compile/nodes/EventHandler.ts index d98dd9859c..a321796c08 100644 --- a/src/compile/nodes/EventHandler.ts +++ b/src/compile/nodes/EventHandler.ts @@ -8,15 +8,7 @@ export default class EventHandler extends Node { modifiers: Set; expression: Expression; handler_name: string; - - usesComponent: boolean; usesContext: boolean; - usesEventObject: boolean; - isCustomEvent: boolean; - - insertionPoint: number; - args: Expression[]; - snippet: string; constructor(component: Component, parent, template_scope, info) { super(component, parent, template_scope, info); @@ -42,8 +34,9 @@ export default class EventHandler extends Node { } render() { - return this.expression - ? this.expression.render() - : `ctx.${this.handler_name}`; + if (this.expression) return this.expression.render(); + + this.component.template_references.add(this.handler_name); + return `ctx.${this.handler_name}`; } } \ No newline at end of file diff --git a/src/compile/nodes/shared/Expression.ts b/src/compile/nodes/shared/Expression.ts index 750203c39a..32be4b5911 100644 --- a/src/compile/nodes/shared/Expression.ts +++ b/src/compile/nodes/shared/Expression.ts @@ -219,7 +219,7 @@ export default class Expression { dependencies.add(name); component.template_references.add(name); } - } else if (!is_synthetic && !component.hoistable_names.has(name)) { + } else if (!is_synthetic && !component.hoistable_names.has(name) && !component.imported_declarations.has(name)) { code.prependRight(node.start, key === 'key' && parent.shorthand ? `${name}: ctx.` : 'ctx.'); diff --git a/src/compile/render-dom/wrappers/Element/index.ts b/src/compile/render-dom/wrappers/Element/index.ts index 78c9f8539b..6c216110f5 100644 --- a/src/compile/render-dom/wrappers/Element/index.ts +++ b/src/compile/render-dom/wrappers/Element/index.ts @@ -621,9 +621,10 @@ export default class ElementWrapper extends Wrapper { block: Block ) { const { intro, outro } = this.node; - if (!intro && !outro) return; + const { component } = this.renderer; + if (intro === outro) { const name = block.getUniqueName(`${this.var}_transition`); const snippet = intro.expression @@ -632,7 +633,7 @@ export default class ElementWrapper extends Wrapper { block.addVariable(name); - const fn = `ctx.${intro.name}`; + const fn = component.qualify(intro.name); block.builders.intro.addConditional(`@intro.enabled`, deindent` if (${name}) ${name}.invalidate(); @@ -662,7 +663,7 @@ export default class ElementWrapper extends Wrapper { ? intro.expression.render() : '{}'; - const fn = `ctx.${intro.name}`; // TODO add built-in transitions? + const fn = component.qualify(intro.name); // TODO add built-in transitions? if (outro) { block.builders.intro.addBlock(deindent` @@ -685,7 +686,7 @@ export default class ElementWrapper extends Wrapper { ? outro.expression.render() : '{}'; - const fn = `ctx.${outro.name}`; + const fn = component.qualify(outro.name); block.builders.intro.addBlock(deindent` if (${outroName}) ${outroName}.abort(1); diff --git a/src/compile/render-dom/wrappers/InlineComponent/index.ts b/src/compile/render-dom/wrappers/InlineComponent/index.ts index 3019236b30..f589ad6c51 100644 --- a/src/compile/render-dom/wrappers/InlineComponent/index.ts +++ b/src/compile/render-dom/wrappers/InlineComponent/index.ts @@ -202,6 +202,7 @@ export default class InlineComponentWrapper extends Wrapper { const name = component.getUniqueName(`${this.var}_${binding.name}_binding`); component.declarations.push(name); + component.template_references.add(name); const updating = block.getUniqueName(`updating_${binding.name}`); block.addVariable(updating); @@ -293,7 +294,7 @@ export default class InlineComponentWrapper extends Wrapper { function ${switch_props}(ctx) { ${(this.node.attributes.length || this.node.bindings.length) && deindent` - ${props && `const ${props} = ${attributeObject};`}`} + ${props && `let ${props} = ${attributeObject};`}`} ${statements} return ${stringifyProps(component_opts)}; } @@ -385,7 +386,7 @@ export default class InlineComponentWrapper extends Wrapper { block.builders.init.addBlock(deindent` ${(this.node.attributes.length || this.node.bindings.length) && deindent` - ${props && `const ${props} = ${attributeObject};`}`} + ${props && `let ${props} = ${attributeObject};`}`} ${statements} var ${name} = new ${expression}(${stringifyProps(component_opts)}); diff --git a/src/compile/render-dom/wrappers/shared/addActions.ts b/src/compile/render-dom/wrappers/shared/addActions.ts index 1196418715..c5180ff043 100644 --- a/src/compile/render-dom/wrappers/shared/addActions.ts +++ b/src/compile/render-dom/wrappers/shared/addActions.ts @@ -30,6 +30,8 @@ export default function addActions( ? action.name : `ctx.${action.name}`; + component.template_references.add(action.name); + block.builders.mount.addLine( `${name} = ${fn}.call(null, ${target}${snippet ? `, ${snippet}` : ''}) || {};` ); diff --git a/src/internal/utils.js b/src/internal/utils.js index d50d2b3385..55feb743e2 100644 --- a/src/internal/utils.js +++ b/src/internal/utils.js @@ -34,7 +34,7 @@ export function exclude(src, prop) { } export function run(fn) { - fn(); + return fn(); } export function blankObject() {