Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
b7f4b1b,
28e9a02. and
676e618.)

This commit adds a linting rule to identify instances of this issue.
  • Loading branch information
Trott committed Apr 18, 2016
commit 54a5ede5959b6abbfa4c53b38953622ae544e7be
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
30 changes: 30 additions & 0 deletions tools/eslint-rules/assert-fail-single-argument.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
};