Skip to content

fix(type-utils): support matching intersection types in TypeOrValueSpecifier with a PackageSpecifier #10667

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
67 changes: 42 additions & 25 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,50 @@ export function typeMatchesSpecifier(
specifier: TypeOrValueSpecifier,
program: ts.Program,
): boolean {
if (tsutils.isIntrinsicErrorType(type)) {
return false;
}
if (typeof specifier === 'string') {
return specifierNameMatches(type, specifier);
}
if (!specifierNameMatches(type, specifier.name)) {
return false;
const wholeTypeMatches = ((): boolean => {
if (tsutils.isIntrinsicErrorType(type)) {
return false;
}
if (typeof specifier === 'string') {
return specifierNameMatches(type, specifier);
}
if (!specifierNameMatches(type, specifier.name)) {
return false;
}
const symbol = type.getSymbol() ?? type.aliasSymbol;
const declarations = symbol?.getDeclarations() ?? [];
const declarationFiles = declarations.map(declaration =>
declaration.getSourceFile(),
);
switch (specifier.from) {
case 'file':
return typeDeclaredInFile(specifier.path, declarationFiles, program);
case 'lib':
return typeDeclaredInLib(declarationFiles, program);
case 'package':
return typeDeclaredInPackageDeclarationFile(
specifier.package,
declarations,
declarationFiles,
program,
);
}
})();

if (wholeTypeMatches) {
return true;
}
const symbol = type.getSymbol() ?? type.aliasSymbol;
const declarations = symbol?.getDeclarations() ?? [];
const declarationFiles = declarations.map(declaration =>
declaration.getSourceFile(),
);
switch (specifier.from) {
case 'file':
return typeDeclaredInFile(specifier.path, declarationFiles, program);
case 'lib':
return typeDeclaredInLib(declarationFiles, program);
case 'package':
return typeDeclaredInPackageDeclarationFile(
specifier.package,
declarations,
declarationFiles,
program,
);

if (
tsutils.isIntersectionType(type) &&
tsutils
.intersectionTypeParts(type)
.some(part => typeMatchesSpecifier(part, specifier, program))
) {
return true;
}

return false;
}

export const typeMatchesSomeSpecifier = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type * as ts from 'typescript';

import * as tsutils from 'ts-api-utils';

export function specifierNameMatches(
type: ts.Type,
names: string | string[],
Expand All @@ -19,9 +17,5 @@ export function specifierNameMatches(
return true;
}

if (tsutils.isIntersectionType(type)) {
return type.types.some(subType => specifierNameMatches(subType, names));
}

return false;
}
55 changes: 53 additions & 2 deletions packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,66 @@ describe('TypeOrValueSpecifier', () => {
[
`
type SafePromise = Promise<number> & { __safeBrand: string };
type JoinedPromise = SafePromise & {};
type ResultType = { foo: 'bar' };
type Test = SafePromise & ResultType;
`,
{ from: 'file', name: ['JoinedPromise'] },
{ from: 'file', name: ['ResultType'] },
Comment on lines 397 to +401
Copy link
Member Author

@ronami ronami Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test wasn't checking for intersections correctly, as it checked the symbol of the "returned" type.

],
])(
'matches a matching type specifier for an intersection type: %s',
runTestPositive,
);

it.each<[string, TypeOrValueSpecifier]>([
[
`
declare module "node:test" {
type SafePromise = Promise<undefined> & { __safeBrand: string };
type ItResult = { foo: 'bar' };

export function it(): SafePromise & ItResult;
}

import { it } from "node:test";

type Test = ReturnType<typeof it>;
`,
{
from: 'package',
name: ['ItResult'],
package: 'node:test',
},
],
])(
'matches a matching package specifier for an intersection type: %s',
runTestPositive,
);

it.each<[string, TypeOrValueSpecifier]>([
[
`
declare module "node:test" {
type SafePromise = Promise<undefined> & { __safeBrand: string };
type ItResult = { foo: 'bar' };

export function it(): SafePromise & ItResult;
}

import { it } from "node:test";

type Test = ReturnType<typeof it>;
`,
{
from: 'package',
name: ['Result'],
package: 'node:test',
},
],
])(
"doesn't match a mismatched package specifier for an intersection type: %s",
runTestNegative,
);

it("does not match a `declare global` with the 'global' package name", () => {
runTestNegative(
`
Expand Down
Loading