|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import { AST_NODE_TYPES, type TSESTree } from "@typescript-eslint/utils"; |
6 | | -import type { Type, UnionType } from "typescript"; |
| 6 | +import type { RuleContext } from "@typescript-eslint/utils/ts-eslint"; |
| 7 | +import typeMatchesSpecifier, { |
| 8 | + type TypeDeclarationSpecifier, |
| 9 | +} from "ts-declaration-location"; |
| 10 | +import type { Program, Type, UnionType } from "typescript"; |
7 | 11 |
|
8 | 12 | import typescript from "#/conditional-imports/typescript"; |
9 | 13 |
|
| 14 | +const libSpecifier = { |
| 15 | + from: "lib", |
| 16 | +} satisfies TypeDeclarationSpecifier; |
| 17 | + |
10 | 18 | /* |
11 | 19 | * TS Types. |
12 | 20 | */ |
@@ -429,41 +437,63 @@ export function isUnionType(type: Type): type is UnionType { |
429 | 437 | return typescript !== undefined && type.flags === typescript.TypeFlags.Union; |
430 | 438 | } |
431 | 439 |
|
432 | | -export function isArrayType(type: Type | null): boolean { |
433 | | - return ( |
434 | | - type !== null && |
435 | | - (((type.symbol as unknown) !== undefined && type.symbol.name === "Array") || |
436 | | - (isUnionType(type) && type.types.some(isArrayType))) |
437 | | - ); |
438 | | -} |
439 | | - |
440 | | -export function isArrayConstructorType(type: Type | null): boolean { |
441 | | - return ( |
442 | | - type !== null && |
443 | | - (((type.symbol as unknown) !== undefined && |
444 | | - type.symbol.name === "ArrayConstructor") || |
445 | | - (isUnionType(type) && type.types.some(isArrayConstructorType))) |
446 | | - ); |
447 | | -} |
448 | | - |
449 | | -export function isObjectConstructorType(type: Type | null): boolean { |
450 | | - return ( |
451 | | - type !== null && |
452 | | - (((type.symbol as unknown) !== undefined && |
453 | | - type.symbol.name === "ObjectConstructor") || |
454 | | - (isUnionType(type) && type.types.some(isObjectConstructorType))) |
455 | | - ); |
456 | | -} |
457 | | - |
458 | 440 | export function isFunctionLikeType(type: Type | null): boolean { |
459 | 441 | return type !== null && type.getCallSignatures().length > 0; |
460 | 442 | } |
461 | 443 |
|
462 | | -export function isPromiseType(type: Type | null): boolean { |
463 | | - return ( |
464 | | - type !== null && |
465 | | - (((type.symbol as unknown) !== undefined && |
466 | | - type.symbol.name === "Promise") || |
467 | | - (isUnionType(type) && type.types.some(isPromiseType))) |
468 | | - ); |
| 444 | +export function isArrayType( |
| 445 | + context: RuleContext<string, ReadonlyArray<unknown>>, |
| 446 | + type: Type | null, |
| 447 | +): boolean { |
| 448 | + return typeMatches(context, "Array", type); |
| 449 | +} |
| 450 | + |
| 451 | +export function isArrayConstructorType( |
| 452 | + context: RuleContext<string, ReadonlyArray<unknown>>, |
| 453 | + type: Type | null, |
| 454 | +): boolean { |
| 455 | + return typeMatches(context, "ArrayConstructor", type); |
| 456 | +} |
| 457 | + |
| 458 | +export function isObjectConstructorType( |
| 459 | + context: RuleContext<string, ReadonlyArray<unknown>>, |
| 460 | + type: Type | null, |
| 461 | +): boolean { |
| 462 | + return typeMatches(context, "ObjectConstructor", type); |
| 463 | +} |
| 464 | + |
| 465 | +export function isPromiseType( |
| 466 | + context: RuleContext<string, ReadonlyArray<unknown>>, |
| 467 | + type: Type | null, |
| 468 | +): boolean { |
| 469 | + return typeMatches(context, "Promise", type); |
| 470 | +} |
| 471 | + |
| 472 | +function typeMatches( |
| 473 | + context: RuleContext<string, ReadonlyArray<unknown>>, |
| 474 | + typeName: string, |
| 475 | + type: Type | null, |
| 476 | +): boolean { |
| 477 | + if (type === null) { |
| 478 | + return false; |
| 479 | + } |
| 480 | + const program = context.sourceCode.parserServices?.program ?? undefined; |
| 481 | + if (program === undefined) { |
| 482 | + return false; |
| 483 | + } |
| 484 | + return typeMatchesHelper(program, typeName)(type); |
| 485 | +} |
| 486 | + |
| 487 | +function typeMatchesHelper( |
| 488 | + program: Program, |
| 489 | + typeName: string, |
| 490 | +): (type: Type) => boolean { |
| 491 | + return function test(type: Type) { |
| 492 | + return ( |
| 493 | + ((type.symbol as unknown) !== undefined && |
| 494 | + type.symbol.name === typeName && |
| 495 | + typeMatchesSpecifier(program, libSpecifier, type)) || |
| 496 | + (isUnionType(type) && type.types.some(test)) |
| 497 | + ); |
| 498 | + }; |
469 | 499 | } |
0 commit comments