Skip to content

Commit 20b1823

Browse files
committed
tools: add rule prefering common.mustNotCall()
Prefer using `common.mustNotCall()` over `common.mustCall(fn, 0)` PR-URL: #12027 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 4f2e372 commit 20b1823

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎test/.eslintrc.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ rules:
99
# Custom rules in tools/eslint-rules
1010
prefer-assert-iferror: 2
1111
prefer-assert-methods: 2
12+
prefer-common-mustnotcall: 2
1213
## common module is mandatory in tests
1314
required-modules: [2, common]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @fileoverview Prefer common.mustNotCall(msg) over common.mustCall(fn, 0)
3+
* @author James M Snell <[email protected]>
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
constmsg='Please use common.mustNotCall(msg) instead of '+
12+
'common.mustCall(fn, 0) or common.mustCall(0).';
13+
14+
functionisCommonMustCall(node){
15+
returnnode&&
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+
functionisArgZero(argument){
24+
returnargument&&
25+
typeofargument.value==='number'&&
26+
argument.value===0;
27+
}
28+
29+
module.exports=function(context){
30+
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+
}
38+
};
39+
};

0 commit comments

Comments
(0)