diff --git a/src/generators/nodes/EventHandler.ts b/src/generators/nodes/EventHandler.ts index fd106d482b..162c4c21a1 100644 --- a/src/generators/nodes/EventHandler.ts +++ b/src/generators/nodes/EventHandler.ts @@ -29,15 +29,16 @@ export default class EventHandler extends Node { this.callee = flattenReference(info.expression.callee); this.insertionPoint = info.expression.start; + this.usesComponent = !validCalleeObjects.has(this.callee.name); + this.usesContext = false; + this.args = info.expression.arguments.map(param => { const expression = new Expression(compiler, this, scope, param); addToSet(this.dependencies, expression.dependencies); + if (expression.usesContext) this.usesContext = true; return expression; }); - this.usesComponent = !validCalleeObjects.has(this.callee.name); - this.usesContext = this.dependencies.size > 0; - this.snippet = `[✂${info.expression.start}-${info.expression.end}✂]`; } else { this.callee = null; diff --git a/src/generators/nodes/shared/Expression.ts b/src/generators/nodes/shared/Expression.ts index bbc4c96a32..9d27aa281e 100644 --- a/src/generators/nodes/shared/Expression.ts +++ b/src/generators/nodes/shared/Expression.ts @@ -9,6 +9,7 @@ export default class Expression { node: any; snippet: string; + usesContext: boolean; references: Set; dependencies: Set; contexts: Set; @@ -29,6 +30,7 @@ export default class Expression { this.snippet = `[✂${info.start}-${info.end}✂]`; + this.usesContext = false; const contextDependencies = new Map(); // TODO const indexes = new Map(); @@ -39,7 +41,7 @@ export default class Expression { let { map, scope: currentScope } = createScopes(info); const isEventHandler = parent.type === 'EventHandler'; - const { thisReferences } = this; + const expression = this; walk(info, { enter(node: any, parent: any, key: string) { @@ -55,7 +57,7 @@ export default class Expression { } if (node.type === 'ThisExpression') { - thisReferences.push(node); + expression.thisReferences.push(node); } if (isReference(node, parent)) { @@ -72,6 +74,8 @@ export default class Expression { return; } + expression.usesContext = true; + code.prependRight(node.start, key === 'key' && parent.shorthand ? `${name}: ctx.` : 'ctx.');