Skip to content

Commit 2c7758f

Browse files
committed
fix: correct start of {:else if} and {:else}
1 parent 95d07de commit 2c7758f

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
@@ -328,13 +328,13 @@ function open(parser) {
328328

329329
/** @param {import('../index.js').Parser} parser */
330330
function next(parser) {
331-
const start = parser.index - 1;
331+
const next_to_bracket = parser.index - 1;
332332

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

335335
if (block.type === 'IfBlock') {
336-
if (!parser.eat('else')) e.expected_token(start, '{:else} or {:else if}');
337-
if (parser.eat('if')) e.block_invalid_elseif(start);
336+
if (!parser.eat('else')) e.expected_token(next_to_bracket, '{:else} or {:else if}');
337+
if (parser.eat('if')) e.block_invalid_elseif(next_to_bracket);
338338

339339
parser.allow_whitespace();
340340

@@ -354,7 +354,7 @@ function next(parser) {
354354

355355
/** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */
356356
const child = parser.append({
357-
start: parser.index,
357+
start: next_to_bracket - 1,
358358
end: -1,
359359
type: 'IfBlock',
360360
elseif: true,
@@ -376,7 +376,7 @@ function next(parser) {
376376
}
377377

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

381381
parser.allow_whitespace();
382382
parser.eat('}', true);
@@ -392,7 +392,7 @@ function next(parser) {
392392
if (block.type === 'AwaitBlock') {
393393
if (parser.eat('then')) {
394394
if (block.then) {
395-
e.block_duplicate_clause(start, '{:then}');
395+
e.block_duplicate_clause(next_to_bracket, '{:then}');
396396
}
397397

398398
if (!parser.eat('}')) {
@@ -411,7 +411,7 @@ function next(parser) {
411411

412412
if (parser.eat('catch')) {
413413
if (block.catch) {
414-
e.block_duplicate_clause(start, '{:catch}');
414+
e.block_duplicate_clause(next_to_bracket, '{:catch}');
415415
}
416416

417417
if (!parser.eat('}')) {
@@ -428,10 +428,10 @@ function next(parser) {
428428
return;
429429
}
430430

431-
e.expected_token(start, '{:then ...} or {:catch ...}');
431+
e.expected_token(next_to_bracket, '{:then ...} or {:catch ...}');
432432
}
433433

434-
e.block_invalid_continuation_placement(start);
434+
e.block_invalid_continuation_placement(next_to_bracket);
435435
}
436436

437437
/** @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)