@@ -7,7 +7,6 @@ namespace ts {
77 setSourceFile ( sourceFile : SourceFile ) : void ;
88 getLeadingComments ( range : TextRange ) : CommentRange [ ] ;
99 getLeadingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
10- getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
1110 getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
1211 getTrailingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
1312 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
@@ -32,9 +31,9 @@ namespace ts {
3231
3332 // This maps start->end for a comment range. See `hasConsumedCommentRange` and
3433 // `consumeCommentRange` for usage.
35- let consumedCommentRanges : number [ ] ;
36- let leadingCommentRangePositions : boolean [ ] ;
37- let trailingCommentRangePositions : boolean [ ] ;
34+ let consumedCommentRanges : Map < number > ;
35+ let leadingCommentRangePositions : Map < boolean > ;
36+ let trailingCommentRangePositions : Map < boolean > ;
3837
3938 return compilerOptions . removeComments
4039 ? createCommentRemovingWriter ( )
@@ -45,7 +44,6 @@ namespace ts {
4544 reset,
4645 setSourceFile,
4746 getLeadingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
48- getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
4947 getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
5048 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
5149 emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode ?: Node , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : void { } ,
@@ -69,7 +67,6 @@ namespace ts {
6967 reset,
7068 setSourceFile,
7169 getLeadingComments,
72- getLeadingCommentsOfPosition,
7370 getTrailingComments,
7471 getTrailingCommentsOfPosition,
7572 emitLeadingComments,
@@ -81,9 +78,14 @@ namespace ts {
8178 function getLeadingComments ( range : TextRange ) : CommentRange [ ] ;
8279 function getLeadingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
8380 function getLeadingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
81+ performance . mark ( "commentStart" ) ;
82+
83+ let comments : CommentRange [ ] = [ ] ;
84+ let ignored = false ;
8485 if ( contextNode ) {
8586 range = getTextRangeCallback ( contextNode ) || range ;
8687 if ( ignoreNodeCallback ( contextNode ) ) {
88+ ignored = true ;
8789 // If the node will not be emitted in JS, remove all the comments (normal,
8890 // pinned and `///`) associated with the node, unless it is a triple slash
8991 // comment at the top of the file.
@@ -97,15 +99,17 @@ namespace ts {
9799 // The first `///` will NOT be removed while the second one will be removed
98100 // even though both nodes will not be emitted.
99101 if ( range . pos === 0 ) {
100- return filter ( getLeadingCommentsOfPosition ( 0 ) , isTripleSlashComment ) ;
102+ comments = filter ( getLeadingCommentsOfPosition ( 0 ) , isTripleSlashComment ) ;
101103 }
102-
103- return undefined ;
104104 }
105105 }
106106
107+ if ( ! ignored ) {
108+ comments = getLeadingCommentsOfPosition ( range . pos ) ;
109+ }
107110
108- return getLeadingCommentsOfPosition ( range . pos ) ;
111+ performance . measure ( "commentTime" , "commentStart" ) ;
112+ return comments ;
109113 }
110114
111115 /**
@@ -127,15 +131,25 @@ namespace ts {
127131 function getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
128132 function getTrailingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
129133 function getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
134+ performance . mark ( "commentStart" ) ;
135+
136+ let ignored = false ;
130137 if ( contextNode ) {
131138 if ( ignoreNodeCallback ( contextNode ) ) {
132- return undefined ;
139+ ignored = true ;
140+ }
141+ else {
142+ range = getTextRangeCallback ( contextNode ) || range ;
133143 }
144+ }
134145
135- range = getTextRangeCallback ( contextNode ) || range ;
146+ let comments : CommentRange [ ] ;
147+ if ( ! ignored ) {
148+ comments = getTrailingCommentsOfPositionWorker ( range . end ) ;
136149 }
137150
138- return getTrailingCommentsOfPosition ( range . end ) ;
151+ performance . measure ( "commentTime" , "commentStart" ) ;
152+ return comments ;
139153 }
140154
141155 function getLeadingCommentsOfPosition ( pos : number ) {
@@ -151,6 +165,13 @@ namespace ts {
151165 }
152166
153167 function getTrailingCommentsOfPosition ( pos : number ) {
168+ performance . mark ( "commentStart" ) ;
169+ const comments = getTrailingCommentsOfPositionWorker ( pos ) ;
170+ performance . measure ( "commentTime" , "commentStart" ) ;
171+ return comments ;
172+ }
173+
174+ function getTrailingCommentsOfPositionWorker ( pos : number ) {
154175 if ( positionIsSynthesized ( pos ) || trailingCommentRangePositions [ pos ] ) {
155176 return undefined ;
156177 }
@@ -163,6 +184,7 @@ namespace ts {
163184 function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] ) : void ;
164185 function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode : Node , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : void ;
165186 function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode ?: Node , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
187+ performance . mark ( "commentStart" ) ;
166188 if ( comments && comments . length > 0 ) {
167189 if ( contextNode ) {
168190 range = getTextRangeCallback ( contextNode ) || range ;
@@ -173,11 +195,14 @@ namespace ts {
173195 // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
174196 emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ false , /*trailingSeparator*/ true , newLine , writeComment ) ;
175197 }
198+ performance . measure ( "commentTime" , "commentStart" ) ;
176199 }
177200
178201 function emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) {
179202 // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
203+ performance . mark ( "commentStart" ) ;
180204 emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ true , /*trailingSeparator*/ false , newLine , writeComment ) ;
205+ performance . measure ( "commentTime" , "commentStart" ) ;
181206 }
182207
183208 function emitLeadingDetachedComments ( range : TextRange ) : void ;
@@ -187,7 +212,9 @@ namespace ts {
187212 return ;
188213 }
189214
215+ performance . mark ( "commentStart" ) ;
190216 emitDetachedCommentsAndUpdateCommentsInfo ( range , /*removeComments*/ false ) ;
217+ performance . measure ( "commentTime" , "commentStart" ) ;
191218 }
192219
193220 function emitTrailingDetachedComments ( range : TextRange ) : void ;
@@ -264,9 +291,9 @@ namespace ts {
264291 currentText = sourceFile . text ;
265292 currentLineMap = getLineStarts ( sourceFile ) ;
266293 detachedCommentsInfo = undefined ;
267- consumedCommentRanges = [ ] ;
268- leadingCommentRangePositions = [ ] ;
269- trailingCommentRangePositions = [ ] ;
294+ consumedCommentRanges = { } ;
295+ leadingCommentRangePositions = { } ;
296+ trailingCommentRangePositions = { } ;
270297 }
271298
272299 function hasDetachedComments ( pos : number ) {
0 commit comments