Skip to content

Commit

Permalink
fix: allow leading and trailing comments in mustache tag (#11866)
Browse files Browse the repository at this point in the history
Fixes #7456

---------

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
BlueGreenMagick and Rich-Harris authored Jul 15, 2024
1 parent a8dc96e commit 81ed425
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-wombats-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: allow leading and trailing comments in mustache expression
5 changes: 5 additions & 0 deletions packages/svelte/scripts/process-messages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ function transform(name, dest) {
}
});

if (comments.length > 0) {
// @ts-expect-error
(ast.trailingComments ||= []).push(...comments);
}

const category = messages[name];

// find the `export function CODE` node
Expand Down
15 changes: 11 additions & 4 deletions packages/svelte/src/compiler/phases/1-parse/acorn.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function parse(source, typescript) {
* @param {string} source
* @param {boolean} typescript
* @param {number} index
* @returns {acorn.Expression & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; }}
*/
export function parse_expression_at(source, typescript, index) {
const parser = typescript ? ParserWithTS : acorn.Parser;
Expand Down Expand Up @@ -92,7 +93,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) {
Expand All @@ -103,14 +104,20 @@ 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));
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default function read_expression(parser) {
}

let index = /** @type {number} */ (node.end);
if (node.trailingComments !== undefined && node.trailingComments.length > 0) {
index = node.trailingComments.at(-1).end;
}

while (num_parens > 0) {
const char = parser.template[index];

Expand Down
7 changes: 6 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export default function tag(parser) {
parser.allow_whitespace();

if (parser.eat('#')) return open(parser);
if (parser.eat('/')) return close(parser);
if (parser.eat(':')) return next(parser);
if (parser.eat('@')) return special(parser);
if (parser.match('/')) {
if (!parser.match('/*') && !parser.match('//')) {
parser.eat('/');
return close(parser);
}
}

const expression = read_expression(parser);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script>
// a leading comment
const a = true; // a trailing comment
const a = 1; // a trailing comment
const b = 2;
/** a comment */
function asd() {
Expand All @@ -14,4 +15,10 @@
/* another comment */
fn();
}}
></button>
>
{/* leading block comment */ a}
</button>
{ // leading line comment
a + b // trailing line comment
/* trailing block comment */
}
Loading

0 comments on commit 81ed425

Please sign in to comment.