Skip to content

Commit fc3ee2f

Browse files
committed
fix: correct start of {:else if} and {:else}
1 parent c42bb04 commit fc3ee2f

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
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/phases/1-parse/state/tag.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,13 @@ function open(parser) {
319319

320320
/** @param {import('../index.js').Parser} parser */
321321
function next(parser) {
322-
const start = parser.index - 1;
322+
const next_to_bracket = parser.index - 1;
323323

324324
const block = parser.current(); // TODO type should not be TemplateNode, that's much too broad
325325

326326
if (block.type === 'IfBlock') {
327-
if (!parser.eat('else')) e.expected_token(start, '{:else} or {:else if}');
328-
if (parser.eat('if')) e.block_invalid_elseif(start);
327+
if (!parser.eat('else')) e.expected_token(next_to_bracket, '{:else} or {:else if}');
328+
if (parser.eat('if')) e.block_invalid_elseif(next_to_bracket);
329329

330330
parser.allow_whitespace();
331331

@@ -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: next_to_bracket - 1,
349349
end: -1,
350350
type: 'IfBlock',
351351
elseif: true,
@@ -367,7 +367,7 @@ function next(parser) {
367367
}
368368

369369
if (block.type === 'EachBlock') {
370-
if (!parser.eat('else')) e.expected_token(start, '{:else}');
370+
if (!parser.eat('else')) e.expected_token(next_to_bracket, '{:else}');
371371

372372
parser.allow_whitespace();
373373
parser.eat('}', true);
@@ -383,7 +383,7 @@ function next(parser) {
383383
if (block.type === 'AwaitBlock') {
384384
if (parser.eat('then')) {
385385
if (block.then) {
386-
e.block_duplicate_clause(start, '{:then}');
386+
e.block_duplicate_clause(next_to_bracket, '{:then}');
387387
}
388388

389389
if (!parser.eat('}')) {
@@ -402,7 +402,7 @@ function next(parser) {
402402

403403
if (parser.eat('catch')) {
404404
if (block.catch) {
405-
e.block_duplicate_clause(start, '{:catch}');
405+
e.block_duplicate_clause(next_to_bracket, '{:catch}');
406406
}
407407

408408
if (!parser.eat('}')) {
@@ -419,10 +419,10 @@ function next(parser) {
419419
return;
420420
}
421421

422-
e.expected_token(start, '{:then ...} or {:catch ...}');
422+
e.expected_token(next_to_bracket, '{:then ...} or {:catch ...}');
423423
}
424424

425-
e.block_invalid_continuation_placement(start);
425+
e.block_invalid_continuation_placement(next_to_bracket);
426426
}
427427

428428
/** @param {import('../index.js').Parser} parser */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"children": [
8383
{
8484
"type": "IfBlock",
85-
"start": 58,
85+
"start": 42,
8686
"end": 89,
8787
"expression": {
8888
"type": "BinaryExpression",

0 commit comments

Comments
 (0)