Skip to content

Commit d59b0a7

Browse files
cjihrigMylesBorins
authored andcommitted
tools: simplify prefer-common-mustnotcall rule
PR-URL: #17572 Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent f45ef44 commit d59b0a7

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

tools/eslint-rules/prefer-common-mustnotcall.js

+13-22
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,21 @@
1010

1111
const msg = 'Please use common.mustNotCall(msg) instead of ' +
1212
'common.mustCall(fn, 0) or common.mustCall(0).';
13-
14-
function isCommonMustCall(node) {
15-
return node &&
16-
node.callee &&
17-
node.callee.object &&
18-
node.callee.object.name === 'common' &&
19-
node.callee.property &&
20-
node.callee.property.name === 'mustCall';
21-
}
22-
23-
function isArgZero(argument) {
24-
return argument &&
25-
typeof argument.value === 'number' &&
26-
argument.value === 0;
27-
}
13+
const mustCallSelector = 'CallExpression[callee.object.name="common"]' +
14+
'[callee.property.name="mustCall"]';
15+
const arg0Selector = `${mustCallSelector}[arguments.0.value=0]`;
16+
const arg1Selector = `${mustCallSelector}[arguments.1.value=0]`;
2817

2918
module.exports = function(context) {
19+
function report(node) {
20+
context.report(node, msg);
21+
}
22+
3023
return {
31-
CallExpression(node) {
32-
if (isCommonMustCall(node) &&
33-
(isArgZero(node.arguments[0]) || // catch common.mustCall(0)
34-
isArgZero(node.arguments[1]))) { // catch common.mustCall(fn, 0)
35-
context.report(node, msg);
36-
}
37-
}
24+
// Catch common.mustCall(0)
25+
[arg0Selector]: report,
26+
27+
// Catch common.mustCall(fn, 0)
28+
[arg1Selector]: report
3829
};
3930
};

0 commit comments

Comments
 (0)