Skip to content

Commit

Permalink
tools: add rule prefering common.mustNotCall()
Browse files Browse the repository at this point in the history
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
jasnell committed Mar 26, 2017
1 parent 4f2e372 commit 20b1823
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ rules:
# Custom rules in tools/eslint-rules
prefer-assert-iferror: 2
prefer-assert-methods: 2
prefer-common-mustnotcall: 2
## common module is mandatory in tests
required-modules: [2, common]
39 changes: 39 additions & 0 deletions tools/eslint-rules/prefer-common-mustnotcall.js
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);
}
}
};
};

0 comments on commit 20b1823

Please sign in to comment.