From aabb23cc3427e3c26a497fb23e2880f5a0c11e68 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sat, 25 Apr 2020 01:02:15 +0800 Subject: [PATCH] fix mutation to imported variable (#4713) --- CHANGELOG.md | 4 ++ src/compiler/compile/Component.ts | 10 ++++- .../reactive-import-statement-2/_config.js | 3 ++ .../reactive-import-statement-2/data.js | 3 ++ .../reactive-import-statement-2/main.svelte | 8 ++++ .../reactive-import-statement/_config.js | 38 +++++++++++++++++++ .../samples/reactive-import-statement/data.js | 1 + .../reactive-import-statement/main.svelte | 19 ++++++++++ 8 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/reactive-import-statement-2/_config.js create mode 100644 test/runtime/samples/reactive-import-statement-2/data.js create mode 100644 test/runtime/samples/reactive-import-statement-2/main.svelte create mode 100644 test/runtime/samples/reactive-import-statement/_config.js create mode 100644 test/runtime/samples/reactive-import-statement/data.js create mode 100644 test/runtime/samples/reactive-import-statement/main.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index f9b3bb8b9a..3fceb5f2fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte changelog +## Unreleased + +* Fix reactivity with imported values that are then mutated ([#4555](https://github.com/sveltejs/svelte/issues/4555)) + ## 3.21.0 * Support dimension bindings in cross-origin environments ([#2147](https://github.com/sveltejs/svelte/issues/2147)) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index bb1546ceee..c83f8153a0 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -632,7 +632,6 @@ export default class Component { this.add_var({ name, initialised: instance_scope.initialised_declarations.has(name), - hoistable: /^Import/.test(node.type), writable }); @@ -986,6 +985,7 @@ export default class Component { hoistable_nodes, var_lookup, injected_reactive_declaration_vars, + imports, } = this; const top_level_function_declarations = new Map(); @@ -1137,6 +1137,14 @@ export default class Component { this.fully_hoisted.push(node); } } + + for (const { specifiers } of imports) { + for (const specifier of specifiers) { + const variable = var_lookup.get(specifier.local.name); + + if (!variable.mutated) variable.hoistable = true; + } + } } extract_reactive_declarations() { diff --git a/test/runtime/samples/reactive-import-statement-2/_config.js b/test/runtime/samples/reactive-import-statement-2/_config.js new file mode 100644 index 0000000000..ccc97cd076 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/_config.js @@ -0,0 +1,3 @@ +export default { + html: `

prop value

` +}; diff --git a/test/runtime/samples/reactive-import-statement-2/data.js b/test/runtime/samples/reactive-import-statement-2/data.js new file mode 100644 index 0000000000..56fb86982c --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/data.js @@ -0,0 +1,3 @@ +export const obj = { + prop: 'prop value' +}; diff --git a/test/runtime/samples/reactive-import-statement-2/main.svelte b/test/runtime/samples/reactive-import-statement-2/main.svelte new file mode 100644 index 0000000000..2582974b71 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/main.svelte @@ -0,0 +1,8 @@ + + +

{prop}

diff --git a/test/runtime/samples/reactive-import-statement/_config.js b/test/runtime/samples/reactive-import-statement/_config.js new file mode 100644 index 0000000000..7fb8097ca5 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement/_config.js @@ -0,0 +1,38 @@ +import * as path from 'path'; + +export default { + html: ` + import +

1 + 2 + 3 + 4 = 10

+ local +

1 + 2 + 3 + 4 = 10

+ + `, + before_test() { + delete require.cache[path.resolve(__dirname, 'data.js')]; + }, + async test({ assert, target, window, }) { + const btn = target.querySelector('button'); + const clickEvent = new window.MouseEvent('click'); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual(target.innerHTML, ` + import +

1 + 2 + 3 + 4 + 5 = 15

+ local +

1 + 2 + 3 + 4 + 5 = 15

+ + `); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual(target.innerHTML, ` + import +

1 + 2 + 3 + 4 + 5 + 6 = 21

+ local +

1 + 2 + 3 + 4 + 5 + 6 = 21

+ + `); + } +}; diff --git a/test/runtime/samples/reactive-import-statement/data.js b/test/runtime/samples/reactive-import-statement/data.js new file mode 100644 index 0000000000..ad494c7cb0 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement/data.js @@ -0,0 +1 @@ +export const numbers = [1, 2, 3, 4]; \ No newline at end of file diff --git a/test/runtime/samples/reactive-import-statement/main.svelte b/test/runtime/samples/reactive-import-statement/main.svelte new file mode 100644 index 0000000000..daded0494f --- /dev/null +++ b/test/runtime/samples/reactive-import-statement/main.svelte @@ -0,0 +1,19 @@ + + +import

{numbers.join(' + ')} = {sum}

+local

{local_numbers.join(' + ')} = {local_sum}

+ + \ No newline at end of file