Skip to content

Commit dcada80

Browse files
Trotttargos
authored andcommitted
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: #41325 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 3c8b25b commit dcada80

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

tools/eslint-rules/no-duplicate-requires.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ const { isRequireCall, isString } = require('./rules-utils.js');
1010
// Rule Definition
1111
//------------------------------------------------------------------------------
1212

13+
const secondLevelTypes = [
14+
'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression',
15+
'ClassBody', 'MethodDefinition',
16+
];
1317

1418
function isTopLevel(node) {
15-
do {
16-
if (node.type === 'FunctionDeclaration' ||
17-
node.type === 'FunctionExpression' ||
18-
node.type === 'ArrowFunctionExpression' ||
19-
node.type === 'ClassBody' ||
20-
node.type === 'MethodDefinition') {
21-
return false;
19+
while (!secondLevelTypes.includes(node.type)) {
20+
node = node.parent;
21+
if (!node) {
22+
return true;
2223
}
23-
} while (node = node.parent);
24-
return true;
24+
}
25+
return false;
2526
}
2627

2728
module.exports = (context) => {

0 commit comments

Comments
 (0)