From 00ef7bd22399562097801ea6f8efd856892a7521 Mon Sep 17 00:00:00 2001 From: rixo Date: Wed, 4 Dec 2019 09:17:50 +0100 Subject: [PATCH] remove specific handling of props from $capture_state --- src/compiler/compile/render_dom/index.ts | 20 ++----------------- .../samples/capture-inject-state/expected.js | 9 ++++----- .../samples/capture-inject-state/input.svelte | 2 ++ test/js/samples/debug-empty/expected.js | 2 +- .../debug-foo-bar-baz-things/expected.js | 5 +---- test/js/samples/debug-foo/expected.js | 5 +---- test/js/samples/debug-hoisted/expected.js | 6 +----- .../expected.js | 2 +- 8 files changed, 13 insertions(+), 38 deletions(-) diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 6f89c91c9c..d7d6d9e764 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -164,28 +164,12 @@ export default function dom( `; } - // we need to filter out store subscriptions ($x) or $inject_state will try to call .set() on them, leading - // to a crash if store is not writable (and probably not intended behaviour to change store value) - const is_not_sub = (variable: Var) => variable.name.substr(0, 1) !== '$'; - const capturable_vars = component.vars.filter( - variable => !variable.module && variable.writable && is_not_sub(variable) + variable => variable.writable && !(variable.injected || variable.name[0] === '$') ); if (uses_props || capturable_vars.length > 0) { - - const capturable_props = writable_props.filter(is_not_sub); - - const local_vars = capturable_vars.filter(variable => !variable.export_name); - - const var_names = (variables: Var[]) => variables.map(prop => p`${prop.name}`); - - capture_state = x` - ({ props: $p = true, local: $l = true } = {}) => ({ - ...${x`$p && { ${var_names(capturable_props)} }`}, - ...${x`$l && { ${var_names(local_vars)} }`} - }) - `; + capture_state = x`() => ({ ${capturable_vars.map(prop => p`${prop.name}`)} }) `; inject_state = x` ${$$props} => { diff --git a/test/js/samples/capture-inject-state/expected.js b/test/js/samples/capture-inject-state/expected.js index ea6028e845..c457566783 100644 --- a/test/js/samples/capture-inject-state/expected.js +++ b/test/js/samples/capture-inject-state/expected.js @@ -39,7 +39,7 @@ function create_fragment(ctx) { t4 = text(/*local*/ ctx[2]); t5 = space(); t6 = text(priv); - add_location(p, file, 11, 0, 204); + add_location(p, file, 13, 0, 232); }, l: function claim(nodes) { throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); @@ -100,10 +100,7 @@ function instance($$self, $$props, $$invalidate) { if ("alias" in $$props) $$invalidate(0, realName = $$props.alias); }; - $$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ - ...$p && ({ prop, realName }), - ...$l && ({ local }) - }); + $$self.$capture_state = () => ({ prop, realName, local }); $$self.$inject_state = $$props => { if ("prop" in $$props) $$subscribe_prop($$invalidate(3, prop = $$props.prop)); @@ -111,6 +108,8 @@ function instance($$self, $$props, $$invalidate) { if ("local" in $$props) $$invalidate(2, local = $$props.local); }; + let computed; + $: computed = local * 2; return [realName, $prop, local, prop]; } diff --git a/test/js/samples/capture-inject-state/input.svelte b/test/js/samples/capture-inject-state/input.svelte index b22a81b617..2a3fb4d291 100644 --- a/test/js/samples/capture-inject-state/input.svelte +++ b/test/js/samples/capture-inject-state/input.svelte @@ -7,6 +7,8 @@ let local; const priv = 'priv'; + + $: computed = local * 2;

{$prop} {realName} {local} {priv}

diff --git a/test/js/samples/debug-empty/expected.js b/test/js/samples/debug-empty/expected.js index 42474785bb..4cbb90b05e 100644 --- a/test/js/samples/debug-empty/expected.js +++ b/test/js/samples/debug-empty/expected.js @@ -79,7 +79,7 @@ function instance($$self, $$props, $$invalidate) { if ("name" in $$props) $$invalidate(0, name = $$props.name); }; - $$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ ...$p && ({ name }), ...$l && ({}) }); + $$self.$capture_state = () => ({ name }); $$self.$inject_state = $$props => { if ("name" in $$props) $$invalidate(0, name = $$props.name); diff --git a/test/js/samples/debug-foo-bar-baz-things/expected.js b/test/js/samples/debug-foo-bar-baz-things/expected.js index 73c6de2c4e..11d22ee889 100644 --- a/test/js/samples/debug-foo-bar-baz-things/expected.js +++ b/test/js/samples/debug-foo-bar-baz-things/expected.js @@ -183,10 +183,7 @@ function instance($$self, $$props, $$invalidate) { if ("baz" in $$props) $$invalidate(3, baz = $$props.baz); }; - $$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ - ...$p && ({ things, foo, bar, baz }), - ...$l && ({}) - }); + $$self.$capture_state = () => ({ things, foo, bar, baz }); $$self.$inject_state = $$props => { if ("things" in $$props) $$invalidate(0, things = $$props.things); diff --git a/test/js/samples/debug-foo/expected.js b/test/js/samples/debug-foo/expected.js index 33df8cfcd7..ed52bcb916 100644 --- a/test/js/samples/debug-foo/expected.js +++ b/test/js/samples/debug-foo/expected.js @@ -173,10 +173,7 @@ function instance($$self, $$props, $$invalidate) { if ("foo" in $$props) $$invalidate(1, foo = $$props.foo); }; - $$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ - ...$p && ({ things, foo }), - ...$l && ({}) - }); + $$self.$capture_state = () => ({ things, foo }); $$self.$inject_state = $$props => { if ("things" in $$props) $$invalidate(0, things = $$props.things); diff --git a/test/js/samples/debug-hoisted/expected.js b/test/js/samples/debug-hoisted/expected.js index 979c2005ea..1129484c10 100644 --- a/test/js/samples/debug-hoisted/expected.js +++ b/test/js/samples/debug-hoisted/expected.js @@ -50,11 +50,7 @@ function create_fragment(ctx) { function instance($$self) { let obj = { x: 5 }; let kobzol = 5; - - $$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ - ...$p && ({}), - ...$l && ({ obj, kobzol }) - }); + $$self.$capture_state = () => ({ obj, kobzol }); $$self.$inject_state = $$props => { if ("obj" in $$props) $$invalidate(0, obj = $$props.obj); diff --git a/test/js/samples/dev-warning-missing-data-computed/expected.js b/test/js/samples/dev-warning-missing-data-computed/expected.js index 59f68b45cd..90e65a24c6 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected.js @@ -76,7 +76,7 @@ function instance($$self, $$props, $$invalidate) { if ("foo" in $$props) $$invalidate(0, foo = $$props.foo); }; - $$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ ...$p && ({ foo }), ...$l && ({ bar }) }); + $$self.$capture_state = () => ({ foo, bar }); $$self.$inject_state = $$props => { if ("foo" in $$props) $$invalidate(0, foo = $$props.foo);