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): [prefer-promise-reject-errors] options to allow any and unknown#10392

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 3 commits intotypescript-eslint:mainfrombkks1004:main
Dec 2, 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,3 +54,26 @@ new Promise((resolve, reject) => {

</TabItem>
</Tabs>

## Options

This rule adds the following options:

```ts
interface Options {
/**
* Whether to always allow throwing values typed as `any`.
*/
allowThrowingAny?: boolean;

/**
* Whether to always allow throwing values typed as `unknown`.
*/
allowThrowingUnknown?: boolean;
}

const defaultOptions: Options = {
allowThrowingAny: false,
allowThrowingUnknown: false,
};
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,8 @@ import {
createRule,
getParserServices,
isErrorLike,
isTypeAnyType,
isTypeUnknownType,
isFunction,
isIdentifier,
isPromiseConstructorLike,
Expand All@@ -19,6 +21,8 @@ export type MessageIds = 'rejectAnError';
export type Options = [
{
allowEmptyReject?: boolean;
allowThrowingAny?: boolean;
allowThrowingUnknown?: boolean;
},
];

Expand All@@ -45,13 +49,25 @@ export default createRule<Options, MessageIds>({
description:
'Whether to allow calls to `Promise.reject()` with no arguments.',
},
allowThrowingAny: {
type: 'boolean',
description:
'Whether to always allow throwing values typed as `any`.',
},
allowThrowingUnknown: {
type: 'boolean',
description:
'Whether to always allow throwing values typed as `unknown`.',
},
},
},
],
},
defaultOptions: [
{
allowEmptyReject: false,
allowThrowingAny: false,
allowThrowingUnknown: false,
},
],
create(context, [options]) {
Expand All@@ -61,6 +77,15 @@ export default createRule<Options, MessageIds>({
const argument = callExpression.arguments.at(0);
if (argument) {
const type = services.getTypeAtLocation(argument);

if (options.allowThrowingAny && isTypeAnyType(type)) {
return;
}

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

if (
isErrorLike(services.program, type) ||
isReadonlyErrorLike(services.program, type)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,20 @@ ruleTester.run('prefer-promise-reject-errors', rule, {
},
],
},
{
code: `
declare const someAnyValue: any;
Promise.reject(someAnyValue);
`,
options: [{ allowThrowingAny: true, allowThrowingUnknown: false }],
},
{
code: `
declare const someUnknownValue: unknown;
Promise.reject(someUnknownValue);
`,
options: [{ allowThrowingAny: false, allowThrowingUnknown: true }],
},
'Promise.reject(new Error());',
'Promise.reject(new TypeError());',
"Promise.reject(new Error('foo'));",
Expand DownExpand Up@@ -281,6 +295,20 @@ ruleTester.run('prefer-promise-reject-errors', rule, {
foo.reject(t);
}
`,
{
code: `
declare const someAnyValue: any;
Promise.reject(someAnyValue);
`,
options: [{ allowThrowingAny: true, allowThrowingUnknown: true }],
},
{
code: `
declare const someUnknownValue: unknown;
Promise.reject(someUnknownValue);
`,
options: [{ allowThrowingAny: true, allowThrowingUnknown: true }],
},
],
invalid: [
{
Expand DownExpand Up@@ -1467,5 +1495,55 @@ function fun<T extends number>(t: T): void {
},
],
},
{
code: `
declare const someAnyValue: any;
Promise.reject(someAnyValue);
`,
errors: [
{
messageId: 'rejectAnError',
type: AST_NODE_TYPES.CallExpression,
},
],
options: [{ allowThrowingAny: false, allowThrowingUnknown: true }],
},
{
code: `
declare const someUnknownValue: unknown;
Promise.reject(someUnknownValue);
`,
errors: [
{
messageId: 'rejectAnError',
type: AST_NODE_TYPES.CallExpression,
},
],
options: [{ allowThrowingAny: true, allowThrowingUnknown: false }],
},
{
code: `
declare const someUnknownValue: unknown;
Promise.reject(someUnknownValue);
`,
errors: [
{
messageId: 'rejectAnError',
type: AST_NODE_TYPES.CallExpression,
},
],
},
{
code: `
declare const someAnyValue: any;
Promise.reject(someAnyValue);
`,
errors: [
{
messageId: 'rejectAnError',
type: AST_NODE_TYPES.CallExpression,
},
],
},
],
});
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