@@ -5,17 +5,16 @@ namespace ts {
5
5
export interface CommentWriter {
6
6
reset ( ) : void ;
7
7
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 ;
9
10
emitBodyWithDetachedComments ( node : Node , detachedRange : TextRange , emitCallback : ( node : Node ) => void ) : void ;
10
11
emitTrailingCommentsOfPosition ( pos : number ) : void ;
11
12
}
12
13
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 ;
19
18
let containerPos = - 1 ;
20
19
let containerEnd = - 1 ;
21
20
let declarationListContainerEnd = - 1 ;
@@ -24,19 +23,20 @@ namespace ts {
24
23
let currentLineMap : number [ ] ;
25
24
let detachedCommentsInfo : { nodePos : number , detachedCommentEndPos : number } [ ] ;
26
25
let hasWrittenComment = false ;
27
- let disabled : boolean = compilerOptions . removeComments ;
26
+ let disabled : boolean = printerOptions . removeComments ;
28
27
29
28
return {
30
29
reset,
30
+ setWriter,
31
31
setSourceFile,
32
32
emitNodeWithComments,
33
33
emitBodyWithDetachedComments,
34
34
emitTrailingCommentsOfPosition,
35
35
} ;
36
36
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 ) {
38
38
if ( disabled ) {
39
- emitCallback ( emitContext , node ) ;
39
+ emitCallback ( hint , node ) ;
40
40
return ;
41
41
}
42
42
@@ -47,11 +47,11 @@ namespace ts {
47
47
// Both pos and end are synthesized, so just emit the node without comments.
48
48
if ( emitFlags & EmitFlags . NoNestedComments ) {
49
49
disabled = true ;
50
- emitCallback ( emitContext , node ) ;
50
+ emitCallback ( hint , node ) ;
51
51
disabled = false ;
52
52
}
53
53
else {
54
- emitCallback ( emitContext , node ) ;
54
+ emitCallback ( hint , node ) ;
55
55
}
56
56
}
57
57
else {
@@ -94,11 +94,11 @@ namespace ts {
94
94
95
95
if ( emitFlags & EmitFlags . NoNestedComments ) {
96
96
disabled = true ;
97
- emitCallback ( emitContext , node ) ;
97
+ emitCallback ( hint , node ) ;
98
98
disabled = false ;
99
99
}
100
100
else {
101
- emitCallback ( emitContext , node ) ;
101
+ emitCallback ( hint , node ) ;
102
102
}
103
103
104
104
if ( extendedDiagnostics ) {
@@ -198,9 +198,9 @@ namespace ts {
198
198
}
199
199
200
200
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
201
- emitPos ( commentPos ) ;
201
+ if ( emitPos ) emitPos ( commentPos ) ;
202
202
writeCommentRange ( currentText , currentLineMap , writer , commentPos , commentEnd , newLine ) ;
203
- emitPos ( commentEnd ) ;
203
+ if ( emitPos ) emitPos ( commentEnd ) ;
204
204
205
205
if ( hasTrailingNewLine ) {
206
206
writer . writeLine ( ) ;
@@ -220,9 +220,9 @@ namespace ts {
220
220
writer . write ( " " ) ;
221
221
}
222
222
223
- emitPos ( commentPos ) ;
223
+ if ( emitPos ) emitPos ( commentPos ) ;
224
224
writeCommentRange ( currentText , currentLineMap , writer , commentPos , commentEnd , newLine ) ;
225
- emitPos ( commentEnd ) ;
225
+ if ( emitPos ) emitPos ( commentEnd ) ;
226
226
227
227
if ( hasTrailingNewLine ) {
228
228
writer . writeLine ( ) ;
@@ -248,9 +248,9 @@ namespace ts {
248
248
function emitTrailingCommentOfPosition ( commentPos : number , commentEnd : number , _kind : SyntaxKind , hasTrailingNewLine : boolean ) {
249
249
// trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space
250
250
251
- emitPos ( commentPos ) ;
251
+ if ( emitPos ) emitPos ( commentPos ) ;
252
252
writeCommentRange ( currentText , currentLineMap , writer , commentPos , commentEnd , newLine ) ;
253
- emitPos ( commentEnd ) ;
253
+ if ( emitPos ) emitPos ( commentEnd ) ;
254
254
255
255
if ( hasTrailingNewLine ) {
256
256
writer . writeLine ( ) ;
@@ -286,6 +286,10 @@ namespace ts {
286
286
detachedCommentsInfo = undefined ;
287
287
}
288
288
289
+ function setWriter ( output : EmitTextWriter ) : void {
290
+ writer = output ;
291
+ }
292
+
289
293
function setSourceFile ( sourceFile : SourceFile ) {
290
294
currentSourceFile = sourceFile ;
291
295
currentText = currentSourceFile . text ;
@@ -323,9 +327,9 @@ namespace ts {
323
327
}
324
328
325
329
function writeComment ( text : string , lineMap : number [ ] , writer : EmitTextWriter , commentPos : number , commentEnd : number , newLine : string ) {
326
- emitPos ( commentPos ) ;
330
+ if ( emitPos ) emitPos ( commentPos ) ;
327
331
writeCommentRange ( text , lineMap , writer , commentPos , commentEnd , newLine ) ;
328
- emitPos ( commentEnd ) ;
332
+ if ( emitPos ) emitPos ( commentEnd ) ;
329
333
}
330
334
331
335
/**
0 commit comments