Skip to content

Commit f2fd1b2

Browse files
committed
fix
1 parent 665cda4 commit f2fd1b2

File tree

6 files changed

+37
-139
lines changed

6 files changed

+37
-139
lines changed

src/parser/converts/block.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ import type { Context } from "../../context"
1313
import { convertChildren } from "./element"
1414
import { getWithLoc, indexOf, lastIndexOf } from "./common"
1515

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+
1640
/** Convert for IfBlock */
1741
export function convertIfBlock(
1842
node: SvAST.IfBlock,
@@ -22,7 +46,7 @@ export function convertIfBlock(
2246
// {#if expr} {:else} {/if}
2347
// {:else if expr} {/if}
2448
const nodeStart = node.elseif
25-
? ctx.code.lastIndexOf("{", node.start)
49+
? startBlockIndex(ctx.code, node.start - 1)
2650
: node.start
2751
const ifBlock: SvelteIfBlock = {
2852
type: "SvelteIfBlock",
@@ -49,7 +73,7 @@ export function convertIfBlock(
4973
return ifBlock
5074
}
5175

52-
const elseStart = ctx.code.lastIndexOf("{", node.else.start - 1)
76+
const elseStart = startBlockIndex(ctx.code, node.else.start - 1)
5377

5478
const elseBlock: SvelteElseBlock = {
5579
type: "SvelteElseBlock",
@@ -149,7 +173,7 @@ export function convertEachBlock(
149173
return eachBlock
150174
}
151175

152-
const elseStart = ctx.code.lastIndexOf("{", node.else.start)
176+
const elseStart = startBlockIndex(ctx.code, node.else.start - 1)
153177

154178
const elseBlock: SvelteElseBlock = {
155179
type: "SvelteElseBlock",

src/parser/converts/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export function indexOf(
1717
/** lastIndexOf */
1818
export function lastIndexOf(
1919
str: string,
20-
search: (c: string) => boolean,
20+
search: (c: string, index: number) => boolean,
2121
end: number,
2222
): number {
2323
for (let index = end; index >= 0; index--) {
2424
const c = str[index]
25-
if (search(c)) {
25+
if (search(c, index)) {
2626
return index
2727
}
2828
}

tests/fixtures/parser/ast/if-block0101-output.json renamed to tests/fixtures/parser/ast/if-block01-output.json

Lines changed: 8 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@
6767
}
6868
},
6969
"range": [
70-
37,
70+
16,
7171
49
7272
],
7373
"loc": {
7474
"start": {
7575
"line": 1,
76-
"column": 37
76+
"column": 16
7777
},
7878
"end": {
7979
"line": 1,
@@ -207,88 +207,16 @@
207207
}
208208
},
209209
{
210-
"type": "Punctuator",
211-
"value": ":",
210+
"type": "MustacheKeyword",
211+
"value": ":else",
212212
"range": [
213213
17,
214-
18
215-
],
216-
"loc": {
217-
"start": {
218-
"line": 1,
219-
"column": 17
220-
},
221-
"end": {
222-
"line": 1,
223-
"column": 18
224-
}
225-
}
226-
},
227-
{
228-
"type": "Identifier",
229-
"value": "e",
230-
"range": [
231-
18,
232-
19
233-
],
234-
"loc": {
235-
"start": {
236-
"line": 1,
237-
"column": 18
238-
},
239-
"end": {
240-
"line": 1,
241-
"column": 19
242-
}
243-
}
244-
},
245-
{
246-
"type": "Identifier",
247-
"value": "l",
248-
"range": [
249-
19,
250-
20
251-
],
252-
"loc": {
253-
"start": {
254-
"line": 1,
255-
"column": 19
256-
},
257-
"end": {
258-
"line": 1,
259-
"column": 20
260-
}
261-
}
262-
},
263-
{
264-
"type": "Identifier",
265-
"value": "s",
266-
"range": [
267-
20,
268-
21
269-
],
270-
"loc": {
271-
"start": {
272-
"line": 1,
273-
"column": 20
274-
},
275-
"end": {
276-
"line": 1,
277-
"column": 21
278-
}
279-
}
280-
},
281-
{
282-
"type": "Identifier",
283-
"value": "e",
284-
"range": [
285-
21,
286214
22
287215
],
288216
"loc": {
289217
"start": {
290218
"line": 1,
291-
"column": 21
219+
"column": 17
292220
},
293221
"end": {
294222
"line": 1,
@@ -297,34 +225,16 @@
297225
}
298226
},
299227
{
300-
"type": "Identifier",
301-
"value": "i",
228+
"type": "MustacheKeyword",
229+
"value": "if",
302230
"range": [
303231
23,
304-
24
305-
],
306-
"loc": {
307-
"start": {
308-
"line": 1,
309-
"column": 23
310-
},
311-
"end": {
312-
"line": 1,
313-
"column": 24
314-
}
315-
}
316-
},
317-
{
318-
"type": "Identifier",
319-
"value": "f",
320-
"range": [
321-
24,
322232
25
323233
],
324234
"loc": {
325235
"start": {
326236
"line": 1,
327-
"column": 24
237+
"column": 23
328238
},
329239
"end": {
330240
"line": 1,
@@ -404,24 +314,6 @@
404314
}
405315
}
406316
},
407-
{
408-
"type": "MustacheKeyword",
409-
"value": ":else",
410-
"range": [
411-
38,
412-
43
413-
],
414-
"loc": {
415-
"start": {
416-
"line": 1,
417-
"column": 38
418-
},
419-
"end": {
420-
"line": 1,
421-
"column": 43
422-
}
423-
}
424-
},
425317
{
426318
"type": "Punctuator",
427319
"value": "}",
@@ -476,24 +368,6 @@
476368
}
477369
}
478370
},
479-
{
480-
"type": "MustacheKeyword",
481-
"value": "if",
482-
"range": [
483-
46,
484-
48
485-
],
486-
"loc": {
487-
"start": {
488-
"line": 1,
489-
"column": 46
490-
},
491-
"end": {
492-
"line": 1,
493-
"column": 48
494-
}
495-
}
496-
},
497371
{
498372
"type": "Punctuator",
499373
"value": "}",

0 commit comments

Comments
 (0)