@@ -5,11 +5,15 @@ namespace ts {
55 export interface CommentWriter {
66 reset ( ) : void ;
77 setSourceFile ( sourceFile : SourceFile ) : void ;
8- getLeadingCommentsToEmit ( node : TextRange ) : CommentRange [ ] ;
9- getTrailingCommentsToEmit ( node : TextRange ) : CommentRange [ ] ;
10- emitDetachedComments ( node : TextRange ) : void ;
11- emitLeadingComments ( node : TextRange , comments ?: CommentRange [ ] ) : void ;
12- emitTrailingComments ( node : TextRange , comments ?: CommentRange [ ] ) : void ;
8+ getLeadingComments ( range : Node , getAdditionalRange ?: ( range : Node ) => Node ) : CommentRange [ ] ;
9+ getLeadingComments ( range : TextRange ) : CommentRange [ ] ;
10+ getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
11+ getTrailingComments ( range : Node , getAdditionalRange ?: ( range : Node ) => Node ) : CommentRange [ ] ;
12+ getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
13+ getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
14+ emitLeadingComments ( range : TextRange , comments ?: CommentRange [ ] ) : void ;
15+ emitTrailingComments ( range : TextRange , comments ?: CommentRange [ ] ) : void ;
16+ emitDetachedComments ( range : TextRange ) : void ;
1317 }
1418
1519 export function createCommentWriter ( host : EmitHost , writer : EmitTextWriter , sourceMap : SourceMapWriter ) : CommentWriter {
@@ -25,8 +29,8 @@ namespace ts {
2529 // This maps start->end for a comment range. See `hasConsumedCommentRange` and
2630 // `consumeCommentRange` for usage.
2731 let consumedCommentRanges : number [ ] ;
28- let leadingCommentRangeNodeStarts : boolean [ ] ;
29- let trailingCommentRangeNodeEnds : boolean [ ] ;
32+ let leadingCommentRangePositions : boolean [ ] ;
33+ let trailingCommentRangePositions : boolean [ ] ;
3034
3135 return compilerOptions . removeComments
3236 ? createCommentRemovingWriter ( )
@@ -36,11 +40,13 @@ namespace ts {
3640 return {
3741 reset,
3842 setSourceFile,
39- getLeadingCommentsToEmit ( node : TextRange ) : CommentRange [ ] { return undefined ; } ,
40- getTrailingCommentsToEmit ( node : TextRange ) : CommentRange [ ] { return undefined ; } ,
43+ getLeadingComments ( range : TextRange , getAdditionalRange ?: ( range : TextRange ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
44+ getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
45+ getTrailingComments ( range : TextRange , getAdditionalRange ?: ( range : TextRange ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
46+ getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
47+ emitLeadingComments ( range : TextRange , comments ?: CommentRange [ ] ) : void { } ,
48+ emitTrailingComments ( range : TextRange , comments ?: CommentRange [ ] ) : void { } ,
4149 emitDetachedComments,
42- emitLeadingComments ( node : TextRange , comments ?: CommentRange [ ] ) : void { } ,
43- emitTrailingComments ( node : TextRange , comments ?: CommentRange [ ] ) : void { } ,
4450 } ;
4551
4652 function emitDetachedComments ( node : TextRange ) : void {
@@ -53,41 +59,85 @@ namespace ts {
5359 return {
5460 reset,
5561 setSourceFile,
56- getLeadingCommentsToEmit,
57- getTrailingCommentsToEmit,
58- emitDetachedComments,
62+ getLeadingComments,
63+ getLeadingCommentsOfPosition,
64+ getTrailingComments,
65+ getTrailingCommentsOfPosition,
5966 emitLeadingComments,
6067 emitTrailingComments,
68+ emitDetachedComments,
6169 } ;
6270
63- function getLeadingCommentsToEmit ( node : TextRange ) {
64- if ( nodeIsSynthesized ( node ) ) {
65- return ;
71+ function getLeadingComments ( range : TextRange | Node , getAdditionalRange ?: ( range : Node ) => Node ) {
72+ let comments = getLeadingCommentsOfPosition ( range . pos ) ;
73+ if ( getAdditionalRange ) {
74+ let additionalRange = getAdditionalRange ( < Node > range ) ;
75+ while ( additionalRange ) {
76+ comments = concatenate (
77+ getLeadingCommentsOfPosition ( additionalRange . pos ) ,
78+ comments
79+ ) ;
80+
81+ additionalRange = getAdditionalRange ( additionalRange ) ;
82+ }
6683 }
6784
68- if ( ! leadingCommentRangeNodeStarts [ node . pos ] ) {
69- leadingCommentRangeNodeStarts [ node . pos ] = true ;
70- const comments = hasDetachedComments ( node . pos )
71- ? getLeadingCommentsWithoutDetachedComments ( )
72- : getLeadingCommentRanges ( currentText , node . pos ) ;
73- return consumeCommentRanges ( comments ) ;
85+ return comments ;
86+ }
87+
88+ function getTrailingComments ( range : TextRange | Node , getAdditionalRange ?: ( range : Node ) => Node ) {
89+ let comments = getTrailingCommentsOfPosition ( range . end ) ;
90+ if ( getAdditionalRange ) {
91+ let additionalRange = getAdditionalRange ( < Node > range ) ;
92+ while ( additionalRange ) {
93+ comments = concatenate (
94+ comments ,
95+ getTrailingCommentsOfPosition ( additionalRange . end )
96+ ) ;
97+
98+ additionalRange = getAdditionalRange ( additionalRange ) ;
99+ }
74100 }
75101
76- return noComments ;
102+ return comments ;
77103 }
78104
79- function getTrailingCommentsToEmit ( node : TextRange ) {
80- if ( nodeIsSynthesized ( node ) ) {
81- return ;
105+ function getLeadingCommentsOfPosition ( pos : number ) {
106+ if ( positionIsSynthesized ( pos ) || leadingCommentRangePositions [ pos ] ) {
107+ return undefined ;
82108 }
83109
84- if ( ! trailingCommentRangeNodeEnds [ node . end ] ) {
85- trailingCommentRangeNodeEnds [ node . end ] = true ;
86- const comments = getTrailingCommentRanges ( currentText , node . end ) ;
87- return consumeCommentRanges ( comments ) ;
110+ leadingCommentRangePositions [ pos ] = true ;
111+ const comments = hasDetachedComments ( pos )
112+ ? getLeadingCommentsWithoutDetachedComments ( )
113+ : getLeadingCommentRanges ( currentText , pos ) ;
114+ return consumeCommentRanges ( comments ) ;
115+ }
116+
117+ function getTrailingCommentsOfPosition ( pos : number ) {
118+ if ( positionIsSynthesized ( pos ) || trailingCommentRangePositions [ pos ] ) {
119+ return undefined ;
88120 }
89121
90- return noComments ;
122+ trailingCommentRangePositions [ pos ] = true ;
123+ const comments = getTrailingCommentRanges ( currentText , pos ) ;
124+ return consumeCommentRanges ( comments ) ;
125+ }
126+
127+ function emitLeadingComments ( range : TextRange , comments = getLeadingComments ( range ) ) {
128+ emitNewLineBeforeLeadingComments ( currentLineMap , writer , range , comments ) ;
129+
130+ // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
131+ emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ false , /*trailingSeparator*/ true , newLine , writeComment ) ;
132+ }
133+
134+ function emitTrailingComments ( range : TextRange , comments = getTrailingComments ( range ) ) {
135+ // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
136+ emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ true , /*trailingSeparator*/ false , newLine , writeComment ) ;
137+ }
138+
139+ function emitDetachedComments ( range : TextRange ) {
140+ emitDetachedCommentsAndUpdateCommentsInfo ( range , /*removeComments*/ false ) ;
91141 }
92142
93143 function hasConsumedCommentRange ( comment : CommentRange ) {
@@ -136,22 +186,6 @@ namespace ts {
136186
137187 return noComments ;
138188 }
139-
140- function emitLeadingComments ( range : TextRange , leadingComments : CommentRange [ ] = getLeadingCommentsToEmit ( range ) ) {
141- emitNewLineBeforeLeadingComments ( currentLineMap , writer , range , leadingComments ) ;
142-
143- // Leading comments are emitted as `/*leading comment1 */space/*leading comment*/space`
144- emitComments ( currentText , currentLineMap , writer , leadingComments , /*trailingSeparator*/ true , newLine , writeComment ) ;
145- }
146-
147- function emitTrailingComments ( range : TextRange , trailingComments = getTrailingCommentsToEmit ( range ) ) {
148- // Trailing comments are emitted as `space/*trailing comment1 */space/*trailing comment*/`
149- emitComments ( currentText , currentLineMap , writer , trailingComments , /*trailingSeparator*/ false , newLine , writeComment ) ;
150- }
151-
152- function emitDetachedComments ( range : TextRange ) {
153- emitDetachedCommentsAndUpdateCommentsInfo ( range , /*removeComments*/ false ) ;
154- }
155189 }
156190
157191 function reset ( ) {
@@ -160,8 +194,8 @@ namespace ts {
160194 currentLineMap = undefined ;
161195 detachedCommentsInfo = undefined ;
162196 consumedCommentRanges = undefined ;
163- trailingCommentRangeNodeEnds = undefined ;
164- leadingCommentRangeNodeStarts = undefined ;
197+ trailingCommentRangePositions = undefined ;
198+ leadingCommentRangePositions = undefined ;
165199 }
166200
167201 function setSourceFile ( sourceFile : SourceFile ) {
@@ -170,8 +204,8 @@ namespace ts {
170204 currentLineMap = getLineStarts ( sourceFile ) ;
171205 detachedCommentsInfo = undefined ;
172206 consumedCommentRanges = [ ] ;
173- leadingCommentRangeNodeStarts = [ ] ;
174- trailingCommentRangeNodeEnds = [ ] ;
207+ leadingCommentRangePositions = [ ] ;
208+ trailingCommentRangePositions = [ ] ;
175209 }
176210
177211 function hasDetachedComments ( pos : number ) {
@@ -182,7 +216,7 @@ namespace ts {
182216 // get the leading comments from detachedPos
183217 const pos = lastOrUndefined ( detachedCommentsInfo ) . detachedCommentEndPos ;
184218 const leadingComments = getLeadingCommentRanges ( currentText , pos ) ;
185- if ( detachedCommentsInfo . length > 1 ) {
219+ if ( detachedCommentsInfo . length - 1 ) {
186220 detachedCommentsInfo . pop ( ) ;
187221 }
188222 else {
0 commit comments