Skip to content

Commit 2dc8aac

Browse files
Trottitaloacasas
authored andcommitted
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: #11168 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 3ca483f commit 2dc8aac

12 files changed

+13
-6
lines changed

.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ rules:
4040
no-octal: 2
4141
no-redeclare: 2
4242
no-self-assign: 2
43+
no-throw-literal: 2
4344
no-unused-labels: 2
4445
no-useless-call: 2
4546
no-useless-escape: 2

test/message/throw_custom_error.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
require('../common');
33

44
// custom error throwing
5+
// eslint-disable-next-line no-throw-literal
56
throw ({ name: 'MyCustomError', message: 'This is a custom message' });

test/message/throw_custom_error.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*test*message*throw_custom_error.js:5
1+
*test*message*throw_custom_error.js:6
22
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
33
^
44
MyCustomError: This is a custom message

test/message/throw_in_line_with_tabs.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ console.error('before');
66

77
(function() {
88
// these lines should contain tab!
9+
// eslint-disable-next-line no-throw-literal
910
throw ({ foo: 'bar' });
1011
})();
1112

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
before
2-
*test*message*throw_in_line_with_tabs.js:9
2+
*test*message*throw_in_line_with_tabs.js:10
33
throw ({ foo: 'bar' });
44
^
55
[object Object]

test/message/throw_non_error.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
require('../common');
33

44
// custom error throwing
5+
// eslint-disable-next-line no-throw-literal
56
throw ({ foo: 'bar' });

test/message/throw_non_error.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*test*message*throw_non_error.js:5
1+
*test*message*throw_non_error.js:6
22
throw ({ foo: 'bar' });
33
^
44
[object Object]

test/message/throw_null.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
22
require('../common');
33

4+
// eslint-disable-next-line no-throw-literal
45
throw null;

test/message/throw_null.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
*test*message*throw_null.js:4
2+
*test*message*throw_null.js:5
33
throw null;
44
^
55
null

test/message/throw_undefined.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
22
require('../common');
33

4+
// eslint-disable-next-line no-throw-literal
45
throw undefined;

test/message/throw_undefined.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
*test*message*throw_undefined.js:4
2+
*test*message*throw_undefined.js:5
33
throw undefined;
44
^
55
undefined

test/parallel/test-assert.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ threw = false;
394394
try {
395395
assert.throws(
396396
function() {
397-
throw ({});
397+
throw ({}); // eslint-disable-line no-throw-literal
398398
},
399399
Array
400400
);
@@ -576,6 +576,7 @@ testBlockTypeError(assert.throws, undefined);
576576
testBlockTypeError(assert.doesNotThrow, undefined);
577577

578578
// https://github.com/nodejs/node/issues/3275
579+
// eslint-disable-next-line no-throw-literal
579580
assert.throws(() => { throw 'error'; }, (err) => err === 'error');
580581
assert.throws(() => { throw new Error(); }, (err) => err instanceof Error);
581582

0 commit comments

Comments
 (0)