@@ -5,14 +5,14 @@ namespace ts {
55 export interface CommentWriter {
66 reset ( ) : void ;
77 setSourceFile ( sourceFile : SourceFile ) : void ;
8- getLeadingComments ( range : Node , getAdditionalRange ?: ( range : Node ) => Node ) : CommentRange [ ] ;
8+ getLeadingComments ( range : Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : CommentRange [ ] ;
99 getLeadingComments ( range : TextRange ) : CommentRange [ ] ;
1010 getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
11- getTrailingComments ( range : Node , getAdditionalRange ?: ( range : Node ) => Node ) : CommentRange [ ] ;
11+ getTrailingComments ( range : Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : CommentRange [ ] ;
1212 getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
1313 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
14- emitLeadingComments ( range : TextRange , comments ? : CommentRange [ ] ) : void ;
15- emitTrailingComments ( range : TextRange , comments ? : CommentRange [ ] ) : void ;
14+ emitLeadingComments ( range : TextRange , comments : CommentRange [ ] ) : void ;
15+ emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) : void ;
1616 emitDetachedComments ( range : TextRange ) : void ;
1717 }
1818
@@ -40,12 +40,12 @@ namespace ts {
4040 return {
4141 reset,
4242 setSourceFile,
43- getLeadingComments ( range : TextRange , getAdditionalRange ?: ( range : TextRange ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
43+ getLeadingComments ( range : TextRange , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : CommentRange [ ] { return undefined ; } ,
4444 getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
45- getTrailingComments ( range : TextRange , getAdditionalRange ?: ( range : TextRange ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
45+ getTrailingComments ( range : TextRange , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : CommentRange [ ] { return undefined ; } ,
4646 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
47- emitLeadingComments ( range : TextRange , comments ? : CommentRange [ ] ) : void { } ,
48- emitTrailingComments ( range : TextRange , comments ? : CommentRange [ ] ) : void { } ,
47+ emitLeadingComments ( range : TextRange , comments : CommentRange [ ] ) : void { } ,
48+ emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) : void { } ,
4949 emitDetachedComments,
5050 } ;
5151
@@ -68,38 +68,48 @@ namespace ts {
6868 emitDetachedComments,
6969 } ;
7070
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 ) ;
71+ function getLeadingComments ( range : TextRange | Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) {
72+ if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( < Node > range ) ) {
73+ // If the node will not be emitted in JS, remove all the comments(normal, pinned and ///) associated with the node,
74+ // unless it is a triple slash comment at the top of the file.
75+ // For Example:
76+ // /// <reference-path ...>
77+ // declare var x;
78+ // /// <reference-path ...>
79+ // interface F {}
80+ // The first /// will NOT be removed while the second one will be removed even though both nodes will not be emitted
81+ if ( range . pos === 0 ) {
82+ return filter ( getLeadingCommentsOfPosition ( 0 ) , isTripleSlashComment ) ;
8283 }
84+
85+ return undefined ;
8386 }
8487
85- return comments ;
88+ return getLeadingCommentsOfPosition ( range . pos ) ;
8689 }
8790
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- }
91+ /**
92+ * Determine if the given comment is a triple-slash
93+ **/
94+ function isTripleSlashComment ( comment : CommentRange ) {
95+ // Verify this is /// comment, but do the regexp match only when we first can find /// in the comment text
96+ // so that we don't end up computing comment string and doing match for all // comments
97+ if ( currentText . charCodeAt ( comment . pos + 1 ) === CharacterCodes . slash &&
98+ comment . pos + 2 < comment . end &&
99+ currentText . charCodeAt ( comment . pos + 2 ) === CharacterCodes . slash ) {
100+ const textSubStr = currentText . substring ( comment . pos , comment . end ) ;
101+ return fullTripleSlashReferencePathRegEx . test ( textSubStr )
102+ || fullTripleSlashAMDReferencePathRegEx . test ( textSubStr ) ;
103+ }
104+ return false ;
105+ }
106+
107+ function getTrailingComments ( range : TextRange | Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) {
108+ if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( < Node > range ) ) {
109+ return undefined ;
100110 }
101111
102- return comments ;
112+ return getTrailingCommentsOfPosition ( range . end ) ;
103113 }
104114
105115 function getLeadingCommentsOfPosition ( pos : number ) {
@@ -124,14 +134,14 @@ namespace ts {
124134 return consumeCommentRanges ( comments ) ;
125135 }
126136
127- function emitLeadingComments ( range : TextRange , comments = getLeadingComments ( range ) ) {
137+ function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] ) {
128138 emitNewLineBeforeLeadingComments ( currentLineMap , writer , range , comments ) ;
129139
130140 // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
131141 emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ false , /*trailingSeparator*/ true , newLine , writeComment ) ;
132142 }
133143
134- function emitTrailingComments ( range : TextRange , comments = getTrailingComments ( range ) ) {
144+ function emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) {
135145 // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
136146 emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ true , /*trailingSeparator*/ false , newLine , writeComment ) ;
137147 }
0 commit comments