@@ -30,7 +30,9 @@ namespace ts {
3030 // setup the writer
3131 let writer = createNewTextWriterWithSymbolWriter ( ) ;
3232 let write = writer . write ;
33- let writeTextOfNode = writer . writeTextOfNode ;
33+ let writeTextOfNode = ( sourceFile : SourceFile , node : Node ) => {
34+ writer . writeTextOfNode ( getSourceFileOfNode ( node ) , node ) ;
35+ } ;
3436 let writeLine = writer . writeLine ;
3537 let increaseIndent = writer . increaseIndent ;
3638 let decreaseIndent = writer . decreaseIndent ;
@@ -365,7 +367,7 @@ namespace ts {
365367
366368 function emitModuleElementDeclarationFlags ( node : Node ) {
367369 // If the node is parented in the current source file we need to emit export declare or just export
368- if ( node . parent === currentSourceFile ) {
370+ if ( node . parent === getSourceFileOfNode ( node ) ) {
369371 // If the node is exported
370372 if ( node . flags & NodeFlags . Export ) {
371373 write ( "export " ) ;
@@ -1309,8 +1311,8 @@ namespace ts {
13091311 }
13101312
13111313 function collectDeclatation ( declaration :Node , errorNode ?: Node ) {
1312- declarationsToProcess . push ( { declaration, errorNode } ) ;
1313- // preprocessDeclaration(declaration, errorNode);
1314+ // declarationsToProcess.push({ declaration, errorNode });
1315+ preprocessDeclaration ( declaration , errorNode ) ;
13141316 }
13151317
13161318 function isDeclarationExported ( node : Node ) : boolean {
@@ -1619,10 +1621,22 @@ namespace ts {
16191621 return ;
16201622 }
16211623
1622- // If this declaration is from a diffrent file, we will get to it later.
1623- // Skip it for now.
1624- if ( getSourceFileOfNode ( declaration ) !== currentSourceFile ) {
1625- return ;
1624+ if ( compilerOptions . mainModule ) {
1625+ if ( declaration . kind === SyntaxKind . ImportSpecifier ) {
1626+ let symbol = resolver . getSymbolAtLocation ( ( < ImportOrExportSpecifier > declaration ) . name ) ;
1627+ let target = symbol && resolver . getAliasedSymbol ( symbol ) ;
1628+ if ( target ) {
1629+ collectDeclarations ( target , errorNode ) ;
1630+ }
1631+ return ;
1632+ }
1633+ }
1634+ else {
1635+ // If this declaration is from a diffrent file, we will get to it later.
1636+ // Skip it for now.
1637+ if ( getSourceFileOfNode ( declaration ) !== currentSourceFile ) {
1638+ return ;
1639+ }
16261640 }
16271641
16281642 let links = getNodeLinks ( declaration ) ;
@@ -1670,6 +1684,11 @@ namespace ts {
16701684 } ) ;
16711685 }
16721686
1687+ function isExternalModuleDeclaration ( node : Node ) {
1688+ return ( node . kind === SyntaxKind . ModuleDeclaration && ( < ModuleDeclaration > node ) . name . kind === SyntaxKind . StringLiteral ) ||
1689+ ( node . kind === SyntaxKind . SourceFile && isExternalModule ( < SourceFile > node ) ) ;
1690+ }
1691+
16731692 function ensureDeclarationVisible ( node : Node , errorNode : Node ) : void {
16741693 let links = getNodeLinks ( node ) ;
16751694 if ( ! links . visited ) {
@@ -1679,7 +1698,13 @@ namespace ts {
16791698 // parent's visible declarations.
16801699 // also make sure the parent is reachable from a top-level
16811700 // declaration
1701+
16821702 let parent = getEnclosingDeclaration ( node ) ;
1703+ if ( compilerOptions . mainModule &&
1704+ parent !== currentSourceFile &&
1705+ isExternalModuleDeclaration ( parent ) ) {
1706+ parent = currentSourceFile ;
1707+ }
16831708 attachVisibleChild ( parent , node ) ;
16841709 if ( parent . kind !== SyntaxKind . SourceFile ) {
16851710 ensureDeclarationVisible ( parent , errorNode ) ;
@@ -1711,6 +1736,15 @@ namespace ts {
17111736 function forEachExpectedOutputFile ( host : EmitHost , targetSourceFile : SourceFile , action : ( name : string , sources : SourceFile [ ] ) => void ) {
17121737 let compilerOptions = host . getCompilerOptions ( ) ;
17131738
1739+ if ( compilerOptions . mainModule ) {
1740+ let mainModuleSourceFile = host . getSourceFile ( compilerOptions . mainModule ) ;
1741+ if ( mainModuleSourceFile ) {
1742+ let outputFilePath = getOwnEmitOutputFilePath ( mainModuleSourceFile , host , ".d.ts" ) ;
1743+ action ( outputFilePath , [ mainModuleSourceFile ] ) ;
1744+ return ;
1745+ }
1746+ }
1747+
17141748 if ( targetSourceFile ) {
17151749 // If we have a targetSourceFile (e.g calling emitter from language service to getEmitOutput)
17161750 // only emit the outputs of this file
0 commit comments