Skip to content

fix: allow trailing comments on functions #12469

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

Closed
wants to merge 1 commit into from
Closed
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/few-mails-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: allow trailing comments on functions
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ 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;
// in simple expressions like {true/*this is literal*/} trailing comments are not included
// in the node end so we need them added to the index to keep the parser in check.
// but for arrow functions trailing comments are actually already included in the end
// and doesn't include whitespace in the end so if we set the parse index to that we
// will loose chars that we already read.
index = Math.max(node.trailingComments.at(-1).end, index);
}

while (num_parens > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<button
onclick={() => {
/* some comment */
}}
></button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"html": {
"type": "Fragment",
"start": 0,
"end": 61,
"children": [
{
"type": "Element",
"start": 0,
"end": 61,
"name": "button",
"attributes": [
{
"type": "Attribute",
"start": 9,
"end": 50,
"name": "onclick",
"value": [
{
"type": "MustacheTag",
"start": 17,
"end": 50,
"expression": {
"type": "ArrowFunctionExpression",
"start": 18,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 4,
"column": 2
}
},
"id": null,
"expression": false,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 24,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 4,
"column": 2
}
},
"body": []
},
"trailingComments": [
{
"type": "Block",
"value": " some comment ",
"start": 28,
"end": 46
}
]
}
}
]
}
],
"children": []
}
]
}
}
Loading