tools: replace assert-throw-arguments custom lint
The functionality of ESLint custom rule assert-throws-arguments can be replaced with no-restricted-syntax entries. PR-URL: https://github.com/nodejs/node/pull/14547 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
548cc72f66
commit
e506bcd899
@ -119,6 +119,12 @@ rules:
|
|||||||
no-mixed-spaces-and-tabs: error
|
no-mixed-spaces-and-tabs: error
|
||||||
no-multiple-empty-lines: [error, {max: 2, maxEOF: 0, maxBOF: 0}]
|
no-multiple-empty-lines: [error, {max: 2, maxEOF: 0, maxBOF: 0}]
|
||||||
no-restricted-syntax: [error, {
|
no-restricted-syntax: [error, {
|
||||||
|
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])",
|
||||||
|
message: "use a regular expression for second argument of assert.throws()"
|
||||||
|
}, {
|
||||||
|
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]",
|
||||||
|
message: "assert.throws() must be invoked with at least two arguments."
|
||||||
|
}, {
|
||||||
selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]",
|
selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]",
|
||||||
message: "setTimeout() must be invoked with at least two arguments."
|
message: "setTimeout() must be invoked with at least two arguments."
|
||||||
}, {
|
}, {
|
||||||
@ -163,7 +169,6 @@ rules:
|
|||||||
template-curly-spacing: error
|
template-curly-spacing: error
|
||||||
|
|
||||||
# Custom rules in tools/eslint-rules
|
# Custom rules in tools/eslint-rules
|
||||||
assert-throws-arguments: [error, { requireTwo: true }]
|
|
||||||
no-unescaped-regexp-dot: error
|
no-unescaped-regexp-dot: error
|
||||||
|
|
||||||
# Global scoped method and vars
|
# Global scoped method and vars
|
||||||
|
@ -583,7 +583,7 @@ Note that `error` can not be a string. If a string is provided as the second
|
|||||||
argument, then `error` is assumed to be omitted and the string will be used for
|
argument, then `error` is assumed to be omitted and the string will be used for
|
||||||
`message` instead. This can lead to easy-to-miss mistakes:
|
`message` instead. This can lead to easy-to-miss mistakes:
|
||||||
|
|
||||||
<!-- eslint-disable assert-throws-arguments -->
|
<!-- eslint-disable no-restricted-syntax -->
|
||||||
```js
|
```js
|
||||||
// THIS IS A MISTAKE! DO NOT DO THIS!
|
// THIS IS A MISTAKE! DO NOT DO THIS!
|
||||||
assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
|
assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
|
||||||
|
@ -412,11 +412,11 @@ function thrower(errorConstructor) {
|
|||||||
assert.throws(makeBlock(thrower, a.AssertionError),
|
assert.throws(makeBlock(thrower, a.AssertionError),
|
||||||
a.AssertionError, 'message');
|
a.AssertionError, 'message');
|
||||||
assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
|
assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
|
||||||
// eslint-disable-next-line assert-throws-arguments
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
assert.throws(makeBlock(thrower, a.AssertionError));
|
assert.throws(makeBlock(thrower, a.AssertionError));
|
||||||
|
|
||||||
// if not passing an error, catch all.
|
// if not passing an error, catch all.
|
||||||
// eslint-disable-next-line assert-throws-arguments
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
assert.throws(makeBlock(thrower, TypeError));
|
assert.throws(makeBlock(thrower, TypeError));
|
||||||
|
|
||||||
// when passing a type, only catch errors of the appropriate type
|
// when passing a type, only catch errors of the appropriate type
|
||||||
@ -618,7 +618,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
|
|||||||
{
|
{
|
||||||
let threw = false;
|
let threw = false;
|
||||||
try {
|
try {
|
||||||
// eslint-disable-next-line assert-throws-arguments
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
assert.ifError(null);
|
assert.ifError(null);
|
||||||
});
|
});
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
/**
|
|
||||||
* @fileoverview Check that assert.throws is never called with a string as
|
|
||||||
* second argument.
|
|
||||||
* @author Michaël Zasso
|
|
||||||
*/
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// Rule Definition
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
function checkThrowsArguments(context, node) {
|
|
||||||
if (node.callee.type === 'MemberExpression' &&
|
|
||||||
node.callee.object.name === 'assert' &&
|
|
||||||
node.callee.property.name === 'throws') {
|
|
||||||
const args = node.arguments;
|
|
||||||
if (args.length > 3) {
|
|
||||||
context.report({
|
|
||||||
message: 'Too many arguments',
|
|
||||||
node: node
|
|
||||||
});
|
|
||||||
} else if (args.length > 1) {
|
|
||||||
const error = args[1];
|
|
||||||
if (error.type === 'Literal' && typeof error.value === 'string' ||
|
|
||||||
error.type === 'TemplateLiteral') {
|
|
||||||
context.report({
|
|
||||||
message: 'Unexpected string as second argument',
|
|
||||||
node: error
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (context.options[0].requireTwo) {
|
|
||||||
context.report({
|
|
||||||
message: 'Expected at least two arguments',
|
|
||||||
node: node
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
meta: {
|
|
||||||
schema: [
|
|
||||||
{
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
requireTwo: {
|
|
||||||
type: 'boolean'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
create: function(context) {
|
|
||||||
return {
|
|
||||||
CallExpression: (node) => checkThrowsArguments(context, node)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
Loading…
x
Reference in New Issue
Block a user