@@ -30,6 +30,7 @@ export function parse(source, typescript) {
30
30
* @param {string } source
31
31
* @param {boolean } typescript
32
32
* @param {number } index
33
+ * @returns {acorn.Expression & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; } }
33
34
*/
34
35
export function parse_expression_at ( source , typescript , index ) {
35
36
const parser = typescript ? ParserWithTS : acorn . Parser ;
@@ -92,7 +93,7 @@ function get_comment_handlers(source) {
92
93
if ( comments . length === 0 ) return ;
93
94
94
95
walk ( ast , null , {
95
- _ ( node , { next } ) {
96
+ _ ( node , { next, path } ) {
96
97
let comment ;
97
98
98
99
while ( comments [ 0 ] && comments [ 0 ] . start < node . start ) {
@@ -103,14 +104,20 @@ function get_comment_handlers(source) {
103
104
next ( ) ;
104
105
105
106
if ( comments [ 0 ] ) {
106
- const slice = source . slice ( node . end , comments [ 0 ] . start ) ;
107
+ const parent = path . at ( - 1 ) ;
108
+ if ( parent === undefined || node . end !== parent . end ) {
109
+ const slice = source . slice ( node . end , comments [ 0 ] . start ) ;
107
110
108
- if ( / ^ [ , ) \t ] * $ / . test ( slice ) ) {
109
- node . trailingComments = [ /** @type {CommentWithLocation } */ ( comments . shift ( ) ) ] ;
111
+ if ( / ^ [ , ) \t ] * $ / . test ( slice ) ) {
112
+ node . trailingComments = [ /** @type {CommentWithLocation } */ ( comments . shift ( ) ) ] ;
113
+ }
110
114
}
111
115
}
112
116
}
113
117
} ) ;
118
+ if ( comments . length > 0 ) {
119
+ ( ast . trailingComments ||= [ ] ) . push ( ...comments . splice ( 0 ) ) ;
120
+ }
114
121
}
115
122
} ;
116
123
}
0 commit comments