From 7751ee5fad31458e62359e38a154427c06304ce0 Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 25 May 2020 15:27:13 +0800 Subject: [PATCH] `no-for-loop`: Use generator function instead of return array --- 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 283ace5367..449952155e 100644 --- a/rules/no-for-loop.js +++ b/rules/no-for-loop.js @@ -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; @@ -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); + } + } }; }