Skip to content

feat(eslint-plugin): [only-throw-error] add allow option #10221

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

Merged
merged 5 commits into from
Nov 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/packages/type-utils/TypeOrValueSpecifier.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ Universal string specifiers will be removed in a future major version of typescr

- [`@typescript-eslint/no-floating-promises` > `allowForKnownSafeCalls`](/rules/no-floating-promises#allowforknownsafecalls)
- [`@typescript-eslint/no-floating-promises` > `allowForKnownSafePromises`](/rules/no-floating-promises#allowforknownsafepromises)
- [`@typescript-eslint/only-throw-error` > `allow`](/rules/only-throw-error/#allow)
- [`@typescript-eslint/prefer-readonly-parameter-types` > `allow`](/rules/prefer-readonly-parameter-types/#allow)
26 changes: 24 additions & 2 deletions packages/eslint-plugin/docs/rules/only-throw-error.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ This rule adds the following options:

```ts
interface Options {
/**
* Type specifiers that can be thrown.
*/
allow?: (
| {
from: 'file';
name: [string, ...string[]] | string;
path?: string;
}
| {
from: 'lib';
name: [string, ...string[]] | string;
}
| {
from: 'package';
name: [string, ...string[]] | string;
package: string;
}
| string
)[];

/**
* Whether to always allow throwing values typed as `any`.
*/
Expand All @@ -114,8 +135,9 @@ interface Options {
}

const defaultOptions: Options = {
allowThrowingAny: false,
allowThrowingUnknown: false,
allow: [],
allowThrowingAny: true,
allowThrowingUnknown: true,
};
```

Expand Down
16 changes: 15 additions & 1 deletion packages/eslint-plugin/src/rules/only-throw-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as ts from 'typescript';

import type { TypeOrValueSpecifier } from '../util';

import {
createRule,
getParserServices,
isErrorLike,
isTypeAnyType,
isTypeUnknownType,
typeMatchesSomeSpecifier,
typeOrValueSpecifiersSchema,
} from '../util';

type MessageIds = 'object' | 'undef';

type Options = [
{
allow?: TypeOrValueSpecifier[];
allowThrowingAny?: boolean;
allowThrowingUnknown?: boolean;
},
Expand All @@ -39,6 +44,10 @@ export default createRule<Options, MessageIds>({
type: 'object',
additionalProperties: false,
properties: {
allow: {
...typeOrValueSpecifiersSchema,
description: 'Type specifiers that can be thrown.',
},
allowThrowingAny: {
type: 'boolean',
description:
Expand All @@ -55,13 +64,14 @@ export default createRule<Options, MessageIds>({
},
defaultOptions: [
{
allow: [],
allowThrowingAny: true,
allowThrowingUnknown: true,
},
],
create(context, [options]) {
const services = getParserServices(context);

const allow = options.allow;
function checkThrowArgument(node: TSESTree.Node): void {
if (
node.type === AST_NODE_TYPES.AwaitExpression ||
Expand All @@ -72,6 +82,10 @@ export default createRule<Options, MessageIds>({

const type = services.getTypeAtLocation(node);

if (typeMatchesSomeSpecifier(type, allow, services.program)) {
return;
}

if (type.flags & ts.TypeFlags.Undefined) {
context.report({ node, messageId: 'undef' });
return;
Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-plugin/tests/fixtures/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-ignore
declare module 'errors' {
class ErrorLike {}

export function createError(): ErrorLike;
}
3 changes: 2 additions & 1 deletion packages/eslint-plugin/tests/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"deprecated.ts",
"mixed-enums-decl.ts",
"react.tsx",
"var-declaration.ts"
"var-declaration.ts",
"errors.ts"
]
}
68 changes: 68 additions & 0 deletions packages/eslint-plugin/tests/rules/only-throw-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,56 @@ function fun<T extends Error>(t: T): void {
throw t;
}
`,
{
code: `
throw undefined;
`,
options: [
{
allow: [{ from: 'lib', name: 'undefined' }],
allowThrowingAny: false,
allowThrowingUnknown: false,
},
],
},
{
code: `
class CustomError implements Error {}
throw new CustomError();
`,
options: [
{
allow: [{ from: 'file', name: 'CustomError' }],
allowThrowingAny: false,
allowThrowingUnknown: false,
},
],
},
{
code: `
throw new Map();
`,
options: [
{
allow: [{ from: 'lib', name: 'Map' }],
allowThrowingAny: false,
allowThrowingUnknown: false,
},
],
},
{
code: `
import { createError } from 'errors';
throw createError();
`,
options: [
{
allow: [{ from: 'package', name: 'ErrorLike', package: 'errors' }],
allowThrowingAny: false,
allowThrowingUnknown: false,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -485,5 +535,23 @@ function fun<T extends number>(t: T): void {
},
],
},
{
code: `
class UnknownError implements Error {}
throw new UnknownError();
`,
errors: [
{
messageId: 'object',
},
],
options: [
{
allow: [{ from: 'file', name: 'CustomError' }],
allowThrowingAny: false,
allowThrowingUnknown: false,
},
],
},
],
});
113 changes: 113 additions & 0 deletions packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading