Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 5 commits intotypescript-eslint:mainfromyeonjuan:feat/9525
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsdocs/packages/type-utils/TypeOrValueSpecifier.mdx
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/docs/rules/only-throw-error.mdx
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionpackages/eslint-plugin/src/rules/only-throw-error.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/tests/fixtures/errors.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
// @ts-ignore
declare module 'errors' {
class ErrorLike {}

export function createError(): ErrorLike;
}
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/tests/fixtures/tsconfig.json
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/tests/rules/only-throw-error.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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,
},
],
},
],
});
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp