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

The modern AST is an opportunity to tweak things. In the old AST, the start of else (if) branches was the content, now it's the opening bracket, which makes more sense.
pull/12342/head
Yuichiro Yamashita 4 months ago committed by GitHub
parent 2ffb55ce7d
commit 67bf7a8067
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: correct start of `{:else if}` and `{:else}`

@ -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); remove_surrounding_whitespace_nodes(node.consequent.nodes);
return { return {
type: 'IfBlock', type: 'IfBlock',
start: node.start, start,
end: node.end, end: node.end,
expression: node.test, expression: node.test,
children: node.consequent.nodes.map( children: node.consequent.nodes.map(

@ -345,7 +345,7 @@ function next(parser) {
/** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */ /** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */
const child = parser.append({ const child = parser.append({
start: parser.index, start: start - 1,
end: -1, end: -1,
type: 'IfBlock', type: 'IfBlock',
elseif: true, elseif: true,

@ -0,0 +1,5 @@
{#if foo}
<p>foo</p>
{:else}
<p>not foo</p>
{/if}

@ -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
}

@ -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}

@ -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
}

@ -0,0 +1,50 @@
{
"css": null,
"js": [],
"start": 0,
"end": 17,
"type": "Root",
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "IfBlock",
"elseif": false,
"start": 0,
"end": 17,
"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": 12,
"raw": "bar",
"data": "bar"
}
],
"transparent": false
},
"alternate": null
}
],
"transparent": false
},
"options": null
}
Loading…
Cancel
Save