Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 8b9daeb

Browse files
tsjs-language-engcopybara-github
authored andcommitted
Use internal module namespace in externs type annotations.
PiperOrigin-RevId: 573179784
1 parent 1e70ee5 commit 8b9daeb

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

src/externs.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ export function generateExterns(
165165
const isDts = isDtsFileName(sourceFile.fileName);
166166
const isExternalModule = ts.isExternalModule(sourceFile);
167167

168-
const mtt =
169-
new ModuleTypeTranslator(sourceFile, typeChecker, host, diagnostics, /*isForExterns*/ true);
170-
171168
// .d.ts files declare symbols. The code below translates these into a form understood by Closure
172169
// Compiler, converting the type syntax, but also converting symbol names into a form accessible
173170
// to Closure Compiler.
@@ -206,6 +203,10 @@ export function generateExterns(
206203
rootNamespace = rootNamespace + '_';
207204
}
208205

206+
const mtt = new ModuleTypeTranslator(
207+
sourceFile, typeChecker, host, diagnostics, /*isForExterns*/ true,
208+
/*useInternalNamespaceForExterns=*/ hasExportEquals);
209+
209210
for (const stmt of sourceFile.statements) {
210211
// Always collect alises for imported symbols.
211212
importsVisitor(stmt);

src/module_type_translator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class ModuleTypeTranslator {
105105
private readonly host: AnnotatorHost&googmodule.GoogModuleProcessorHost,
106106
private readonly diagnostics: ts.Diagnostic[],
107107
private readonly isForExterns: boolean,
108+
private readonly useInternalNamespaceForExterns = false,
108109
) {
109110
// TODO: remove once AnnotatorHost.typeBlackListPaths is removed.
110111
this.host.unknownTypesPaths =
@@ -165,6 +166,8 @@ export class ModuleTypeTranslator {
165166
this.symbolToNameCache,
166167
(sym: ts.Symbol) => void this.ensureSymbolDeclared(sym));
167168
translator.isForExterns = this.isForExterns;
169+
translator.useInternalNamespaceForExterns =
170+
this.useInternalNamespaceForExterns;
168171
translator.warn = msg => void this.debugWarn(context, msg);
169172
return translator;
170173
}

src/type_translator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ export class TypeTranslator {
252252
*/
253253
isForExterns = false;
254254

255+
/**
256+
* Whether use internal namespace (with underscore) for the externs types.
257+
* It is set when the declaration file has an export equals ("export = Foo;")
258+
* statement.
259+
*/
260+
useInternalNamespaceForExterns = false;
261+
255262
/**
256263
* When translating the type of an 'extends' clause, e.g. Y in
257264
* class X extends Y<T> { ... }
@@ -419,6 +426,11 @@ export class TypeTranslator {
419426
context = '';
420427
}
421428
const mangled = moduleNameAsIdentifier(this.host, fileName, context);
429+
if (this.isForExterns && this.useInternalNamespaceForExterns &&
430+
!ambientModuleDeclaration &&
431+
isDeclaredInSameFile(this.node, declarations[0])) {
432+
return mangled + '_.';
433+
}
422434
return mangled + '.';
423435
}
424436

test_files/augment/externs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Generated from: test_files/augment/angular/index.d.ts
77
/** @const */
88
var test_files$augment$angular$index_ = {};
9-
/** @type {!test_files$augment$angular$index.angular.IAngularStatic} */
9+
/** @type {!test_files$augment$angular$index_.angular.IAngularStatic} */
1010
test_files$augment$angular$index_.angular;
1111
/** @const */
1212
test_files$augment$angular$index_.angular = {};

test_files/declare_export_dts/externs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ test_files$declare_export_dts$relative_ambient_external_module.user;
5959
// Generated from: test_files/declare_export_dts/declare_export_var.d.ts
6060
/** @const */
6161
var test_files$declare_export_dts$declare_export_var_ = {};
62-
/** @type {!test_files$declare_export_dts$declare_export_var.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
62+
/** @type {!test_files$declare_export_dts$declare_export_var_.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
6363
test_files$declare_export_dts$declare_export_var_.variableDeclaredInDtsModule;
64-
/** @type {!test_files$declare_export_dts$declare_export_var.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
64+
/** @type {!test_files$declare_export_dts$declare_export_var_.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
6565
var variableDeclaredInDtsModule;
6666
/** @const */
6767
test_files$declare_export_dts$declare_export_var_.namespaceInDtsModule = {};

test_files/import_equals/externs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_files$import_equals$import_equals_module_.namespaceInModule = {};
5353
* @struct
5454
*/
5555
test_files$import_equals$import_equals_module_.namespaceInModule.InNamespace = function() {};
56-
/** @type {!test_files$import_equals$import_equals_module.namespaceInModule.InNamespace} */
56+
/** @type {!test_files$import_equals$import_equals_module_.namespaceInModule.InNamespace} */
5757
test_files$import_equals$import_equals_module_.namespaceInModule.myVar;
5858
/** @type {!test_files$import_equals$exporter.Exported.Nested} */
5959
test_files$import_equals$import_equals_module_.namespaceInModule.otherVar;

0 commit comments

Comments
 (0)