allow $ references in reactive declarations

pull/1880/head
Rich Harris 7 years ago
parent 102d28fb12
commit 6da02329a3

@ -813,10 +813,19 @@ export default class Component {
assignees.add(getObject(node.argument).name); assignees.add(getObject(node.argument).name);
this.skip(); this.skip();
} else if (isReference(node, parent)) { } else if (isReference(node, parent)) {
const { name } = getObject(node); const object = getObject(node);
const { name } = object;
if (component.declarations.indexOf(name) !== -1) { if (component.declarations.indexOf(name) !== -1) {
dependencies.add(name); dependencies.add(name);
} else if (name[0] === '$') {
component.warn_if_undefined(object, null);
// cheeky hack
component.template_references.add(name);
dependencies.add(name);
} }
this.skip(); this.skip();
} }
}, },
@ -907,7 +916,7 @@ export default class Component {
warn_if_undefined(node, template_scope: TemplateScope, allow_implicit?: boolean) { warn_if_undefined(node, template_scope: TemplateScope, allow_implicit?: boolean) {
let { name } = node; let { name } = node;
if (allow_implicit && name[0] === '$') { if (name[0] === '$') {
name = name.slice(1); name = name.slice(1);
this.has_reactive_assignments = true; this.has_reactive_assignments = true;
} }
@ -915,7 +924,7 @@ export default class Component {
if (allow_implicit && !this.instance_script) return; if (allow_implicit && !this.instance_script) return;
if (this.instance_scope && this.instance_scope.declarations.has(name)) return; if (this.instance_scope && this.instance_scope.declarations.has(name)) return;
if (this.module_scope && this.module_scope.declarations.has(name)) return; if (this.module_scope && this.module_scope.declarations.has(name)) return;
if (template_scope.names.has(name)) return; if (template_scope && template_scope.names.has(name)) return;
if (globalWhitelist.has(name)) return; if (globalWhitelist.has(name)) return;
this.warn(node, { this.warn(node, {

@ -0,0 +1,28 @@
import { writable } from '../../../../store.js';
export default {
props: {
count: writable(0)
},
html: `
<button>double 0</button>
`,
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, `
<button>double 2</button>
`);
await component.count.set(42);
assert.htmlEqual(target.innerHTML, `
<button>double 84</button>
`);
}
};

@ -0,0 +1,8 @@
<script>
export let count;
let double;
$: double = $count * 2;
</script>
<button on:click="{() => count.update(n => n + 1)}">double {double}</button>
Loading…
Cancel
Save