0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

Merge branch 'tanhauhau/exported-variable-could-be-reassigned' of https://github.com/tanhauhau/svelte into tanhauhau-tanhauhau/exported-variable-could-be-reassigned

This commit is contained in:
Richard Harris 2019-10-18 20:45:25 -04:00
commit 78c226b7b1
4 changed files with 42 additions and 12 deletions

View File

@ -759,7 +759,7 @@ export default class Component {
invalidate(name, value?) {
const variable = this.var_lookup.get(name);
if (variable && (variable.subscribable && variable.reassigned)) {
if (variable && (variable.subscribable && (variable.reassigned || variable.export_name))) {
return x`${`$$subscribe_${name}`}($$invalidate('${name}', ${value || name}))`;
}

View File

@ -221,18 +221,15 @@ export default function dom(
}
});
component.rewrite_props(({ name, reassigned }) => {
component.rewrite_props(({ name, reassigned, export_name }) => {
const value = `$${name}`;
const insert = (reassigned || export_name)
? b`${`$$subscribe_${name}`}()`
: b`@component_subscribe($$self, ${name}, #value => $$invalidate('${value}', ${value} = #value))`;
if (reassigned) {
return b`${`$$subscribe_${name}`}()`;
}
const callback = x`$$value => $$invalidate('${value}', ${value} = $$value)`;
let insert = b`@component_subscribe($$self, ${name}, $${callback})`;
if (component.compile_options.dev) {
insert = b`@validate_store(${name}, '${name}'); ${insert}`;
return b`@validate_store(${name}, '${name}'); ${insert}`;
}
return insert;
@ -311,7 +308,7 @@ export default function dom(
const resubscribable_reactive_store_unsubscribers = reactive_stores
.filter(store => {
const variable = component.var_lookup.get(store.name.slice(1));
return variable && variable.reassigned;
return variable && (variable.reassigned || variable.export_name);
})
.map(({ name }) => b`$$self.$$.on_destroy.push(() => ${`$$unsubscribe_${name.slice(1)}`}());`);
@ -353,7 +350,7 @@ export default function dom(
const name = $name.slice(1);
const store = component.var_lookup.get(name);
if (store && store.reassigned) {
if (store && (store.reassigned || store.export_name)) {
const unsubscribe = `$$unsubscribe_${name}`;
const subscribe = `$$subscribe_${name}`;
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe(${name}, $$value => $$invalidate('${$name}', ${$name} = $$value)), ${name})`;

View File

@ -0,0 +1,27 @@
let subscribeCalled = false;
const fakeStore = val => ({
subscribe: cb => {
cb(val);
return {
unsubscribe: () => {
subscribeCalled = true;
},
};
},
});
export default {
props: {
foo: fakeStore(1),
},
html: `
<h1>1</h1>
`,
async test({ assert, component, target }) {
component.foo = fakeStore(5);
return assert.htmlEqual(target.innerHTML, `<h1>5</h1>`);
},
};

View File

@ -0,0 +1,6 @@
<script>
import { writable } from 'svelte/store';
export let foo = writable(0);
</script>
<h1>{$foo}</h1>