From 2c7758f41a461d8684d534ac795ac5d611ffdcfe Mon Sep 17 00:00:00 2001 From: baseballyama Date: Sun, 16 Jun 2024 16:10:30 +0900 Subject: [PATCH] fix: correct start of `{:else if}` and `{:else}` --- .changeset/cold-teachers-turn.md | 5 +++++ .../src/compiler/phases/1-parse/state/tag.js | 18 +++++++++--------- .../samples/if-block-elseif/output.json | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 .changeset/cold-teachers-turn.md diff --git a/.changeset/cold-teachers-turn.md b/.changeset/cold-teachers-turn.md new file mode 100644 index 000000000..e86ffbcdf --- /dev/null +++ b/.changeset/cold-teachers-turn.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: correct start of `{:else if}` and `{:else}` diff --git a/packages/svelte/src/compiler/phases/1-parse/state/tag.js b/packages/svelte/src/compiler/phases/1-parse/state/tag.js index e11346942..7b1ea842c 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/tag.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/tag.js @@ -328,13 +328,13 @@ function open(parser) { /** @param {import('../index.js').Parser} parser */ function next(parser) { - const start = parser.index - 1; + const next_to_bracket = parser.index - 1; const block = parser.current(); // TODO type should not be TemplateNode, that's much too broad if (block.type === 'IfBlock') { - if (!parser.eat('else')) e.expected_token(start, '{:else} or {:else if}'); - if (parser.eat('if')) e.block_invalid_elseif(start); + if (!parser.eat('else')) e.expected_token(next_to_bracket, '{:else} or {:else if}'); + if (parser.eat('if')) e.block_invalid_elseif(next_to_bracket); parser.allow_whitespace(); @@ -354,7 +354,7 @@ function next(parser) { /** @type {ReturnType>} */ const child = parser.append({ - start: parser.index, + start: next_to_bracket - 1, end: -1, type: 'IfBlock', elseif: true, @@ -376,7 +376,7 @@ function next(parser) { } if (block.type === 'EachBlock') { - if (!parser.eat('else')) e.expected_token(start, '{:else}'); + if (!parser.eat('else')) e.expected_token(next_to_bracket, '{:else}'); parser.allow_whitespace(); parser.eat('}', true); @@ -392,7 +392,7 @@ function next(parser) { if (block.type === 'AwaitBlock') { if (parser.eat('then')) { if (block.then) { - e.block_duplicate_clause(start, '{:then}'); + e.block_duplicate_clause(next_to_bracket, '{:then}'); } if (!parser.eat('}')) { @@ -411,7 +411,7 @@ function next(parser) { if (parser.eat('catch')) { if (block.catch) { - e.block_duplicate_clause(start, '{:catch}'); + e.block_duplicate_clause(next_to_bracket, '{:catch}'); } if (!parser.eat('}')) { @@ -428,10 +428,10 @@ function next(parser) { return; } - e.expected_token(start, '{:then ...} or {:catch ...}'); + e.expected_token(next_to_bracket, '{:then ...} or {:catch ...}'); } - e.block_invalid_continuation_placement(start); + e.block_invalid_continuation_placement(next_to_bracket); } /** @param {import('../index.js').Parser} parser */ diff --git a/packages/svelte/tests/parser-legacy/samples/if-block-elseif/output.json b/packages/svelte/tests/parser-legacy/samples/if-block-elseif/output.json index 3e6953ab0..40411d677 100644 --- a/packages/svelte/tests/parser-legacy/samples/if-block-elseif/output.json +++ b/packages/svelte/tests/parser-legacy/samples/if-block-elseif/output.json @@ -82,7 +82,7 @@ "children": [ { "type": "IfBlock", - "start": 58, + "start": 42, "end": 89, "expression": { "type": "BinaryExpression",