From dcada80f30433df104db9496f59238a94b746bc7 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 25 Dec 2021 19:51:07 -0800 Subject: [PATCH] tools: remove conditional assignment in custom ESLint rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These changes no-duplicate-require.js so that it doesn't use an assignment in a conditional, which can be easy to misread as a comparison rather than an assignment. It also means we change a do/while (which we don't use much in our code) to the much more common while construct. PR-URL: https://github.com/nodejs/node/pull/41325 Reviewed-By: Michaƫl Zasso --- tools/eslint-rules/no-duplicate-requires.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tools/eslint-rules/no-duplicate-requires.js b/tools/eslint-rules/no-duplicate-requires.js index 8c4b7a4c273457..6e149edceb8d6e 100644 --- a/tools/eslint-rules/no-duplicate-requires.js +++ b/tools/eslint-rules/no-duplicate-requires.js @@ -10,18 +10,19 @@ const { isRequireCall, isString } = require('./rules-utils.js'); // Rule Definition //------------------------------------------------------------------------------ +const secondLevelTypes = [ + 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', + 'ClassBody', 'MethodDefinition', +]; function isTopLevel(node) { - do { - if (node.type === 'FunctionDeclaration' || - node.type === 'FunctionExpression' || - node.type === 'ArrowFunctionExpression' || - node.type === 'ClassBody' || - node.type === 'MethodDefinition') { - return false; + while (!secondLevelTypes.includes(node.type)) { + node = node.parent; + if (!node) { + return true; } - } while (node = node.parent); - return true; + } + return false; } module.exports = (context) => {