Skip to content

Commit fce3d9f

Browse files
authored
Check the ambientness of a symbol name before attempting to trim it (#26312)
* Check the ambientness of a symbol name before attempting to trim it * Use find instead of forEach, remember to also exclude global augmentations
1 parent f6af618 commit fce3d9f

17 files changed

+271
-44
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3880,31 +3880,34 @@ namespace ts {
38803880
}
38813881
}
38823882
}
3883-
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
3884-
}
3885-
else {
3886-
if (!context.enclosingDeclaration || !context.tracker.moduleResolverHost) {
3887-
// If there's no context declaration, we can't lookup a non-ambient specifier, so we just use the symbol name
3883+
if (ambientModuleSymbolRegex.test(symbol.escapedName as string)) {
38883884
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
38893885
}
3890-
const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration));
3891-
const links = getSymbolLinks(symbol);
3892-
let specifier = links.specifierCache && links.specifierCache.get(contextFile.path);
3893-
if (!specifier) {
3894-
specifier = moduleSpecifiers.getModuleSpecifierForDeclarationFile(
3895-
symbol,
3896-
compilerOptions,
3897-
contextFile,
3898-
context.tracker.moduleResolverHost,
3899-
context.tracker.moduleResolverHost.getSourceFiles!(), // TODO: GH#18217
3900-
{ importModuleSpecifierPreference: "non-relative" },
3901-
host.redirectTargetsMap,
3902-
);
3903-
links.specifierCache = links.specifierCache || createMap();
3904-
links.specifierCache.set(contextFile.path, specifier);
3886+
}
3887+
if (!context.enclosingDeclaration || !context.tracker.moduleResolverHost) {
3888+
// If there's no context declaration, we can't lookup a non-ambient specifier, so we just use the symbol name
3889+
if (ambientModuleSymbolRegex.test(symbol.escapedName as string)) {
3890+
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
39053891
}
3906-
return specifier;
3892+
return getSourceFileOfNode(getNonAugmentationDeclaration(symbol)!).fileName; // A resolver may not be provided for baselines and errors - in those cases we use the fileName in full
3893+
}
3894+
const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration));
3895+
const links = getSymbolLinks(symbol);
3896+
let specifier = links.specifierCache && links.specifierCache.get(contextFile.path);
3897+
if (!specifier) {
3898+
specifier = moduleSpecifiers.getModuleSpecifierForDeclarationFile(
3899+
symbol,
3900+
compilerOptions,
3901+
contextFile,
3902+
context.tracker.moduleResolverHost,
3903+
context.tracker.moduleResolverHost.getSourceFiles!(), // TODO: GH#18217
3904+
{ importModuleSpecifierPreference: "non-relative" },
3905+
host.redirectTargetsMap,
3906+
);
3907+
links.specifierCache = links.specifierCache || createMap();
3908+
links.specifierCache.set(contextFile.path, specifier);
39073909
}
3910+
return specifier;
39083911
}
39093912

39103913
function symbolToTypeNode(symbol: Symbol, context: NodeBuilderContext, meaning: SymbolFlags, overrideTypeArguments?: ReadonlyArray<TypeNode>): TypeNode {

src/compiler/moduleSpecifiers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace ts.moduleSpecifiers {
5151
if (!files) {
5252
return Debug.fail("Files list must be present to resolve symlinks in specifier resolution");
5353
}
54-
const moduleSourceFile = getSourceFileOfNode(moduleSymbol.valueDeclaration);
54+
const moduleSourceFile = getSourceFileOfNode(moduleSymbol.valueDeclaration || getNonAugmentationDeclaration(moduleSymbol));
5555
const modulePaths = getAllModulePaths(files, importingSourceFile.path, moduleSourceFile.fileName, info.getCanonicalFileName, host, redirectTargetsMap);
5656

5757
const global = mapDefined(modulePaths, moduleFileName => getGlobalModuleSpecifier(moduleFileName, info, host, compilerOptions));
@@ -232,8 +232,10 @@ namespace ts.moduleSpecifiers {
232232
}
233233

234234
function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol): string | undefined {
235-
const decl = moduleSymbol.valueDeclaration;
236-
if (isModuleDeclaration(decl) && isStringLiteral(decl.name)) {
235+
const decl = find(moduleSymbol.declarations,
236+
d => isNonGlobalAmbientModule(d) && (!isExternalModuleAugmentation(d) || !isExternalModuleNameRelative(getTextOfIdentifierOrLiteral(d.name)))
237+
) as (ModuleDeclaration & { name: StringLiteral }) | undefined;
238+
if (decl) {
237239
return decl.name.text;
238240
}
239241
}

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ namespace ts {
638638
return false;
639639
}
640640

641+
export function getNonAugmentationDeclaration(symbol: Symbol) {
642+
return find(symbol.declarations, d => !isExternalModuleAugmentation(d) && !(isModuleDeclaration(d) && isGlobalScopeAugmentation(d)));
643+
}
644+
641645
export function isEffectiveExternalModule(node: SourceFile, compilerOptions: CompilerOptions) {
642646
return isExternalModule(node) || compilerOptions.isolatedModules || ((getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS) && !!node.commonJsModuleIndicator);
643647
}

tests/baselines/reference/augmentExportEquals3.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
=== tests/cases/compiler/file1.ts ===
22
function foo() {}
3-
>foo : typeof import("o")
3+
>foo : typeof import("tests/cases/compiler/file1.ts")
44

55
namespace foo {
6-
>foo : typeof import("o")
6+
>foo : typeof import("tests/cases/compiler/file1.ts")
77

88
export var v = 1;
99
>v : number

tests/baselines/reference/augmentExportEquals3_1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ declare module "file1" {
33
>"file1" : typeof import("file1")
44

55
function foo(): void;
6-
>foo : typeof import("o")
6+
>foo : typeof import("tests/cases/compiler/file1.d.ts")
77

88
namespace foo {
9-
>foo : typeof import("o")
9+
>foo : typeof import("tests/cases/compiler/file1.d.ts")
1010

1111
export var v: number;
1212
>v : number

tests/baselines/reference/augmentExportEquals4.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
=== tests/cases/compiler/file1.ts ===
22
class foo {}
3-
>foo : import("o")
3+
>foo : import("tests/cases/compiler/file1.ts")
44

55
namespace foo {
6-
>foo : typeof import("o")
6+
>foo : typeof import("tests/cases/compiler/file1.ts")
77

88
export var v = 1;
99
>v : number

tests/baselines/reference/augmentExportEquals4_1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ declare module "file1" {
33
>"file1" : typeof import("file1")
44

55
class foo {}
6-
>foo : import("o")
6+
>foo : import("tests/cases/compiler/file1.d.ts")
77

88
namespace foo {
9-
>foo : typeof import("o")
9+
>foo : typeof import("tests/cases/compiler/file1.d.ts")
1010

1111
export var v: number;
1212
>v : number

tests/baselines/reference/augmentExportEquals5.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ declare module "express" {
99
>"express" : typeof import("express")
1010

1111
function e(): e.Express;
12-
>e : typeof import("e")
12+
>e : typeof import("tests/cases/compiler/express.d.ts")
1313
>e : any
1414

1515
namespace e {
16-
>e : typeof import("e")
16+
>e : typeof import("tests/cases/compiler/express.d.ts")
1717

1818
interface IRoute {
1919
all(...handler: RequestHandler[]): IRoute;

tests/baselines/reference/augmentExportEquals6.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
=== tests/cases/compiler/file1.ts ===
22
class foo {}
3-
>foo : import("o")
3+
>foo : import("tests/cases/compiler/file1.ts")
44

55
namespace foo {
6-
>foo : typeof import("o")
6+
>foo : typeof import("tests/cases/compiler/file1.ts")
77

88
export class A {}
99
>A : A

tests/baselines/reference/augmentExportEquals6_1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ declare module "file1" {
33
>"file1" : typeof import("file1")
44

55
class foo {}
6-
>foo : import("o")
6+
>foo : import("tests/cases/compiler/file1.d.ts")
77

88
namespace foo {
9-
>foo : typeof import("o")
9+
>foo : typeof import("tests/cases/compiler/file1.d.ts")
1010

1111
class A {}
1212
>A : A

0 commit comments

Comments
 (0)