Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [no-throw-literal] add options to to disallow any/unknown #4207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/eslint-plugin/docs/rules/no-throw-literal.md
Expand Up @@ -3,7 +3,7 @@
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.
The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated.

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.
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`.

## Rule Details

Expand Down Expand Up @@ -93,6 +93,20 @@ throw new CustomError();
}
```

### Options

```jsonc
{
"@typescript-eslint/no-throw-literal": [
"error",
{
"allowThrowingAny": true, // Default is to allow throwing values of type any
"allowThrowingUnknown": true // Default is to allow throwing values of type unknown
}
]
}
```

---

<sup>
Expand Down
49 changes: 40 additions & 9 deletions packages/eslint-plugin/src/rules/no-throw-literal.ts
Expand Up @@ -5,7 +5,16 @@ import {
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';

export default util.createRule({
type MessageIds = 'object' | 'undef';

type Options = [
{
allowThrowingAny?: boolean;
allowThrowingUnknown?: boolean;
},
];

export default util.createRule<Options, MessageIds>({
name: 'no-throw-literal',
meta: {
type: 'problem',
Expand All @@ -15,14 +24,32 @@ export default util.createRule({
extendsBaseRule: true,
requiresTypeChecking: true,
},
schema: [],
schema: [
{
type: 'object',
properties: {
allowThrowingAny: {
type: 'boolean',
},
allowThrowingUnknown: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
object: 'Expected an error object to be thrown.',
undef: 'Do not throw undefined.',
},
},
defaultOptions: [],
create(context) {
defaultOptions: [
{
allowThrowingAny: true,
allowThrowingUnknown: true,
},
],
create(context, [options]) {
const parserServices = util.getParserServices(context);
const program = parserServices.program;
const checker = program.getTypeChecker();
Expand Down Expand Up @@ -77,11 +104,15 @@ export default util.createRule({
return;
}

if (
util.isTypeAnyType(type) ||
util.isTypeUnknownType(type) ||
isErrorLike(type)
) {
if (options.allowThrowingAny && util.isTypeAnyType(type)) {
return;
}

if (options.allowThrowingUnknown && util.isTypeUnknownType(type)) {
return;
}

if (isErrorLike(type)) {
return;
}

Expand Down
44 changes: 44 additions & 0 deletions packages/eslint-plugin/tests/rules/no-throw-literal.test.ts
Expand Up @@ -123,6 +123,16 @@ throw nullishError || new Error();
declare const nullishError: Error | undefined;
throw nullishError ? nullishError : new Error();
`,
`
function fun(value: any) {
throw value;
}
`,
`
function fun(value: unknown) {
throw value;
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -423,5 +433,39 @@ throw foo as string;
`,
errors: [{ messageId: 'object' }],
},
{
code: `
function fun(value: any) {
throw value;
}
`,
errors: [
{
messageId: 'object',
},
],
options: [
{
allowThrowingAny: false,
},
],
},
{
code: `
function fun(value: unknown) {
throw value;
}
`,
errors: [
{
messageId: 'object',
},
],
options: [
{
allowThrowingUnknown: false,
},
],
},
],
});