@@ -5,17 +5,16 @@ namespace ts {
55 export interface CommentWriter {
66 reset ( ) : void ;
77 setSourceFile ( sourceFile : SourceFile ) : void ;
8- emitNodeWithComments ( emitContext : EmitContext , node : Node , emitCallback : ( emitContext : EmitContext , node : Node ) => void ) : void ;
8+ setWriter ( writer : EmitTextWriter ) : void ;
9+ emitNodeWithComments ( hint : EmitHint , node : Node , emitCallback : ( hint : EmitHint , node : Node ) => void ) : void ;
910 emitBodyWithDetachedComments ( node : Node , detachedRange : TextRange , emitCallback : ( node : Node ) => void ) : void ;
1011 emitTrailingCommentsOfPosition ( pos : number ) : void ;
1112 }
1213
13- export function createCommentWriter ( host : EmitHost , writer : EmitTextWriter , sourceMap : SourceMapWriter ) : CommentWriter {
14- const compilerOptions = host . getCompilerOptions ( ) ;
15- const extendedDiagnostics = compilerOptions . extendedDiagnostics ;
16- const newLine = host . getNewLine ( ) ;
17- const { emitPos } = sourceMap ;
18-
14+ export function createCommentWriter ( printerOptions : PrinterOptions , emitPos : ( ( pos : number ) => void ) | undefined ) : CommentWriter {
15+ const extendedDiagnostics = printerOptions . extendedDiagnostics ;
16+ const newLine = getNewLineCharacter ( printerOptions ) ;
17+ let writer : EmitTextWriter ;
1918 let containerPos = - 1 ;
2019 let containerEnd = - 1 ;
2120 let declarationListContainerEnd = - 1 ;
@@ -24,19 +23,20 @@ namespace ts {
2423 let currentLineMap : number [ ] ;
2524 let detachedCommentsInfo : { nodePos : number , detachedCommentEndPos : number } [ ] ;
2625 let hasWrittenComment = false ;
27- let disabled : boolean = compilerOptions . removeComments ;
26+ let disabled : boolean = printerOptions . removeComments ;
2827
2928 return {
3029 reset,
30+ setWriter,
3131 setSourceFile,
3232 emitNodeWithComments,
3333 emitBodyWithDetachedComments,
3434 emitTrailingCommentsOfPosition,
3535 } ;
3636
37- function emitNodeWithComments ( emitContext : EmitContext , node : Node , emitCallback : ( emitContext : EmitContext , node : Node ) => void ) {
37+ function emitNodeWithComments ( hint : EmitHint , node : Node , emitCallback : ( hint : EmitHint , node : Node ) => void ) {
3838 if ( disabled ) {
39- emitCallback ( emitContext , node ) ;
39+ emitCallback ( hint , node ) ;
4040 return ;
4141 }
4242
@@ -47,11 +47,11 @@ namespace ts {
4747 // Both pos and end are synthesized, so just emit the node without comments.
4848 if ( emitFlags & EmitFlags . NoNestedComments ) {
4949 disabled = true ;
50- emitCallback ( emitContext , node ) ;
50+ emitCallback ( hint , node ) ;
5151 disabled = false ;
5252 }
5353 else {
54- emitCallback ( emitContext , node ) ;
54+ emitCallback ( hint , node ) ;
5555 }
5656 }
5757 else {
@@ -94,11 +94,11 @@ namespace ts {
9494
9595 if ( emitFlags & EmitFlags . NoNestedComments ) {
9696 disabled = true ;
97- emitCallback ( emitContext , node ) ;
97+ emitCallback ( hint , node ) ;
9898 disabled = false ;
9999 }
100100 else {
101- emitCallback ( emitContext , node ) ;
101+ emitCallback ( hint , node ) ;
102102 }
103103
104104 if ( extendedDiagnostics ) {
@@ -198,9 +198,9 @@ namespace ts {
198198 }
199199
200200 // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
201- emitPos ( commentPos ) ;
201+ if ( emitPos ) emitPos ( commentPos ) ;
202202 writeCommentRange ( currentText , currentLineMap , writer , commentPos , commentEnd , newLine ) ;
203- emitPos ( commentEnd ) ;
203+ if ( emitPos ) emitPos ( commentEnd ) ;
204204
205205 if ( hasTrailingNewLine ) {
206206 writer . writeLine ( ) ;
@@ -220,9 +220,9 @@ namespace ts {
220220 writer . write ( " " ) ;
221221 }
222222
223- emitPos ( commentPos ) ;
223+ if ( emitPos ) emitPos ( commentPos ) ;
224224 writeCommentRange ( currentText , currentLineMap , writer , commentPos , commentEnd , newLine ) ;
225- emitPos ( commentEnd ) ;
225+ if ( emitPos ) emitPos ( commentEnd ) ;
226226
227227 if ( hasTrailingNewLine ) {
228228 writer . writeLine ( ) ;
@@ -248,9 +248,9 @@ namespace ts {
248248 function emitTrailingCommentOfPosition ( commentPos : number , commentEnd : number , _kind : SyntaxKind , hasTrailingNewLine : boolean ) {
249249 // trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space
250250
251- emitPos ( commentPos ) ;
251+ if ( emitPos ) emitPos ( commentPos ) ;
252252 writeCommentRange ( currentText , currentLineMap , writer , commentPos , commentEnd , newLine ) ;
253- emitPos ( commentEnd ) ;
253+ if ( emitPos ) emitPos ( commentEnd ) ;
254254
255255 if ( hasTrailingNewLine ) {
256256 writer . writeLine ( ) ;
@@ -286,6 +286,10 @@ namespace ts {
286286 detachedCommentsInfo = undefined ;
287287 }
288288
289+ function setWriter ( output : EmitTextWriter ) : void {
290+ writer = output ;
291+ }
292+
289293 function setSourceFile ( sourceFile : SourceFile ) {
290294 currentSourceFile = sourceFile ;
291295 currentText = currentSourceFile . text ;
@@ -323,9 +327,9 @@ namespace ts {
323327 }
324328
325329 function writeComment ( text : string , lineMap : number [ ] , writer : EmitTextWriter , commentPos : number , commentEnd : number , newLine : string ) {
326- emitPos ( commentPos ) ;
330+ if ( emitPos ) emitPos ( commentPos ) ;
327331 writeCommentRange ( text , lineMap , writer , commentPos , commentEnd , newLine ) ;
328- emitPos ( commentEnd ) ;
332+ if ( emitPos ) emitPos ( commentEnd ) ;
329333 }
330334
331335 /**
0 commit comments