Skip to content

Commit 81ed425

Browse files
fix: allow leading and trailing comments in mustache tag (#11866)
Fixes #7456 --------- Co-authored-by: Rich Harris <[email protected]>
1 parent a8dc96e commit 81ed425

File tree

7 files changed

+292
-67
lines changed

7 files changed

+292
-67
lines changed

.changeset/fresh-wombats-learn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: allow leading and trailing comments in mustache expression

packages/svelte/scripts/process-messages/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ function transform(name, dest) {
129129
}
130130
});
131131

132+
if (comments.length > 0) {
133+
// @ts-expect-error
134+
(ast.trailingComments ||= []).push(...comments);
135+
}
136+
132137
const category = messages[name];
133138

134139
// find the `export function CODE` node

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function parse(source, typescript) {
3030
* @param {string} source
3131
* @param {boolean} typescript
3232
* @param {number} index
33+
* @returns {acorn.Expression & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; }}
3334
*/
3435
export function parse_expression_at(source, typescript, index) {
3536
const parser = typescript ? ParserWithTS : acorn.Parser;
@@ -92,7 +93,7 @@ function get_comment_handlers(source) {
9293
if (comments.length === 0) return;
9394

9495
walk(ast, null, {
95-
_(node, { next }) {
96+
_(node, { next, path }) {
9697
let comment;
9798

9899
while (comments[0] && comments[0].start < node.start) {
@@ -103,14 +104,20 @@ function get_comment_handlers(source) {
103104
next();
104105

105106
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);
107110

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+
}
110114
}
111115
}
112116
}
113117
});
118+
if (comments.length > 0) {
119+
(ast.trailingComments ||= []).push(...comments.splice(0));
120+
}
114121
}
115122
};
116123
}

packages/svelte/src/compiler/phases/1-parse/read/expression.js

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export default function read_expression(parser) {
1717
}
1818

1919
let index = /** @type {number} */ (node.end);
20+
if (node.trailingComments !== undefined && node.trailingComments.length > 0) {
21+
index = node.trailingComments.at(-1).end;
22+
}
23+
2024
while (num_parens > 0) {
2125
const char = parser.template[index];
2226

packages/svelte/src/compiler/phases/1-parse/state/tag.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ export default function tag(parser) {
1515
parser.allow_whitespace();
1616

1717
if (parser.eat('#')) return open(parser);
18-
if (parser.eat('/')) return close(parser);
1918
if (parser.eat(':')) return next(parser);
2019
if (parser.eat('@')) return special(parser);
20+
if (parser.match('/')) {
21+
if (!parser.match('/*') && !parser.match('//')) {
22+
parser.eat('/');
23+
return close(parser);
24+
}
25+
}
2126

2227
const expression = read_expression(parser);
2328

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
// a leading comment
3-
const a = true; // a trailing comment
3+
const a = 1; // a trailing comment
4+
const b = 2;
45
56
/** a comment */
67
function asd() {
@@ -14,4 +15,10 @@
1415
/* another comment */
1516
fn();
1617
}}
17-
></button>
18+
>
19+
{/* leading block comment */ a}
20+
</button>
21+
{ // leading line comment
22+
a + b // trailing line comment
23+
/* trailing block comment */
24+
}

0 commit comments

Comments
 (0)