@@ -5,11 +5,15 @@ namespace ts {
5
5
export interface CommentWriter {
6
6
reset ( ) : void ;
7
7
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 ;
13
17
}
14
18
15
19
export function createCommentWriter ( host : EmitHost , writer : EmitTextWriter , sourceMap : SourceMapWriter ) : CommentWriter {
@@ -25,8 +29,8 @@ namespace ts {
25
29
// This maps start->end for a comment range. See `hasConsumedCommentRange` and
26
30
// `consumeCommentRange` for usage.
27
31
let consumedCommentRanges : number [ ] ;
28
- let leadingCommentRangeNodeStarts : boolean [ ] ;
29
- let trailingCommentRangeNodeEnds : boolean [ ] ;
32
+ let leadingCommentRangePositions : boolean [ ] ;
33
+ let trailingCommentRangePositions : boolean [ ] ;
30
34
31
35
return compilerOptions . removeComments
32
36
? createCommentRemovingWriter ( )
@@ -36,11 +40,13 @@ namespace ts {
36
40
return {
37
41
reset,
38
42
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 { } ,
41
49
emitDetachedComments,
42
- emitLeadingComments ( node : TextRange , comments ?: CommentRange [ ] ) : void { } ,
43
- emitTrailingComments ( node : TextRange , comments ?: CommentRange [ ] ) : void { } ,
44
50
} ;
45
51
46
52
function emitDetachedComments ( node : TextRange ) : void {
@@ -53,41 +59,85 @@ namespace ts {
53
59
return {
54
60
reset,
55
61
setSourceFile,
56
- getLeadingCommentsToEmit,
57
- getTrailingCommentsToEmit,
58
- emitDetachedComments,
62
+ getLeadingComments,
63
+ getLeadingCommentsOfPosition,
64
+ getTrailingComments,
65
+ getTrailingCommentsOfPosition,
59
66
emitLeadingComments,
60
67
emitTrailingComments,
68
+ emitDetachedComments,
61
69
} ;
62
70
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
+ }
66
83
}
67
84
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
+ }
74
100
}
75
101
76
- return noComments ;
102
+ return comments ;
77
103
}
78
104
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 ;
82
108
}
83
109
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 ;
88
120
}
89
121
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 ) ; <
10000
/div>
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 ) ;
91
141
}
92
142
93
143
function hasConsumedCommentRange ( comment : CommentRange ) {
@@ -136,22 +186,6 @@ namespace ts {
136
186
137
187
return noComments ;
138
188
}
139
-
140
- function emitLeadingComments ( range : TextRange , leadingComments : CommentRange [ ] = getLeadingCommentsToEmit ( range ) ) {
141
- emitNewLineBeforeLeadingComments ( currentLineMap , writer , range , leadingComments ) ;
142
-
143
- // Leading comments are emitted at /*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 at 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
- }
155
189
}
156
190
157
191
function reset ( ) {
@@ -160,8 +194,8 @@ namespace ts {
160
194
currentLineMap = undefined ;
161
195
detachedCommentsInfo = undefined ;
162
196
consumedCommentRanges = undefined ;
163
- trailingCommentRangeNodeEnds = undefined ;
164
- leadingCommentRangeNodeStarts = undefined ;
197
+ trailingCommentRangePositions = undefined ;
198
+ leadingCommentRangePositions = undefined ;
165
199
}
166
200
167
201
function setSourceFile ( sourceFile : SourceFile ) {
@@ -170,8 +204,8 @@ namespace ts {
170
204
currentLineMap = getLineStarts ( sourceFile ) ;
171
205
detachedCommentsInfo = undefined ;
172
206
consumedCommentRanges = [ ] ;
173
- leadingCommentRangeNodeStarts = [ ] ;
174
- trailingCommentRangeNodeEnds = [ ] ;
207
+ leadingCommentRangePositions = [ ] ;
208
+ trailingCommentRangePositions = [ ] ;
175
209
}
176
210
177
211
function hasDetachedComments ( pos : number ) {
0 commit comments