Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix event handlers that are dynamic via reactive declarations or stores #4394

Merged
merged 5 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>