Skip to content

Commit 078746a

Browse files
committed
fix: allow trailing comments on functions
1 parent 9666215 commit 078746a

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

.changeset/few-mails-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: allow trailing comments on functions

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export default function read_expression(parser) {
1818

1919
let index = /** @type {number} */ (node.end);
2020
if (node.trailingComments !== undefined && node.trailingComments.length > 0) {
21-
index = node.trailingComments.at(-1).end;
21+
// in simple expressions like {true/*this is literal*/} trailing comments are not included
22+
// in the node end so we need them added to the index to keep the parser in check.
23+
// but for arrow functions trailing comments are actually already included in the end
24+
// and doesn't include whitespace in the end so if we set the parse index to that we
25+
// will loose chars that we already read.
26+
index = Math.max(node.trailingComments.at(-1).end, index);
2227
}
2328

2429
while (num_parens > 0) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<button
2+
onclick={() => {
3+
/* some comment */
4+
}}
5+
></button>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"html": {
3+
"type": "Fragment",
4+
"start": 0,
5+
"end": 61,
6+
"children": [
7+
{
8+
"type": "Element",
9+
"start": 0,
10+
"end": 61,
11+
"name": "button",
12+
"attributes": [
13+
{
14+
"type": "Attribute",
15+
"start": 9,
16+
"end": 50,
17+
"name": "onclick",
18+
"value": [
19+
{
20+
"type": "MustacheTag",
21+
"start": 17,
22+
"end": 50,
23+
"expression": {
24+
"type": "ArrowFunctionExpression",
25+
"start": 18,
26+
"end": 49,
27+
"loc": {
28+
"start": {
29+
"line": 2,
30+
"column": 10
31+
},
32+
"end": {
33+
"line": 4,
34+
"column": 2
35+
}
36+
},
37+
"id": null,
38+
"expression": false,
39+
"generator": false,
40+
"async": false,
41+
"params": [],
42+
"body": {
43+
"type": "BlockStatement",
44+
"start": 24,
45+
"end": 49,
46+
"loc": {
47+
"start": {
48+
"line": 2,
49+
"column": 16
50+
},
51+
"end": {
52+
"line": 4,
53+
"column": 2
54+
}
55+
},
56+
"body": []
57+
},
58+
"trailingComments": [
59+
{
60+
"type": "Block",
61+
"value": " some comment ",
62+
"start": 28,
63+
"end": 46
64+
}
65+
]
66+
}
67+
}
68+
]
69+
}
70+
],
71+
"children": []
72+
}
73+
]
74+
}
75+
}

0 commit comments

Comments
 (0)