use context in event handlers regardless of dependencies

pull/1367/head
Rich Harris 7 years ago
parent 041ad806ee
commit b73a3f6b1a

@ -29,15 +29,16 @@ export default class EventHandler extends Node {
this.callee = flattenReference(info.expression.callee); this.callee = flattenReference(info.expression.callee);
this.insertionPoint = info.expression.start; this.insertionPoint = info.expression.start;
this.usesComponent = !validCalleeObjects.has(this.callee.name);
this.usesContext = false;
this.args = info.expression.arguments.map(param => { this.args = info.expression.arguments.map(param => {
const expression = new Expression(compiler, this, scope, param); const expression = new Expression(compiler, this, scope, param);
addToSet(this.dependencies, expression.dependencies); addToSet(this.dependencies, expression.dependencies);
if (expression.usesContext) this.usesContext = true;
return expression; return expression;
}); });
this.usesComponent = !validCalleeObjects.has(this.callee.name);
this.usesContext = this.dependencies.size > 0;
this.snippet = `[✂${info.expression.start}-${info.expression.end}✂]`; this.snippet = `[✂${info.expression.start}-${info.expression.end}✂]`;
} else { } else {
this.callee = null; this.callee = null;

@ -9,6 +9,7 @@ export default class Expression {
node: any; node: any;
snippet: string; snippet: string;
usesContext: boolean;
references: Set<string>; references: Set<string>;
dependencies: Set<string>; dependencies: Set<string>;
contexts: Set<string>; contexts: Set<string>;
@ -29,6 +30,7 @@ export default class Expression {
this.snippet = `[✂${info.start}-${info.end}✂]`; this.snippet = `[✂${info.start}-${info.end}✂]`;
this.usesContext = false;
const contextDependencies = new Map(); // TODO const contextDependencies = new Map(); // TODO
const indexes = new Map(); const indexes = new Map();
@ -39,7 +41,7 @@ export default class Expression {
let { map, scope: currentScope } = createScopes(info); let { map, scope: currentScope } = createScopes(info);
const isEventHandler = parent.type === 'EventHandler'; const isEventHandler = parent.type === 'EventHandler';
const { thisReferences } = this; const expression = this;
walk(info, { walk(info, {
enter(node: any, parent: any, key: string) { enter(node: any, parent: any, key: string) {
@ -55,7 +57,7 @@ export default class Expression {
} }
if (node.type === 'ThisExpression') { if (node.type === 'ThisExpression') {
thisReferences.push(node); expression.thisReferences.push(node);
} }
if (isReference(node, parent)) { if (isReference(node, parent)) {
@ -72,6 +74,8 @@ export default class Expression {
return; return;
} }
expression.usesContext = true;
code.prependRight(node.start, key === 'key' && parent.shorthand code.prependRight(node.start, key === 'key' && parent.shorthand
? `${name}: ctx.` ? `${name}: ctx.`
: 'ctx.'); : 'ctx.');

Loading…
Cancel
Save