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 <xydybaseball@gmail.com>
pull/8335/head
Vaibhav Rai 1 year ago committed by GitHub
parent 9edd2df0d3
commit 26104eaaba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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;

@ -0,0 +1,3 @@
export default {
html: '<p>2, 1</p>'
};

@ -0,0 +1,6 @@
<script>
const arr = [1, 2];
[arr[0], arr[1]] = [arr[1], arr[0]];
</script>
<p>{arr[0]}, {arr[1]}</p>

@ -0,0 +1,3 @@
export default {
html: '<p>[{"a":2},100]</p>'
};

@ -0,0 +1,7 @@
<script>
const a = 100;
const arr = [{ a: 1 }, 2];
[arr[0].a, arr[1] = a] = [arr[1]];
</script>
<p>{JSON.stringify(arr)}</p>

@ -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
}
]

@ -0,0 +1,6 @@
<script>
const arr = [1, 2];
[arr, arr[1]] = [arr[1], arr[0]];
</script>
{arr}

@ -0,0 +1,6 @@
<script>
const arr = [1, 2];
[arr[0], arr[1]] = [arr[1], arr[0]];
</script>
{arr}

@ -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
}
]

@ -0,0 +1,6 @@
<script>
const arr = [{1: {arr: 2}}, 2];
[{1: { arr }}, arr[1]] = [arr[1], arr[0]];
</script>
{arr}

@ -0,0 +1,6 @@
<script>
const arr = [1, 2];
[arr[0], arr[1] = arr] = [arr[1], arr[0]];
</script>
{arr}
Loading…
Cancel
Save