diff --git a/.eslintrc b/.eslintrc index e8496e8560..af0e6ddc15 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"] align-multiline-assignment: 2 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 0000000000..4ce7902381 --- /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); + } + } + }; +};