@@ -13,6 +13,30 @@ import type { Context } from "../../context"
13
13
import { convertChildren } from "./element"
14
14
import { getWithLoc , indexOf , lastIndexOf } from "./common"
15
15
16
+ /** Get start index of block */
17
+ function startBlockIndex ( code : string , endIndex : number ) : number {
18
+ return lastIndexOf (
19
+ code ,
20
+ ( c , index ) => {
21
+ if ( c !== "{" ) {
22
+ return false
23
+ }
24
+ for ( let next = index + 1 ; next < code . length ; next ++ ) {
25
+ const nextC = code [ next ]
26
+ if ( ! nextC . trim ( ) ) {
27
+ continue
28
+ }
29
+ return (
30
+ code . startsWith ( "#if" , next ) ||
31
+ code . startsWith ( ":else" , next )
32
+ )
33
+ }
34
+ return false
35
+ } ,
36
+ endIndex ,
37
+ )
38
+ }
39
+
16
40
/** Convert for IfBlock */
17
41
export function convertIfBlock (
18
42
node : SvAST . IfBlock ,
@@ -22,7 +46,7 @@ export function convertIfBlock(
22
46
// {#if expr} {:else} {/if}
23
47
// {:else if expr} {/if}
24
48
const nodeStart = node . elseif
25
- ? ctx . code . lastIndexOf ( "{" , node . start )
49
+ ? startBlockIndex ( ctx . code , node . start - 1 )
26
50
: node . start
27
51
const ifBlock : SvelteIfBlock = {
28
52
type : "SvelteIfBlock" ,
@@ -49,7 +73,7 @@ export function convertIfBlock(
49
73
return ifBlock
50
74
}
51
75
52
- const elseStart = ctx . code . lastIndexOf ( "{" , node . else . start )
76
+ const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 )
53
77
54
78
const elseBlock : SvelteElseBlock = {
55
79
type : "SvelteElseBlock" ,
@@ -149,7 +173,7 @@ export function convertEachBlock(
149
173
return eachBlock
150
174
}
151
175
152
- const elseStart = ctx . code . lastIndexOf ( "{" , node . else . start )
176
+ const elseStart = startBlockIndex ( ctx . code , node . else . start - 1 )
153
177
154
178
const elseBlock : SvelteElseBlock = {
155
179
type : "SvelteElseBlock" ,
0 commit comments