Skip to content

Commit eb98160

Browse files
committed
fix
1 parent 9dc7439 commit eb98160

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/parser/converts/block.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { getWithLoc, indexOf, lastIndexOf } from "./common";
2222
import type * as ESTree from "estree";
2323

2424
/** Get start index of block */
25-
function startBlockIndex(code: string, endIndex: number): number {
25+
function startBlockIndex(
26+
code: string,
27+
endIndex: number,
28+
block: string,
29+
): number {
2630
return lastIndexOf(
2731
code,
2832
(c, index) => {
@@ -34,7 +38,7 @@ function startBlockIndex(code: string, endIndex: number): number {
3438
if (!nextC.trim()) {
3539
continue;
3640
}
37-
return code.startsWith("#if", next) || code.startsWith(":else", next);
41+
return code.startsWith(block, next);
3842
}
3943
return false;
4044
},
@@ -62,9 +66,11 @@ export function convertIfBlock(
6266
): SvelteIfBlock {
6367
// {#if expr} {:else} {/if}
6468
// {:else if expr} {/if}
65-
const nodeStart = elseif
66-
? startBlockIndex(ctx.code, node.start - 1)
67-
: node.start;
69+
const nodeStart = startBlockIndex(
70+
ctx.code,
71+
elseif ? node.start - 1 : node.start,
72+
elseif ? ":else" : "#if",
73+
);
6874
const ifBlock: SvelteIfBlock = {
6975
type: "SvelteIfBlock",
7076
elseif: Boolean(elseif),
@@ -90,7 +96,7 @@ export function convertIfBlock(
9096
return ifBlock;
9197
}
9298

93-
const elseStart = startBlockIndex(ctx.code, node.else.start - 1);
99+
const elseStart = startBlockIndex(ctx.code, node.else.start - 1, ":else");
94100

95101
if (node.else.children.length === 1) {
96102
const c = node.else.children[0];
@@ -145,6 +151,7 @@ export function convertEachBlock(
145151
ctx: Context,
146152
): SvelteEachBlock {
147153
// {#each expr as item, index (key)} {/each}
154+
const nodeStart = startBlockIndex(ctx.code, node.start, "#each");
148155
const eachBlock: SvelteEachBlock = {
149156
type: "SvelteEachBlock",
150157
expression: null as any,
@@ -154,7 +161,7 @@ export function convertEachBlock(
154161
children: [],
155162
else: null,
156163
parent,
157-
...ctx.getConvertLocation(node),
164+
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
158165
};
159166

160167
let indexRange: null | { start: number; end: number } = null;
@@ -199,7 +206,7 @@ export function convertEachBlock(
199206
return eachBlock;
200207
}
201208

202-
const elseStart = startBlockIndex(ctx.code, node.else.start - 1);
209+
const elseStart = startBlockIndex(ctx.code, node.else.start - 1, ":else");
203210

204211
const elseBlock: SvelteElseBlockAlone = {
205212
type: "SvelteElseBlock",
@@ -227,6 +234,7 @@ export function convertAwaitBlock(
227234
parent: SvelteAwaitBlock["parent"],
228235
ctx: Context,
229236
): SvelteAwaitBlock {
237+
const nodeStart = startBlockIndex(ctx.code, node.start, "#await");
230238
const awaitBlock = {
231239
type: "SvelteAwaitBlock",
232240
expression: null as any,
@@ -235,7 +243,7 @@ export function convertAwaitBlock(
235243
then: null as any,
236244
catch: null as any,
237245
parent,
238-
...ctx.getConvertLocation(node),
246+
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
239247
} as SvelteAwaitBlock;
240248

241249
ctx.scriptLet.addExpression(
@@ -413,12 +421,13 @@ export function convertKeyBlock(
413421
parent: SvelteKeyBlock["parent"],
414422
ctx: Context,
415423
): SvelteKeyBlock {
424+
const nodeStart = startBlockIndex(ctx.code, node.start, "#key");
416425
const keyBlock: SvelteKeyBlock = {
417426
type: "SvelteKeyBlock",
418427
expression: null as any,
419428
children: [],
420429
parent,
421-
...ctx.getConvertLocation(node),
430+
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
422431
};
423432

424433
ctx.scriptLet.addExpression(node.expression, keyBlock, null, (expression) => {
@@ -441,13 +450,14 @@ export function convertSnippetBlock(
441450
ctx: Context,
442451
): SvelteSnippetBlock {
443452
// {#snippet x(args)}...{/snippet}
453+
const nodeStart = startBlockIndex(ctx.code, node.start, "#snippet");
444454
const snippetBlock: SvelteSnippetBlock = {
445455
type: "SvelteSnippetBlock",
446456
id: null as any,
447457
context: null as any,
448458
children: [],
449459
parent,
450-
...ctx.getConvertLocation(node),
460+
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
451461
};
452462

453463
const closeParenIndex = ctx.code.indexOf(

0 commit comments

Comments
 (0)