@@ -3733,19 +3733,56 @@ namespace ts {
3733
3733
return top;
3734
3734
}
3735
3735
3736
+ function getSpecifierForModuleSymbol(symbol: Symbol, context: NodeBuilderContext) {
3737
+ const file = getDeclarationOfKind<SourceFile>(symbol, SyntaxKind.SourceFile);
3738
+ if (file && file.moduleName !== undefined) {
3739
+ // Use the amd name if it is available
3740
+ return file.moduleName;
3741
+ }
3742
+ if (!file) {
3743
+ if (context.tracker.trackReferencedAmbientModule) {
3744
+ const ambientDecls = filter(symbol.declarations, isAmbientModule);
3745
+ if (length(ambientDecls)) {
3746
+ for (const decl of ambientDecls) {
3747
+ context.tracker.trackReferencedAmbientModule(decl);
3748
+ }
3749
+ }
3750
+ }
3751
+ return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
3752
+ }
3753
+ else {
3754
+ if (!context.enclosingDeclaration || !context.tracker.moduleResolverHost) {
3755
+ // If there's no context declaration, we can't lookup a non-ambient specifier, so we just use the symbol name
3756
+ return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
3757
+ }
3758
+ const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration));
3759
+ const links = getSymbolLinks(symbol);
3760
+ let specifier = links.specifierCache && links.specifierCache.get(contextFile.path);
3761
+ if (!specifier) {
3762
+ specifier = flatten(moduleSpecifiers.getModuleSpecifiers(
3763
+ symbol,
3764
+ compilerOptions,
3765
+ contextFile,
3766
+ context.tracker.moduleResolverHost,
3767
+ context.tracker.moduleResolverHost.getSourceFiles!(), // TODO: GH#18217
3768
+ { importModuleSpecifierPreference: "non-relative" }
3769
+ ))[0];
3770
+ links.specifierCache = links.specifierCache || createMap();
3771
+ links.specifierCache.set(contextFile.path, specifier);
3772
+ }
3773
+ return specifier;
3774
+ }
3775
+ }
3776
+
3736
3777
function symbolToTypeNode(symbol: Symbol, context: NodeBuilderContext, meaning: SymbolFlags, overrideTypeArguments?: ReadonlyArray<TypeNode>): TypeNode {
3737
3778
const chain = lookupSymbolChain(symbol, context, meaning, !(context.flags & NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope)); // If we're using aliases outside the current scope, dont bother with the module
3738
3779
3739
- context.flags |= NodeBuilderFlags.InInitialEntityName;
3740
- const rootName = getNameOfSymbolAsWritten(chain[0], context);
3741
- context.flags ^= NodeBuilderFlags.InInitialEntityName;
3742
-
3743
3780
const isTypeOf = meaning === SymbolFlags.Value;
3744
- if (ambientModuleSymbolRegex.test(rootName )) {
3781
+ if (some(chain[0].declarations, hasNonGlobalAugmentationExternalModuleSymbol )) {
3745
3782
// module is root, must use `ImportTypeNode`
3746
3783
const nonRootParts = chain.length > 1 ? createAccessFromSymbolChain(chain, chain.length - 1, 1) : undefined;
3747
3784
const typeParameterNodes = overrideTypeArguments || lookupTypeParameterNodes(chain, 0, context);
3748
- const lit = createLiteralTypeNode(createLiteral(rootName.substring(1, rootName.length - 1 )));
3785
+ const lit = createLiteralTypeNode(createLiteral(getSpecifierForModuleSymbol(chain[0], context )));
3749
3786
if (!nonRootParts || isEntityName(nonRootParts)) {
3750
3787
if (nonRootParts) {
3751
3788
const lastId = isIdentifier(nonRootParts) ? nonRootParts : nonRootParts.right;
@@ -3990,41 +4027,6 @@ namespace ts {
3990
4027
return "default";
3991
4028
}
3992
4029
if (symbol.declarations && symbol.declarations.length) {
3993
- if (some(symbol.declarations, hasExternalModuleSymbol) && context!.enclosingDeclaration) { // TODO: GH#18217
3994
- const file = getDeclarationOfKind<SourceFile>(symbol, SyntaxKind.SourceFile);
3995
- if (!file || !context!.tracker.moduleResolverHost) {
3996
- if (context!.tracker.trackReferencedAmbientModule) {
3997
- const ambientDecls = filter(symbol.declarations, isAmbientModule);
3998
- if (length(ambientDecls)) {
3999
- for (const decl of ambientDecls) {
4000
- context!.tracker.trackReferencedAmbientModule!(decl); // TODO: GH#18217
4001
- }
4002
- }
4003
- }
4004
- // ambient module, just use declaration/symbol name (fallthrough)
4005
- }
4006
- else {
4007
- if (file.moduleName) {
4008
- return `"${file.moduleName}"`;
4009
- }
4010
- const contextFile = getSourceFileOfNode(getOriginalNode(context!.enclosingDeclaration))!;
4011
- const links = getSymbolLinks(symbol);
4012
- let specifier = links.specifierCache && links.specifierCache.get(contextFile.path);
4013
- if (!specifier) {
4014
- specifier = flatten(moduleSpecifiers.getModuleSpecifiers(
4015
- symbol,
4016
- compilerOptions,
4017
- contextFile,
4018
- context!.tracker.moduleResolverHost!,
4019
- context!.tracker.moduleResolverHost!.getSourceFiles!(),
4020
- { importModuleSpecifierPreference: "non-relative" }
4021
- ))[0];
4022
- links.specifierCache = links.specifierCache || createMap();
4023
- links.specifierCache.set(contextFile.path, specifier);
4024
- }
4025
- return `"${specifier}"`;
4026
- }
4027
- }
4028
4030
const declaration = symbol.declarations[0];
4029
4031
const name = getNameOfDeclaration(declaration);
4030
4032
if (name) {
0 commit comments