From 54a5ede5959b6abbfa4c53b38953622ae544e7be Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 18 Apr 2016 10:56:37 -0700 Subject: [PATCH] tools: lint rule for assert.fail() `assert.fail()` is often mistakenly used with a single argument even in Node.js core. (See fixes to previous instances in b7f4b1ba4cca320cf576aa73fae15902fd801190, 28e9a022df3ef89a0ea21e4ad86655eb408a8d96. and 676e61872f54dd546e324599c7871c20b798386a.) This commit adds a linting rule to identify instances of this issue. --- .eslintrc | 1 + .../assert-fail-single-argument.js | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tools/eslint-rules/assert-fail-single-argument.js diff --git a/.eslintrc b/.eslintrc index 8e01cc4c94c0e4..38d01df0778210 100644 --- a/.eslintrc +++ b/.eslintrc @@ -85,6 +85,7 @@ rules: prefer-const: 2 # Custom rules in tools/eslint-rules + assert-fail-single-argument: 2 new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"] diff --git a/tools/eslint-rules/assert-fail-single-argument.js b/tools/eslint-rules/assert-fail-single-argument.js new file mode 100644 index 00000000000000..4ce790238181cc --- /dev/null +++ b/tools/eslint-rules/assert-fail-single-argument.js @@ -0,0 +1,30 @@ +/** + * @fileoverview Prohibit use of a single argument only in `assert.fail()`. It + * is almost always an error. + * @author Rich Trott + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const msg = 'assert.fail() message should be third argument'; + +function isAssert(node) { + return node.callee.object && node.callee.object.name === 'assert'; +} + +function isFail(node) { + return node.callee.property && node.callee.property.name === 'fail'; +} + +module.exports = function(context) { + return { + 'CallExpression': function(node) { + if (isAssert(node) && isFail(node) && node.arguments.length === 1) { + context.report(node, msg); + } + } + }; +};