Skip to content

Commit a59cd36

Browse files
authored
Merge pull request sveltejs#2321 from sveltejs/sveltejsgh-2295
Fix exception and warning for global variables
2 parents 33bf4a4 + ecd7c6e commit a59cd36

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

src/compile/Component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ export default class Component {
11821182
if (name[0] === '$') return; // $$props
11831183
}
11841184

1185-
if (this.var_lookup.has(name)) return;
1185+
if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return;
11861186
if (template_scope && template_scope.names.has(name)) return;
11871187
if (globals.has(name)) return;
11881188

src/compile/nodes/Binding.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class Binding extends Node {
3939
} else {
4040
const variable = component.var_lookup.get(name);
4141

42-
if (!variable) component.error(this.expression.node, {
42+
if (!variable || variable.global) component.error(this.expression.node, {
4343
code: 'binding-undeclared',
4444
message: `${name} is not declared`
4545
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[{
2+
"code": "binding-undeclared",
3+
"message": "foo is not declared",
4+
"pos": 58,
5+
"start": {
6+
"line": 4,
7+
"column": 19,
8+
"character": 58
9+
},
10+
"end": {
11+
"line": 4,
12+
"column": 22,
13+
"character": 61
14+
}
15+
}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
console.log(foo);
3+
</script>
4+
<input bind:value={foo}>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
console.log(potato);
3+
</script>
4+
5+
<p>{potato}</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[{
2+
"code": "missing-declaration",
3+
"message": "'potato' is not defined",
4+
"pos": 46,
5+
"start": {
6+
"line": 5,
7+
"column": 4,
8+
"character": 46
9+
},
10+
"end": {
11+
"line": 5,
12+
"column": 10,
13+
"character": 52
14+
}
15+
}]

0 commit comments

Comments
 (0)