[fix] Error assigning to a const while element swapping

pull/7966/head
Vaibhav Rai 4 years ago
parent 146e7a6310
commit 58b512752f

@ -793,9 +793,19 @@ export default class Component {
} }
let deep = false; let deep = false;
let names: string[] | undefined; let names: string[] = [];
if (node.type === 'AssignmentExpression') { 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'; deep = node.left.type === 'MemberExpression';
names = deep names = deep
? [get_object(node.left).name] ? [get_object(node.left).name]
@ -803,10 +813,9 @@ export default class Component {
} else if (node.type === 'UpdateExpression') { } else if (node.type === 'UpdateExpression') {
deep = node.argument.type === 'MemberExpression'; deep = node.argument.type === 'MemberExpression';
const { name } = get_object(node.argument); const { name } = get_object(node.argument);
names = [name]; names.push(name);
} }
if (names.length > 0) {
if (names) {
names.forEach(name => { names.forEach(name => {
let current_scope = scope; let current_scope = scope;
let declaration; let declaration;

@ -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}
Loading…
Cancel
Save