Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [no-throw-literal] add options to to disallow `a…
…ny`/`unknown` (#4207)

* feat: add options to no-throw-literal to disallow any/unknown

* fix: eslint errors

Co-authored-by: Daniel Playfair Cal <dcal@atlassian.com>
  • Loading branch information
hedgepigdaniel and Daniel Playfair Cal committed Dec 20, 2021
1 parent 17d91ce commit ff0adf9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
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,
},
],
},
],
});

0 comments on commit ff0adf9

Please sign in to comment.