From 58b512752f58f678f017df35de50193c752ad039 Mon Sep 17 00:00:00 2001 From: Vaibhav Rai Date: Fri, 21 Oct 2022 22:02:52 +0530 Subject: [PATCH] [fix] Error assigning to a const while element swapping --- src/compiler/compile/Component.ts | 21 +++++++++++++------ .../samples/assignment-to-const-5/errors.json | 17 +++++++++++++++ .../assignment-to-const-5/input.svelte | 6 ++++++ .../samples/assignment-to-const-6/errors.json | 1 + .../assignment-to-const-6/input.svelte | 6 ++++++ .../samples/assignment-to-const-7/errors.json | 17 +++++++++++++++ .../assignment-to-const-7/input.svelte | 6 ++++++ 7 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 test/validator/samples/assignment-to-const-5/errors.json create mode 100644 test/validator/samples/assignment-to-const-5/input.svelte create mode 100644 test/validator/samples/assignment-to-const-6/errors.json create mode 100644 test/validator/samples/assignment-to-const-6/input.svelte create mode 100644 test/validator/samples/assignment-to-const-7/errors.json create mode 100644 test/validator/samples/assignment-to-const-7/input.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index c0d703892b..4b27488bf4 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -793,9 +793,19 @@ export default class Component { } let deep = false; - let names: string[] | undefined; - - if (node.type === 'AssignmentExpression') { + let names: string[] = []; + + if (node.type === 'AssignmentExpression' && + node.left.type === 'ArrayPattern') { + walk(node.left, { + enter(node: Node, parent: Node) { + if (node.type === 'Identifier' && + parent.type !== 'MemberExpression') { + names.push(node.name); + } + } + }); + } else if (node.type === 'AssignmentExpression') { deep = node.left.type === 'MemberExpression'; names = deep ? [get_object(node.left).name] @@ -803,10 +813,9 @@ export default class Component { } else if (node.type === 'UpdateExpression') { deep = node.argument.type === 'MemberExpression'; const { name } = get_object(node.argument); - names = [name]; + names.push(name); } - - if (names) { + if (names.length > 0) { names.forEach(name => { let current_scope = scope; let declaration; diff --git a/test/validator/samples/assignment-to-const-5/errors.json b/test/validator/samples/assignment-to-const-5/errors.json new file mode 100644 index 0000000000..21de70863c --- /dev/null +++ b/test/validator/samples/assignment-to-const-5/errors.json @@ -0,0 +1,17 @@ +[ + { + "code": "assignment-to-const", + "message": "You are assigning to a const", + "start": { + "line": 3, + "column": 1, + "character": 31 + }, + "end": { + "line": 3, + "column": 33, + "character": 63 + }, + "pos": 31 + } +] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-5/input.svelte b/test/validator/samples/assignment-to-const-5/input.svelte new file mode 100644 index 0000000000..76907b94be --- /dev/null +++ b/test/validator/samples/assignment-to-const-5/input.svelte @@ -0,0 +1,6 @@ + + +{arr} \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-6/errors.json b/test/validator/samples/assignment-to-const-6/errors.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/test/validator/samples/assignment-to-const-6/errors.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-6/input.svelte b/test/validator/samples/assignment-to-const-6/input.svelte new file mode 100644 index 0000000000..8da59b261e --- /dev/null +++ b/test/validator/samples/assignment-to-const-6/input.svelte @@ -0,0 +1,6 @@ + + +{arr} \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-7/errors.json b/test/validator/samples/assignment-to-const-7/errors.json new file mode 100644 index 0000000000..34c88bebf6 --- /dev/null +++ b/test/validator/samples/assignment-to-const-7/errors.json @@ -0,0 +1,17 @@ +[ + { + "code": "assignment-to-const", + "message": "You are assigning to a const", + "start": { + "line": 3, + "column": 1, + "character": 43 + }, + "end": { + "line": 3, + "column": 42, + "character": 84 + }, + "pos": 43 + } +] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-7/input.svelte b/test/validator/samples/assignment-to-const-7/input.svelte new file mode 100644 index 0000000000..ed7b37ae26 --- /dev/null +++ b/test/validator/samples/assignment-to-const-7/input.svelte @@ -0,0 +1,6 @@ + + +{arr} \ No newline at end of file