-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @fileoverview Prefer common.mustNotCall(msg) over common.mustCall(fn, 0) | ||
* @author James M Snell <[email protected]> | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const msg = 'Please use common.mustNotCall(msg) instead of ' + | ||
'common.mustCall(fn, 0) or common.mustCall(0).'; | ||
|
||
function isCommonMustCall(node) { | ||
return node && | ||
node.callee && | ||
node.callee.object && | ||
node.callee.object.name === 'common' && | ||
node.callee.property && | ||
node.callee.property.name === 'mustCall'; | ||
} | ||
|
||
function isArgZero(argument) { | ||
return argument && | ||
typeof argument.value === 'number' && | ||
argument.value === 0; | ||
} | ||
|
||
module.exports = function(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isCommonMustCall(node) && | ||
(isArgZero(node.arguments[0]) || // catch common.mustCall(0) | ||
isArgZero(node.arguments[1]))) { // catch common.mustCall(fn, 0) | ||
context.report(node, msg); | ||
} | ||
} | ||
}; | ||
}; |