Skip to content

Commit 95422e2

Browse files
authored
fix: handle empty else if block in legacy AST (#12397)
1 parent 587bbe4 commit 95422e2

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

.changeset/tall-dragons-switch.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: handle empty else if block in legacy AST

packages/svelte/src/compiler/legacy.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ export function convert(source, ast) {
353353
};
354354
}
355355

356-
const start = node.elseif ? node.consequent.nodes[0].start : node.start;
356+
const start = node.elseif
357+
? node.consequent.nodes[0]?.start ??
358+
source.lastIndexOf('{', /** @type {number} */ (node.end) - 1)
359+
: node.start;
360+
357361
remove_surrounding_whitespace_nodes(node.consequent.nodes);
358362

359363
return {

packages/svelte/tests/parser-legacy/samples/if-block-elseif/input.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
<p>x is greater than 10</p>
33
{:else if x < 5}
44
<p>x is less than 5</p>
5-
{/if}
5+
{:else if x === 1}{/if}

packages/svelte/tests/parser-legacy/samples/if-block-elseif/output.json

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"html": {
33
"type": "Fragment",
44
"start": 0,
5-
"end": 89,
5+
"end": 107,
66
"children": [
77
{
88
"type": "IfBlock",
99
"start": 0,
10-
"end": 89,
10+
"end": 107,
1111
"expression": {
1212
"type": "BinaryExpression",
1313
"start": 5,
@@ -78,12 +78,12 @@
7878
"else": {
7979
"type": "ElseBlock",
8080
"start": 58,
81-
"end": 84,
81+
"end": 102,
8282
"children": [
8383
{
8484
"type": "IfBlock",
8585
"start": 58,
86-
"end": 89,
86+
"end": 107,
8787
"expression": {
8888
"type": "BinaryExpression",
8989
"start": 52,
@@ -151,6 +151,69 @@
151151
]
152152
}
153153
],
154+
"else": {
155+
"type": "ElseBlock",
156+
"start": 102,
157+
"end": 102,
158+
"children": [
159+
{
160+
"type": "IfBlock",
161+
"start": 102,
162+
"end": 107,
163+
"expression": {
164+
"type": "BinaryExpression",
165+
"start": 94,
166+
"end": 101,
167+
"loc": {
168+
"start": {
169+
"line": 5,
170+
"column": 10
171+
},
172+
"end": {
173+
"line": 5,
174+
"column": 17
175+
}
176+
},
177+
"left": {
178+
"type": "Identifier",
179+
"start": 94,
180+
"end": 95,
181+
"loc": {
182+
"start": {
183+
"line": 5,
184+
"column": 10
185+
},
186+
"end": {
187+
"line": 5,
188+
"column": 11
189+
}
190+
},
191+
"name": "x"
192+
},
193+
"operator": "===",
194+
"right": {
195+
"type": "Literal",
196+
"start": 100,
197+
"end": 101,
198+
"loc": {
199+
"start": {
200+
"line": 5,
201+
"column": 16
202+
},
203+
"end": {
204+
"line": 5,
205+
"column": 17
206+
}
207+
},
208+
"value": 1,
209+
"raw": "1"
210+
}
211+
},
212+
"children": [],
213+
"elseif": true
214+
}
215+
]
216+
},
154217
"elseif": true
155218
}
156219
]

0 commit comments

Comments
 (0)