From f7a1e391396763fad607b4e778b0c4ee54126ec7 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 9 Dec 2017 10:21:23 -0500 Subject: [PATCH] tools: simplify no-let-in-for-declaration rule PR-URL: https://github.com/nodejs/node/pull/17572 Reviewed-By: Anatoli Papirovski --- .../eslint-rules/no-let-in-for-declaration.js | 46 ++++++------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/tools/eslint-rules/no-let-in-for-declaration.js b/tools/eslint-rules/no-let-in-for-declaration.js index 34ad2d5761fda2..1ae49a48dee8a2 100644 --- a/tools/eslint-rules/no-let-in-for-declaration.js +++ b/tools/eslint-rules/no-let-in-for-declaration.js @@ -11,46 +11,28 @@ //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ +const message = 'Use of `let` as the loop variable in a for-loop is ' + + 'not recommended. Please use `var` instead.'; +const forSelector = 'ForStatement[init.kind="let"]'; +const forInOfSelector = 'ForOfStatement[left.kind="let"],' + + 'ForInStatement[left.kind="let"]'; module.exports = { create(context) { const sourceCode = context.getSourceCode(); - const msg = 'Use of `let` as the loop variable in a for-loop is ' + - 'not recommended. Please use `var` instead.'; - - /** - * Report function to test if the for-loop is declared using `let`. - */ - function testForLoop(node) { - if (node.init && node.init.kind === 'let') { - context.report({ - node: node.init, - message: msg, - fix: (fixer) => - fixer.replaceText(sourceCode.getFirstToken(node.init), 'var') - }); - } - } - /** - * Report function to test if the for-in or for-of loop - * is declared using `let`. - */ - function testForInOfLoop(node) { - if (node.left && node.left.kind === 'let') { - context.report({ - node: node.left, - message: msg, - fix: (fixer) => - fixer.replaceText(sourceCode.getFirstToken(node.left), 'var') - }); - } + function report(node) { + context.report({ + node, + message, + fix: (fixer) => + fixer.replaceText(sourceCode.getFirstToken(node), 'var') + }); } return { - 'ForStatement': testForLoop, - 'ForInStatement': testForInOfLoop, - 'ForOfStatement': testForInOfLoop + [forSelector]: (node) => report(node.init), + [forInOfSelector]: (node) => report(node.left), }; } };