@@ -22,7 +22,11 @@ import { getWithLoc, indexOf, lastIndexOf } from "./common";
22
22
import type * as ESTree from "estree" ;
23
23
24
24
/** 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 {
26
30
return lastIndexOf (
27
31
code ,
28
32
( c , index ) => {
@@ -34,7 +38,7 @@ function startBlockIndex(code: string, endIndex: number): number {
34
38
if ( ! nextC . trim ( ) ) {
35
39
continue ;
36
40
}
37
- return code . startsWith ( "#if" , next ) || code . startsWith ( ":else" , next ) ;
41
+ return code . startsWith ( block , next ) ;
38
42
}
39
43
return false ;
40
44
} ,
@@ -62,9 +66,11 @@ export function convertIfBlock(
62
66
) : SvelteIfBlock {
63
67
// {#if expr} {:else} {/if}
64
68
// {: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
+ ) ;
68
74
const ifBlock : SvelteIfBlock = {
69
75
type : "SvelteIfBlock" ,
70
76
elseif : Boolean ( elseif ) ,
@@ -90,7 +96,7 @@ export function convertIfBlock(
90
96
return ifBlock ;
91
97
}
92
98
93
- const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 ) ;
99
+ const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 , ":else" ) ;
94
100
95
101
if ( node . else . children . length === 1 ) {
96
102
const c = node . else . children [ 0 ] ;
@@ -145,6 +151,7 @@ export function convertEachBlock(
145
151
ctx : Context ,
146
152
) : SvelteEachBlock {
147
153
// {#each expr as item, index (key)} {/each}
154
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#each" ) ;
148
155
const eachBlock : SvelteEachBlock = {
149
156
type : "SvelteEachBlock" ,
150
157
expression : null as any ,
@@ -154,7 +161,7 @@ export function convertEachBlock(
154
161
children : [ ] ,
155
162
else : null ,
156
163
parent,
157
- ...ctx . getConvertLocation ( node ) ,
164
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
158
165
} ;
159
166
160
167
let indexRange : null | { start : number ; end : number } = null ;
@@ -199,7 +206,7 @@ export function convertEachBlock(
199
206
return eachBlock ;
200
207
}
201
208
202
- const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 ) ;
209
+ const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 , ":else" ) ;
203
210
204
211
const elseBlock : SvelteElseBlockAlone = {
205
212
type : "SvelteElseBlock" ,
@@ -227,6 +234,7 @@ export function convertAwaitBlock(
227
234
parent : SvelteAwaitBlock [ "parent" ] ,
228
235
ctx : Context ,
229
236
) : SvelteAwaitBlock {
237
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#await" ) ;
230
238
const awaitBlock = {
231
239
type : "SvelteAwaitBlock" ,
232
240
expression : null as any ,
@@ -235,7 +243,7 @@ export function convertAwaitBlock(
235
243
then : null as any ,
236
244
catch : null as any ,
237
245
parent,
238
- ...ctx . getConvertLocation ( node ) ,
246
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
239
247
} as SvelteAwaitBlock ;
240
248
241
249
ctx . scriptLet . addExpression (
@@ -413,12 +421,13 @@ export function convertKeyBlock(
413
421
parent : SvelteKeyBlock [ "parent" ] ,
414
422
ctx : Context ,
415
423
) : SvelteKeyBlock {
424
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#key" ) ;
416
425
const keyBlock : SvelteKeyBlock = {
417
426
type : "SvelteKeyBlock" ,
418
427
expression : null as any ,
419
428
children : [ ] ,
420
429
parent,
421
- ...ctx . getConvertLocation ( node ) ,
430
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
422
431
} ;
423
432
424
433
ctx . scriptLet . addExpression ( node . expression , keyBlock , null , ( expression ) => {
@@ -441,13 +450,14 @@ export function convertSnippetBlock(
441
450
ctx : Context ,
442
451
) : SvelteSnippetBlock {
443
452
// {#snippet x(args)}...{/snippet}
453
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#snippet" ) ;
444
454
const snippetBlock : SvelteSnippetBlock = {
445
455
type : "SvelteSnippetBlock" ,
446
456
id : null as any ,
447
457
context : null as any ,
448
458
children : [ ] ,
449
459
parent,
450
- ...ctx . getConvertLocation ( node ) ,
460
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
451
461
} ;
452
462
453
463
const closeParenIndex = ctx . code . indexOf (
0 commit comments