From 20b18236de3dff8a14ef8af8eed2c763f2d46f86 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 24 Mar 2017 12:37:59 -0700 Subject: [PATCH] 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 Reviewed-By: Richard Lau Reviewed-By: Gibson Fahnestock Reviewed-By: Teddy Katz Reviewed-By: Colin Ihrig Reviewed-By: Franziska Hinkelmann --- test/.eslintrc.yaml | 1 + .../eslint-rules/prefer-common-mustnotcall.js | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tools/eslint-rules/prefer-common-mustnotcall.js diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index e76bd632e6927c..501c40781c6ebd 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -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] diff --git a/tools/eslint-rules/prefer-common-mustnotcall.js b/tools/eslint-rules/prefer-common-mustnotcall.js new file mode 100644 index 00000000000000..6bc428ed59a8b8 --- /dev/null +++ b/tools/eslint-rules/prefer-common-mustnotcall.js @@ -0,0 +1,39 @@ +/** + * @fileoverview Prefer common.mustNotCall(msg) over common.mustCall(fn, 0) + * @author James M Snell + */ +'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); + } + } + }; +};