Warn on improper use of reactive declarations

pull/2679/head
Emil Tholin 6 years ago
parent 985bf870d2
commit 0286dce026

@ -541,9 +541,21 @@ export default class Component {
}
walk_module_js() {
const component = this;
const script = this.ast.module;
if (!script) return;
walk(script.content, {
enter(node) {
if (node.type === 'LabeledStatement' && node.label.name === '$') {
component.warn(node, {
code: 'module-script-reactive-declaration',
message: '$: has no effect in a module script'
});
}
}
});
this.add_sourcemap_locations(script.content);
let { scope, globals } = create_scopes(script.content);
@ -735,6 +747,13 @@ export default class Component {
scope = map.get(node);
}
if (node.type === 'LabeledStatement' && node.label.name === '$' && parent.type !== 'Program') {
component.warn(node, {
code: 'non-top-level-reactive-declaration',
message: '$: has no effect outside of the top-level'
});
}
if (is_reference(node, parent)) {
const object = get_object(node);
const { name } = object;

@ -0,0 +1,5 @@
<script context="module">
let num = 2;
let square;
$: square = num * num;
</script>

@ -0,0 +1,7 @@
[{
"message": "$: has no effect in a module script",
"code": "module-script-reactive-declaration",
"start": { "line": 4, "column": 1, "character": 54 },
"end": { "line": 4, "column": 23, "character": 76 },
"pos": 54
}]

@ -0,0 +1,8 @@
<script>
let num = 2;
$: square = num * num;
function myFunc() {
$: double = num * 2;
}
</script>

@ -0,0 +1,7 @@
[{
"message": "$: has no effect outside of the top-level",
"code": "non-top-level-reactive-declaration",
"start": { "line": 6, "column": 2, "character": 71 },
"end": { "line": 6, "column": 22, "character": 91 },
"pos": 71
}]
Loading…
Cancel
Save