fix: handle empty else if block in legacy AST (#12397)

pull/12400/head
Lyu, Wei-Da 4 months ago committed by GitHub
parent 587bbe4b5f
commit 95422e22b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: handle empty else if block in legacy AST

@ -353,7 +353,11 @@ export function convert(source, ast) {
};
}
const start = node.elseif ? node.consequent.nodes[0].start : node.start;
const start = node.elseif
? node.consequent.nodes[0]?.start ??
source.lastIndexOf('{', /** @type {number} */ (node.end) - 1)
: node.start;
remove_surrounding_whitespace_nodes(node.consequent.nodes);
return {

@ -2,4 +2,4 @@
<p>x is greater than 10</p>
{:else if x < 5}
<p>x is less than 5</p>
{/if}
{:else if x === 1}{/if}

@ -2,12 +2,12 @@
"html": {
"type": "Fragment",
"start": 0,
"end": 89,
"end": 107,
"children": [
{
"type": "IfBlock",
"start": 0,
"end": 89,
"end": 107,
"expression": {
"type": "BinaryExpression",
"start": 5,
@ -78,12 +78,12 @@
"else": {
"type": "ElseBlock",
"start": 58,
"end": 84,
"end": 102,
"children": [
{
"type": "IfBlock",
"start": 58,
"end": 89,
"end": 107,
"expression": {
"type": "BinaryExpression",
"start": 52,
@ -151,6 +151,69 @@
]
}
],
"else": {
"type": "ElseBlock",
"start": 102,
"end": 102,
"children": [
{
"type": "IfBlock",
"start": 102,
"end": 107,
"expression": {
"type": "BinaryExpression",
"start": 94,
"end": 101,
"loc": {
"start": {
"line": 5,
"column": 10
},
"end": {
"line": 5,
"column": 17
}
},
"left": {
"type": "Identifier",
"start": 94,
"end": 95,
"loc": {
"start": {
"line": 5,
"column": 10
},
"end": {
"line": 5,
"column": 11
}
},
"name": "x"
},
"operator": "===",
"right": {
"type": "Literal",
"start": 100,
"end": 101,
"loc": {
"start": {
"line": 5,
"column": 16
},
"end": {
"line": 5,
"column": 17
}
},
"value": 1,
"raw": "1"
}
},
"children": [],
"elseif": true
}
]
},
"elseif": true
}
]

Loading…
Cancel
Save