From 6f3d7beaae00910eec831127b76fd848f94135b0 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Fri, 24 Apr 2020 08:13:25 +0800 Subject: [PATCH] fix mutation to imported variable --- src/compiler/compile/Component.ts | 10 ++++- .../reactive-import-statement-2/_config.js | 5 +++ .../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 ++++++++++ 7 files changed, 83 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/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..03b5b97e6e --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/_config.js @@ -0,0 +1,5 @@ +export default { + html: ` + Im a prop + `, +}; 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..28c7c65602 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/data.js @@ -0,0 +1,3 @@ +export const obj = { + prop: 'Im a prop' +}; \ No newline at end of file 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..4f6f0abf6f --- /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..0ee6270024 --- /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