From 4c8f3a296c91504d9a3f7f8d5da7b8555a46091a Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sat, 3 Aug 2019 21:19:40 -0400 Subject: [PATCH 1/2] 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 From 46cd6ff3707925071955ffc931671b630edf733c Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sun, 4 Aug 2019 09:22:14 -0400 Subject: [PATCH 2/2] disallow global references to $$-prefixed vars --- src/compiler/compile/Component.ts | 18 ++++++++++-------- .../dollar-dollar-global-in-markup/errors.json | 15 +++++++++++++++ .../input.svelte | 1 + .../dollar-dollar-global-in-script/errors.json | 15 +++++++++++++++ .../input.svelte | 3 +++ .../dollar-global-in-markup/errors.json | 2 +- .../dollar-global-in-script/errors.json | 2 +- 7 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 test/validator/samples/dollar-dollar-global-in-markup/errors.json create mode 100644 test/validator/samples/dollar-dollar-global-in-markup/input.svelte create mode 100644 test/validator/samples/dollar-dollar-global-in-script/errors.json create mode 100644 test/validator/samples/dollar-dollar-global-in-script/input.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 2ce2852d7e..709db1447d 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -679,10 +679,10 @@ export default class Component { injected: true }); } else if (name[0] === '$') { - if (name === '$') { + if (name === '$' || name[1] === '$') { this.error(node, { code: 'illegal-global', - message: 'The $ sign is an illegal variable name' + message: `${name} is an illegal variable name` }); } @@ -1242,16 +1242,18 @@ export default class Component { warn_if_undefined(name: string, node, template_scope: TemplateScope) { if (name[0] === '$') { - name = name.slice(1); - this.has_reactive_assignments = true; // TODO does this belong here? - - if (name[0] === '$') return; // $$props - if (name === '') { + if (name === '$' || name[1] === '$' && name !== '$$props') { this.error(node, { code: 'illegal-global', - message: 'The $ sign is an illegal variable name' + message: `${name} is an illegal variable name` }); } + + this.has_reactive_assignments = true; // TODO does this belong here? + + if (name === '$$props') return; + + name = name.slice(1); } if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return; diff --git a/test/validator/samples/dollar-dollar-global-in-markup/errors.json b/test/validator/samples/dollar-dollar-global-in-markup/errors.json new file mode 100644 index 0000000000..4730c5152e --- /dev/null +++ b/test/validator/samples/dollar-dollar-global-in-markup/errors.json @@ -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 + } +}] \ No newline at end of file diff --git a/test/validator/samples/dollar-dollar-global-in-markup/input.svelte b/test/validator/samples/dollar-dollar-global-in-markup/input.svelte new file mode 100644 index 0000000000..d197237056 --- /dev/null +++ b/test/validator/samples/dollar-dollar-global-in-markup/input.svelte @@ -0,0 +1 @@ +{$$billsyall} \ No newline at end of file diff --git a/test/validator/samples/dollar-dollar-global-in-script/errors.json b/test/validator/samples/dollar-dollar-global-in-script/errors.json new file mode 100644 index 0000000000..5c923e35f7 --- /dev/null +++ b/test/validator/samples/dollar-dollar-global-in-script/errors.json @@ -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 + } +}] \ No newline at end of file diff --git a/test/validator/samples/dollar-dollar-global-in-script/input.svelte b/test/validator/samples/dollar-dollar-global-in-script/input.svelte new file mode 100644 index 0000000000..9f7fc06a7d --- /dev/null +++ b/test/validator/samples/dollar-dollar-global-in-script/input.svelte @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/test/validator/samples/dollar-global-in-markup/errors.json b/test/validator/samples/dollar-global-in-markup/errors.json index 6447844f5c..6cdf67e872 100644 --- a/test/validator/samples/dollar-global-in-markup/errors.json +++ b/test/validator/samples/dollar-global-in-markup/errors.json @@ -1,6 +1,6 @@ [{ "code": "illegal-global", - "message": "The $ sign is an illegal variable name", + "message": "$ is an illegal variable name", "pos": 1, "start": { "line": 1, diff --git a/test/validator/samples/dollar-global-in-script/errors.json b/test/validator/samples/dollar-global-in-script/errors.json index a9fe6bb5d8..2a6bd7ce7a 100644 --- a/test/validator/samples/dollar-global-in-script/errors.json +++ b/test/validator/samples/dollar-global-in-script/errors.json @@ -1,6 +1,6 @@ [{ "code": "illegal-global", - "message": "The $ sign is an illegal variable name", + "message": "$ is an illegal variable name", "pos": 10, "start": { "line": 2,