Skip to content

Commit 4a1dfdc

Browse files
TrottMyles Borins
authored andcommitted
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. 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>
1 parent fff6a84 commit 4a1dfdc

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎.eslintrc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ rules:
8585
prefer-const: 2
8686

8787
# Custom rules in tools/eslint-rules
88+
assert-fail-single-argument: 2
8889
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]
8990
align-multiline-assignment: 2
9091

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @fileoverview Prohibit use of a single argument only in `assert.fail()`. It
3+
* is almost always an error.
4+
* @author Rich Trott
5+
*/
6+
'use strict';
7+
8+
//------------------------------------------------------------------------------
9+
// Rule Definition
10+
//------------------------------------------------------------------------------
11+
12+
constmsg='assert.fail() message should be third argument';
13+
14+
functionisAssert(node){
15+
returnnode.callee.object&&node.callee.object.name==='assert';
16+
}
17+
18+
functionisFail(node){
19+
returnnode.callee.property&&node.callee.property.name==='fail';
20+
}
21+
22+
module.exports=function(context){
23+
return{
24+
'CallExpression': function(node){
25+
if(isAssert(node)&&isFail(node)&&node.arguments.length===1){
26+
context.report(node,msg);
27+
}
28+
}
29+
};
30+
};

0 commit comments

Comments
 (0)