From 26104eaaba38ad1e14221870c2c97d00f51ef868 Mon Sep 17 00:00:00 2001 From: Vaibhav Rai Date: Mon, 27 Feb 2023 18:15:14 +0530 Subject: [PATCH] fix: no error assigning to a `const` property (#7966) Fixes #7964 currently for a case where the parent type is ArrayPattern code needs to check if the elements are of direct type MemberExpression or Identifier, in the case of MemberExpression there will be an Identifier check for the Object of the MemberExpression. --------- Co-authored-by: Yuichiro Yamashita --- src/compiler/compile/Component.ts | 27 +++++++++++++------ .../samples/assignment-to-const1/_config.js | 3 +++ .../samples/assignment-to-const1/main.svelte | 6 +++++ .../samples/assignment-to-const2/_config.js | 3 +++ .../samples/assignment-to-const2/main.svelte | 7 +++++ .../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 +++++ .../samples/assignment-to-const-8/errors.json | 1 + .../assignment-to-const-8/input.svelte | 6 +++++ 13 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 test/runtime/samples/assignment-to-const1/_config.js create mode 100644 test/runtime/samples/assignment-to-const1/main.svelte create mode 100644 test/runtime/samples/assignment-to-const2/_config.js create mode 100644 test/runtime/samples/assignment-to-const2/main.svelte 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 create mode 100644 test/validator/samples/assignment-to-const-8/errors.json create mode 100644 test/validator/samples/assignment-to-const-8/input.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index ffac6e1f07..f7d8e8f568 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -794,20 +794,31 @@ export default class Component { } let deep = false; - let names: string[] | undefined; + let names: string[] = []; if (node.type === 'AssignmentExpression') { - deep = node.left.type === 'MemberExpression'; - names = deep - ? [get_object(node.left).name] - : extract_names(node.left); + if (node.left.type === 'ArrayPattern') { + walk(node.left, { + enter(node: Node, parent: Node) { + if (node.type === 'Identifier' && + parent.type !== 'MemberExpression' && + (parent.type !== 'AssignmentPattern' || parent.right !== node)) { + names.push(node.name); + } + } + }); + } else { + deep = node.left.type === 'MemberExpression'; + names = deep + ? [get_object(node.left).name] + : extract_names(node.left); + } } 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/runtime/samples/assignment-to-const1/_config.js b/test/runtime/samples/assignment-to-const1/_config.js new file mode 100644 index 0000000000..2fd0447458 --- /dev/null +++ b/test/runtime/samples/assignment-to-const1/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

2, 1

' +}; diff --git a/test/runtime/samples/assignment-to-const1/main.svelte b/test/runtime/samples/assignment-to-const1/main.svelte new file mode 100644 index 0000000000..f221249193 --- /dev/null +++ b/test/runtime/samples/assignment-to-const1/main.svelte @@ -0,0 +1,6 @@ + + +

{arr[0]}, {arr[1]}

diff --git a/test/runtime/samples/assignment-to-const2/_config.js b/test/runtime/samples/assignment-to-const2/_config.js new file mode 100644 index 0000000000..9802ca2c44 --- /dev/null +++ b/test/runtime/samples/assignment-to-const2/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

[{"a":2},100]

' +}; diff --git a/test/runtime/samples/assignment-to-const2/main.svelte b/test/runtime/samples/assignment-to-const2/main.svelte new file mode 100644 index 0000000000..c805e4b589 --- /dev/null +++ b/test/runtime/samples/assignment-to-const2/main.svelte @@ -0,0 +1,7 @@ + + +

{JSON.stringify(arr)}

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 diff --git a/test/validator/samples/assignment-to-const-8/errors.json b/test/validator/samples/assignment-to-const-8/errors.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/test/validator/samples/assignment-to-const-8/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/assignment-to-const-8/input.svelte b/test/validator/samples/assignment-to-const-8/input.svelte new file mode 100644 index 0000000000..8af787f4f0 --- /dev/null +++ b/test/validator/samples/assignment-to-const-8/input.svelte @@ -0,0 +1,6 @@ + + +{arr}