From da01e7b75eb194410e93c9da7ae9f0f3940358ba Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 27 May 2020 12:56:32 +0800 Subject: [PATCH] `no-for-loop`: Use generator function instead of return array (#748) --- rules/no-for-loop.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/rules/no-for-loop.js b/rules/no-for-loop.js index 5d8803f8fe..07393681f9 100644 --- a/rules/no-for-loop.js +++ b/rules/no-for-loop.js @@ -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; @@ -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); + } + } }; }