From fa79a72348949467f7d845db82a45068c847cd71 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Tue, 26 Mar 2019 13:46:28 -0400 Subject: [PATCH 1/2] disallow bindings to global variables (#2295) --- src/compile/nodes/Binding.ts | 2 +- .../binding-invalid-value-global/errors.json | 15 +++++++++++++++ .../binding-invalid-value-global/input.svelte | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/validator/samples/binding-invalid-value-global/errors.json create mode 100644 test/validator/samples/binding-invalid-value-global/input.svelte diff --git a/src/compile/nodes/Binding.ts b/src/compile/nodes/Binding.ts index 939af8a8d9..3ab2cd8ff5 100644 --- a/src/compile/nodes/Binding.ts +++ b/src/compile/nodes/Binding.ts @@ -39,7 +39,7 @@ export default class Binding extends Node { } else { const variable = component.var_lookup.get(name); - if (!variable) component.error(this.expression.node, { + if (!variable || variable.global) component.error(this.expression.node, { code: 'binding-undeclared', message: `${name} is not declared` }); diff --git a/test/validator/samples/binding-invalid-value-global/errors.json b/test/validator/samples/binding-invalid-value-global/errors.json new file mode 100644 index 0000000000..be23a21523 --- /dev/null +++ b/test/validator/samples/binding-invalid-value-global/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "binding-undeclared", + "message": "foo is not declared", + "pos": 58, + "start": { + "line": 4, + "column": 19, + "character": 58 + }, + "end": { + "line": 4, + "column": 22, + "character": 61 + } +}] diff --git a/test/validator/samples/binding-invalid-value-global/input.svelte b/test/validator/samples/binding-invalid-value-global/input.svelte new file mode 100644 index 0000000000..81357cf5de --- /dev/null +++ b/test/validator/samples/binding-invalid-value-global/input.svelte @@ -0,0 +1,4 @@ + + From ecd7c6e9aaab1f9878f0f9576ac498e9e7baf273 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Tue, 26 Mar 2019 13:51:44 -0400 Subject: [PATCH 2/2] warn on template references to global variables (#2295) --- src/compile/Component.ts | 2 +- .../samples/undefined-value-global/input.svelte | 5 +++++ .../samples/undefined-value-global/warnings.json | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/validator/samples/undefined-value-global/input.svelte create mode 100644 test/validator/samples/undefined-value-global/warnings.json diff --git a/src/compile/Component.ts b/src/compile/Component.ts index f8ee5a0b78..21f0b6ef05 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -1182,7 +1182,7 @@ export default class Component { if (name[0] === '$') return; // $$props } - if (this.var_lookup.has(name)) return; + if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return; if (template_scope && template_scope.names.has(name)) return; if (globals.has(name)) return; diff --git a/test/validator/samples/undefined-value-global/input.svelte b/test/validator/samples/undefined-value-global/input.svelte new file mode 100644 index 0000000000..44e7332bf5 --- /dev/null +++ b/test/validator/samples/undefined-value-global/input.svelte @@ -0,0 +1,5 @@ + + +

{potato}

diff --git a/test/validator/samples/undefined-value-global/warnings.json b/test/validator/samples/undefined-value-global/warnings.json new file mode 100644 index 0000000000..ee9e3d6c90 --- /dev/null +++ b/test/validator/samples/undefined-value-global/warnings.json @@ -0,0 +1,15 @@ +[{ + "code": "missing-declaration", + "message": "'potato' is not defined", + "pos": 46, + "start": { + "line": 5, + "column": 4, + "character": 46 + }, + "end": { + "line": 5, + "column": 10, + "character": 52 + } +}]