@@ -5,19 +5,19 @@ namespace ts {
55 export interface CommentWriter {
66 reset ( ) : void ;
77 setSourceFile ( sourceFile : SourceFile ) : void ;
8- getLeadingComments ( range : Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : CommentRange [ ] ;
98 getLeadingComments ( range : TextRange ) : CommentRange [ ] ;
9+ getLeadingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
1010 getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
11- getTrailingComments ( range : Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : CommentRange [ ] ;
1211 getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
12+ getTrailingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
1313 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] ;
14- emitLeadingComments ( range : Node , comments : CommentRange [ ] , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : void ;
1514 emitLeadingComments ( range : TextRange , comments : CommentRange [ ] ) : void ;
15+ emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode : Node , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : void ;
1616 emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) : void ;
1717 emitLeadingDetachedComments ( range : TextRange ) : void ;
18- emitLeadingDetachedComments ( range : TextRange , contextNode : Node , shouldSkipCommentsForNodeCallback : ( node : Node ) => boolean ) : void ;
18+ emitLeadingDetachedComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean ) : void ;
1919 emitTrailingDetachedComments ( range : TextRange ) : void ;
20- emitTrailingDetachedComments ( range : TextRange , contextNode : Node , shouldSkipCommentsForNodeCallback : ( node : Node ) => boolean ) : void ;
20+ emitTrailingDetachedComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean ) : void ;
2121 }
2222
2323 export function createCommentWriter ( host : EmitHost , writer : EmitTextWriter , sourceMap : SourceMapWriter ) : CommentWriter {
@@ -44,18 +44,18 @@ namespace ts {
4444 return {
4545 reset,
4646 setSourceFile,
47- getLeadingComments ( range : TextRange , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
47+ getLeadingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
4848 getLeadingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
49- getTrailingComments ( range : TextRange , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
49+ getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : CommentRange [ ] { return undefined ; } ,
5050 getTrailingCommentsOfPosition ( pos : number ) : CommentRange [ ] { return undefined ; } ,
51- emitLeadingComments ( range : Node | TextRange , comments : CommentRange [ ] , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : void { } ,
51+ emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode ?: Node , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) : void { } ,
5252 emitTrailingComments ( range : TextRange , comments : CommentRange [ ] ) : void { } ,
5353 emitLeadingDetachedComments,
54- emitTrailingDetachedComments ( node : TextRange , contextNode ?: Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : void { }
54+ emitTrailingDetachedComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean ) : void { }
5555 } ;
5656
57- function emitLeadingDetachedComments ( node : TextRange , contextNode ?: Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : void {
58- if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( contextNode ) ) {
57+ function emitLeadingDetachedComments ( node : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean ) : void {
58+ if ( ignoreNodeCallback && ignoreNodeCallback ( contextNode ) ) {
5959 return ;
6060 }
6161
@@ -79,33 +79,32 @@ namespace ts {
7979 } ;
8080
8181 function getLeadingComments ( range : TextRange ) : CommentRange [ ] ;
82- function getLeadingComments ( node : Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : CommentRange [ ] ;
83- function getLeadingComments ( nodeOrRange : TextRange | Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) {
84- let range = nodeOrRange ;
85- if ( getCustomTextRangeForNodeCallback ) {
86- range = getCustomTextRangeForNodeCallback ( < Node > nodeOrRange ) || range ;
87- }
82+ function getLeadingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
83+ function getLeadingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
84+ if ( contextNode ) {
85+ range = getTextRangeCallback ( contextNode ) || range ;
86+ if ( ignoreNodeCallback ( contextNode ) ) {
87+ // If the node will not be emitted in JS, remove all the comments (normal,
88+ // pinned and `///`) associated with the node, unless it is a triple slash
89+ // comment at the top of the file.
90+ //
91+ // For Example:
92+ // /// <reference-path ...>
93+ // declare var x;
94+ // /// <reference-path ...>
95+ // interface F {}
96+ //
97+ // The first `///` will NOT be removed while the second one will be removed
98+ // even though both nodes will not be emitted.
99+ if ( range . pos === 0 ) {
100+ return filter ( getLeadingCommentsOfPosition ( 0 ) , isTripleSlashComment ) ;
101+ }
88102
89- if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( < Node > nodeOrRange ) ) {
90- // If the node will not be emitted in JS, remove all the comments (normal,
91- // pinned and `///`) associated with the node, unless it is a triple slash
92- // comment at the top of the file.
93- //
94- // For Example:
95- // /// <reference-path ...>
96- // declare var x;
97- // /// <reference-path ...>
98- // interface F {}
99- //
100- // The first `///` will NOT be removed while the second one will be removed
101- // even though both nodes will not be emitted.
102- if ( range . pos === 0 ) {
103- return filter ( getLeadingCommentsOfPosition ( 0 ) , isTripleSlashComment ) ;
103+ return undefined ;
104104 }
105-
106- return undefined ;
107105 }
108106
107+
109108 return getLeadingCommentsOfPosition ( range . pos ) ;
110109 }
111110
@@ -126,15 +125,14 @@ namespace ts {
126125 }
127126
128127 function getTrailingComments ( range : TextRange ) : CommentRange [ ] ;
129- function getTrailingComments ( node : Node , shouldSkipCommentsForNodeCallback ? : ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ? : ( node : Node ) => TextRange ) : CommentRange [ ] ;
130- function getTrailingComments ( nodeOrRange : TextRange | Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) {
131- let range = < TextRange > nodeOrRange ;
132- if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( < Node > nodeOrRange ) ) {
133- return undefined ;
134- }
128+ function getTrailingComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( contextNode : Node ) => boolean , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : CommentRange [ ] ;
129+ function getTrailingComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( contextNode : Node ) => boolean , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
130+ if ( contextNode ) {
131+ if ( ignoreNodeCallback ( contextNode ) ) {
132+ return undefined ;
133+ }
135134
136- if ( getCustomTextRangeForNodeCallback ) {
137- range = getCustomTextRangeForNodeCallback ( < Node > nodeOrRange ) || range ;
135+ range = getTextRangeCallback ( contextNode ) || range ;
138136 }
139137
140138 return getTrailingCommentsOfPosition ( range . end ) ;
@@ -162,13 +160,12 @@ namespace ts {
162160 return consumeCommentRanges ( comments ) ;
163161 }
164162
165- function emitLeadingComments ( range : Node , comments : CommentRange [ ] , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) : void ;
166163 function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] ) : void ;
167- function emitLeadingComments ( nodeOrRange : Node | TextRange , comments : CommentRange [ ] , getCustomTextRangeForNodeCallback ?: ( node : Node ) => TextRange ) {
164+ function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode : Node , getTextRangeCallback : ( contextNode : Node ) => TextRange ) : void ;
165+ function emitLeadingComments ( range : TextRange , comments : CommentRange [ ] , contextNode ?: Node , getTextRangeCallback ?: ( contextNode : Node ) => TextRange ) {
168166 if ( comments && comments . length > 0 ) {
169- let range = < TextRange > nodeOrRange ;
170- if ( getCustomTextRangeForNodeCallback ) {
171- range = getCustomTextRangeForNodeCallback ( < Node > nodeOrRange ) ;
167+ if ( contextNode ) {
168+ range = getTextRangeCallback ( contextNode ) || range ;
172169 }
173170
174171 emitNewLineBeforeLeadingComments ( currentLineMap , writer , range , comments ) ;
@@ -183,16 +180,20 @@ namespace ts {
183180 emitComments ( currentText , currentLineMap , writer , comments , /*leadingSeparator*/ true , /*trailingSeparator*/ false , newLine , writeComment ) ;
184181 }
185182
186- function emitLeadingDetachedComments ( range : TextRange , contextNode ?: Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : void {
187- if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( contextNode ) ) {
183+ function emitLeadingDetachedComments ( range : TextRange ) : void ;
184+ function emitLeadingDetachedComments ( range : TextRange , contextNode : Node , ignoreNodeCallback : ( node : Node ) => boolean ) : void ;
185+ function emitLeadingDetachedComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( node : Node ) => boolean ) : void {
186+ if ( contextNode && ignoreNodeCallback ( contextNode ) ) {
188187 return ;
189188 }
190189
191190 emitDetachedCommentsAndUpdateCommentsInfo ( range , /*removeComments*/ false ) ;
192191 }
193192
194- function emitTrailingDetachedComments ( range : TextRange , contextNode ?: Node , shouldSkipCommentsForNodeCallback ?: ( node : Node ) => boolean ) : void {
195- if ( shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback ( contextNode ) ) {
193+ function emitTrailingDetachedComments ( range : TextRange ) : void ;
194+ function emitTrailingDetachedComments ( range : TextRange , contextNode : Node , ignoreNodeCallback ?: ( node : Node ) => boolean ) : void ;
195+ function emitTrailingDetachedComments ( range : TextRange , contextNode ?: Node , ignoreNodeCallback ?: ( node : Node ) => boolean ) : void {
196+ if ( contextNode && ignoreNodeCallback ( contextNode ) ) {
196197 return ;
197198 }
198199
0 commit comments