@@ -9,6 +9,7 @@ module ts {
9
9
getTextPos ( ) : number ;
10
10
getLine ( ) : number ;
11
11
getColumn ( ) : number ;
12
+ getIndent ( ) : number ;
12
13
}
13
14
14
15
var indentStrings : string [ ] = [ ] ;
@@ -141,6 +142,7 @@ module ts {
141
142
writeLine : writeLine ,
142
143
increaseIndent : ( ) => indent ++ ,
143
144
decreaseIndent : ( ) => indent -- ,
145
+ getIndent : ( ) => indent ,
144
146
getTextPos : ( ) => output . length ,
145
147
getLine : ( ) => lineCount + 1 ,
146
148
getColumn : ( ) => lineStart ? indent * 4 + 1 : output . length - linePos + 1 ,
@@ -1867,6 +1869,13 @@ module ts {
1867
1869
var enclosingDeclaration : Node ;
1868
1870
var reportedDeclarationError = false ;
1869
1871
1872
+ var aliasDeclarationEmitInfo : {
1873
+ declaration : ImportDeclaration ;
1874
+ outputPos : number ;
1875
+ indent : number ;
1876
+ asynchronousOutput ?: string ; // If the output for alias was written asynchronously, the corresponding output
1877
+ } [ ] = [ ] ;
1878
+
1870
1879
var getSymbolVisibilityDiagnosticMessage : ( symbolAccesibilityResult : SymbolAccessiblityResult ) => {
1871
1880
errorNode : Node ;
1872
1881
diagnosticMessage : DiagnosticMessage ;
@@ -1875,9 +1884,25 @@ module ts {
1875
1884
1876
1885
function writeSymbol ( symbol : Symbol , enclosingDeclaration ?: Node , meaning ?: SymbolFlags ) {
1877
1886
var symbolAccesibilityResult = resolver . isSymbolAccessible ( symbol , enclosingDeclaration , meaning ) ;
1878
- // TODO(shkamat): Since we dont have error reporting for all the cases as yet we have this check on handler being present
1887
+ // TODO(shkamat): Since we dont have error reporting for all the cases as yet we have this check on handler being present
1879
1888
if ( ! getSymbolVisibilityDiagnosticMessage || symbolAccesibilityResult . accessibility === SymbolAccessibility . Accessible ) {
1880
1889
resolver . writeSymbol ( symbol , enclosingDeclaration , meaning , writer ) ;
1890
+
1891
+ // write the aliases
1892
+ if ( symbolAccesibilityResult && symbolAccesibilityResult . aliasesToMakeVisible ) {
1893
+ var oldWriter = writer ;
1894
+ forEach ( symbolAccesibilityResult . aliasesToMakeVisible . sort ( ) , aliasToWrite => {
1895
+ var aliasEmitInfo = forEach ( aliasDeclarationEmitInfo , declEmitInfo => declEmitInfo . declaration === aliasToWrite ? declEmitInfo : undefined ) ;
1896
+ writer = createTextWriter ( writeSymbol ) ;
1897
+ for ( var declarationIndent = aliasEmitInfo . indent ; declarationIndent ; declarationIndent -- ) {
1898
+ writer . increaseIndent ( ) ;
1899
+ }
1900
+
1901
+ writeImportDeclaration ( aliasToWrite ) ;
1902
+ aliasEmitInfo . asynchronousOutput = writer . getText ( ) ;
1903
+ } ) ;
1904
+ writer = oldWriter ;
1905
+ }
1881
1906
}
1882
1907
else {
1883
1908
// Report error
@@ -1951,24 +1976,37 @@ module ts {
1951
1976
}
1952
1977
1953
1978
function emitImportDeclaration ( node : ImportDeclaration ) {
1954
- if ( resolver . isDeclarationVisible ( node ) ) {
1955
- if ( node . flags & NodeFlags . Export ) {
1956
- write ( "export " ) ;
1957
- }
1958
- write ( "import " ) ;
1959
- emitSourceTextOfNode ( node . name ) ;
1960
- write ( " = " ) ;
1961
- if ( node . entityName ) {
1962
- emitSourceTextOfNode ( node . entityName ) ;
1963
- write ( ";" ) ;
1964
- }
1965
- else {
1966
- write ( "require(" ) ;
1967
- emitSourceTextOfNode ( node . externalModuleName ) ;
1968
- write ( ");" ) ;
1969
- }
1970
- writeLine ( ) ;
1979
+ var nodeEmitInfo = {
1980
+ declaration : node ,
1981
+ outputPos : writer . getTextPos ( ) ,
1982
+ indent : writer . getIndent ( ) ,
1983
+ hasWritten : resolver . isDeclarationVisible ( node )
1984
+ } ;
1985
+ aliasDeclarationEmitInfo . push ( nodeEmitInfo ) ;
1986
+ if ( nodeEmitInfo . hasWritten ) {
1987
+ writeImportDeclaration ( node ) ;
1988
+ }
1989
+ }
1990
+
1991
+ function writeImportDeclaration ( node : ImportDeclaration ) {
1992
+ // note usage of writer. methods instead of aliases created, just to make sure we are using
1993
+ // correct writer especially to handle asynchronous alias writing
1994
+ if ( node . flags & NodeFlags . Export ) {
1995
+ writer . write ( "export " ) ;
1996
+ }
1997
+ writer . write ( "import " ) ;
1998
+ writer . write ( getSourceTextOfLocalNode ( node . name ) ) ;
1999
+ writer . write ( " = " ) ;
2000
+ if ( node . entityName ) {
2001
+ writer . write ( getSourceTextOfLocalNode ( node . entityName ) ) ;
2002
+ writer . write ( ";" ) ;
2003
+ }
2004
+ else {
2005
+ writer . write ( "require(" ) ;
2006
+ writer . write ( getSourceTextOfLocalNode ( node . externalModuleName ) ) ;
2007
+ writer . write ( ");" ) ;
1971
2008
}
2009
+ writer . writeLine ( ) ;
1972
2010
}
1973
2011
1974
2012
function emitModuleDeclaration ( node : ModuleDeclaration ) {
@@ -2484,7 +2522,19 @@ module ts {
2484
2522
// TODO(shkamat): Should we not write any declaration file if any of them can produce error,
2485
2523
// or should we just not write this file like we are doing now
2486
2524
if ( ! reportedDeclarationError ) {
2487
- writeFile ( getModuleNameFromFilename ( jsFilePath ) + ".d.ts" , referencePathsOutput + writer . getText ( ) ) ;
2525
+ var declarationOutput = referencePathsOutput ;
2526
+ var synchronousDeclarationOutput = writer . getText ( ) ;
2527
+ // apply additions
2528
+ var appliedSyncOutputPos = 0 ;
2529
+ forEach ( aliasDeclarationEmitInfo , aliasEmitInfo => {
2530
+ if ( aliasEmitInfo . asynchronousOutput ) {
2531
+ declarationOutput += synchronousDeclarationOutput . substring ( appliedSyncOutputPos , aliasEmitInfo . outputPos ) ;
2532
+ declarationOutput += aliasEmitInfo . asynchronousOutput ;
2533
+ appliedSyncOutputPos = aliasEmitInfo . outputPos ;
2534
+ }
2535
+ } ) ;
2536
+ declarationOutput += synchronousDeclarationOutput . substring ( appliedSyncOutputPos ) ;
2537
+ writeFile ( getModuleNameFromFilename ( jsFilePath ) + ".d.ts" , declarationOutput ) ;
2488
2538
}
2489
2539
}
2490
2540
0 commit comments