Skip to content

fix: properly assign trailing comments #12471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-toes-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: properly assign trailing comments
32 changes: 28 additions & 4 deletions packages/svelte/src/compiler/phases/1-parse/acorn.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,42 @@ function get_comment_handlers(source) {
next();

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

if (parent === undefined || node.end !== parent.end) {
const slice = source.slice(node.end, comments[0].start);

if (/^[,) \t]*$/.test(slice)) {
const is_last_in_body =
((parent?.type === 'BlockStatement' || parent?.type === 'Program') &&
parent.body.indexOf(node) === parent.body.length - 1) ||
(parent?.type === 'ArrayExpression' &&
parent.elements.indexOf(node) === parent.elements.length - 1) ||
(parent?.type === 'ObjectExpression' &&
parent.properties.indexOf(node) === parent.properties.length - 1);

if (is_last_in_body) {
// Special case: There can be multiple trailing comments after the last node in a block,
// and they can be separated by newlines
let end = node.end;

while (comments.length) {
const comment = comments[0];
if (parent && comment.start >= parent.end) break;

(node.trailingComments ||= []).push(comment);
comments.shift();
end = comment.end;
}
} else if (node.end <= comments[0].start && /^[,) \t]*$/.test(slice)) {
node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
}
}
}
}
});
if (comments.length > 0) {

// Special case: Trailing comments after the root node (which can only happen for expression tags or for Program nodes).
// Adding them ensures that we can later detect the end of the expression tag correctly.
if (comments.length > 0 && (comments[0].start >= ast.end || ast.type === 'Program')) {
(ast.trailingComments ||= []).push(...comments.splice(0));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@

/** a comment */
function asd() {

foo; // trailing
/* leading comment 1 */
/* leading comment 2 */
/* leading comment 3 */
bar;
/* trailing comment 1 */
/* trailing comment 2 */
/* trailing comment 3 */
}

const array = [
// leading comment 1
// leading comment 2
1, // trailing comment 1
/* trailing comment 2 */
];

const object = {
// leading comment 1
// leading comment 2
a: 1, // trailing comment 1
/* trailing comment 2 */
};
</script>

<button
on:click={// comment
() => {
/* another comment */
fn();
fn(); // a trailing comment
/* trailing block comment */
}}
>
{/* leading block comment */ a}
Expand Down
Loading
Loading