/
IfElseBlock.ts
41 lines (38 loc) · 1.57 KB
/
IfElseBlock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import MagicString from 'magic-string';
import { Node } from 'estree-walker';
import { withTrailingPropertyAccess } from '../utils/node-utils';
/**
* Transforms #if and :else if to a regular if control block.
*/
export function handleIf(str: MagicString, ifBlock: Node): void {
if (ifBlock.elseif) {
// {:else if expr} --> } else if(expr) {
const start = str.original.lastIndexOf('{', ifBlock.expression.start);
str.overwrite(start, ifBlock.expression.start, '} else if (');
} else {
// {#if expr} --> if (expr){
str.overwrite(ifBlock.start, ifBlock.expression.start, 'if(');
}
const expressionEnd = withTrailingPropertyAccess(str.original, ifBlock.expression.end);
const end = str.original.indexOf('}', expressionEnd);
str.overwrite(expressionEnd, end + 1, '){');
// {/if} -> }
const endif = str.original.lastIndexOf('{', ifBlock.end - 1);
str.overwrite(endif, ifBlock.end, '}');
}
/**
* {:else} ---> } else {
*/
export function handleElse(str: MagicString, elseBlock: Node, parent: Node): void {
if (parent.type !== 'IfBlock') {
// This is the else branch of an #each block which is handled elsewhere
return;
}
const elseEnd = str.original.lastIndexOf('}', elseBlock.start);
const elseword = str.original.lastIndexOf(':else', elseEnd);
const elseStart = str.original.lastIndexOf('{', elseword);
str.overwrite(elseStart, elseStart + 1, '}');
str.overwrite(elseEnd, elseEnd + 1, '{');
const colon = str.original.indexOf(':', elseword);
str.remove(colon, colon + 1);
}