Skip to content

Commit b6ec803

Browse files
committed
support multiple trailing comments after last statement in a body
1 parent 3b46852 commit b6ec803

File tree

4 files changed

+206
-87
lines changed

4 files changed

+206
-87
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,43 @@ function get_comment_handlers(source) {
104104
next();
105105

106106
if (comments[0]) {
107-
const parent = path.at(-1);
107+
const parent = /** @type {any} */ (path.at(-1));
108108

109109
if (parent === undefined || node.end !== parent.end) {
110110
const slice = source.slice(node.end, comments[0].start);
111-
112-
if (node.end <= comments[0].start && /^[,) \t]*$/.test(slice)) {
111+
const is_last_in_body =
112+
// BlockStatement and Program nodes have a body property
113+
parent?.body?.length &&
114+
Array.isArray(parent?.body) &&
115+
parent.body.indexOf(node) === parent.body.length - 1;
116+
117+
if (is_last_in_body) {
118+
// Special case: There can be multiple trailing comments after the last node in a block,
119+
// and they can be separated by newlines
120+
let end = node.end;
121+
122+
while (comments.length) {
123+
const comment = comments[0];
124+
if (parent && comment.start > parent.end) break;
125+
126+
const slice = source.slice(end, comment.start);
127+
if (node.end === end ? !/^[,)\s]*$/.test(slice) : slice.trim() !== '') break;
128+
129+
(node.trailingComments ||= []).push(comment);
130+
comments.shift();
131+
end = comment.end;
132+
}
133+
} else if (node.end <= comments[0].start && /^[,) \t]*$/.test(slice)) {
113134
node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
114135
}
115136
}
116137
}
117138
}
118139
});
119140

120-
// Special case: Trailing comments after the root node (which can only happen for expression tags) get added
121-
// regardless of line breaks between the root node and the comments. This ensures that we can later detect
122-
// the end of the expression tag correctly.
123-
if (comments.length > 0 && comments[0].start >= ast.end) {
141+
// Special case: Trailing comments after the root node (which can only happen for expression tags or for Program nodes).
142+
// Adding them ensures that we can later detect the end of the expression tag correctly.
143+
if (comments.length > 0 && (comments[0].start >= ast.end || ast.type === 'Program')) {
124144
(ast.trailingComments ||= []).push(...comments.splice(0));
125145
}
126146
}

packages/svelte/tests/parser-legacy/samples/javascript-comments/input.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
66
/** a comment */
77
function asd() {
8-
foo;
9-
/* comment belonging to noone */
8+
foo; // trailing
9+
/* leading comment 1 */
10+
/* leading comment 2 */
11+
/* leading comment 3 */
12+
bar;
13+
/* trailing comment 1 */
14+
/* trailing comment 2 */
15+
/* trailing comment 3 */
1016
}
1117
</script>
1218

@@ -15,7 +21,7 @@
1521
() => {
1622
/* another comment */
1723
fn(); // a trailing comment
18-
/* comment belonging to noone */
24+
/* trailing block comment */
1925
}}
2026
>
2127
{/* leading block comment */ a}

0 commit comments

Comments
 (0)