Skip to content

fix: correct start of {:else if} and {:else} #12043

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

Merged
merged 6 commits into from
Jul 8, 2024
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/cold-teachers-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: correct start of `{:else if}` and `{:else}`
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,12 @@ export function convert(source, ast) {
};
}

const start = node.elseif ? node.consequent.nodes[0].start : node.start;
remove_surrounding_whitespace_nodes(node.consequent.nodes);

return {
type: 'IfBlock',
start: node.start,
start,
end: node.end,
expression: node.test,
children: node.consequent.nodes.map(
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function next(parser) {

/** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */
const child = parser.append({
start: parser.index,
start: start - 1,
end: -1,
type: 'IfBlock',
elseif: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#if foo}
<p>foo</p>
{:else}
<p>not foo</p>
{/if}
116 changes: 116 additions & 0 deletions packages/svelte/tests/parser-modern/samples/if-block-else/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"css": null,
"js": [],
"start": 0,
"end": 51,
"type": "Root",
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "IfBlock",
"elseif": false,
"start": 0,
"end": 51,
"test": {
"type": "Identifier",
"start": 5,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 8
}
},
"name": "foo"
},
"consequent": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 9,
"end": 11,
"raw": "\n\t",
"data": "\n\t"
},
{
"type": "RegularElement",
"start": 11,
"end": 21,
"name": "p",
"attributes": [],
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 14,
"end": 17,
"raw": "foo",
"data": "foo"
}
],
"transparent": true
}
},
{
"type": "Text",
"start": 21,
"end": 22,
"raw": "\n",
"data": "\n"
}
],
"transparent": false
},
"alternate": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 29,
"end": 31,
"raw": "\n\t",
"data": "\n\t"
},
{
"type": "RegularElement",
"start": 31,
"end": 45,
"name": "p",
"attributes": [],
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 34,
"end": 41,
"raw": "not foo",
"data": "not foo"
}
],
"transparent": true
}
},
{
"type": "Text",
"start": 45,
"end": 46,
"raw": "\n",
"data": "\n"
}
],
"transparent": false
}
}
],
"transparent": false
},
"options": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#if x > 10}
<p>x is greater than 10</p>
{:else if x < 5}
<p>x is less than 5</p>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"css": null,
"js": [],
"start": 0,
"end": 89,
"type": "Root",
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "IfBlock",
"elseif": false,
"start": 0,
"end": 89,
"test": {
"type": "BinaryExpression",
"start": 5,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 11
}
},
"left": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
}
},
"name": "x"
},
"operator": ">",
"right": {
"type": "Literal",
"start": 9,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 11
}
},
"value": 10,
"raw": "10"
}
},
"consequent": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 12,
"end": 14,
"raw": "\n\t",
"data": "\n\t"
},
{
"type": "RegularElement",
"start": 14,
"end": 41,
"name": "p",
"attributes": [],
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 17,
"end": 37,
"raw": "x is greater than 10",
"data": "x is greater than 10"
}
],
"transparent": true
}
},
{
"type": "Text",
"start": 41,
"end": 42,
"raw": "\n",
"data": "\n"
}
],
"transparent": false
},
"alternate": {
"type": "Fragment",
"nodes": [
{
"start": 42,
"end": 89,
"type": "IfBlock",
"elseif": true,
"test": {
"type": "BinaryExpression",
"start": 52,
"end": 57,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 15
}
},
"left": {
"type": "Identifier",
"start": 52,
"end": 53,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 11
}
},
"name": "x"
},
"operator": "<",
"right": {
"type": "Literal",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 15
}
},
"value": 5,
"raw": "5"
}
},
"consequent": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 58,
"end": 60,
"raw": "\n\t",
"data": "\n\t"
},
{
"type": "RegularElement",
"start": 60,
"end": 83,
"name": "p",
"attributes": [],
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 63,
"end": 79,
"raw": "x is less than 5",
"data": "x is less than 5"
}
],
"transparent": true
}
},
{
"type": "Text",
"start": 83,
"end": 84,
"raw": "\n",
"data": "\n"
}
],
"transparent": false
},
"alternate": null
}
],
"transparent": false
}
}
],
"transparent": false
},
"options": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{#if foo}bar{/if}
Loading
Loading