Skip to content

Commit ee098cc

Browse files
always attach trailingComments to parent node if possible
this is required to correctly calculate expression's ending index correctly with expr.trailingComments.at(-1).end in `compiler/phases/1-parse/read/expression.js`
1 parent e94f1ae commit ee098cc

File tree

1 file changed

+8
-5
lines changed
  • packages/svelte/src/compiler/phases/1-parse

1 file changed

+8
-5
lines changed

packages/svelte/src/compiler/phases/1-parse/acorn.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function get_comment_handlers(source) {
9292
if (comments.length === 0) return;
9393

9494
walk(ast, null, {
95-
_(node, { next }) {
95+
_(node, { next, path }) {
9696
let comment;
9797

9898
while (comments[0] && comments[0].start < node.start) {
@@ -103,16 +103,19 @@ function get_comment_handlers(source) {
103103
next();
104104

105105
if (comments[0]) {
106-
const slice = source.slice(node.end, comments[0].start);
106+
const parent = path.at(-1);
107+
if (parent === undefined || node.end !== parent.end) {
108+
const slice = source.slice(node.end, comments[0].start);
107109

108-
if (/^[,) \t]*$/.test(slice)) {
109-
node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
110+
if (/^[,) \t]*$/.test(slice)) {
111+
node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
112+
}
110113
}
111114
}
112115
}
113116
});
114117
if (comments.length > 0) {
115-
(ast.trailingComments ||= []).push(...comments.splice(0))
118+
(ast.trailingComments ||= []).push(...comments.splice(0));
116119
}
117120
}
118121
};

0 commit comments

Comments
 (0)