Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: correctly parse leading comments in function binding #15020

Merged
merged 2 commits into from
Jan 15, 2025
Merged
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/violet-apes-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly parse leading comments in function binding
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export default function read_expression(parser, opening_token, disallow_loose) {

let num_parens = 0;

if (node.leadingComments !== undefined && node.leadingComments.length > 0) {
parser.index = node.leadingComments.at(-1).end;
}

for (let i = parser.index; i < /** @type {number} */ (node.start); i += 1) {
if (parser.template[i] === '(') num_parens += 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,19 @@ export function BindDirective(node, context) {
}

let i = /** @type {number} */ (node.expression.start);
let leading_comments_start = /**@type {any}*/ (node.expression.leadingComments?.at(0))?.start;
let leading_comments_end = /**@type {any}*/ (node.expression.leadingComments?.at(-1))?.end;
while (context.state.analysis.source[--i] !== '{') {
if (context.state.analysis.source[i] === '(') {
if (
context.state.analysis.source[i] === '(' &&
// if the parenthesis is in a leading comment we don't need to throw the error
!(
leading_comments_start &&
leading_comments_end &&
i <= leading_comments_end &&
i >= leading_comments_start
)
) {
e.bind_invalid_parens(node, node.name);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let value = '';
</script>

<input bind:value={
/** ( */
() => value,
(v) => value = v.toLowerCase()
}
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
{
"css": null,
"js": [],
"start": 37,
"end": 117,
"type": "Root",
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 35,
"end": 37,
"raw": "\n\n",
"data": "\n\n"
},
{
"type": "RegularElement",
"start": 37,
"end": 117,
"name": "input",
"attributes": [
{
"start": 44,
"end": 114,
"type": "BindDirective",
"name": "value",
"expression": {
"type": "SequenceExpression",
"start": 68,
"end": 112,
"loc": {
"start": {
"line": 7,
"column": 1
},
"end": {
"line": 8,
"column": 31
}
},
"expressions": [
{
"type": "ArrowFunctionExpression",
"start": 68,
"end": 79,
"loc": {
"start": {
"line": 7,
"column": 1
},
"end": {
"line": 7,
"column": 12
}
},
"id": null,
"expression": true,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "Identifier",
"start": 74,
"end": 79,
"loc": {
"start": {
"line": 7,
"column": 7
},
"end": {
"line": 7,
"column": 12
}
},
"name": "value"
}
},
{
"type": "ArrowFunctionExpression",
"start": 82,
"end": 112,
"loc": {
"start": {
"line": 8,
"column": 1
},
"end": {
"line": 8,
"column": 31
}
},
"id": null,
"expression": true,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 83,
"end": 84,
"loc": {
"start": {
"line": 8,
"column": 2
},
"end": {
"line": 8,
"column": 3
}
},
"name": "v"
}
],
"body": {
"type": "AssignmentExpression",
"start": 89,
"end": 112,
"loc": {
"start": {
"line": 8,
"column": 8
},
"end": {
"line": 8,
"column": 31
}
},
"operator": "=",
"left": {
"type": "Identifier",
"start": 89,
"end": 94,
"loc": {
"start": {
"line": 8,
"column": 8
},
"end": {
"line": 8,
"column": 13
}
},
"name": "value"
},
"right": {
"type": "CallExpression",
"start": 97,
"end": 112,
"loc": {
"start": {
"line": 8,
"column": 16
},
"end": {
"line": 8,
"column": 31
}
},
"callee": {
"type": "MemberExpression",
"start": 97,
"end": 110,
"loc": {
"start": {
"line": 8,
"column": 16
},
"end": {
"line": 8,
"column": 29
}
},
"object": {
"type": "Identifier",
"start": 97,
"end": 98,
"loc": {
"start": {
"line": 8,
"column": 16
},
"end": {
"line": 8,
"column": 17
}
},
"name": "v"
},
"property": {
"type": "Identifier",
"start": 99,
"end": 110,
"loc": {
"start": {
"line": 8,
"column": 18
},
"end": {
"line": 8,
"column": 29
}
},
"name": "toLowerCase"
},
"computed": false,
"optional": false
},
"arguments": [],
"optional": false
}
}
}
],
"leadingComments": [
{
"type": "Block",
"value": "* ( ",
"start": 58,
"end": 66
}
]
},
"modifiers": []
}
],
"fragment": {
"type": "Fragment",
"nodes": []
}
}
]
},
"options": null,
"instance": {
"type": "Script",
"start": 0,
"end": 35,
"context": "default",
"content": {
"type": "Program",
"start": 8,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 0
}
},
"body": [
{
"type": "VariableDeclaration",
"start": 10,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 16
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 14,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 15
}
},
"id": {
"type": "Identifier",
"start": 14,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 10
}
},
"name": "value"
},
"init": {
"type": "Literal",
"start": 22,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 13
},
"end": {
"line": 2,
"column": 15
}
},
"value": "",
"raw": "''"
}
}
],
"kind": "let"
}
],
"sourceType": "module"
},
"attributes": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let value = '';
</script>

<input bind:value={
/** ( */
() => value,
(v) => value = v.toLowerCase()
}
/>
Loading