Skip to content

Commit

Permalink
tools: simplify prefer-common-mustnotcall rule
Browse files Browse the repository at this point in the history
PR-URL: #17572
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
cjihrig authored and gibfahn committed Dec 20, 2017
1 parent 8eef736 commit 9ece12f
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions tools/eslint-rules/prefer-common-mustnotcall.js
Expand Up @@ -10,30 +10,21 @@

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;
}
const mustCallSelector = 'CallExpression[callee.object.name="common"]' +
'[callee.property.name="mustCall"]';
const arg0Selector = `${mustCallSelector}[arguments.0.value=0]`;
const arg1Selector = `${mustCallSelector}[arguments.1.value=0]`;

module.exports = function(context) {
function report(node) {
context.report(node, msg);
}

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);
}
}
// Catch common.mustCall(0)
[arg0Selector]: report,

// Catch common.mustCall(fn, 0)
[arg1Selector]: report
};
};

0 comments on commit 9ece12f

Please sign in to comment.