From 95422e22b6b727c6ed13f29f2c6051c7ca0bb549 Mon Sep 17 00:00:00 2001 From: "Lyu, Wei-Da" <36730922+jasonlyu123@users.noreply.github.com> Date: Thu, 11 Jul 2024 10:38:19 +0800 Subject: [PATCH] fix: handle empty else if block in legacy AST (#12397) --- .changeset/tall-dragons-switch.md | 5 ++ packages/svelte/src/compiler/legacy.js | 6 +- .../samples/if-block-elseif/input.svelte | 2 +- .../samples/if-block-elseif/output.json | 71 +++++++++++++++++-- 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 .changeset/tall-dragons-switch.md diff --git a/.changeset/tall-dragons-switch.md b/.changeset/tall-dragons-switch.md new file mode 100644 index 0000000000..9691b72569 --- /dev/null +++ b/.changeset/tall-dragons-switch.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: handle empty else if block in legacy AST diff --git a/packages/svelte/src/compiler/legacy.js b/packages/svelte/src/compiler/legacy.js index da0ba058d3..1525522d98 100644 --- a/packages/svelte/src/compiler/legacy.js +++ b/packages/svelte/src/compiler/legacy.js @@ -353,7 +353,11 @@ export function convert(source, ast) { }; } - const start = node.elseif ? node.consequent.nodes[0].start : node.start; + const start = node.elseif + ? node.consequent.nodes[0]?.start ?? + source.lastIndexOf('{', /** @type {number} */ (node.end) - 1) + : node.start; + remove_surrounding_whitespace_nodes(node.consequent.nodes); return { diff --git a/packages/svelte/tests/parser-legacy/samples/if-block-elseif/input.svelte b/packages/svelte/tests/parser-legacy/samples/if-block-elseif/input.svelte index 2566aef258..e809fdf2db 100644 --- a/packages/svelte/tests/parser-legacy/samples/if-block-elseif/input.svelte +++ b/packages/svelte/tests/parser-legacy/samples/if-block-elseif/input.svelte @@ -2,4 +2,4 @@
x is greater than 10
{:else if x < 5}x is less than 5
-{/if} +{:else if x === 1}{/if} 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 3e6953ab0b..f2af3d5a4b 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 @@ -2,12 +2,12 @@ "html": { "type": "Fragment", "start": 0, - "end": 89, + "end": 107, "children": [ { "type": "IfBlock", "start": 0, - "end": 89, + "end": 107, "expression": { "type": "BinaryExpression", "start": 5, @@ -78,12 +78,12 @@ "else": { "type": "ElseBlock", "start": 58, - "end": 84, + "end": 102, "children": [ { "type": "IfBlock", "start": 58, - "end": 89, + "end": 107, "expression": { "type": "BinaryExpression", "start": 52, @@ -151,6 +151,69 @@ ] } ], + "else": { + "type": "ElseBlock", + "start": 102, + "end": 102, + "children": [ + { + "type": "IfBlock", + "start": 102, + "end": 107, + "expression": { + "type": "BinaryExpression", + "start": 94, + "end": 101, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 17 + } + }, + "left": { + "type": "Identifier", + "start": 94, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 11 + } + }, + "name": "x" + }, + "operator": "===", + "right": { + "type": "Literal", + "start": 100, + "end": 101, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 17 + } + }, + "value": 1, + "raw": "1" + } + }, + "children": [], + "elseif": true + } + ] + }, "elseif": true } ]