From e1148b659018d40c898d812c49fa40e865a68ac5 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Mon, 24 Jun 2019 21:35:39 -0400 Subject: [PATCH] always bail out of hoisting on encountering local state - fixes #3044 --- src/compiler/compile/Component.ts | 4 +++- .../reactive-value-function-hoist-b/_config.js | 15 +++++++++++++++ .../reactive-value-function-hoist-b/main.svelte | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/reactive-value-function-hoist-b/_config.js create mode 100644 test/runtime/samples/reactive-value-function-hoist-b/main.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 53838f90b8..5b08b795ce 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1040,6 +1040,8 @@ export default class Component { walk(fn_declaration, { enter(node, parent) { + if (!hoistable) return this.skip(); + if (map.has(node)) { scope = map.get(node); } @@ -1048,7 +1050,7 @@ export default class Component { const { name } = flatten_reference(node); const owner = scope.find_owner(name); - if (node.type === 'Identifier' && injected_reactive_declaration_vars.has(name)) { + if (injected_reactive_declaration_vars.has(name)) { hoistable = false; } else if (name[0] === '$' && !owner) { hoistable = false; diff --git a/test/runtime/samples/reactive-value-function-hoist-b/_config.js b/test/runtime/samples/reactive-value-function-hoist-b/_config.js new file mode 100644 index 0000000000..04c4fd3f11 --- /dev/null +++ b/test/runtime/samples/reactive-value-function-hoist-b/_config.js @@ -0,0 +1,15 @@ +export default { + html: ` + + `, + + async test({ assert, target, window }) { + const event = new window.MouseEvent('click'); + const button = target.querySelector('button'); + + await button.dispatchEvent(event); + assert.htmlEqual(target.innerHTML, ` + + `); + } +}; diff --git a/test/runtime/samples/reactive-value-function-hoist-b/main.svelte b/test/runtime/samples/reactive-value-function-hoist-b/main.svelte new file mode 100644 index 0000000000..2e9ee6eefe --- /dev/null +++ b/test/runtime/samples/reactive-value-function-hoist-b/main.svelte @@ -0,0 +1,10 @@ + + +