-
Notifications
You must be signed in to change notification settings - Fork 30k
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 <fishrock123@rocketmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
- 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 <jasnell@gmail.com> | ||
*/ | ||
'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); | ||
} | ||
} | ||
}; | ||
}; |