Skip to content

Commit 6b3a8ce

Browse files
JoshuaKGoldbergyeonjuanauvred
committed
feat(eslint-plugin): deprecate no-throw-literal and add a renamed only-throw-error (typescript-eslint#8701)
* feat(eslint-plugin): deprecate no-throw-literal and add a renamed only-throw-error * Update packages/eslint-plugin/docs/rules/no-throw-literal.mdx Co-authored-by: YeonJuan <yeonjuan93@naver.com> * Updated options snapshot * Update packages/eslint-plugin/docs/rules/no-throw-literal.mdx Co-authored-by: auvred <61150013+auvred@users.noreply.github.com> --------- Co-authored-by: YeonJuan <yeonjuan93@naver.com> Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
1 parent 6d07f42 commit 6b3a8ce

File tree

15 files changed

+743
-110
lines changed

15 files changed

+743
-110
lines changed

packages/eslint-plugin/docs/rules/no-throw-literal.mdx

Lines changed: 7 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -12,104 +12,14 @@ import TabItem from '@theme/TabItem';
1212
It is considered good practice to only `throw` the `Error` object itself or an object using the `Error` object as base objects for user-defined exceptions.
1313
The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated.
1414

15-
This rule restricts what can be thrown as an exception. When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object. With the `allowThrowingAny` and `allowThrowingUnknown`, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`.
15+
This rule restricts what can be thrown as an exception.
1616

17-
## Examples
17+
:::warning
18+
This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx).
19+
When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object.
20+
With the `allowThrowingAny` and `allowThrowingUnknown` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`.
1821

19-
This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object.
20-
21-
<Tabs>
22-
<TabItem value="❌ Incorrect">
23-
24-
```ts
25-
throw 'error';
26-
27-
throw 0;
28-
29-
throw undefined;
30-
31-
throw null;
32-
33-
const err = new Error();
34-
throw 'an ' + err;
35-
36-
const err = new Error();
37-
throw `${err}`;
38-
39-
const err = '';
40-
throw err;
41-
42-
function err() {
43-
return '';
44-
}
45-
throw err();
46-
47-
const foo = {
48-
bar: '',
49-
};
50-
throw foo.bar;
51-
```
52-
53-
</TabItem>
54-
<TabItem value="✅ Correct">
55-
56-
```ts
57-
throw new Error();
58-
59-
throw new Error('error');
60-
61-
const e = new Error('error');
62-
throw e;
63-
64-
try {
65-
throw new Error('error');
66-
} catch (e) {
67-
throw e;
68-
}
69-
70-
const err = new Error();
71-
throw err;
72-
73-
function err() {
74-
return new Error();
75-
}
76-
throw err();
77-
78-
const foo = {
79-
bar: new Error(),
80-
};
81-
throw foo.bar;
82-
83-
class CustomError extends Error {
84-
// ...
85-
}
86-
throw new CustomError();
87-
```
88-
89-
</TabItem>
90-
</Tabs>
91-
92-
## Options
93-
94-
This rule adds the following options:
95-
96-
```ts
97-
interface Options {
98-
/**
99-
* Whether to always allow throwing values typed as `any`.
100-
*/
101-
allowThrowingAny?: boolean;
102-
103-
/**
104-
* Whether to always allow throwing values typed as `unknown`.
105-
*/
106-
allowThrowingUnknown?: boolean;
107-
}
108-
109-
const defaultOptions: Options = {
110-
allowThrowingAny: false,
111-
allowThrowingUnknown: false,
112-
};
113-
```
22+
The current name `no-throw-literal` will be removed in a future major version of typescript-eslint.
23+
:::
11424

11525
{/* Intentionally Omitted: When Not To Use It */}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
description: 'Disallow throwing non-`Error` values as exceptions.'
3+
---
4+
5+
import Tabs from '@theme/Tabs';
6+
import TabItem from '@theme/TabItem';
7+
8+
> 🛑 This file is source code, not the primary documentation location! 🛑
9+
>
10+
> See **https://typescript-eslint.io/rules/only-throw-error** for documentation.
11+
12+
It is considered good practice to only `throw` the `Error` object itself or an object using the `Error` object as base objects for user-defined exceptions.
13+
The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated.
14+
15+
This rule restricts what can be thrown as an exception.
16+
17+
## Examples
18+
19+
This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object.
20+
21+
<Tabs>
22+
<TabItem value="❌ Incorrect">
23+
24+
```ts
25+
throw 'error';
26+
27+
throw 0;
28+
29+
throw undefined;
30+
31+
throw null;
32+
33+
const err = new Error();
34+
throw 'an ' + err;
35+
36+
const err = new Error();
37+
throw `${err}`;
38+
39+
const err = '';
40+
throw err;
41+
42+
function err() {
43+
return '';
44+
}
45+
throw err();
46+
47+
const foo = {
48+
bar: '',
49+
};
50+
throw foo.bar;
51+
```
52+
53+
</TabItem>
54+
<TabItem value="✅ Correct">
55+
56+
```ts
57+
throw new Error();
58+
59+
throw new Error('error');
60+
61+
const e = new Error('error');
62+
throw e;
63+
64+
try {
65+
throw new Error('error');
66+
} catch (e) {
67+
throw e;
68+
}
69+
70+
const err = new Error();
71+
throw err;
72+
73+
function err() {
74+
return new Error();
75+
}
76+
throw err();
77+
78+
const foo = {
79+
bar: new Error(),
80+
};
81+
throw foo.bar;
82+
83+
class CustomError extends Error {
84+
// ...
85+
}
86+
throw new CustomError();
87+
```
88+
89+
</TabItem>
90+
</Tabs>
91+
92+
## Options
93+
94+
This rule adds the following options:
95+
96+
```ts
97+
interface Options {
98+
/**
99+
* Whether to always allow throwing values typed as `any`.
100+
*/
101+
allowThrowingAny?: boolean;
102+
103+
/**
104+
* Whether to always allow throwing values typed as `unknown`.
105+
*/
106+
allowThrowingUnknown?: boolean;
107+
}
108+
109+
const defaultOptions: Options = {
110+
allowThrowingAny: false,
111+
allowThrowingUnknown: false,
112+
};
113+
```
114+
115+
{/* Intentionally Omitted: When Not To Use It */}

packages/eslint-plugin/src/configs/all.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ export = {
9090
'no-shadow': 'off',
9191
'@typescript-eslint/no-shadow': 'error',
9292
'@typescript-eslint/no-this-alias': 'error',
93-
'no-throw-literal': 'off',
94-
'@typescript-eslint/no-throw-literal': 'error',
9593
'@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error',
9694
'@typescript-eslint/no-unnecessary-condition': 'error',
9795
'@typescript-eslint/no-unnecessary-qualifier': 'error',
@@ -118,6 +116,8 @@ export = {
118116
'@typescript-eslint/no-useless-template-literals': 'error',
119117
'@typescript-eslint/no-var-requires': 'error',
120118
'@typescript-eslint/non-nullable-type-assertion-style': 'error',
119+
'no-throw-literal': 'off',
120+
'@typescript-eslint/only-throw-error': 'error',
121121
'@typescript-eslint/parameter-properties': 'error',
122122
'@typescript-eslint/prefer-as-const': 'error',
123123
'prefer-destructuring': 'off',

packages/eslint-plugin/src/configs/disable-type-checked.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export = {
4141
'@typescript-eslint/no-unsafe-unary-minus': 'off',
4242
'@typescript-eslint/no-useless-template-literals': 'off',
4343
'@typescript-eslint/non-nullable-type-assertion-style': 'off',
44+
'@typescript-eslint/only-throw-error': 'off',
4445
'@typescript-eslint/prefer-destructuring': 'off',
4546
'@typescript-eslint/prefer-find': 'off',
4647
'@typescript-eslint/prefer-includes': 'off',

packages/eslint-plugin/src/configs/strict-type-checked-only.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ export = {
2323
'@typescript-eslint/no-misused-promises': 'error',
2424
'@typescript-eslint/no-mixed-enums': 'error',
2525
'@typescript-eslint/no-redundant-type-constituents': 'error',
26-
'no-throw-literal': 'off',
27-
'@typescript-eslint/no-throw-literal': 'error',
2826
'@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error',
2927
'@typescript-eslint/no-unnecessary-condition': 'error',
3028
'@typescript-eslint/no-unnecessary-type-arguments': 'error',
@@ -36,6 +34,8 @@ export = {
3634
'@typescript-eslint/no-unsafe-member-access': 'error',
3735
'@typescript-eslint/no-unsafe-return': 'error',
3836
'@typescript-eslint/no-useless-template-literals': 'error',
37+
'no-throw-literal': 'off',
38+
'@typescript-eslint/only-throw-error': 'error',
3939
'@typescript-eslint/prefer-includes': 'error',
4040
'prefer-promise-reject-errors': 'off',
4141
'@typescript-eslint/prefer-promise-reject-errors': 'error',

packages/eslint-plugin/src/configs/strict-type-checked.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export = {
4444
'@typescript-eslint/no-non-null-assertion': 'error',
4545
'@typescript-eslint/no-redundant-type-constituents': 'error',
4646
'@typescript-eslint/no-this-alias': 'error',
47-
'no-throw-literal': 'off',
48-
'@typescript-eslint/no-throw-literal': 'error',
4947
'@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error',
5048
'@typescript-eslint/no-unnecessary-condition': 'error',
5149
'@typescript-eslint/no-unnecessary-type-arguments': 'error',
@@ -64,6 +62,8 @@ export = {
6462
'@typescript-eslint/no-useless-constructor': 'error',
6563
'@typescript-eslint/no-useless-template-literals': 'error',
6664
'@typescript-eslint/no-var-requires': 'error',
65+
'no-throw-literal': 'off',
66+
'@typescript-eslint/only-throw-error': 'error',
6767
'@typescript-eslint/prefer-as-const': 'error',
6868
'@typescript-eslint/prefer-includes': 'error',
6969
'@typescript-eslint/prefer-literal-enum-member': 'error',

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import noUselessTemplateLiterals from './no-useless-template-literals';
101101
import noVarRequires from './no-var-requires';
102102
import nonNullableTypeAssertionStyle from './non-nullable-type-assertion-style';
103103
import objectCurlySpacing from './object-curly-spacing';
104+
import onlyThrowError from './only-throw-error';
104105
import paddingLineBetweenStatements from './padding-line-between-statements';
105106
import parameterProperties from './parameter-properties';
106107
import preferAsConst from './prefer-as-const';
@@ -245,6 +246,7 @@ export default {
245246
'no-var-requires': noVarRequires,
246247
'non-nullable-type-assertion-style': nonNullableTypeAssertionStyle,
247248
'object-curly-spacing': objectCurlySpacing,
249+
'only-throw-error': onlyThrowError,
248250
'padding-line-between-statements': paddingLineBetweenStatements,
249251
'parameter-properties': parameterProperties,
250252
'prefer-as-const': preferAsConst,

packages/eslint-plugin/src/rules/no-throw-literal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ export default createRule<Options, MessageIds>({
2323
name: 'no-throw-literal',
2424
meta: {
2525
type: 'problem',
26+
deprecated: true,
27+
replacedBy: ['@typescript-eslint/only-throw-error'],
2628
docs: {
2729
description: 'Disallow throwing literals as exceptions',
28-
recommended: 'strict',
2930
extendsBaseRule: true,
3031
requiresTypeChecking: true,
3132
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy