diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 9d02821abe..64a83a7ed3 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -165,24 +165,21 @@ export default function dom( } const capturable_vars = component.vars.filter( - variable => variable.writable && !(variable.injected || variable.name[0] === '$') + variable => variable.writable && (!variable.injected || variable.name[0] === '$') ); - if (uses_props || capturable_vars.length > 0) { - capture_state = x`() => ({ ${capturable_vars.map(prop => p`${prop.name}`)} }) `; + const injectable_vars = capturable_vars.filter(variable => variable.name[0] !== '$') - inject_state = x` - ${$$props} => { - ${uses_props && renderer.invalidate('$$props', x`$$props = @assign(@assign({}, $$props), $$new_props)`)} - ${capturable_vars.map(prop => b` - if ('${prop.name}' in $$props) ${renderer.invalidate(prop.name, x`${prop.name} = ${$$props}.${prop.name}`)}; - `)} - } - `; - } else { - capture_state = x`() => ({})`; - inject_state = x`() => {}`; - } + capture_state = x`() => ({ ${capturable_vars.map(prop => p`${prop.name}`)} }) `; + + inject_state = x` + ${(uses_props || injectable_vars.length > 0) && $$props} => { + ${uses_props && renderer.invalidate('$$props', x`$$props = @assign(@assign({}, $$props), $$new_props)`)} + ${injectable_vars.map(prop => b` + if ('${prop.name}' in $$props) ${renderer.invalidate(prop.name, x`${prop.name} = ${$$props}.${prop.name}`)}; + `)} + } + `; } // instrument assignments diff --git a/test/js/samples/capture-inject-state/expected.js b/test/js/samples/capture-inject-state/expected.js index c457566783..c850d82f33 100644 --- a/test/js/samples/capture-inject-state/expected.js +++ b/test/js/samples/capture-inject-state/expected.js @@ -100,7 +100,7 @@ function instance($$self, $$props, $$invalidate) { if ("alias" in $$props) $$invalidate(0, realName = $$props.alias); }; - $$self.$capture_state = () => ({ prop, realName, local }); + $$self.$capture_state = () => ({ prop, realName, local, $prop }); $$self.$inject_state = $$props => { if ("prop" in $$props) $$subscribe_prop($$invalidate(3, prop = $$props.prop)); @@ -154,4 +154,4 @@ class Component extends SvelteComponentDev { } } -export default Component; \ No newline at end of file +export default Component;