Skip to content

Commit

Permalink
Fix 6794: var scope should not extend reactive block
Browse files Browse the repository at this point in the history
Fixes: #6794
  • Loading branch information
RaiVaibhav committed Oct 2, 2021
1 parent 547fbc6 commit 987aa30
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1284,10 +1284,23 @@ export default class Component {
const module_dependencies = new Set<string>();

let scope = this.instance_scope;
const {declarations: outsetScopeDecalarations} = this.instance_scope;
const map = this.instance_scope_map;

walk(node.body, {
enter(node: Node, parent) {
if (node.type === 'VariableDeclaration' && node.kind === 'var') {
const names = extract_names(node.declarations[0].id);
const is_var_in_outset = names.every((i: string) => {
if (outsetScopeDecalarations.has(i)) {
const varNode = outsetScopeDecalarations.get(i);
return varNode === node;
}
});
if (is_var_in_outset) {
return component.error(node as any, compiler_errors.invalid_var_declaration);
}
}
if (map.has(node)) {
scope = map.get(node);
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/compile/compiler_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,9 @@ export default {
invalid_directive_value: {
code: 'invalid-directive-value',
message: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
},
invalid_var_declaration: {
code: 'invalid_var_declaration',
message: '"var" scope should not extend outside the reactive block'
}
};
7 changes: 7 additions & 0 deletions test/validator/samples/invalid-reactive-var-1/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[{
"code": "invalid_var_declaration",
"message": "\"var\" scope should not extend outside the reactive block",
"start": { "line": 14, "column": 7, "character": 204 },
"end": { "line": 14, "column": 16, "character": 213 },
"pos": 204
}]
20 changes: 20 additions & 0 deletions test/validator/samples/invalid-reactive-var-1/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
var a;
var {a, b: [d, f], c}= {a: 1, b: [1, 2], c: 2};
$: {
function one() {
var a = 'a';
function two() {
var a = 'b';
return a;
}
return two();
}
a = one();
for (var i = 0; i<5; i ++ ) {
// Todo
}
}
</script>

<h1>Hello {a}</h1>
7 changes: 7 additions & 0 deletions test/validator/samples/invalid-reactive-var-2/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[{
"code": "invalid_var_declaration",
"message": "\"var\" scope should not extend outside the reactive block",
"start": { "line": 4, "column": 2, "character": 32 },
"end": { "line": 4, "column": 50, "character": 80 },
"pos": 32
}]
8 changes: 8 additions & 0 deletions test/validator/samples/invalid-reactive-var-2/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
$: {
let f = 'f';
var {a, b: [c, d], e} = {a: 1, b: [2, 3], e: 4};
}
</script>

<h1>Hello</h1>
1 change: 1 addition & 0 deletions test/validator/samples/valid-reactive-vars/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
18 changes: 18 additions & 0 deletions test/validator/samples/valid-reactive-vars/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
var name = 'name';
var c = 'cc';
let d = 'd';
$: {
function a() {
function b() {
var c = 'c';
return c;
}
return b;
}
let d = 'dd';
name = a()();
}
</script>

<h1>Hello {name}</h1>

0 comments on commit 987aa30

Please sign in to comment.