Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(type-utils): check for type parameters on isBuiltinSymbolLikeRecurser() #10026

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
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
17 changes: 17 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 @@ -132,6 +132,11 @@ function fun(value: any) {
`
function fun(value: unknown) {
throw value;
}
`,
`
function fun<T extends Error>(t: T): void {
throw t;
}
`,
],
Expand Down Expand Up @@ -468,5 +473,17 @@ function fun(value: unknown) {
},
],
},
{
code: `
function fun<T extends number>(t: T): void {
throw t;
}
`,
errors: [
{
messageId: 'object',
},
],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ ruleTester.run('prefer-promise-reject-errors', rule, {
}
}
`,
`
declare const foo: PromiseConstructor;
function fun<T extends Error>(t: T): void {
foo.reject(t);
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -1443,5 +1449,23 @@ Bar.reject(5);
},
],
},
{
code: `
declare const foo: PromiseConstructor;
function fun<T extends number>(t: T): void {
foo.reject(t);
}
`,
errors: [
{
messageId: 'rejectAnError',
type: AST_NODE_TYPES.CallExpression,
line: 4,
endLine: 4,
column: 3,
endColumn: 16,
},
],
},
],
});
11 changes: 11 additions & 0 deletions packages/type-utils/src/builtinSymbolLikes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import { isSymbolFromDefaultLibrary } from './isSymbolFromDefaultLibrary';
Expand Down Expand Up @@ -158,6 +159,16 @@
isBuiltinSymbolLikeRecurser(program, t, predicate),
);
}
// https://github.com/JoshuaKGoldberg/ts-api-utils/issues/382
if ((tsutils.isTypeParameter as (type: ts.Type) => boolean)(type)) {
const t = type.getConstraint();

Check warning on line 164 in packages/type-utils/src/builtinSymbolLikes.ts

View check run for this annotation

Codecov / codecov/patch

packages/type-utils/src/builtinSymbolLikes.ts#L164

Added line #L164 was not covered by tests

if (t) {
return isBuiltinSymbolLikeRecurser(program, t, predicate);

Check warning on line 167 in packages/type-utils/src/builtinSymbolLikes.ts

View check run for this annotation

Codecov / codecov/patch

packages/type-utils/src/builtinSymbolLikes.ts#L167

Added line #L167 was not covered by tests
}

return false;

Check warning on line 170 in packages/type-utils/src/builtinSymbolLikes.ts

View check run for this annotation

Codecov / codecov/patch

packages/type-utils/src/builtinSymbolLikes.ts#L170

Added line #L170 was not covered by tests
}

const predicateResult = predicate(type);
if (typeof predicateResult === 'boolean') {
Expand Down
Loading