@@ -105,14 +105,19 @@ export default (options = {}) => {
105
105
106
106
/**
107
107
* @param {Context } context
108
- * @param {{ line: number, column: number } } loc
108
+ * @param {{ line: number, column: number } } prev
109
+ * @param {{ line: number, column: number } | null } next
109
110
* @returns {boolean } true if final comment is a line comment
110
111
*/
111
- function flush_trailing_comments ( context , loc ) {
112
+ function flush_trailing_comments ( context , prev , next ) {
112
113
while ( comment_index < comments . length ) {
113
114
const comment = comments [ comment_index ] ;
114
115
115
- if ( comment && comment . loc . start . line === loc . line ) {
116
+ if (
117
+ comment &&
118
+ comment . loc . start . line === prev . line &&
119
+ ( next === null || before ( comment . loc . end , next ) )
120
+ ) {
116
121
context . write ( ' ' ) ;
117
122
write_comment ( comment , context ) ;
118
123
@@ -154,10 +159,8 @@ export default (options = {}) => {
154
159
* @param {boolean } pad
155
160
*/
156
161
function sequence ( context , node , nodes , pad , separator = ',' ) {
157
- if ( nodes . length === 0 ) return ;
158
-
159
162
let multiline = false ;
160
- let length = - 2 ;
163
+ let length = - 1 ;
161
164
162
165
/** @type {boolean[] } */
163
166
const multiline_nodes = [ ] ;
@@ -172,7 +175,8 @@ export default (options = {}) => {
172
175
child_context . write ( separator ) ;
173
176
}
174
177
175
- if ( child && flush_trailing_comments ( child_context , child . loc . end ) ) {
178
+ const next = i === nodes . length - 1 ? node . loc . end : nodes [ i + 1 ] ?. loc . start || null ;
179
+ if ( child && flush_trailing_comments ( child_context , child . loc . end , next ) ) {
176
180
multiline = true ;
177
181
}
178
182
@@ -187,7 +191,7 @@ export default (options = {}) => {
187
191
if ( multiline ) {
188
192
context . indent ( ) ;
189
193
context . newline ( ) ;
190
- } else if ( pad ) {
194
+ } else if ( pad && length > 0 ) {
191
195
context . write ( ' ' ) ;
192
196
}
193
197
@@ -219,7 +223,7 @@ export default (options = {}) => {
219
223
if ( multiline ) {
220
224
context . dedent ( ) ;
221
225
context . newline ( ) ;
222
- } else if ( pad ) {
226
+ } else if ( pad && length > 0 ) {
223
227
context . write ( ' ' ) ;
224
228
}
225
229
}
@@ -538,7 +542,7 @@ export default (options = {}) => {
538
542
visit ( node ) ;
539
543
540
544
if ( is_statement && node . loc ) {
541
- flush_trailing_comments ( context , node . loc . end ) ;
545
+ flush_trailing_comments ( context , node . loc . end , null ) ;
542
546
}
543
547
} ,
544
548
0 commit comments