From e06a900b2331e70f8b057dcbd36f6bd304f4c7dc Mon Sep 17 00:00:00 2001 From: vlasy Date: Sat, 14 Mar 2020 21:15:37 +0100 Subject: [PATCH] throw compiler error when binding directly to const variables (#4506) --- src/compiler/compile/nodes/Binding.ts | 5 +++++ .../samples/binding-const-field/errors.json | 1 + .../samples/binding-const-field/input.svelte | 7 +++++++ test/validator/samples/binding-const/errors.json | 15 +++++++++++++++ test/validator/samples/binding-const/input.svelte | 5 +++++ 5 files changed, 33 insertions(+) create mode 100644 test/validator/samples/binding-const-field/errors.json create mode 100644 test/validator/samples/binding-const-field/input.svelte create mode 100644 test/validator/samples/binding-const/errors.json create mode 100644 test/validator/samples/binding-const/input.svelte diff --git a/src/compiler/compile/nodes/Binding.ts b/src/compiler/compile/nodes/Binding.ts index 7d6fad0a81..2f199709c8 100644 --- a/src/compiler/compile/nodes/Binding.ts +++ b/src/compiler/compile/nodes/Binding.ts @@ -72,6 +72,11 @@ export default class Binding extends Node { }); variable[this.expression.node.type === 'MemberExpression' ? 'mutated' : 'reassigned'] = true; + + if (info.expression.type === 'Identifier' && !variable.writable) component.error(this.expression.node, { + code: 'invalid-binding', + message: 'Cannot bind to a variable which is not writable', + }); } const type = parent.get_static_attribute_value('type'); diff --git a/test/validator/samples/binding-const-field/errors.json b/test/validator/samples/binding-const-field/errors.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/test/validator/samples/binding-const-field/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/binding-const-field/input.svelte b/test/validator/samples/binding-const-field/input.svelte new file mode 100644 index 0000000000..055a16438d --- /dev/null +++ b/test/validator/samples/binding-const-field/input.svelte @@ -0,0 +1,7 @@ + + + diff --git a/test/validator/samples/binding-const/errors.json b/test/validator/samples/binding-const/errors.json new file mode 100644 index 0000000000..6d48af9c4e --- /dev/null +++ b/test/validator/samples/binding-const/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "invalid-binding", + "message": "Cannot bind to a variable which is not writable", + "pos": 61, + "start": { + "line": 5, + "column": 19, + "character": 61 + }, + "end": { + "line": 5, + "column": 24, + "character": 66 + } +}] \ No newline at end of file diff --git a/test/validator/samples/binding-const/input.svelte b/test/validator/samples/binding-const/input.svelte new file mode 100644 index 0000000000..1857a1932c --- /dev/null +++ b/test/validator/samples/binding-const/input.svelte @@ -0,0 +1,5 @@ + + +