Merge pull request #3348 from sveltejs/gh-3272

throw error if $ is referenced as global
pull/3358/head
Rich Harris 5 years ago committed by GitHub
commit e41a4846c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -669,7 +669,7 @@ export default class Component {
this.node_for_declaration.set(name, node); this.node_for_declaration.set(name, node);
}); });
globals.forEach((_node, name) => { globals.forEach((node, name) => {
if (this.var_lookup.has(name)) return; if (this.var_lookup.has(name)) return;
if (this.injected_reactive_declaration_vars.has(name)) { if (this.injected_reactive_declaration_vars.has(name)) {
@ -686,6 +686,13 @@ export default class Component {
injected: true injected: true
}); });
} else if (name[0] === '$') { } else if (name[0] === '$') {
if (name === '$' || name[1] === '$') {
this.error(node, {
code: 'illegal-global',
message: `${name} is an illegal variable name`
});
}
this.add_var({ this.add_var({
name, name,
injected: true, injected: true,
@ -1242,10 +1249,18 @@ export default class Component {
warn_if_undefined(name: string, node, template_scope: TemplateScope) { warn_if_undefined(name: string, node, template_scope: TemplateScope) {
if (name[0] === '$') { if (name[0] === '$') {
name = name.slice(1); if (name === '$' || name[1] === '$' && name !== '$$props') {
this.error(node, {
code: 'illegal-global',
message: `${name} is an illegal variable name`
});
}
this.has_reactive_assignments = true; // TODO does this belong here? this.has_reactive_assignments = true; // TODO does this belong here?
if (name[0] === '$') return; // $$props if (name === '$$props') return;
name = name.slice(1);
} }
if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return; if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return;

@ -0,0 +1,15 @@
[{
"code": "illegal-global",
"message": "$$billsyall is an illegal variable name",
"pos": 1,
"start": {
"line": 1,
"column": 1,
"character": 1
},
"end": {
"line": 1,
"column": 12,
"character": 12
}
}]

@ -0,0 +1,15 @@
[{
"code": "illegal-global",
"message": "$$billsyall is an illegal variable name",
"pos": 10,
"start": {
"line": 2,
"column": 1,
"character": 10
},
"end": {
"line": 2,
"column": 12,
"character": 21
}
}]

@ -0,0 +1,15 @@
[{
"code": "illegal-global",
"message": "$ is an illegal variable name",
"pos": 1,
"start": {
"line": 1,
"column": 1,
"character": 1
},
"end": {
"line": 1,
"column": 2,
"character": 2
}
}]

@ -0,0 +1,15 @@
[{
"code": "illegal-global",
"message": "$ is an illegal variable name",
"pos": 10,
"start": {
"line": 2,
"column": 1,
"character": 10
},
"end": {
"line": 2,
"column": 2,
"character": 11
}
}]
Loading…
Cancel
Save