Skip to content

Commit 67bf7a8

Browse files
authored
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.
1 parent 2ffb55c commit 67bf7a8

File tree

9 files changed

+396
-2
lines changed

9 files changed

+396
-2
lines changed

.changeset/cold-teachers-turn.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: correct start of `{:else if}` and `{:else}`

packages/svelte/src/compiler/legacy.js

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

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

358359
return {
359360
type: 'IfBlock',
360-
start: node.start,
361+
start,
361362
end: node.end,
362363
expression: node.test,
363364
children: node.consequent.nodes.map(

packages/svelte/src/compiler/phases/1-parse/state/tag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ function next(parser) {
345345

346346
/** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */
347347
const child = parser.append({
348-
start: parser.index,
348+
start: start - 1,
349349
end: -1,
350350
type: 'IfBlock',
351351
elseif: true,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{#if foo}
2+
<p>foo</p>
3+
{:else}
4+
<p>not foo</p>
5+
{/if}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"css": null,
3+
"js": [],
4+
"start": 0,
5+
"end": 51,
6+
"type": "Root",
7+
"fragment": {
8+
"type": "Fragment",
9+
"nodes": [
10+
{
11+
"type": "IfBlock",
12+
"elseif": false,
13+
"start": 0,
14+
"end": 51,
15+
"test": {
16+
"type": "Identifier",
17+
"start": 5,
18+
"end": 8,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 5
23+
},
24+
"end": {
25+
"line": 1,
26+
"column": 8
27+
}
28+
},
29+
"name": "foo"
30+
},
31+
"consequent": {
32+
"type": "Fragment",
33+
"nodes": [
34+
{
35+
"type": "Text",
36+
"start": 9,
37+
"end": 11,
38+
"raw": "\n\t",
39+
"data": "\n\t"
40+
},
41+
{
42+
"type": "RegularElement",
43+
"start": 11,
44+
"end": 21,
45+
"name": "p",
46+
"attributes": [],
47+
"fragment": {
48+
"type": "Fragment",
49+
"nodes": [
50+
{
51+
"type": "Text",
52+
"start": 14,
53+
"end": 17,
54+
"raw": "foo",
55+
"data": "foo"
56+
}
57+
],
58+
"transparent": true
59+
}
60+
},
61+
{
62+
"type": "Text",
63+
"start": 21,
64+
"end": 22,
65+
"raw": "\n",
66+
"data": "\n"
67+
}
68+
],
69+
"transparent": false
70+
},
71+
"alternate": {
72+
"type": "Fragment",
73+
"nodes": [
74+
{
75+
"type": "Text",
76+
"start": 29,
77+
"end": 31,
78+
"raw": "\n\t",
79+
"data": "\n\t"
80+
},
81+
{
82+
"type": "RegularElement",
83+
"start": 31,
84+
"end": 45,
85+
"name": "p",
86+
"attributes": [],
87+
"fragment": {
88+
"type": "Fragment",
89+
"nodes": [
90+
{
91+
"type": "Text",
92+
"start": 34,
93+
"end": 41,
94+
"raw": "not foo",
95+
"data": "not foo"
96+
}
97+
],
98+
"transparent": true
99+
}
100+
},
101+
{
102+
"type": "Text",
103+
"start": 45,
104+
"end": 46,
105+
"raw": "\n",
106+
"data": "\n"
107+
}
108+
],
109+
"transparent": false
110+
}
111+
}
112+
],
113+
"transparent": false
114+
},
115+
"options": null
116+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{#if x > 10}
2+
<p>x is greater than 10</p>
3+
{:else if x < 5}
4+
<p>x is less than 5</p>
5+
{/if}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
{
2+
"css": null,
3+
"js": [],
4+
"start": 0,
5+
"end": 89,
6+
"type": "Root",
7+
"fragment": {
8+
"type": "Fragment",
9+
"nodes": [
10+
{
11+
"type": "IfBlock",
12+
"elseif": false,
13+
"start": 0,
14+
"end": 89,
15+
"test": {
16+
"type": "BinaryExpression",
17+
"start": 5,
18+
"end": 11,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 5
23+
},
24+
"end": {
25+
"line": 1,
26+
"column": 11
27+
}
28+
},
29+
"left": {
30+
"type": "Identifier",
31+
"start": 5,
32+
"end": 6,
33+
"loc": {
34+
"start": {
35+
"line": 1,
36+
"column": 5
37+
},
38+
"end": {
39+
"line": 1,
40+
"column": 6
41+
}
42+
},
43+
"name": "x"
44+
},
45+
"operator": ">",
46+
"right": {
47+
"type": "Literal",
48+
"start": 9,
49+
"end": 11,
50+
"loc": {
51+
"start": {
52+
"line": 1,
53+
"column": 9
54+
},
55+
"end": {
56+
"line": 1,
57+
"column": 11
58+
}
59+
},
60+
"value": 10,
61+
"raw": "10"
62+
}
63+
},
64+
"consequent": {
65+
"type": "Fragment",
66+
"nodes": [
67+
{
68+
"type": "Text",
69+
"start": 12,
70+
"end": 14,
71+
"raw": "\n\t",
72+
"data": "\n\t"
73+
},
74+
{
75+
"type": "RegularElement",
76+
"start": 14,
77+
"end": 41,
78+
"name": "p",
79+
"attributes": [],
80+
"fragment": {
81+
"type": "Fragment",
82+
"nodes": [
83+
{
84+
"type": "Text",
85+
"start": 17,
86+
"end": 37,
87+
"raw": "x is greater than 10",
88+
"data": "x is greater than 10"
89+
}
90+
],
91+
"transparent": true
92+
}
93+
},
94+
{
95+
"type": "Text",
96+
"start": 41,
97+
"end": 42,
98+
"raw": "\n",
99+
"data": "\n"
100+
}
101+
],
102+
"transparent": false
103+
},
104+
"alternate": {
105+
"type": "Fragment",
106+
"nodes": [
107+
{
108+
"start": 42,
109+
"end": 89,
110+
"type": "IfBlock",
111+
"elseif": true,
112+
"test": {
113+
"type": "BinaryExpression",
114+
"start": 52,
115+
"end": 57,
116+
"loc": {
117+
"start": {
118+
"line": 3,
119+
"column": 10
120+
},
121+
"end": {
122+
"line": 3,
123+
"column": 15
124+
}
125+
},
126+
"left": {
127+
"type": "Identifier",
128+
"start": 52,
129+
"end": 53,
130+
"loc": {
131+
"start": {
132+
"line": 3,
133+
"column": 10
134+
},
135+
"end": {
136+
"line": 3,
137+
"column": 11
138+
}
139+
},
140+
"name": "x"
141+
},
142+
"operator": "<",
143+
"right": {
144+
"type": "Literal",
145+
"start": 56,
146+
"end": 57,
147+
"loc": {
148+
"start": {
149+
"line": 3,
150+
"column": 14
151+
},
152+
"end": {
153+
"line": 3,
154+
"column": 15
155+
}
156+
},
157+
"value": 5,
158+
"raw": "5"
159+
}
160+
},
161+
"consequent": {
162+
"type": "Fragment",
163+
"nodes": [
164+
{
165+
"type": "Text",
166+
"start": 58,
167+
"end": 60,
168+
"raw": "\n\t",
169+
"data": "\n\t"
170+
},
171+
{
172+
"type": "RegularElement",
173+
"start": 60,
174+
"end": 83,
175+
"name": "p",
176+
"attributes": [],
177+
"fragment": {
178+
"type": "Fragment",
179+
"nodes": [
180+
{
181+
"type": "Text",
182+
"start": 63,
183+
"end": 79,
184+
"raw": "x is less than 5",
185+
"data": "x is less than 5"
186+
}
187+
],
188+
"transparent": true
189+
}
190+
},
191+
{
192+
"type": "Text",
193+
"start": 83,
194+
"end": 84,
195+
"raw": "\n",
196+
"data": "\n"
197+
}
198+
],
199+
"transparent": false
200+
},
201+
"alternate": null
202+
}
203+
],
204+
"transparent": false
205+
}
206+
}
207+
],
208+
"transparent": false
209+
},
210+
"options": null
211+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{#if foo}bar{/if}

0 commit comments

Comments
 (0)