From 0fc9ea08ff9ff062b18eae68464c453e5a75d11f Mon Sep 17 00:00:00 2001 From: Billy Levin Date: Wed, 10 Jun 2020 23:47:27 +0100 Subject: [PATCH] fix logic and add more tests --- .../compile/nodes/shared/Expression.ts | 12 ++++++----- .../samples/assignment-to-const-3/errors.json | 17 +++++++++++++++ .../assignment-to-const-3/input.svelte | 20 ++++++++++++++++++ .../samples/assignment-to-const-4/errors.json | 17 +++++++++++++++ .../assignment-to-const-4/input.svelte | 21 +++++++++++++++++++ .../samples/assignment-to-const/errors.json | 6 +++--- .../samples/assignment-to-const/input.svelte | 12 +++++------ 7 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 test/validator/samples/assignment-to-const-3/errors.json create mode 100644 test/validator/samples/assignment-to-const-3/input.svelte create mode 100644 test/validator/samples/assignment-to-const-4/errors.json create mode 100644 test/validator/samples/assignment-to-const-4/input.svelte diff --git a/src/compiler/compile/nodes/shared/Expression.ts b/src/compiler/compile/nodes/shared/Expression.ts index 4410943a31..6cbab30b44 100644 --- a/src/compiler/compile/nodes/shared/Expression.ts +++ b/src/compiler/compile/nodes/shared/Expression.ts @@ -164,11 +164,13 @@ export default class Expression { current_scope = current_scope.parent; } - if (declaration && declaration.kind === 'const' && !deep) { - component.error(node as any, { - code: 'assignment-to-const', - message: 'You are assigning to a const' - }); + if (declaration) { + if (declaration.kind === 'const' && !deep) { + component.error(node as any, { + code: 'assignment-to-const', + message: 'You are assigning to a const' + }); + } } else if (variable && variable.writable === false && !deep) { component.error(node as any, { code: 'assignment-to-const', diff --git a/test/validator/samples/assignment-to-const-3/errors.json b/test/validator/samples/assignment-to-const-3/errors.json new file mode 100644 index 0000000000..4842d55dc3 --- /dev/null +++ b/test/validator/samples/assignment-to-const-3/errors.json @@ -0,0 +1,17 @@ +[ + { + "code": "assignment-to-const", + "message": "You are assigning to a const", + "start": { + "line": 14, + "column": 3, + "character": 172 + }, + "end": { + "line": 14, + "column": 10, + "character": 179 + }, + "pos": 172 + } +] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-3/input.svelte b/test/validator/samples/assignment-to-const-3/input.svelte new file mode 100644 index 0000000000..faaf448823 --- /dev/null +++ b/test/validator/samples/assignment-to-const-3/input.svelte @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-4/errors.json b/test/validator/samples/assignment-to-const-4/errors.json new file mode 100644 index 0000000000..aa97538a60 --- /dev/null +++ b/test/validator/samples/assignment-to-const-4/errors.json @@ -0,0 +1,17 @@ +[ + { + "code": "assignment-to-const", + "message": "You are assigning to a const", + "start": { + "line": 17, + "column": 2, + "character": 189 + }, + "end": { + "line": 17, + "column": 9, + "character": 196 + }, + "pos": 189 + } +] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-4/input.svelte b/test/validator/samples/assignment-to-const-4/input.svelte new file mode 100644 index 0000000000..12229132bf --- /dev/null +++ b/test/validator/samples/assignment-to-const-4/input.svelte @@ -0,0 +1,21 @@ + + + + + \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const/errors.json b/test/validator/samples/assignment-to-const/errors.json index 9e673499f0..7f4f264f1d 100644 --- a/test/validator/samples/assignment-to-const/errors.json +++ b/test/validator/samples/assignment-to-const/errors.json @@ -5,13 +5,13 @@ "start": { "line": 16, "column": 2, - "character": 237 + "character": 225 }, "end": { "line": 16, "column": 18, - "character": 253 + "character": 241 }, - "pos": 237 + "pos": 225 } ] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const/input.svelte b/test/validator/samples/assignment-to-const/input.svelte index fa688a1202..d570e7c86b 100644 --- a/test/validator/samples/assignment-to-const/input.svelte +++ b/test/validator/samples/assignment-to-const/input.svelte @@ -4,21 +4,21 @@ const obj1 = { prop: true }; const obj2 = { prop: 0 }; - function funcShouldNotError() { + function shouldNotError() { obj1.prop = false; } - function funcShouldNotError2() { + function shouldNotError2() { obj2.prop++; } - function funcShouldError() { + function shouldError() { immutable = true } - - - + + +