Skip to content

Commit e94f1ae

Browse files
allow leading and trailing comments in mustache tags
1 parent e4faa78 commit e94f1ae

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function parse(source, typescript) {
2929
* @param {string} source
3030
* @param {boolean} typescript
3131
* @param {number} index
32+
* @returns {acorn.Expression & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; }}
3233
*/
3334
export function parse_expression_at(source, typescript, index) {
3435
const parser = typescript ? ParserWithTS : acorn.Parser;
@@ -110,6 +111,9 @@ function get_comment_handlers(source) {
110111
}
111112
}
112113
});
114+
if (comments.length > 0) {
115+
(ast.trailingComments ||= []).push(...comments.splice(0))
116+
}
113117
}
114118
};
115119
}

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ export default function mustache(parser) {
1414
parser.allow_whitespace();
1515

1616
if (parser.eat('#')) return open(parser);
17-
if (parser.eat('/')) return close(parser);
1817
if (parser.eat(':')) return next(parser);
1918
if (parser.eat('@')) return special(parser);
19+
if (parser.match('/')) {
20+
if (!parser.match('/*') && !parser.match('//')) {
21+
parser.eat('/');
22+
return close(parser);
23+
}
24+
}
2025

2126
const expression = read_expression(parser);
2227

0 commit comments

Comments
 (0)