From d8445e63509724404b394fd210278f44dd02e00f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Aug 2019 16:30:41 -0400 Subject: [PATCH] mark reassigned/mutated values as non-hoistable - fixes #2731 --- src/compiler/compile/Component.ts | 5 ++++- .../samples/reactive-value-function/_config.js | 9 +++++++++ .../samples/reactive-value-function/main.svelte | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/reactive-value-function/_config.js create mode 100644 test/runtime/samples/reactive-value-function/main.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 355ad0c423..77ab48545e 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1069,9 +1069,12 @@ export default class Component { } else if (name[0] === '$' && !owner) { hoistable = false; } else if (owner === instance_scope) { + const variable = var_lookup.get(name); + + if (variable.reassigned || variable.mutated) hoistable = false; + if (name === fn_declaration.id.name) return; - const variable = var_lookup.get(name); if (variable.hoistable) return; if (top_level_function_declarations.has(name)) { diff --git a/test/runtime/samples/reactive-value-function/_config.js b/test/runtime/samples/reactive-value-function/_config.js new file mode 100644 index 0000000000..c2de1af713 --- /dev/null +++ b/test/runtime/samples/reactive-value-function/_config.js @@ -0,0 +1,9 @@ +export default { + html: `1-2`, + + async test({ assert, component, target }) { + await component.update(); + + assert.htmlEqual(target.innerHTML, `3-4`); + } +}; diff --git a/test/runtime/samples/reactive-value-function/main.svelte b/test/runtime/samples/reactive-value-function/main.svelte new file mode 100644 index 0000000000..5b132d638e --- /dev/null +++ b/test/runtime/samples/reactive-value-function/main.svelte @@ -0,0 +1,14 @@ + + +{foo()}-{bar()}