|
import Node from './shared/Node.js'; |
|
import Expression from './shared/Expression.js'; |
|
import { sanitize } from '../../utils/names.js'; |
|
|
|
const regex_contains_term_function_expression = /FunctionExpression/; |
|
|
|
|
|
export default class EventHandler extends Node { |
|
|
|
name; |
|
|
|
|
|
modifiers; |
|
|
|
|
|
expression; |
|
|
|
|
|
handler_name; |
|
|
|
uses_context = false; |
|
|
|
can_make_passive = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(component, parent, template_scope, info) { |
|
super(component, parent, template_scope, info); |
|
this.name = info.name; |
|
this.modifiers = new Set(info.modifiers); |
|
if (info.expression) { |
|
this.expression = new Expression(component, this, template_scope, info.expression); |
|
this.uses_context = this.expression.uses_context; |
|
if ( |
|
regex_contains_term_function_expression.test(info.expression.type) && |
|
info.expression.params.length === 0 |
|
) { |
|
|
|
|
|
this.can_make_passive = true; |
|
} else if (info.expression.type === 'Identifier') { |
|
let node = component.node_for_declaration.get(info.expression.name); |
|
if (node) { |
|
if (node.type === 'VariableDeclaration') { |
|
|
|
const declarator = node.declarations.find( |
|
(d) => (d.id).name === info.expression.name |
|
); |
|
node = declarator && declarator.init; |
|
} |
|
if ( |
|
node && |
|
(node.type === 'FunctionExpression' || |
|
node.type === 'FunctionDeclaration' || |
|
node.type === 'ArrowFunctionExpression') && |
|
node.params.length === 0 |
|
) { |
|
this.can_make_passive = true; |
|
} |
|
} |
|
} |
|
} else { |
|
this.handler_name = component.get_unique_name(`${sanitize(this.name)}_handler`); |
|
} |
|
} |
|
|
|
|
|
get reassigned() { |
|
if (!this.expression) { |
|
return false; |
|
} |
|
const node = this.expression.node; |
|
if (regex_contains_term_function_expression.test(node.type)) { |
|
return false; |
|
} |
|
return this.expression.dynamic_dependencies().length > 0; |
|
} |
|
} |
|
|