Skip to content

Commit

Permalink
fix event handlers that are dynamic via reactive declarations or stor…
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry authored and taylorzane committed Dec 17, 2020
1 parent 98aba12 commit 00f71a3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Permit reserved keywords as destructuring keys in `{#each}` ([#4372](https://github.com/sveltejs/svelte/issues/4372))
* Disallow reserved keywords in `{expressions}` ([#4372](https://github.com/sveltejs/svelte/issues/4372))
* Fix code generation error with precedence of arrow functions ([#4384](https://github.com/sveltejs/svelte/issues/4384))
* Fix event handlers that are dynamic via reactive declarations or stores ([#4388](https://github.com/sveltejs/svelte/issues/4388))
* Fix invalidation in expressions like `++foo.bar` ([#4393](https://github.com/sveltejs/svelte/issues/4393))

## 3.18.1
Expand Down
7 changes: 0 additions & 7 deletions src/compiler/compile/nodes/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ export default class EventHandler extends Node {
}
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;
}
Expand Down
33 changes: 33 additions & 0 deletions test/runtime/samples/event-handler-dynamic-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default {
html: `
<button>toggle</button>
<p>0</p>
<button>handler_a</button>
<button>handler_b</button>
`,

async test({ assert, target, window }) {
const [toggle, handler_a, handler_b] = target.querySelectorAll('button');
const p = target.querySelector('p');

const event = new window.MouseEvent('click');

await handler_a.dispatchEvent(event);
assert.equal(p.innerHTML, '1');

await toggle.dispatchEvent(event);

await handler_a.dispatchEvent(event);
assert.equal(p.innerHTML, '2');

await toggle.dispatchEvent(event);

await handler_b.dispatchEvent(event);
assert.equal(p.innerHTML, '1');

await toggle.dispatchEvent(event);

await handler_b.dispatchEvent(event);
assert.equal(p.innerHTML, '2');
},
};
20 changes: 20 additions & 0 deletions test/runtime/samples/event-handler-dynamic-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
import { writable } from 'svelte/store';
let number = 0;
const handler_1 = () => number = 1;
const handler_2 = () => number = 2;
let flag = true;
$: handler_a = flag ? handler_1 : handler_2;
const handler_b = writable();
$: handler_b.set(flag ? handler_1 : handler_2);
</script>

<button on:click={() => flag = !flag}>toggle</button>

<p>{number}</p>

<button on:click={handler_a}>handler_a</button>
<button on:click={$handler_b}>handler_b</button>

0 comments on commit 00f71a3

Please sign in to comment.