@@ -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,15 @@ export function convertIfBlock(
90
96
return ifBlock ;
91
97
}
92
98
93
- const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 ) ;
99
+ let baseStart = node . else . start ;
100
+ if ( node . else . children . length === 1 ) {
101
+ const c = node . else . children [ 0 ] ;
102
+ if ( c . type === "IfBlock" && c . elseif ) {
103
+ baseStart = Math . min ( baseStart , c . start , getWithLoc ( c . expression ) . start ) ;
104
+ }
105
+ }
106
+
107
+ const elseStart = startBlockIndex ( ctx . code , baseStart - 1 , ":else" ) ;
94
108
95
109
if ( node . else . children . length === 1 ) {
96
110
const c = node . else . children [ 0 ] ;
@@ -145,6 +159,7 @@ export function convertEachBlock(
145
159
ctx : Context ,
146
160
) : SvelteEachBlock {
147
161
// {#each expr as item, index (key)} {/each}
162
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#each" ) ;
148
163
const eachBlock : SvelteEachBlock = {
149
164
type : "SvelteEachBlock" ,
150
165
expression : null as any ,
@@ -154,7 +169,7 @@ export function convertEachBlock(
154
169
children : [ ] ,
155
170
else : null ,
156
171
parent,
157
- ...ctx . getConvertLocation ( node ) ,
172
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
158
173
} ;
159
174
160
175
let indexRange : null | { start : number ; end : number } = null ;
@@ -199,7 +214,7 @@ export function convertEachBlock(
199
214
return eachBlock ;
200
215
}
201
216
202
- const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 ) ;
217
+ const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 , ":else" ) ;
203
218
204
219
const elseBlock : SvelteElseBlockAlone = {
205
220
type : "SvelteElseBlock" ,
@@ -227,6 +242,7 @@ export function convertAwaitBlock(
227
242
parent : SvelteAwaitBlock [ "parent" ] ,
228
243
ctx : Context ,
229
244
) : SvelteAwaitBlock {
245
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#await" ) ;
230
246
const awaitBlock = {
231
247
type : "SvelteAwaitBlock" ,
232
248
expression : null as any ,
@@ -235,7 +251,7 @@ export function convertAwaitBlock(
235
251
then : null as any ,
236
252
catch : null as any ,
237
253
parent,
238
- ...ctx . getConvertLocation ( node ) ,
254
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
239
255
} as SvelteAwaitBlock ;
240
256
241
257
ctx . scriptLet . addExpression (
@@ -413,12 +429,13 @@ export function convertKeyBlock(
413
429
parent : SvelteKeyBlock [ "parent" ] ,
414
430
ctx : Context ,
415
431
) : SvelteKeyBlock {
432
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#key" ) ;
416
433
const keyBlock : SvelteKeyBlock = {
417
434
type : "SvelteKeyBlock" ,
418
435
expression : null as any ,
419
436
children : [ ] ,
420
437
parent,
421
- ...ctx . getConvertLocation ( node ) ,
438
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
422
439
} ;
423
440
424
441
ctx . scriptLet . addExpression ( node . expression , keyBlock , null , ( expression ) => {
@@ -441,13 +458,14 @@ export function convertSnippetBlock(
441
458
ctx : Context ,
442
459
) : SvelteSnippetBlock {
443
460
// {#snippet x(args)}...{/snippet}
461
+ const nodeStart = startBlockIndex ( ctx . code , node . start , "#snippet" ) ;
444
462
const snippetBlock : SvelteSnippetBlock = {
445
463
type : "SvelteSnippetBlock" ,
446
464
id : null as any ,
447
465
context : null as any ,
448
466
children : [ ] ,
449
467
parent,
450
- ...ctx . getConvertLocation ( node ) ,
468
+ ...ctx . getConvertLocation ( { start : nodeStart , end : node . end } ) ,
451
469
} ;
452
470
453
471
const closeParenIndex = ctx . code . indexOf (
0 commit comments