@@ -41,18 +41,14 @@ namespace ts {
4141 }
4242
4343 if ( node ) {
44- const { pos, end } = getCommentRange ( node ) ;
45- const emitFlags = getEmitFlags ( node ) ;
44+ hasWrittenComment = false ;
45+
46+ const emitNode = node . emitNode ;
47+ const emitFlags = emitNode && emitNode . flags ;
48+ const { pos, end } = emitNode && emitNode . commentRange || node ;
4649 if ( ( pos < 0 && end < 0 ) || ( pos === end ) ) {
4750 // Both pos and end are synthesized, so just emit the node without comments.
48- if ( emitFlags & EmitFlags . NoNestedComments ) {
49- disabled = true ;
50- emitCallback ( hint , node ) ;
51- disabled = false ;
52- }
53- else {
54- emitCallback ( hint , node ) ;
55- }
51+ emitNodeWithSynthesizedComments ( hint , node , emitNode , emitFlags , emitCallback ) ;
5652 }
5753 else {
5854 if ( extendedDiagnostics ) {
@@ -92,17 +88,10 @@ namespace ts {
9288 performance . measure ( "commentTime" , "preEmitNodeWithComment" ) ;
9389 }
9490
95- if ( emitFlags & EmitFlags . NoNestedComments ) {
96- disabled = true ;
97- emitCallback ( hint , node ) ;
98- disabled = false ;
99- }
100- else {
101- emitCallback ( hint , node ) ;
102- }
91+ emitNodeWithSynthesizedComments ( hint , node , emitNode , emitFlags , emitCallback ) ;
10392
10493 if ( extendedDiagnostics ) {
105- performance . mark ( "beginEmitNodeWithComment " ) ;
94+ performance . mark ( "postEmitNodeWithComment " ) ;
10695 }
10796
10897 // Restore previous container state.
@@ -117,12 +106,89 @@ namespace ts {
117106 }
118107
119108 if ( extendedDiagnostics ) {
120- performance . measure ( "commentTime" , "beginEmitNodeWithComment " ) ;
109+ performance . measure ( "commentTime" , "postEmitNodeWithComment " ) ;
121110 }
122111 }
123112 }
124113 }
125114
115+ function emitNodeWithSynthesizedComments ( hint : EmitHint , node : Node , emitNode : EmitNode , emitFlags : EmitFlags , emitCallback : ( hint : EmitHint , node : Node ) => void ) {
116+ const leadingComments = emitNode && emitNode . leadingComments ;
117+ if ( some ( leadingComments ) ) {
118+ if ( extendedDiagnostics ) {
119+ performance . mark ( "preEmitNodeWithSynthesizedComments" ) ;
120+ }
121+
122+ forEach ( leadingComments , emitLeadingSynthesizedComment ) ;
123+
124+ if ( extendedDiagnostics ) {
125+ performance . measure ( "commentTime" , "preEmitNodeWithSynthesizedComments" ) ;
126+ }
127+ }
128+
129+ emitNodeWithNestedComments ( hint , node , emitFlags , emitCallback ) ;
130+
131+ const trailingComments = emitNode && emitNode . trailingComments ;
132+ if ( some ( trailingComments ) ) {
133+ if ( extendedDiagnostics ) {
134+ performance . mark ( "postEmitNodeWithSynthesizedComments" ) ;
135+ }
136+
137+ debugger ;
138+ forEach ( trailingComments , emitTrailingSynthesizedComment ) ;
139+
140+ if ( extendedDiagnostics ) {
141+ performance . measure ( "commentTime" , "postEmitNodeWithSynthesizedComments" ) ;
142+ }
143+ }
144+ }
145+
146+ function emitLeadingSynthesizedComment ( comment : SynthesizedComment ) {
147+ if ( comment . kind === SyntaxKind . SingleLineCommentTrivia ) {
148+ writer . writeLine ( ) ;
149+ }
150+ writeSynthesizedComment ( comment ) ;
151+ if ( comment . hasTrailingNewLine || comment . kind === SyntaxKind . SingleLineCommentTrivia ) {
152+ writer . writeLine ( ) ;
153+ }
154+ else {
155+ writer . write ( " " ) ;
156+ }
157+ }
158+
159+ function emitTrailingSynthesizedComment ( comment : SynthesizedComment ) {
160+ if ( ! writer . isAtStartOfLine ( ) ) {
161+ writer . write ( " " ) ;
162+ }
163+ writeSynthesizedComment ( comment ) ;
164+ if ( comment . hasTrailingNewLine ) {
165+ writer . writeLine ( ) ;
166+ }
167+ }
168+
169+ function writeSynthesizedComment ( comment : SynthesizedComment ) {
170+ const text = formatSynthesizedComment ( comment ) ;
171+ const lineMap = comment . kind === SyntaxKind . MultiLineCommentTrivia ? computeLineStarts ( text ) : undefined ;
172+ writeCommentRange ( text , lineMap , writer , 0 , text . length , newLine ) ;
173+ }
174+
175+ function formatSynthesizedComment ( comment : SynthesizedComment ) {
176+ return comment . kind === SyntaxKind . MultiLineCommentTrivia
177+ ? `/*${ comment . text } */`
178+ : `//${ comment . text } ` ;
179+ }
180+
181+ function emitNodeWithNestedComments ( hint : EmitHint , node : Node , emitFlags : EmitFlags , emitCallback : ( hint : EmitHint , node : Node ) => void ) {
182+ if ( emitFlags & EmitFlags . NoNestedComments ) {
183+ disabled = true ;
184+ emitCallback ( hint , node ) ;
185+ disabled = false ;
186+ }
187+ else {
188+ emitCallback ( hint , node ) ;
189+ }
190+ }
191+
126192 function emitBodyWithDetachedComments ( node : Node , detachedRange : TextRange , emitCallback : ( node : Node ) => void ) {
127193 if ( extendedDiagnostics ) {
128194 performance . mark ( "preEmitBodyWithDetachedComments" ) ;
0 commit comments