diff --git a/packages/svelte/src/compiler/phases/1-parse/acorn.js b/packages/svelte/src/compiler/phases/1-parse/acorn.js index 9db9149fb8..2da58a6ab3 100644 --- a/packages/svelte/src/compiler/phases/1-parse/acorn.js +++ b/packages/svelte/src/compiler/phases/1-parse/acorn.js @@ -92,7 +92,7 @@ function get_comment_handlers(source) { if (comments.length === 0) return; walk(ast, null, { - _(node, { next }) { + _(node, { next, path }) { let comment; while (comments[0] && comments[0].start < node.start) { @@ -103,16 +103,19 @@ function get_comment_handlers(source) { next(); if (comments[0]) { - const slice = source.slice(node.end, comments[0].start); + const parent = path.at(-1); + if (parent === undefined || node.end !== parent.end) { + const slice = source.slice(node.end, comments[0].start); - if (/^[,) \t]*$/.test(slice)) { - node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())]; + if (/^[,) \t]*$/.test(slice)) { + node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())]; + } } } } }); if (comments.length > 0) { - (ast.trailingComments ||= []).push(...comments.splice(0)) + (ast.trailingComments ||= []).push(...comments.splice(0)); } } };