Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid autofix with destructuring assignment in no-for-loops rule #476

Merged
merged 5 commits into from Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions rules/no-for-loop.js
Expand Up @@ -329,9 +329,24 @@ const create = context => {
const element = elementIdentifierName || defaultElementName;
const array = arrayIdentifierName;

let declarationElement = element;
let declarationType = 'const';
let removeDeclaration = true;
if (
elementNode &&
elementNode.id.type === 'ObjectPattern'
) {
removeDeclaration = arrayReferences.length === 1;

if (removeDeclaration) {
declarationType = elementNode.parent.kind;
declarationElement = sourceCode.getText(elementNode.id);
}
}

const replacement = shouldGenerateIndex ?
`const [${index}, ${element}] of ${array}.entries()` :
`const ${element} of ${array}`;
`${declarationType} [${index}, ${declarationElement}] of ${array}.entries()` :
`${declarationType} ${declarationElement} of ${array}`;

return [
fixer.replaceTextRange([
Expand All @@ -345,7 +360,11 @@ const create = context => {

return fixer.replaceText(reference.identifier.parent, element);
}),
elementNode && fixer.removeRange(getRemovalRange(elementNode, sourceCode))
elementNode && (
removeDeclaration ?
fixer.removeRange(getRemovalRange(elementNode, sourceCode)) :
fixer.replaceText(elementNode.init, element)
)
].filter(Boolean);
};
}
Expand Down
64 changes: 64 additions & 0 deletions test/no-for-loop.js
Expand Up @@ -356,6 +356,70 @@ ruleTester.run('no-for-loop', rule, {
use(element);
}
}
`),

// Destructuring assignment in usage:
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];
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];
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];
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];
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];
console.log(a, b, arr[i]);
}
`, outdent`
for (const element of arr) {
const { a, b } = element;
console.log(a, b, element);
}
`)
]
});
Expand Down