Skip to content

Commit

Permalink
Use internal module namespace in externs type annotations.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 568539593
  • Loading branch information
tsjs-language-eng authored and copybara-github committed Oct 2, 2023
1 parent 1b5e61d commit c9c574c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/externs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ export function generateExterns(
const isDts = isDtsFileName(sourceFile.fileName);
const isExternalModule = ts.isExternalModule(sourceFile);

const mtt =
new ModuleTypeTranslator(sourceFile, typeChecker, host, diagnostics, /*isForExterns*/ true);

// .d.ts files declare symbols. The code below translates these into a form understood by Closure
// Compiler, converting the type syntax, but also converting symbol names into a form accessible
// to Closure Compiler.
Expand Down Expand Up @@ -206,6 +203,10 @@ export function generateExterns(
rootNamespace = rootNamespace + '_';
}

const mtt = new ModuleTypeTranslator(
sourceFile, typeChecker, host, diagnostics, /*isForExterns*/ true,
/*useInternalNamespaceForExterns=*/ hasExportEquals);

for (const stmt of sourceFile.statements) {
// Always collect alises for imported symbols.
importsVisitor(stmt);
Expand Down
3 changes: 3 additions & 0 deletions src/module_type_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class ModuleTypeTranslator {
private readonly host: AnnotatorHost&googmodule.GoogModuleProcessorHost,
private readonly diagnostics: ts.Diagnostic[],
private readonly isForExterns: boolean,
private readonly useInternalNamespaceForExterns = false,
) {
// TODO: remove once AnnotatorHost.typeBlackListPaths is removed.
this.host.unknownTypesPaths =
Expand Down Expand Up @@ -165,6 +166,8 @@ export class ModuleTypeTranslator {
this.symbolToNameCache,
(sym: ts.Symbol) => void this.ensureSymbolDeclared(sym));
translator.isForExterns = this.isForExterns;
translator.useInternalNamespaceForExterns =
this.useInternalNamespaceForExterns;
translator.warn = msg => void this.debugWarn(context, msg);
return translator;
}
Expand Down
12 changes: 12 additions & 0 deletions src/type_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ export class TypeTranslator {
*/
isForExterns = false;

/**
* Whether use internal namespace (with underscore) for the externs types.
* It is set when the declaration file has an export equals ("export = Foo;")
* statement.
*/
useInternalNamespaceForExterns = false;

/**
* When translating the type of an 'extends' clause, e.g. Y in
* class X extends Y<T> { ... }
Expand Down Expand Up @@ -419,6 +426,11 @@ export class TypeTranslator {
context = '';
}
const mangled = moduleNameAsIdentifier(this.host, fileName, context);
if (this.isForExterns && this.useInternalNamespaceForExterns &&
!ambientModuleDeclaration &&
isDeclaredInSameFile(this.node, declarations[0])) {
return mangled + '_.';
}
return mangled + '.';
}

Expand Down
2 changes: 1 addition & 1 deletion test_files/augment/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Generated from: test_files/augment/angular/index.d.ts
/** @const */
var test_files$augment$angular$index_ = {};
/** @type {!test_files$augment$angular$index.angular.IAngularStatic} */
/** @type {!test_files$augment$angular$index_.angular.IAngularStatic} */
test_files$augment$angular$index_.angular;
/** @const */
test_files$augment$angular$index_.angular = {};
Expand Down
4 changes: 2 additions & 2 deletions test_files/declare_export_dts/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ test_files$declare_export_dts$relative_ambient_external_module.user;
// Generated from: test_files/declare_export_dts/declare_export_var.d.ts
/** @const */
var test_files$declare_export_dts$declare_export_var_ = {};
/** @type {!test_files$declare_export_dts$declare_export_var.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
/** @type {!test_files$declare_export_dts$declare_export_var_.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
test_files$declare_export_dts$declare_export_var_.variableDeclaredInDtsModule;
/** @type {!test_files$declare_export_dts$declare_export_var.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
/** @type {!test_files$declare_export_dts$declare_export_var_.namespaceInDtsModule.InterfaceNestedInModuleScopedNamespace} */
var variableDeclaredInDtsModule;
/** @const */
test_files$declare_export_dts$declare_export_var_.namespaceInDtsModule = {};
Expand Down
2 changes: 1 addition & 1 deletion test_files/import_equals/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test_files$import_equals$import_equals_module_.namespaceInModule = {};
* @struct
*/
test_files$import_equals$import_equals_module_.namespaceInModule.InNamespace = function() {};
/** @type {!test_files$import_equals$import_equals_module.namespaceInModule.InNamespace} */
/** @type {!test_files$import_equals$import_equals_module_.namespaceInModule.InNamespace} */
test_files$import_equals$import_equals_module_.namespaceInModule.myVar;
/** @type {!test_files$import_equals$exporter.Exported.Nested} */
test_files$import_equals$import_equals_module_.namespaceInModule.otherVar;
Expand Down

0 comments on commit c9c574c

Please sign in to comment.