diff --git a/packages/type-utils/src/TypeOrValueSpecifier.ts b/packages/type-utils/src/TypeOrValueSpecifier.ts index 12c7679349d9..16958370f57b 100644 --- a/packages/type-utils/src/TypeOrValueSpecifier.ts +++ b/packages/type-utils/src/TypeOrValueSpecifier.ts @@ -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( @@ -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, + ); } } diff --git a/packages/type-utils/typings/typescript.d.ts b/packages/type-utils/typings/typescript.d.ts index 73304155ee74..335b12b86fe5 100644 --- a/packages/type-utils/typings/typescript.d.ts +++ b/packages/type-utils/typings/typescript.d.ts @@ -30,4 +30,15 @@ declare module 'typescript' { */ intrinsicName?: string; } + + interface Program { + /** + * Maps from a SourceFile's `.path` to the name of the package it was imported with. + */ + readonly sourceFileToPackageName: ReadonlyMap; + } + + interface SourceFile extends Declaration, LocalsContainer { + path: Path; + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy