Skip to content

Commit

Permalink
tools: remove conditional assignment in custom ESLint rule
Browse files Browse the repository at this point in the history
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: nodejs#41325
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Trott committed Jan 5, 2022
1 parent a706342 commit e7d4e6b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions tools/eslint-rules/no-duplicate-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit e7d4e6b

Please sign in to comment.