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

no-for-loop: Use generator function instead of return array #748

Merged
merged 1 commit into from May 27, 2020
Merged
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
38 changes: 19 additions & 19 deletions rules/no-for-loop.js
Expand Up @@ -331,7 +331,7 @@ const create = context => {
const shouldFix = !someVariablesLeakOutOfTheLoop(node, [indexVariable, elementVariable].filter(Boolean), forScope);

if (shouldFix) {
problem.fix = fixer => {
problem.fix = function * (fixer) {
const shouldGenerateIndex = isIndexVariableUsedElsewhereInTheLoopBody(indexVariable, bodyScope, arrayIdentifierName);

const index = indexIdentifierName;
Expand All @@ -357,24 +357,24 @@ const create = context => {
`${declarationType} [${index}, ${declarationElement}] of ${array}.entries()` :
`${declarationType} ${declarationElement} of ${array}`;

return [
fixer.replaceTextRange([
node.init.range[0],
node.update.range[1]
], replacement),
...arrayReferences.map(reference => {
if (reference === elementReference) {
return;
}

return fixer.replaceText(reference.identifier.parent, element);
}),
elementNode && (
removeDeclaration ?
fixer.removeRange(getRemovalRange(elementNode, sourceCode)) :
fixer.replaceText(elementNode.init, element)
)
].filter(Boolean);
yield fixer.replaceTextRange([
node.init.range[0],
node.update.range[1]
], replacement);

for (const reference of arrayReferences) {
if (reference !== elementReference) {
yield fixer.replaceText(reference.identifier.parent, element);
}
}

if (elementNode) {
if (removeDeclaration) {
yield fixer.removeRange(getRemovalRange(elementNode, sourceCode));
} else {
yield fixer.replaceText(elementNode.init, element);
}
}
};
}

Expand Down