Skip to content

Commit

Permalink
no-for-loop: Use generator function instead of return array (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 27, 2020
1 parent 2002093 commit da01e7b
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions rules/no-for-loop.js
Expand Up @@ -345,7 +345,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 @@ -372,24 +372,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

0 comments on commit da01e7b

Please sign in to comment.