Skip to content
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

fix(type-utils): treat custom type roots as external #6870

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function specifierNameMatches(type: ts.Type, name: string[] | string): boolean {
if (symbol === undefined) {
return false;
}
return name.some(item => (item as ts.__String) === symbol.escapedName);
return name.some(item => item === (symbol.escapedName as string));
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

function typeDeclaredInFile(
Expand All @@ -155,16 +155,20 @@ function typeDeclaredInFile(
function typeDeclaredInPackage(
packageName: string,
declarationFiles: ts.SourceFile[],
program: ts.Program,
): boolean {
// Handle scoped packages - if the name starts with @, remove it and replace / with __
const typesPackageName =
'@types/' + packageName.replace(/^@([^/]+)\//, '$1__');
const matcher = new RegExp(
`node_modules/(?:${packageName}|${typesPackageName})/`,
);
return declarationFiles.some(declaration =>
matcher.test(declaration.fileName),
);
const typesPackageName = packageName.replace(/^@([^/]+)\//, '$1__');

const matcher = new RegExp(`${packageName}|${typesPackageName}`);
return declarationFiles.some(declaration => {
const packageIdName = program.sourceFileToPackageName.get(declaration.path);
return (
packageIdName !== undefined &&
matcher.test(packageIdName) &&
program.isSourceFileFromExternalLibrary(declaration)
);
});
}

function typeDeclaredInLib(
Expand Down Expand Up @@ -207,6 +211,10 @@ export function typeMatchesSpecifier(
case 'lib':
return typeDeclaredInLib(declarationFiles, program);
case 'package':
return typeDeclaredInPackage(specifier.package, declarationFiles);
return typeDeclaredInPackage(
specifier.package,
declarationFiles,
program,
);
}
}
11 changes: 11 additions & 0 deletions packages/type-utils/typings/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ declare module 'typescript' {
*/
intrinsicName?: string;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding our own type declarations for the internal sourceFileToPackageName seems fishy to me, but that's a decision the maintainers need to do.

I presume you are aware of #6861 - however, there has been no response in the corresponding TS issue....

Copy link
Member

Choose a reason for hiding this comment

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

Yeah for now I think it's fine. If they change that API we should see breakages in the TS version upgrade PR.

interface Program {
/**
* Maps from a SourceFile's `.path` to the name of the package it was imported with.
*/
readonly sourceFileToPackageName: ReadonlyMap<Path, string>;
}

interface SourceFile extends Declaration, LocalsContainer {
path: Path;
}
}