From 4c8f3a296c91504d9a3f7f8d5da7b8555a46091a Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sat, 3 Aug 2019 21:19:40 -0400 Subject: [PATCH] throw error if $ is referenced as global - fixes #3272 --- src/compiler/compile/Component.ts | 15 ++++++++++++++- .../samples/dollar-global-in-markup/errors.json | 15 +++++++++++++++ .../samples/dollar-global-in-markup/input.svelte | 1 + .../samples/dollar-global-in-script/errors.json | 15 +++++++++++++++ .../samples/dollar-global-in-script/input.svelte | 3 +++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/validator/samples/dollar-global-in-markup/errors.json create mode 100644 test/validator/samples/dollar-global-in-markup/input.svelte create mode 100644 test/validator/samples/dollar-global-in-script/errors.json create mode 100644 test/validator/samples/dollar-global-in-script/input.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 5b08b795ce..2ce2852d7e 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -662,7 +662,7 @@ export default class Component { this.node_for_declaration.set(name, node); }); - globals.forEach((_node, name) => { + globals.forEach((node, name) => { if (this.var_lookup.has(name)) return; if (this.injected_reactive_declaration_vars.has(name)) { @@ -679,6 +679,13 @@ export default class Component { injected: true }); } else if (name[0] === '$') { + if (name === '$') { + this.error(node, { + code: 'illegal-global', + message: 'The $ sign is an illegal variable name' + }); + } + this.add_var({ name, injected: true, @@ -1239,6 +1246,12 @@ export default class Component { this.has_reactive_assignments = true; // TODO does this belong here? if (name[0] === '$') return; // $$props + if (name === '') { + this.error(node, { + code: 'illegal-global', + message: 'The $ sign is an illegal variable name' + }); + } } if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return; diff --git a/test/validator/samples/dollar-global-in-markup/errors.json b/test/validator/samples/dollar-global-in-markup/errors.json new file mode 100644 index 0000000000..6447844f5c --- /dev/null +++ b/test/validator/samples/dollar-global-in-markup/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-global", + "message": "The $ sign is an illegal variable name", + "pos": 1, + "start": { + "line": 1, + "column": 1, + "character": 1 + }, + "end": { + "line": 1, + "column": 2, + "character": 2 + } +}] \ No newline at end of file diff --git a/test/validator/samples/dollar-global-in-markup/input.svelte b/test/validator/samples/dollar-global-in-markup/input.svelte new file mode 100644 index 0000000000..f6b2dceee5 --- /dev/null +++ b/test/validator/samples/dollar-global-in-markup/input.svelte @@ -0,0 +1 @@ +{$} \ No newline at end of file diff --git a/test/validator/samples/dollar-global-in-script/errors.json b/test/validator/samples/dollar-global-in-script/errors.json new file mode 100644 index 0000000000..a9fe6bb5d8 --- /dev/null +++ b/test/validator/samples/dollar-global-in-script/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-global", + "message": "The $ sign is an illegal variable name", + "pos": 10, + "start": { + "line": 2, + "column": 1, + "character": 10 + }, + "end": { + "line": 2, + "column": 2, + "character": 11 + } +}] \ No newline at end of file diff --git a/test/validator/samples/dollar-global-in-script/input.svelte b/test/validator/samples/dollar-global-in-script/input.svelte new file mode 100644 index 0000000000..a06da03657 --- /dev/null +++ b/test/validator/samples/dollar-global-in-script/input.svelte @@ -0,0 +1,3 @@ + \ No newline at end of file