@@ -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,20 +31,23 @@ 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
39- return compilerOptions . removeComments
38+ const commentWriter = compilerOptions . removeComments
4039 ? createCommentRemovingWriter ( )
4140 : createCommentPreservingWriter ( ) ;
4241
42+ return compilerOptions . extendedDiagnostics
43+ ? createCommentWriterWithExtendedDiagnostics ( commentWriter )
44+ : commentWriter ;
45+
4346 function createCommentRemovingWriter ( ) : CommentWriter {
4447 return {
4548 reset,
4649 setSourceFile,
4750 getLeadingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
48- getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
4951 getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
5052 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
5153 emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode ?: Node , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : void { } ,
@@ -69,7 +71,6 @@ namespace ts {
6971 reset,
7072 setSourceFile,
7173 getLeadingComments,
72- getLeadingCommentsOfPosition,
7374 getTrailingComments,
7475 getTrailingCommentsOfPosition,
7576 emitLeadingComments,
@@ -100,11 +101,10 @@ namespace ts {
100101 return filter ( getLeadingCommentsOfPosition ( 0 ) , isTripleSlashComment ) ;
101102 }
102103
103- return undefined ;
104+ return ;
104105 }
105106 }
106107
107-
108108 return getLeadingCommentsOfPosition ( range . pos ) ;
109109 }
110110
@@ -127,15 +127,21 @@ namespace ts {
127127 function getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
128128 function getTrailingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
129129 function getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
130+ let ignored = false ;
130131 if ( contextNode ) {
131132 if ( ignoreNodeCallback ( contextNode ) ) {
132- return undefined ;
133+ ignored = true ;
134+ }
135+ else {
136+ range = getTextRangeCallback ( contextNode ) || range ;
133137 }
134-
135- range = getTextRangeCallback ( contextNode ) || range ;
136138 }
137139
138- return getTrailingCommentsOfPosition ( range . end ) ;
140+ let comments : CommentRange [ ] ;
141+ if ( ! ignored ) {
142+ comments = getTrailingCommentsOfPosition ( range . end ) ;
143+ }
144+ return comments ;
139145 }
140146
141147 function getLeadingCommentsOfPosition ( pos : number ) {
@@ -249,6 +255,63 @@ namespace ts {
249255 }
250256 }
251257
258+ function createCommentWriterWithExtendedDiagnostics ( writer : CommentWriter ) : CommentWriter {
259+ const {
260+ reset,
261+ setSourceFile,
262+ getLeadingComments,
263+ getTrailingComments,
264+ getTrailingCommentsOfPosition,
265+ emitLeadingComments,
266+ emitTrailingComments,
267+ emitLeadingDetachedComments,
268+ emitTrailingDetachedComments
269+ } = writer ;
270+
271+ return {
272+ reset,
273+ setSourceFile,
274+ getLeadingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] {
275+ const commentStart = performance . mark ( ) ;
276+ const comments = getLeadingComments ( range , contextNode , ignoreNodeCallback , getTextRangeCallback ) ;
277+ performance . measure ( "commentTime" , commentStart ) ;
278+ return comments ;
279+ } ,
280+ getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] {
281+ const commentStart = performance . mark ( ) ;
282+ const comments = getTrailingComments ( range , contextNode , ignoreNodeCallback , getTextRangeCallback ) ;
283+ performance . measure ( "commentTime" , commentStart ) ;
284+ return comments ;
285+ } ,
286+ getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] {
287+ const commentStart = performance . mark ( ) ;
288+ const comments = getTrailingCommentsOfPosition ( pos ) ;
289+ performance . measure ( "commentTime" , commentStart ) ;
290+ return comments ;
291+ } ,
292+ emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode ?: Node , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : void {
293+ const commentStart = performance . mark ( ) ;
294+ emitLeadingComments ( range , comments , contextNode , getTextRangeCallback ) ;
295+ performance . measure ( "commentTime" , commentStart ) ;
296+ } ,
297+ emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) : void {
298+ const commentStart = performance . mark ( ) ;
299+ emitLeadingComments ( range , comments ) ;
300+ performance . measure ( "commentTime" , commentStart ) ;
301+ } ,
302+ emitLeadingDetachedComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean ) : void {
303+ const commentStart = performance . mark ( ) ;
304+ emitLeadingDetachedComments ( range , contextNode , ignoreNodeCallback ) ;
305+ performance . measure ( "commentTime" , commentStart ) ;
306+ } ,
307+ emitTrailingDetachedComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean ) : void {
308+ const commentStart = performance . mark ( ) ;
309+ emitTrailingDetachedComments ( range , contextNode , ignoreNodeCallback ) ;
310+ performance . measure ( "commentTime" , commentStart ) ;
311+ }
312+ } ;
313+ }
314+
252315 function reset ( ) {
253316 currentSourceFile = undefined ;
254317 currentText = undefined ;
@@ -264,9 +327,9 @@ namespace ts {
264327 currentText = sourceFile . text ;
265328 currentLineMap = getLineStarts ( sourceFile ) ;
266329 detachedCommentsInfo = undefined ;
267- consumedCommentRanges = [ ] ;
268- leadingCommentRangePositions = [ ] ;
269- trailingCommentRangePositions = [ ] ;
330+ consumedCommentRanges = { } ;
331+ leadingCommentRangePositions = { } ;
332+ trailingCommentRangePositions = { } ;
270333 }
271334
272335 function hasDetachedComments ( pos : number ) {
0 commit comments