diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 9a9a3676efa..3667b43805b 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1895,28 +1895,7 @@ module ts { writeLine(); } - function emitImportDeclaration(node: ImportDeclaration) { - // TODO(shkamat): Emit if import decl is used to declare type in this context or as export assignment - if (node.flags & NodeFlags.Export) { - //TODO: only emit when export flag once the above condition is modified - write("export "); - write("import "); - emitSourceTextOfNode(node.name); - write(" = "); - if (node.entityName) { - emitSourceTextOfNode(node.entityName); - write(";"); - } - else { - write("require("); - emitSourceTextOfNode(node.externalModuleName); - write(");"); - } - writeLine(); - } - } - - function canEmitModuleElementDeclaration(node: Declaration) { + function isModuleElementExternallyVisible(node: Declaration) { if (node.flags & NodeFlags.Export) { // Exported member - emit this declaration return true; @@ -1927,8 +1906,17 @@ module ts { return resolver.isReferencedInExportAssignment(node); } + return false; + } + + function canEmitModuleElementDeclaration(node: Declaration) { + if (isModuleElementExternallyVisible(node)) { + // Either exported module element or is referenced in export assignment + return true; + } + // emit the declaration if this is global source file - return node.parent.kind === SyntaxKind.SourceFile; + return node.parent.kind === SyntaxKind.SourceFile && !(node.parent.flags & NodeFlags.ExternalModule); } function emitDeclarationFlags(node: Declaration) { @@ -1955,6 +1943,28 @@ module ts { } } } + + function emitImportDeclaration(node: ImportDeclaration) { + // TODO(shkamat): Emit if import decl is used to declare type in this context + if (isModuleElementExternallyVisible(node)) { + if (node.flags & NodeFlags.Export) { + write("export "); + } + write("import "); + emitSourceTextOfNode(node.name); + write(" = "); + if (node.entityName) { + emitSourceTextOfNode(node.entityName); + write(";"); + } + else { + write("require("); + emitSourceTextOfNode(node.externalModuleName); + write(");"); + } + writeLine(); + } + } function emitModuleDeclaration(node: ModuleDeclaration) { if (canEmitModuleElementDeclaration(node)) { diff --git a/tests/baselines/reference/declFileExportAssignmentImportInternalModule.js b/tests/baselines/reference/declFileExportAssignmentImportInternalModule.js index 1608f4c8d89..2ead0783eeb 100644 --- a/tests/baselines/reference/declFileExportAssignmentImportInternalModule.js +++ b/tests/baselines/reference/declFileExportAssignmentImportInternalModule.js @@ -31,4 +31,5 @@ module.exports = m; //// [declFileExportAssignmentImportInternalModule.d.ts] +import m = m3; export = m; diff --git a/tests/baselines/reference/declFileExportImportChain.js b/tests/baselines/reference/declFileExportImportChain.js index 7d1961044f2..6b5ad6ef341 100644 --- a/tests/baselines/reference/declFileExportImportChain.js +++ b/tests/baselines/reference/declFileExportImportChain.js @@ -69,6 +69,7 @@ export = m1; //// [declFileExportImportChain_b.d.ts] export import a = require("declFileExportImportChain_a"); //// [declFileExportImportChain_b1.d.ts] +import b = require("declFileExportImportChain_b"); export = b; //// [declFileExportImportChain_c.d.ts] export import b1 = require("declFileExportImportChain_b1"); diff --git a/tests/baselines/reference/declFileExportImportChain2.js b/tests/baselines/reference/declFileExportImportChain2.js index 5e5a87ccfba..f950d6063d9 100644 --- a/tests/baselines/reference/declFileExportImportChain2.js +++ b/tests/baselines/reference/declFileExportImportChain2.js @@ -60,6 +60,7 @@ declare module m1 { } export = m1; //// [declFileExportImportChain2_b.d.ts] +import a = require("declFileExportImportChain2_a"); export = a; //// [declFileExportImportChain2_c.d.ts] export import b = require("declFileExportImportChain2_b");