@@ -40,9 +40,26 @@ interface IFollowAliasesResult {
4040 external : boolean ;
4141}
4242
43+ interface IWritingSpanArgs {
44+ readonly span : Span ;
45+ readonly previousSpan : Span | undefined ;
46+ readonly parentSpan : Span | undefined ;
47+ readonly kind : ts . SyntaxKind ;
48+ readonly parentKind : ts . SyntaxKind ;
49+
50+ prefix : string ;
51+ suffix : string ;
52+
53+ skipSeparatorBefore : boolean ;
54+ skipChildrenAndSuffix : boolean ;
55+ skipSeparatorAfter : boolean ;
56+ }
57+
4358export default class DtsGenerator {
4459 private _extractor : Extractor ;
4560 private _typeChecker : ts . TypeChecker ;
61+ private _indentedWriter : IndentedWriter = new IndentedWriter ( ) ;
62+
4663 private readonly _entriesBySymbol : Map < ts . Symbol , Entry > = new Map < ts . Symbol , Entry > ( ) ;
4764 private readonly _entries : Entry [ ] = [ ] ;
4865
@@ -53,15 +70,17 @@ export default class DtsGenerator {
5370 private static _matchParent < T extends ts . Node > ( node : ts . Node , parentKinds : ts . SyntaxKind [ ] ) : T {
5471 let current : ts . Node = node ;
5572
56- for ( let i : number = 0 ; ; ++ i ) {
73+ let i : number = 0 ;
74+ while ( true ) { // tslint:disable-line:no-constant-condition
5775 if ( ! current || current . kind !== parentKinds [ i ] ) {
5876 return undefined ;
5977 }
6078
61- if ( i >= parentKinds . length ) {
79+ if ( i >= parentKinds . length - 1 ) {
6280 break ;
6381 }
6482
83+ ++ i ;
6584 current = current . parent ;
6685 }
6786
@@ -122,6 +141,9 @@ export default class DtsGenerator {
122141 }
123142
124143 public generateDtsFileContent ( ) : string {
144+ this . _indentedWriter . spacing = '' ;
145+ this . _indentedWriter . clear ( ) ;
146+
125147 const packageSymbol : ts . Symbol = this . _extractor . package . getDeclarationSymbol ( ) ;
126148
127149 const exportSymbols : ts . Symbol [ ] = this . _typeChecker . getExportsOfModule ( packageSymbol ) || [ ] ;
@@ -130,29 +152,137 @@ export default class DtsGenerator {
130152 const entry : Entry | undefined = this . _fetchEntryForSymbol ( exportSymbol ) ;
131153
132154 if ( ! entry ) {
133- throw new Error ( 'Unsupported export type' ) ;
155+ // We are reexporting an external definition
156+ this . _indentedWriter . writeLine ( '// Unsupported re-export: ' + exportSymbol . name ) ;
157+ } else {
158+ entry . exported = true ;
134159 }
135-
136- entry . exported = true ;
137160 }
138161
139162 this . _makeUniqueNames ( ) ;
140163
141164 this . _entries . sort ( ( a , b ) => a . getSortKey ( ) . localeCompare ( b . getSortKey ( ) ) ) ;
142165
143- let content : string = '' ;
144166 for ( const entry of this . _entries ) {
145167 for ( const declaration of entry . followedSymbol . declarations ) {
146- content += '// ====> ' + entry . uniqueName + '\n' ;
147- content += declaration . getText ( ) + '\n' ;
168+ console . log ( PrettyPrinter . dumpTree ( declaration ) ) ;
169+ console . log ( '-------------------------------------' ) ;
170+
171+ // console.log(declaration.getText());
172+ // console.log('=====================================');
173+
174+ const span : Span = new Span ( declaration ) ;
175+
176+ this . _indentedWriter . writeLine ( ) ;
177+ this . _writeSpanTree ( span ) ;
178+ this . _indentedWriter . writeLine ( ) ;
148179 }
149180 }
150181
151182 // Normalize to CRLF
152- const fileContent : string = content . toString ( ) . replace ( / \r ? \n / g, '\r\n' ) ;
183+ const fileContent : string = this . _indentedWriter . toString ( ) . replace ( / \r ? \n / g, '\r\n' ) ;
153184 return fileContent ;
154185 }
155186
187+ private _writeSpanTree ( span : Span ) : void {
188+ this . _writeSpanTreeHelper ( [ span ] , undefined ) ;
189+ }
190+
191+ private _writeSpanTreeHelper ( children : Span [ ] , parentSpan : Span | undefined ) : void {
192+ let previousArgs : IWritingSpanArgs | undefined = undefined ;
193+ for ( const child of children ) {
194+ const args : IWritingSpanArgs = {
195+ span : child ,
196+ previousSpan : previousArgs ? previousArgs . span : undefined ,
197+ parentSpan : parentSpan ,
198+ kind : child . node . kind ,
199+ parentKind : child . node . parent ? child . node . parent . kind : ts . SyntaxKind . Unknown ,
200+
201+ prefix : child . prefix ,
202+ suffix : child . suffix ,
203+
204+ skipSeparatorBefore : previousArgs ? previousArgs . skipSeparatorAfter : false ,
205+ skipChildrenAndSuffix : false ,
206+ skipSeparatorAfter : false
207+ } ;
208+
209+ this . _onWritingSpan ( args ) ;
210+
211+ this . _writeSpanTreeHelper2 ( previousArgs , args ) ;
212+
213+ previousArgs = args ;
214+ }
215+ this . _writeSpanTreeHelper2 ( previousArgs , undefined ) ;
216+ }
217+
218+ private _writeSpanTreeHelper2 ( previousArgs : IWritingSpanArgs , args : IWritingSpanArgs | undefined ) : void {
219+ if ( previousArgs ) {
220+ this . _indentedWriter . write ( previousArgs . prefix ) ;
221+
222+ if ( ! previousArgs . skipChildrenAndSuffix ) {
223+ this . _writeSpanTreeHelper ( previousArgs . span . children , previousArgs . span ) ;
224+ this . _indentedWriter . write ( previousArgs . suffix ) ;
225+ }
226+
227+ if ( ! args || ! args . skipSeparatorBefore ) {
228+ this . _indentedWriter . write ( previousArgs . span . separator ) ;
229+ }
230+ }
231+ }
232+
233+ private _onWritingSpan ( args : IWritingSpanArgs ) : void {
234+ switch ( args . kind ) {
235+ case ts . SyntaxKind . Block :
236+ // Replace code blocks with a semicolon
237+ args . prefix = ';' ;
238+ args . skipSeparatorBefore = true ;
239+ args . skipChildrenAndSuffix = true ;
240+ break ;
241+
242+ case ts . SyntaxKind . VariableDeclaration :
243+ if ( ! args . parentSpan ) {
244+ // The VariableDeclaration node is part of a VariableDeclarationList, however
245+ // the Entry.followedSymbol points to the VariableDeclaration part because
246+ // multiple definitions might share the same VariableDeclarationList.
247+ //
248+ // Since we are emitting a separate declaration for each one, we need to look upwards
249+ // in the ts.Node tree and write a copy of the enclosing VariableDeclarationList
250+ // content (e.g. "var" from "var x=1, y=2").
251+ const list : ts . VariableDeclarationList = DtsGenerator . _matchParent ( args . span . node ,
252+ [ ts . SyntaxKind . VariableDeclaration , ts . SyntaxKind . VariableDeclarationList ] ) ;
253+ if ( ! list ) {
254+ throw new Error ( 'Unsupported variable declaration' ) ;
255+ }
256+ const listPrefix : string = list . getSourceFile ( ) . text
257+ . substring ( list . getStart ( ) , list . declarations [ 0 ] . getStart ( ) ) ;
258+ args . prefix = listPrefix + args . prefix ;
259+ args . suffix = ';' ;
260+ }
261+ break ;
262+
263+ case ts . SyntaxKind . Identifier :
264+ if ( args . parentKind ) {
265+ switch ( args . parentKind ) {
266+ case ts . SyntaxKind . ExpressionWithTypeArguments :
267+ case ts . SyntaxKind . TypeReference :
268+ {
269+ const symbol : ts . Symbol = this . _typeChecker . getSymbolAtLocation ( args . span . node ) ;
270+ if ( ! symbol ) {
271+ throw new Error ( 'Symbol not found' ) ;
272+ }
273+
274+ const entry : Entry = this . _fetchEntryForSymbol ( symbol ) ;
275+ if ( entry ) {
276+ args . prefix = '/**/' + entry . uniqueName ;
277+ }
278+ }
279+ break ;
280+ }
281+ }
282+ break ;
283+ }
284+ }
285+
156286 private _makeUniqueNames ( ) : void {
157287 const usedNames : Set < string > = new Set < string > ( ) ;
158288 for ( const entry of this . _entries ) {
@@ -192,10 +322,9 @@ export default class DtsGenerator {
192322
193323 for ( const declaration of followedSymbol . declarations ) {
194324 // console.log(PrettyPrinter.dumpTree(declaration));
195- console . log ( '-------------------------------------' ) ;
196- const span : Span = new Span ( declaration ) ;
197- console . log ( span . getText ( ) ) ;
198- console . log ( '=====================================' ) ;
325+ // console.log('-------------------------------------');
326+ // console.log(declaration.getText());
327+ // console.log('=====================================');
199328
200329 this . _collectTypes ( declaration ) ;
201330 }
@@ -214,23 +343,22 @@ export default class DtsGenerator {
214343 switch ( node . parent . kind ) {
215344 case ts . SyntaxKind . ExpressionWithTypeArguments :
216345 case ts . SyntaxKind . TypeReference :
217- this . _processTypeReference ( node ) ;
346+ {
347+ const symbol : ts . Symbol = this . _typeChecker . getSymbolAtLocation ( node ) ;
348+ if ( ! symbol ) {
349+ throw new Error ( 'Symbol not found' ) ;
350+ }
351+
352+ this . _fetchEntryForSymbol ( symbol ) ;
353+ }
218354 break ;
219355 }
220356 }
221357 return ;
222358 }
359+
223360 for ( const child of node . getChildren ( ) || [ ] ) {
224361 this . _collectTypes ( child ) ;
225362 }
226363 }
227-
228- private _processTypeReference ( identifier : ts . Node ) : void {
229- const symbol : ts . Symbol = this . _typeChecker . getSymbolAtLocation ( identifier ) ;
230- if ( ! symbol ) {
231- throw new Error ( 'Symbol not found' ) ;
232- }
233-
234- this . _fetchEntryForSymbol ( symbol ) ;
235- }
236364}
0 commit comments