tools: remove conditional assignment in custom ESLint rule

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 <targos@protonmail.com>
This commit is contained in:
Rich Trott 2021-12-25 19:51:07 -08:00
parent a706342368
commit e7d4e6b680

View File

@ -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) => {