tools: simplify prefer-common-mustnotcall rule

PR-URL: https://github.com/nodejs/node/pull/17572
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
This commit is contained in:
cjihrig 2017-12-09 09:38:14 -05:00
parent c84ca17305
commit e4a71098ef
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -10,30 +10,21 @@
const msg = 'Please use common.mustNotCall(msg) instead of ' + const msg = 'Please use common.mustNotCall(msg) instead of ' +
'common.mustCall(fn, 0) or common.mustCall(0).'; 'common.mustCall(fn, 0) or common.mustCall(0).';
const mustCallSelector = 'CallExpression[callee.object.name="common"]' +
function isCommonMustCall(node) { '[callee.property.name="mustCall"]';
return node && const arg0Selector = `${mustCallSelector}[arguments.0.value=0]`;
node.callee && const arg1Selector = `${mustCallSelector}[arguments.1.value=0]`;
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) { module.exports = function(context) {
function report(node) {
context.report(node, msg);
}
return { return {
CallExpression(node) { // Catch common.mustCall(0)
if (isCommonMustCall(node) && [arg0Selector]: report,
(isArgZero(node.arguments[0]) || // catch common.mustCall(0)
isArgZero(node.arguments[1]))) { // catch common.mustCall(fn, 0) // Catch common.mustCall(fn, 0)
context.report(node, msg); [arg1Selector]: report
}
}
}; };
}; };