tools: add rule prefering common.mustNotCall()
Prefer using `common.mustNotCall()` over `common.mustCall(fn, 0)` PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
4f2e372716
commit
20b18236de
@ -9,5 +9,6 @@ rules:
|
|||||||
# Custom rules in tools/eslint-rules
|
# Custom rules in tools/eslint-rules
|
||||||
prefer-assert-iferror: 2
|
prefer-assert-iferror: 2
|
||||||
prefer-assert-methods: 2
|
prefer-assert-methods: 2
|
||||||
|
prefer-common-mustnotcall: 2
|
||||||
## common module is mandatory in tests
|
## common module is mandatory in tests
|
||||||
required-modules: [2, common]
|
required-modules: [2, common]
|
||||||
|
39
tools/eslint-rules/prefer-common-mustnotcall.js
Normal file
39
tools/eslint-rules/prefer-common-mustnotcall.js
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user