🌐 AI搜索 & 代理 主页
Skip to content
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
31 changes: 31 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 @@ -191,6 +191,19 @@ throw new Map();
},
{
code: `
function func<T1, T2>() {
let err: Promise<T1> | Promise<T2>;
throw err;
}
`,
options: [
{
allow: ['Promise'],
},
],
},
{
code: `
try {
} catch (e) {
throw e;
Expand Down Expand Up @@ -615,6 +628,24 @@ function fun<T extends number>(t: T): void {
},
{
code: `
function func<T1, T2>() {
let err: Promise<T1> | Promise<T2> | void;
throw err;
}
`,
errors: [
{
messageId: 'object',
},
],
options: [
{
allow: ['Promise'],
},
],
},
{
code: `
class UnknownError implements Error {}
throw new UnknownError();
`,
Expand Down
4 changes: 4 additions & 0 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ export function typeMatchesSpecifier(
specifier: TypeOrValueSpecifier,
program: ts.Program,
): boolean {
if (tsutils.isUnionType(type)) {
return type.types.every(t => typeMatchesSpecifier(t, specifier, program));
}

const wholeTypeMatches = ((): boolean => {
if (tsutils.isIntrinsicErrorType(type)) {
return false;
Expand Down
11 changes: 11 additions & 0 deletions packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ describe('TypeOrValueSpecifier', () => {
['interface Foo {prop: string}; type Test = Foo;', 'RegExp'],
['type Test = RegExp;', 'Foo'],
['type Test = RegExp;', 'BigInt'],
['type Test = RegExp | BigInt;', 'BigInt'],
] as const satisfies [string, TypeOrValueSpecifier][])(
"doesn't match a mismatched universal string specifier: %s\n\t%s",
([code, typeOrValueSpecifier], { expect }) => {
Expand Down Expand Up @@ -267,6 +268,10 @@ describe('TypeOrValueSpecifier', () => {
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Bar' },
],
[
'interface Foo {prop: string}; type Test = Foo | string;',
{ from: 'file', name: 'Foo' },
],
[
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: ['Bar', 'Baz'] },
Expand Down Expand Up @@ -306,6 +311,7 @@ describe('TypeOrValueSpecifier', () => {

it.for([
['type Test = RegExp;', { from: 'lib', name: 'BigInt' }],
['type Test = RegExp | BigInt;', { from: 'lib', name: 'BigInt' }],
['type Test = RegExp;', { from: 'lib', name: ['BigInt', 'Date'] }],
] as const satisfies [string, TypeOrValueSpecifier][])(
"doesn't match a mismatched lib specifier: %s\n\t%s",
Expand All @@ -326,6 +332,7 @@ describe('TypeOrValueSpecifier', () => {

it.for([
['type Test = string;', { from: 'lib', name: 'number' }],
['type Test = string | number;', { from: 'lib', name: 'number' }],
['type Test = string;', { from: 'lib', name: ['number', 'boolean'] }],
] as const satisfies [string, TypeOrValueSpecifier][])(
"doesn't match a mismatched intrinsic type specifier: %s\n\t%s",
Expand Down Expand Up @@ -545,6 +552,10 @@ describe('TypeOrValueSpecifier', () => {
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: 'Symbol', package: 'typescript' },
],
[
'import type {Node} from "typescript"; type Test = Node | Symbol;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: ['Symbol', 'Checker'], package: 'typescript' },
Expand Down
Loading