Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix invalid autofix with array destructuring in no-for-loops rule (#…
  • Loading branch information
fisker authored and sindresorhus committed Dec 30, 2019
1 parent 1db098d commit 94345a3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rules/no-for-loop.js
Expand Up @@ -334,7 +334,7 @@ const create = context => {
let removeDeclaration = true;
if (
elementNode &&
elementNode.id.type === 'ObjectPattern'
(elementNode.id.type === 'ObjectPattern' || elementNode.id.type === 'ArrayPattern')
) {
removeDeclaration = arrayReferences.length === 1;

Expand Down
62 changes: 62 additions & 0 deletions test/no-for-loop.js
Expand Up @@ -369,6 +369,16 @@ ruleTester.run('no-for-loop', rule, {
console.log(a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
const [ a, b ] = arr[i];
console.log(a, b);
}
`, outdent`
for (const [ a, b ] of arr) {
console.log(a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
var { a, b } = arr[i];
Expand All @@ -379,6 +389,16 @@ ruleTester.run('no-for-loop', rule, {
console.log(a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
var [ a, b ] = arr[i];
console.log(a, b);
}
`, outdent`
for (var [ a, b ] of arr) {
console.log(a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
let { a, b } = arr[i];
Expand All @@ -389,6 +409,16 @@ ruleTester.run('no-for-loop', rule, {
console.log(a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
let [ a, b ] = arr[i];
console.log(a, b);
}
`, outdent`
for (let [ a, b ] of arr) {
console.log(a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
var { a, b } = arr[i];
Expand All @@ -399,6 +429,16 @@ ruleTester.run('no-for-loop', rule, {
console.log(i, a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
var [ a, b ] = arr[i];
console.log(i, a, b);
}
`, outdent`
for (var [i, [ a, b ]] of arr.entries()) {
console.log(i, a, b);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
const { a, b } = arr[i];
Expand All @@ -410,6 +450,17 @@ ruleTester.run('no-for-loop', rule, {
console.log(a, b, i, element);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
const [ a, b ] = arr[i];
console.log(a, b, i, arr[i]);
}
`, outdent`
for (const [i, element] of arr.entries()) {
const [ a, b ] = element;
console.log(a, b, i, element);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
const { a, b } = arr[i];
Expand All @@ -420,6 +471,17 @@ ruleTester.run('no-for-loop', rule, {
const { a, b } = element;
console.log(a, b, element);
}
`),
testCase(outdent`
for (let i = 0; i < arr.length; i++) {
const [ a, b ] = arr[i];
console.log(a, b, arr[i]);
}
`, outdent`
for (const element of arr) {
const [ a, b ] = element;
console.log(a, b, element);
}
`)
]
});
Expand Down

0 comments on commit 94345a3

Please sign in to comment.