Skip to content

Commit f51aeb9

Browse files
authored
Support SuppressedError (#2669)
1 parent 3646efb commit f51aeb9

File tree

9 files changed

+415
-41
lines changed

9 files changed

+415
-41
lines changed

docs/rules/error-message.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,39 @@
77

88
This rule enforces a `message` value to be passed in when creating an instance of a built-in [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object, which leads to more readable and debuggable code.
99

10-
## Fail
10+
## Examples
1111

1212
```js
13-
throw Error();
14-
```
13+
//
14+
throw new Error();
1515

16-
```js
17-
throw Error('');
16+
//
17+
throw new Error('');
18+
19+
//
20+
throw new Error('Unexpected property.');
1821
```
1922

2023
```js
24+
//
2125
throw new TypeError();
26+
27+
//
28+
throw new TypeError('Array expected.');
2229
```
2330

2431
```js
32+
//
2533
const error = new AggregateError(errors);
26-
```
27-
28-
## Pass
2934

30-
```js
31-
throw Error('Unexpected property.');
35+
//
36+
const error = new AggregateError(errors, 'Promises rejected.');
3237
```
3338

3439
```js
35-
throw new TypeError('Array expected.');
36-
```
40+
//
41+
const error = new SuppressedError(error, suppressed);
3742

38-
```js
39-
const error = new AggregateError(errors, 'Promises rejected.');
43+
//
44+
const error = new SuppressedError(error, suppressed, 'This is a suppressed error.');
4045
```

rules/error-message.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const messages = {
1212
[MESSAGE_ID_NOT_STRING]: 'Error message should be a string.',
1313
};
1414

15+
const messageArgumentIndexes = new Map([
16+
['AggregateError', 1],
17+
['SuppressedError', 2],
18+
]);
19+
1520
/** @param {import('eslint').Rule.RuleContext} context */
1621
const create = context => {
1722
context.on(['CallExpression', 'NewExpression'], expression => {
@@ -28,7 +33,9 @@ const create = context => {
2833
}
2934

3035
const constructorName = expression.callee.name;
31-
const messageArgumentIndex = constructorName === 'AggregateError' ? 1 : 0;
36+
const messageArgumentIndex = messageArgumentIndexes.has(constructorName)
37+
? messageArgumentIndexes.get(constructorName)
38+
: 0;
3239
const callArguments = expression.arguments;
3340

3441
// If message is `SpreadElement` or there is `SpreadElement` before message

rules/shared/builtin-errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const builtinErrors = [
99
'URIError',
1010
'InternalError',
1111
'AggregateError',
12+
'SuppressedError',
1213
];
1314

1415
export default builtinErrors;

test/error-message.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,39 @@ test.snapshot({
104104
'const error = new AggregateError;',
105105
],
106106
});
107+
108+
// `SuppressedError`
109+
test.snapshot({
110+
valid: [
111+
'new SuppressedError(error, suppressed, "message")',
112+
'new NotSuppressedError(error, suppressed)',
113+
'new SuppressedError(...foo)',
114+
'new SuppressedError(...foo, "")',
115+
'new SuppressedError(error, suppressed, ...foo)',
116+
'new SuppressedError(error, suppressed, message, "")',
117+
'new SuppressedError("", "", message, "")',
118+
],
119+
invalid: [
120+
'new SuppressedError(error, suppressed,)',
121+
'new SuppressedError(error,)',
122+
'new SuppressedError()',
123+
'SuppressedError(error, suppressed,)',
124+
'SuppressedError(error,)',
125+
'SuppressedError()',
126+
'new SuppressedError(error, suppressed, "")',
127+
'new SuppressedError(error, suppressed, ``)',
128+
'new SuppressedError(error, suppressed, "", options)',
129+
outdent`
130+
const errorMessage = Object.freeze({errorMessage: 1}).errorMessage;
131+
throw new SuppressedError(error, suppressed, errorMessage)
132+
`,
133+
'new SuppressedError(error, suppressed, [])',
134+
'new SuppressedError(error, suppressed, [foo])',
135+
'new SuppressedError(error, suppressed, [0][0])',
136+
'new SuppressedError(error, suppressed, {})',
137+
'new SuppressedError(error, suppressed, {foo})',
138+
'new SuppressedError(error, suppressed, {foo: 0}.foo)',
139+
'new SuppressedError(error, suppressed, lineNumber=2)',
140+
'const error = new SuppressedError;',
141+
],
142+
});

test/no-instanceof-builtins.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const strictStrategyInvalid = [
2525
'foo instanceof URIError',
2626
'foo instanceof InternalError',
2727
'foo instanceof AggregateError',
28+
'foo instanceof SuppressedError',
2829

2930
// Collection types
3031
'foo instanceof Map',
@@ -102,6 +103,7 @@ test.snapshot({
102103
'err instanceof URIError',
103104
'err instanceof InternalError',
104105
'err instanceof AggregateError',
106+
'err instanceof SuppressedError',
105107
].map(code => ({code, options: [{useErrorIsError: true, strategy: 'strict'}]})),
106108
});
107109

0 commit comments

Comments
 (0)