tools: enable no-throw-literal ESLint rule
Only throw the Error object itself or an object using the Error object as base objects for user-defined exceptions. PR-URL: https://github.com/nodejs/node/pull/11168 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
parent
78ba4943c5
commit
4334658635
@ -40,6 +40,7 @@ rules:
|
|||||||
no-octal: 2
|
no-octal: 2
|
||||||
no-redeclare: 2
|
no-redeclare: 2
|
||||||
no-self-assign: 2
|
no-self-assign: 2
|
||||||
|
no-throw-literal: 2
|
||||||
no-unused-labels: 2
|
no-unused-labels: 2
|
||||||
no-useless-call: 2
|
no-useless-call: 2
|
||||||
no-useless-escape: 2
|
no-useless-escape: 2
|
||||||
|
@ -2,4 +2,5 @@
|
|||||||
require('../common');
|
require('../common');
|
||||||
|
|
||||||
// custom error throwing
|
// custom error throwing
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
|
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
*test*message*throw_custom_error.js:5
|
*test*message*throw_custom_error.js:6
|
||||||
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
|
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
|
||||||
^
|
^
|
||||||
MyCustomError: This is a custom message
|
MyCustomError: This is a custom message
|
||||||
|
@ -6,6 +6,7 @@ console.error('before');
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
// these lines should contain tab!
|
// these lines should contain tab!
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
throw ({ foo: 'bar' });
|
throw ({ foo: 'bar' });
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
before
|
before
|
||||||
*test*message*throw_in_line_with_tabs.js:9
|
*test*message*throw_in_line_with_tabs.js:10
|
||||||
throw ({ foo: 'bar' });
|
throw ({ foo: 'bar' });
|
||||||
^
|
^
|
||||||
[object Object]
|
[object Object]
|
||||||
|
@ -2,4 +2,5 @@
|
|||||||
require('../common');
|
require('../common');
|
||||||
|
|
||||||
// custom error throwing
|
// custom error throwing
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
throw ({ foo: 'bar' });
|
throw ({ foo: 'bar' });
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
*test*message*throw_non_error.js:5
|
*test*message*throw_non_error.js:6
|
||||||
throw ({ foo: 'bar' });
|
throw ({ foo: 'bar' });
|
||||||
^
|
^
|
||||||
[object Object]
|
[object Object]
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
require('../common');
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
throw null;
|
throw null;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
*test*message*throw_null.js:4
|
*test*message*throw_null.js:5
|
||||||
throw null;
|
throw null;
|
||||||
^
|
^
|
||||||
null
|
null
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
require('../common');
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
throw undefined;
|
throw undefined;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
*test*message*throw_undefined.js:4
|
*test*message*throw_undefined.js:5
|
||||||
throw undefined;
|
throw undefined;
|
||||||
^
|
^
|
||||||
undefined
|
undefined
|
||||||
|
@ -394,7 +394,7 @@ threw = false;
|
|||||||
try {
|
try {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
function() {
|
function() {
|
||||||
throw ({});
|
throw ({}); // eslint-disable-line no-throw-literal
|
||||||
},
|
},
|
||||||
Array
|
Array
|
||||||
);
|
);
|
||||||
@ -576,6 +576,7 @@ testBlockTypeError(assert.throws, undefined);
|
|||||||
testBlockTypeError(assert.doesNotThrow, undefined);
|
testBlockTypeError(assert.doesNotThrow, undefined);
|
||||||
|
|
||||||
// https://github.com/nodejs/node/issues/3275
|
// https://github.com/nodejs/node/issues/3275
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
assert.throws(() => { throw 'error'; }, (err) => err === 'error');
|
assert.throws(() => { throw 'error'; }, (err) => err === 'error');
|
||||||
assert.throws(() => { throw new Error(); }, (err) => err instanceof Error);
|
assert.throws(() => { throw new Error(); }, (err) => err instanceof Error);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user