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 authored and MylesBorins committed Aug 16, 2017
1 parent a7b9450 commit 7bb200f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ rules:
no-var: 2
prefer-const: 2

# Custom rules in tools/eslint-rules
prefer-common-mustnotcall: 2

## common module is mandatory in tests
required-modules: [2, common]
prefer-assert-methods: 2
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 7bb200f

Please sign in to comment.