Skip to content

Commit

Permalink
tools: lint rule for assert.fail()
Browse files Browse the repository at this point in the history
`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.

PR-URL: #6261
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Trott authored and jasnell committed Apr 20, 2016
1 parent 4bbd418 commit 8ede3d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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"]
align-multiline-assignment: 2

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);
}
}
};
};

0 comments on commit 8ede3d5

Please sign in to comment.