diff --git a/src/compiler/compile/nodes/EventHandler.ts b/src/compiler/compile/nodes/EventHandler.ts index bee10be659..4242c82394 100644 --- a/src/compiler/compile/nodes/EventHandler.ts +++ b/src/compiler/compile/nodes/EventHandler.ts @@ -12,7 +12,6 @@ export default class EventHandler extends Node { handler_name: Identifier; uses_context = false; can_make_passive = false; - reassigned?: boolean; constructor(component: Component, parent, template_scope, info) { super(component, parent, template_scope, info); @@ -41,14 +40,30 @@ export default class EventHandler extends Node { if (node && (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression') && node.params.length === 0) { this.can_make_passive = true; } - - this.reassigned = component.var_lookup.get(info.expression.name).reassigned; } - } else if (this.expression.dynamic_dependencies().length > 0) { - this.reassigned = true; } } else { this.handler_name = component.get_unique_name(`${sanitize(this.name)}_handler`); } } + + get reassigned(): boolean { + if (!this.expression) { + return false; + } + const node = this.expression.node; + + if (node.type === 'Identifier') { + return ( + this.component.node_for_declaration.get(node.name) && + this.component.var_lookup.get(node.name).reassigned + ); + } + + if (/FunctionExpression/.test(node.type)) { + return false; + } + + return this.expression.dynamic_dependencies().length > 0; + } } diff --git a/test/runtime/samples/event-handler-dynamic-bound-var/Nested.svelte b/test/runtime/samples/event-handler-dynamic-bound-var/Nested.svelte new file mode 100644 index 0000000000..948fc308c0 --- /dev/null +++ b/test/runtime/samples/event-handler-dynamic-bound-var/Nested.svelte @@ -0,0 +1,7 @@ + +{text} \ No newline at end of file diff --git a/test/runtime/samples/event-handler-dynamic-bound-var/_config.js b/test/runtime/samples/event-handler-dynamic-bound-var/_config.js new file mode 100644 index 0000000000..c832127c09 --- /dev/null +++ b/test/runtime/samples/event-handler-dynamic-bound-var/_config.js @@ -0,0 +1,20 @@ +export default { + html: ` + + Hello World + `, + async test({ assert, component, target, window }) { + const button = target.querySelector('button'); + + const event = new window.MouseEvent('click'); + + await button.dispatchEvent(event); + assert.htmlEqual( + target.innerHTML, + ` + + Bye World + ` + ); + }, +}; diff --git a/test/runtime/samples/event-handler-dynamic-bound-var/main.svelte b/test/runtime/samples/event-handler-dynamic-bound-var/main.svelte new file mode 100644 index 0000000000..bb7d9befc4 --- /dev/null +++ b/test/runtime/samples/event-handler-dynamic-bound-var/main.svelte @@ -0,0 +1,8 @@ + + + + +