mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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 inb7f4b1ba4c,28e9a022df. and 676e61872f54dd546e324599c7871c20b798386a.) This commit adds a linting rule to identify instances of this issue. PR-URL: https://github.com/nodejs/node/pull/6261 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
1 parent
fff6a84da5
commit
4a1dfdcc0f
2 files changed
+31
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user