From 6e2b81663c28cf8a7693ae4f506e6112755cd54a Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Tue, 23 Jun 2020 22:00:43 +0800 Subject: [PATCH] fix extracting names from ThisExpression (#5036) --- CHANGELOG.md | 4 ++++ src/compiler/compile/nodes/shared/Expression.ts | 7 ++----- .../samples/this-in-function-expressions/_config.js | 10 ++++++++++ .../samples/this-in-function-expressions/main.svelte | 2 ++ 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 test/runtime/samples/this-in-function-expressions/_config.js create mode 100644 test/runtime/samples/this-in-function-expressions/main.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index dfa08fdf42..6757221f48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte changelog +## Unreleased + +* Fix handling of `this` in inline function expressions in the template ([#5033](https://github.com/sveltejs/svelte/issues/5033)) + ## 3.23.2 * Fix `bind:group` inside `{#each}` ([#3243](https://github.com/sveltejs/svelte/issues/3243)) diff --git a/src/compiler/compile/nodes/shared/Expression.ts b/src/compiler/compile/nodes/shared/Expression.ts index f7c9897d58..c7c66940fd 100644 --- a/src/compiler/compile/nodes/shared/Expression.ts +++ b/src/compiler/compile/nodes/shared/Expression.ts @@ -120,12 +120,9 @@ export default class Expression { if (function_expression) { if (node.type === 'AssignmentExpression') { deep = node.left.type === 'MemberExpression'; - names = deep - ? [get_object(node.left).name] - : extract_names(node.left); + names = extract_names(deep ? get_object(node.left) : node.left); } else if (node.type === 'UpdateExpression') { - const { name } = get_object(node.argument); - names = [name]; + names = extract_names(get_object(node.argument)); } } diff --git a/test/runtime/samples/this-in-function-expressions/_config.js b/test/runtime/samples/this-in-function-expressions/_config.js new file mode 100644 index 0000000000..4110d05a48 --- /dev/null +++ b/test/runtime/samples/this-in-function-expressions/_config.js @@ -0,0 +1,10 @@ +export default { + async test({ assert, component, target, window, raf }) { + const [_, btn] = target.querySelectorAll("button"); + const clickEvent = new window.MouseEvent("click"); + + await btn.dispatchEvent(clickEvent); + + assert.equal(btn.x, 1); + }, +}; diff --git a/test/runtime/samples/this-in-function-expressions/main.svelte b/test/runtime/samples/this-in-function-expressions/main.svelte new file mode 100644 index 0000000000..af732920e9 --- /dev/null +++ b/test/runtime/samples/this-in-function-expressions/main.svelte @@ -0,0 +1,2 @@ +