diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ff1da5db91102..236f88bff3e3e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4091,15 +4091,25 @@ namespace ts { // ambient module, just use declaration/symbol name (fallthrough) } else { + if (file.moduleName) { + return `"${file.moduleName}"`; + } const contextFile = getSourceFileOfNode(getOriginalNode(context!.enclosingDeclaration))!; - return `"${file.moduleName || moduleSpecifiers.getModuleSpecifiers( - symbol, - compilerOptions, - contextFile, - context!.tracker.moduleResolverHost!, - context!.tracker.moduleResolverHost!.getSourceFiles!(), - { importModuleSpecifierPreference: "non-relative" } - )[0]}"`; + const links = getSymbolLinks(symbol); + let specifier = links.specifierCache && links.specifierCache.get(contextFile.path); + if (!specifier) { + specifier = flatten(moduleSpecifiers.getModuleSpecifiers( + symbol, + compilerOptions, + contextFile, + context!.tracker.moduleResolverHost!, + context!.tracker.moduleResolverHost!.getSourceFiles!(), + { importModuleSpecifierPreference: "non-relative" } + ))[0]; + links.specifierCache = links.specifierCache || createMap(); + links.specifierCache.set(contextFile.path, specifier); + } + return `"${specifier}"`; } } const declaration = symbol.declarations[0]; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index b1e52d68bc01e..268fa50154f96 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3492,6 +3492,7 @@ namespace ts { enumKind?: EnumKind; // Enum declaration classification originatingImport?: ImportDeclaration | ImportCall; // Import declaration which produced the symbol, present if the symbol is marked as uncallable but had call signatures in `resolveESModuleSymbol` lateSymbol?: Symbol; // Late-bound symbol for a computed property + specifierCache?: Map; // For symbols corresponding to external modules, a cache of incoming path -> module specifier name mappings } /* @internal */ diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 114b2341fa95e..e60d6865fc345 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -3048,6 +3048,7 @@ declare namespace ts { enumKind?: EnumKind; originatingImport?: ImportDeclaration | ImportCall; lateSymbol?: Symbol; + specifierCache?: Map; } enum EnumKind { Numeric = 0,