From 9955ac13ac5b7ad3980fa0a788ae09b71fa5bbb1 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Mar 2019 14:13:29 -0400 Subject: [PATCH] handle assignments to store values in reactive declarations (#2119) --- src/compile/Component.ts | 4 +- src/compile/render-dom/index.ts | 2 +- .../_config.js | 42 +++++++++++++++++++ .../main.svelte | 20 +++++++++ .../store-assignment-updates/_config.js | 1 - 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 test/runtime/samples/store-assignment-updates-reactive/_config.js create mode 100644 test/runtime/samples/store-assignment-updates-reactive/main.svelte diff --git a/src/compile/Component.ts b/src/compile/Component.ts index f7c1c7dc5d..5d9730091e 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -593,7 +593,7 @@ export default class Component { const { type, name } = node.body.expression.left; - if (type === 'Identifier' && !this.var_lookup.has(name)) { + if (type === 'Identifier' && !this.var_lookup.has(name) && name[0] !== '$') { this.injected_reactive_declaration_vars.add(name); } }); @@ -761,7 +761,7 @@ export default class Component { rewrite_props(get_insert: (variable: Var) => string) { const component = this; - const { code, instance_scope, instance_scope_map: map, component_options } = this; + const { code, instance_scope, instance_scope_map: map } = this; let scope = instance_scope; const coalesced_declarations = []; diff --git a/src/compile/render-dom/index.ts b/src/compile/render-dom/index.ts index 5b37ac031c..c47717a395 100644 --- a/src/compile/render-dom/index.ts +++ b/src/compile/render-dom/index.ts @@ -364,7 +364,7 @@ export default function dom( const injected = Array.from(component.injected_reactive_declaration_vars).filter(name => { const variable = component.var_lookup.get(name); - return variable.injected; + return variable.injected && variable.name[0] !== '$'; }); const reactive_store_declarations = reactive_stores.map(variable => { diff --git a/test/runtime/samples/store-assignment-updates-reactive/_config.js b/test/runtime/samples/store-assignment-updates-reactive/_config.js new file mode 100644 index 0000000000..a7439bde5c --- /dev/null +++ b/test/runtime/samples/store-assignment-updates-reactive/_config.js @@ -0,0 +1,42 @@ +import { writable } from '../../../../store.js'; + +const c = writable(0); + +export default { + props: { + c + }, + + html: ` +

a: 0

+

b: 0

+

c: 0

+ + + `, + + async test({ assert, component, target, window }) { + const button = target.querySelector('button'); + const click = new window.MouseEvent('click'); + + await button.dispatchEvent(click); + + assert.htmlEqual(target.innerHTML, ` +

a: 1

+

b: 1

+

c: 1

+ + + `); + + await component.c.set(42); + + assert.htmlEqual(target.innerHTML, ` +

a: 42

+

b: 42

+

c: 42

+ + + `); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/store-assignment-updates-reactive/main.svelte b/test/runtime/samples/store-assignment-updates-reactive/main.svelte new file mode 100644 index 0000000000..18ec3bd7a7 --- /dev/null +++ b/test/runtime/samples/store-assignment-updates-reactive/main.svelte @@ -0,0 +1,20 @@ + + +

a: {$a}

+

b: {$b}

+

c: {$c}

+ + \ No newline at end of file diff --git a/test/runtime/samples/store-assignment-updates/_config.js b/test/runtime/samples/store-assignment-updates/_config.js index 74cff8b4d5..27f0ce274b 100644 --- a/test/runtime/samples/store-assignment-updates/_config.js +++ b/test/runtime/samples/store-assignment-updates/_config.js @@ -1,7 +1,6 @@ import { writable } from '../../../../store.js'; export default { - show: 1, props: { count: writable(0) },