diff --git a/src/externs.ts b/src/externs.ts index 77335032d..a30f6caf0 100644 --- a/src/externs.ts +++ b/src/externs.ts @@ -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. @@ -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); diff --git a/src/module_type_translator.ts b/src/module_type_translator.ts index eae5b2783..151347dac 100644 --- a/src/module_type_translator.ts +++ b/src/module_type_translator.ts @@ -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 = @@ -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; } diff --git a/src/type_translator.ts b/src/type_translator.ts index 9df9c0539..324ec77e8 100644 --- a/src/type_translator.ts +++ b/src/type_translator.ts @@ -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 { ... } @@ -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 + '.'; } diff --git a/test_files/augment/externs.js b/test_files/augment/externs.js index 3b0e39aa9..18fb334ad 100644 --- a/test_files/augment/externs.js +++ b/test_files/augment/externs.js @@ -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 = {}; diff --git a/test_files/declare_export_dts/externs.js b/test_files/declare_export_dts/externs.js index 68b311d48..a282a37b6 100644 --- a/test_files/declare_export_dts/externs.js +++ b/test_files/declare_export_dts/externs.js @@ -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 = {}; diff --git a/test_files/import_equals/externs.js b/test_files/import_equals/externs.js index 049f2dd96..d8ac8eeee 100644 --- a/test_files/import_equals/externs.js +++ b/test_files/import_equals/externs.js @@ -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;