@@ -136,17 +136,27 @@ export default (options = {}) => {
136
136
137
137
/**
138
138
* @param {Context } context
139
- * @param {{ line: number, column: number } } loc
139
+ * @param {{ line: number, column: number } | null } from
140
+ * @param {{ line: number, column: number } } to
140
141
* @param {boolean } pad
141
142
*/
142
- function flush_comments_until ( context , loc , pad ) {
143
+ function flush_comments_until ( context , from , to , pad ) {
144
+ let first = true ;
145
+
143
146
while ( comment_index < comments . length ) {
144
147
const comment = comments [ comment_index ] ;
145
148
146
- if ( comment && before ( comment . loc . start , loc ) ) {
149
+ if ( comment && before ( comment . loc . start , to ) ) {
150
+ if ( first && from !== null && comment . loc . start . line > from . line ) {
151
+ context . margin ( ) ;
152
+ context . newline ( ) ;
153
+ }
154
+
155
+ first = false ;
156
+
147
157
write_comment ( comment , context ) ;
148
158
149
- if ( comment . loc . end . line < loc . line ) {
159
+ if ( comment . loc . end . line < to . line ) {
150
160
context . newline ( ) ;
151
161
} else if ( pad ) {
152
162
context . write ( ' ' ) ;
@@ -227,7 +237,7 @@ export default (options = {}) => {
227
237
prev = child ;
228
238
}
229
239
230
- flush_comments_until ( context , until ) ;
240
+ flush_comments_until ( context , nodes [ nodes . length - 1 ] ?. loc . end ?? null , until , false ) ;
231
241
232
242
if ( multiline ) {
233
243
context . dedent ( ) ;
@@ -270,7 +280,12 @@ export default (options = {}) => {
270
280
271
281
if ( node . loc ) {
272
282
context . newline ( ) ;
273
- flush_comments_until ( context , node . loc . end ) ;
283
+ flush_comments_until (
284
+ context ,
285
+ node . body [ node . body . length - 1 ] ?. loc . end ?? null ,
286
+ node . loc . end ,
287
+ false
288
+ ) ;
274
289
}
275
290
}
276
291
@@ -529,7 +544,7 @@ export default (options = {}) => {
529
544
const is_statement = / ( S t a t e m e n t | D e c l a r a t i o n ) $ / . test ( node . type ) ;
530
545
531
546
if ( node . loc ) {
532
- flush_comments_until ( context , node . loc . start , true ) ;
547
+ flush_comments_until ( context , null , node . loc . start , true ) ;
533
548
}
534
549
535
550
visit ( node ) ;
0 commit comments