Skip to content

Commit 1626852

Browse files
authoredMay 7, 2024··
throw-new-error: Check all call expressions instead of just argument of ThrowStatement (#2332)
1 parent f3771b1 commit 1626852

9 files changed

+123
-37
lines changed
 

Diff for: ‎docs/rules/throw-new-error.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Require `new` when throwing an error
1+
# Require `new` when creating an error
22

33
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs).
44

@@ -12,27 +12,27 @@ While it's possible to create a new error without using the `new` keyword, it's
1212
## Fail
1313

1414
```js
15-
throw Error();
15+
const error = Error('unicorn');
1616
```
1717

1818
```js
1919
throw TypeError('unicorn');
2020
```
2121

2222
```js
23-
throw lib.TypeError();
23+
throw lib.TypeError('unicorn');
2424
```
2525

2626
## Pass
2727

2828
```js
29-
throw new Error();
29+
const error = new Error('unicorn');
3030
```
3131

3232
```js
3333
throw new TypeError('unicorn');
3434
```
3535

3636
```js
37-
throw new lib.TypeError();
37+
throw new lib.TypeError('unicorn');
3838
```

Diff for: ‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ If you don't use the preset, ensure you use the same `env` and `parserOptions` c
223223
| [switch-case-braces](docs/rules/switch-case-braces.md) | Enforce consistent brace style for `case` clauses. || 🔧 | |
224224
| [template-indent](docs/rules/template-indent.md) | Fix whitespace-insensitive template indentation. || 🔧 | |
225225
| [text-encoding-identifier-case](docs/rules/text-encoding-identifier-case.md) | Enforce consistent case for text encoding identifiers. || 🔧 | 💡 |
226-
| [throw-new-error](docs/rules/throw-new-error.md) | Require `new` when throwing an error. || 🔧 | |
226+
| [throw-new-error](docs/rules/throw-new-error.md) | Require `new` when creating an error. || 🔧 | |
227227

228228
<!-- end auto-generated rules list -->
229229

Diff for: ‎rules/throw-new-error.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@ const {switchCallExpressionToNewExpression} = require('./fix/index.js');
33

44
const messageId = 'throw-new-error';
55
const messages = {
6-
[messageId]: 'Use `new` when throwing an error.',
6+
[messageId]: 'Use `new` when creating an error.',
77
};
88

99
const customError = /^(?:[A-Z][\da-z]*)*Error$/;
1010

1111
/** @param {import('eslint').Rule.RuleContext} context */
1212
const create = context => ({
1313
CallExpression(node) {
14-
if (!(
15-
node.parent.type === 'ThrowStatement'
16-
&& node.parent.argument === node
17-
)) {
18-
return;
19-
}
20-
2114
const {callee} = node;
2215
if (!(
2316
(callee.type === 'Identifier' && customError.test(callee.name))
@@ -45,7 +38,7 @@ module.exports = {
4538
meta: {
4639
type: 'suggestion',
4740
docs: {
48-
description: 'Require `new` when throwing an error.',
41+
description: 'Require `new` when creating an error.',
4942
},
5043
fixable: 'code',
5144
messages,

Diff for: ‎test/error-message.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ test.snapshot({
6969
'throw new Error({foo: 0}.foo)',
7070
'throw new Error(lineNumber=2)',
7171
'const error = new RangeError;',
72+
'throw Object.assign(new Error(), {foo})',
7273
],
7374
});
7475

Diff for: ‎test/snapshots/error-message.mjs.md

+15
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,21 @@ Generated by [AVA](https://avajs.dev).
286286
| ^^^^^^^^^^^^^^ Pass a message to the \`RangeError\` constructor.␊
287287
`
288288

289+
## invalid(19): throw Object.assign(new Error(), {foo})
290+
291+
> Input
292+
293+
`␊
294+
1 | throw Object.assign(new Error(), {foo})␊
295+
`
296+
297+
> Error 1/1
298+
299+
`␊
300+
> 1 | throw Object.assign(new Error(), {foo})␊
301+
| ^^^^^^^^^^^ Pass a message to the \`Error\` constructor.␊
302+
`
303+
289304
## invalid(1): new AggregateError(errors)
290305

291306
> Input

Diff for: ‎test/snapshots/error-message.mjs.snap

42 Bytes
Binary file not shown.

Diff for: ‎test/snapshots/throw-new-error.mjs.md

+91-22
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Generated by [AVA](https://avajs.dev).
2222
2323
`␊
2424
> 1 | throw Error()␊
25-
| ^^^^^^^ Use \`new\` when throwing an error.␊
25+
| ^^^^^^^ Use \`new\` when creating an error.␊
2626
`
2727

2828
## invalid(2): throw (Error)()
@@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev).
4343
4444
`␊
4545
> 1 | throw (Error)()␊
46-
| ^^^^^^^^^ Use \`new\` when throwing an error.␊
46+
| ^^^^^^^^^ Use \`new\` when creating an error.␊
4747
`
4848

4949
## invalid(3): throw lib.Error()
@@ -64,7 +64,7 @@ Generated by [AVA](https://avajs.dev).
6464
6565
`␊
6666
> 1 | throw lib.Error()␊
67-
| ^^^^^^^^^^^ Use \`new\` when throwing an error.␊
67+
| ^^^^^^^^^^^ Use \`new\` when creating an error.␊
6868
`
6969

7070
## invalid(4): throw lib.mod.Error()
@@ -85,7 +85,7 @@ Generated by [AVA](https://avajs.dev).
8585
8686
`␊
8787
> 1 | throw lib.mod.Error()␊
88-
| ^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
88+
| ^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
8989
`
9090

9191
## invalid(5): throw lib[mod].Error()
@@ -106,7 +106,7 @@ Generated by [AVA](https://avajs.dev).
106106
107107
`␊
108108
> 1 | throw lib[mod].Error()␊
109-
| ^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
109+
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
110110
`
111111

112112
## invalid(6): throw (lib.mod).Error()
@@ -127,7 +127,7 @@ Generated by [AVA](https://avajs.dev).
127127
128128
`␊
129129
> 1 | throw (lib.mod).Error()␊
130-
| ^^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
130+
| ^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
131131
`
132132

133133
## invalid(7): throw Error('foo')
@@ -148,7 +148,7 @@ Generated by [AVA](https://avajs.dev).
148148
149149
`␊
150150
> 1 | throw Error('foo')␊
151-
| ^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
151+
| ^^^^^^^^^^^^ Use \`new\` when creating an error.␊
152152
`
153153

154154
## invalid(8): throw CustomError('foo')
@@ -169,7 +169,7 @@ Generated by [AVA](https://avajs.dev).
169169
170170
`␊
171171
> 1 | throw CustomError('foo')␊
172-
| ^^^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
172+
| ^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
173173
`
174174

175175
## invalid(9): throw FooBarBazError('foo')
@@ -190,7 +190,7 @@ Generated by [AVA](https://avajs.dev).
190190
191191
`␊
192192
> 1 | throw FooBarBazError('foo')␊
193-
| ^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
193+
| ^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
194194
`
195195

196196
## invalid(10): throw ABCError('foo')
@@ -211,7 +211,7 @@ Generated by [AVA](https://avajs.dev).
211211
212212
`␊
213213
> 1 | throw ABCError('foo')␊
214-
| ^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
214+
| ^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
215215
`
216216

217217
## invalid(11): throw Abc3Error('foo')
@@ -232,7 +232,7 @@ Generated by [AVA](https://avajs.dev).
232232
233233
`␊
234234
> 1 | throw Abc3Error('foo')␊
235-
| ^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
235+
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
236236
`
237237

238238
## invalid(12): throw TypeError()
@@ -253,7 +253,7 @@ Generated by [AVA](https://avajs.dev).
253253
254254
`␊
255255
> 1 | throw TypeError()␊
256-
| ^^^^^^^^^^^ Use \`new\` when throwing an error.␊
256+
| ^^^^^^^^^^^ Use \`new\` when creating an error.␊
257257
`
258258

259259
## invalid(13): throw EvalError()
@@ -274,7 +274,7 @@ Generated by [AVA](https://avajs.dev).
274274
275275
`␊
276276
> 1 | throw EvalError()␊
277-
| ^^^^^^^^^^^ Use \`new\` when throwing an error.␊
277+
| ^^^^^^^^^^^ Use \`new\` when creating an error.␊
278278
`
279279

280280
## invalid(14): throw RangeError()
@@ -295,7 +295,7 @@ Generated by [AVA](https://avajs.dev).
295295
296296
`␊
297297
> 1 | throw RangeError()␊
298-
| ^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
298+
| ^^^^^^^^^^^^ Use \`new\` when creating an error.␊
299299
`
300300

301301
## invalid(15): throw ReferenceError()
@@ -316,7 +316,7 @@ Generated by [AVA](https://avajs.dev).
316316
317317
`␊
318318
> 1 | throw ReferenceError()␊
319-
| ^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
319+
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
320320
`
321321

322322
## invalid(16): throw SyntaxError()
@@ -337,7 +337,7 @@ Generated by [AVA](https://avajs.dev).
337337
338338
`␊
339339
> 1 | throw SyntaxError()␊
340-
| ^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
340+
| ^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
341341
`
342342

343343
## invalid(17): throw URIError()
@@ -358,7 +358,7 @@ Generated by [AVA](https://avajs.dev).
358358
359359
`␊
360360
> 1 | throw URIError()␊
361-
| ^^^^^^^^^^ Use \`new\` when throwing an error.␊
361+
| ^^^^^^^^^^ Use \`new\` when creating an error.␊
362362
`
363363

364364
## invalid(18): throw (( URIError() ))
@@ -379,7 +379,7 @@ Generated by [AVA](https://avajs.dev).
379379
380380
`␊
381381
> 1 | throw (( URIError() ))␊
382-
| ^^^^^^^^^^ Use \`new\` when throwing an error.␊
382+
| ^^^^^^^^^^ Use \`new\` when creating an error.␊
383383
`
384384

385385
## invalid(19): throw (( URIError ))()
@@ -400,7 +400,7 @@ Generated by [AVA](https://avajs.dev).
400400
401401
`␊
402402
> 1 | throw (( URIError ))()␊
403-
| ^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
403+
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
404404
`
405405

406406
## invalid(20): throw getGlobalThis().Error()
@@ -421,7 +421,7 @@ Generated by [AVA](https://avajs.dev).
421421
422422
`␊
423423
> 1 | throw getGlobalThis().Error()␊
424-
| ^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
424+
| ^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
425425
`
426426

427427
## invalid(21): throw utils.getGlobalThis().Error()
@@ -442,7 +442,7 @@ Generated by [AVA](https://avajs.dev).
442442
443443
`␊
444444
> 1 | throw utils.getGlobalThis().Error()␊
445-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
445+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
446446
`
447447

448448
## invalid(22): throw (( getGlobalThis().Error ))()
@@ -463,5 +463,74 @@ Generated by [AVA](https://avajs.dev).
463463
464464
`␊
465465
> 1 | throw (( getGlobalThis().Error ))()␊
466-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when throwing an error.␊
466+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
467+
`
468+
469+
## invalid(23): const error = Error()
470+
471+
> Input
472+
473+
`␊
474+
1 | const error = Error()␊
475+
`
476+
477+
> Output
478+
479+
`␊
480+
1 | const error = new Error()␊
481+
`
482+
483+
> Error 1/1
484+
485+
`␊
486+
> 1 | const error = Error()␊
487+
| ^^^^^^^ Use \`new\` when creating an error.␊
488+
`
489+
490+
## invalid(24): throw Object.assign(Error(), {foo})
491+
492+
> Input
493+
494+
`␊
495+
1 | throw Object.assign(Error(), {foo})␊
496+
`
497+
498+
> Output
499+
500+
`␊
501+
1 | throw Object.assign(new Error(), {foo})␊
502+
`
503+
504+
> Error 1/1
505+
506+
`␊
507+
> 1 | throw Object.assign(Error(), {foo})␊
508+
| ^^^^^^^ Use \`new\` when creating an error.␊
509+
`
510+
511+
## invalid(25): new Promise((resolve, reject) => { reject(Error('message')); });
512+
513+
> Input
514+
515+
`␊
516+
1 | new Promise((resolve, reject) => {␊
517+
2 | reject(Error('message'));␊
518+
3 | });␊
519+
`
520+
521+
> Output
522+
523+
`␊
524+
1 | new Promise((resolve, reject) => {␊
525+
2 | reject(new Error('message'));␊
526+
3 | });␊
527+
`
528+
529+
> Error 1/1
530+
531+
`␊
532+
1 | new Promise((resolve, reject) => {␊
533+
> 2 | reject(Error('message'));␊
534+
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
535+
3 | });␊
467536
`

Diff for: ‎test/snapshots/throw-new-error.mjs.snap

223 Bytes
Binary file not shown.

Diff for: ‎test/throw-new-error.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {outdent} from 'outdent';
12
import {getTester} from './utils/test.mjs';
23

34
const {test} = getTester(import.meta);
@@ -52,5 +53,12 @@ test.snapshot({
5253
'throw getGlobalThis().Error()',
5354
'throw utils.getGlobalThis().Error()',
5455
'throw (( getGlobalThis().Error ))()',
56+
'const error = Error()',
57+
'throw Object.assign(Error(), {foo})',
58+
outdent`
59+
new Promise((resolve, reject) => {
60+
reject(Error('message'));
61+
});
62+
`,
5563
],
5664
});

0 commit comments

Comments
 (0)
Please sign in to comment.