Skip to content

Commit 987aa30

Browse files
committed
Fix 6794: var scope should not extend reactive block
Fixes: #6794
1 parent 547fbc6 commit 987aa30

File tree

8 files changed

+78
-0
lines changed

8 files changed

+78
-0
lines changed

src/compiler/compile/Component.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,23 @@ export default class Component {
12841284
const module_dependencies = new Set<string>();
12851285

12861286
let scope = this.instance_scope;
1287+
const {declarations: outsetScopeDecalarations} = this.instance_scope;
12871288
const map = this.instance_scope_map;
12881289

12891290
walk(node.body, {
12901291
enter(node: Node, parent) {
1292+
if (node.type === 'VariableDeclaration' && node.kind === 'var') {
1293+
const names = extract_names(node.declarations[0].id);
1294+
const is_var_in_outset = names.every((i: string) => {
1295+
if (outsetScopeDecalarations.has(i)) {
1296+
const varNode = outsetScopeDecalarations.get(i);
1297+
return varNode === node;
1298+
}
1299+
});
1300+
if (is_var_in_outset) {
1301+
return component.error(node as any, compiler_errors.invalid_var_declaration);
1302+
}
1303+
}
12911304
if (map.has(node)) {
12921305
scope = map.get(node);
12931306
}

src/compiler/compile/compiler_errors.ts

+4
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,9 @@ export default {
241241
invalid_directive_value: {
242242
code: 'invalid-directive-value',
243243
message: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
244+
},
245+
invalid_var_declaration: {
246+
code: 'invalid_var_declaration',
247+
message: '"var" scope should not extend outside the reactive block'
244248
}
245249
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[{
2+
"code": "invalid_var_declaration",
3+
"message": "\"var\" scope should not extend outside the reactive block",
4+
"start": { "line": 14, "column": 7, "character": 204 },
5+
"end": { "line": 14, "column": 16, "character": 213 },
6+
"pos": 204
7+
}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script>
2+
var a;
3+
var {a, b: [d, f], c}= {a: 1, b: [1, 2], c: 2};
4+
$: {
5+
function one() {
6+
var a = 'a';
7+
function two() {
8+
var a = 'b';
9+
return a;
10+
}
11+
return two();
12+
}
13+
a = one();
14+
for (var i = 0; i<5; i ++ ) {
15+
// Todo
16+
}
17+
}
18+
</script>
19+
20+
<h1>Hello {a}</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[{
2+
"code": "invalid_var_declaration",
3+
"message": "\"var\" scope should not extend outside the reactive block",
4+
"start": { "line": 4, "column": 2, "character": 32 },
5+
"end": { "line": 4, "column": 50, "character": 80 },
6+
"pos": 32
7+
}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
$: {
3+
let f = 'f';
4+
var {a, b: [c, d], e} = {a: 1, b: [2, 3], e: 4};
5+
}
6+
</script>
7+
8+
<h1>Hello</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
var name = 'name';
3+
var c = 'cc';
4+
let d = 'd';
5+
$: {
6+
function a() {
7+
function b() {
8+
var c = 'c';
9+
return c;
10+
}
11+
return b;
12+
}
13+
let d = 'dd';
14+
name = a()();
15+
}
16+
</script>
17+
18+
<h1>Hello {name}</h1>

0 commit comments

Comments
 (0)