-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if.ts
187 lines (175 loc) · 4.36 KB
/
if.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Related to Svelte AST node {@link IfBlock}.
* @see {@link https://svelte.dev/docs/logic-blocks#if}
* @module
*/
import { print } from "esrap";
import { print_fragment } from "#node/fragment";
import { define_printer } from "#printer";
import type { Fragment, IfBlock } from "#types";
const has_alternate_else_if = (node: Fragment): boolean => node.nodes.some((n) => n.type === "IfBlock");
/**
* Print Svelte AST node {@link IfBlock} as string.
* @see {@link https://svelte.dev/docs/logic-blocks#if}
*
* @example simple
* ```svelte
* {#if expression}...{/if}
* ```
*
* @example with else if
* ```svelte
* {#if expression}...{:else if expression}...{/if}
* ```
*
* @example with else
* ```svelte
* {#if expression}...{:else}...{/if}
* ```
*
* @example with else if and else
* ```svelte
* {#if expression}...{:else if expression}...{:else}...{/if}
* ```
*
* @example with multiple else if and else
* ```svelte
* {#if expression}...{:else if expression}...{:else if expression}...{:else}...{/if}
* ```
*/
export const print_if_block = define_printer((node: IfBlock, options) => {
const { alternate, consequent, elseif, test } = node;
if (elseif) {
return [
"{:else if",
" ",
print(test).code,
"}",
print_fragment(consequent, options),
alternate
? [
//
!has_alternate_else_if(alternate) ? "{:else}" : "",
print_fragment(alternate, options),
].join("")
: "",
].join("");
}
return [
"{#if",
" ",
print(test).code,
"}",
print_fragment(consequent, options),
alternate
? [
//
!has_alternate_else_if(alternate) ? "{:else}" : "",
print_fragment(alternate, options),
].join("")
: "",
"{/if}",
].join("");
});
if (import.meta.vitest) {
const { describe, it } = import.meta.vitest;
const [{ parse_and_extract_svelte_node }, { DEFAULT_OPTIONS }] = await Promise.all([
import("#test/mod"),
import("#options"),
]);
describe("IfBlock", () => {
it("correctly prints simple {#if} block", ({ expect }) => {
const code = `
{#if test}
<span>simple if</span>
{/if}
`;
const node = parse_and_extract_svelte_node<IfBlock>(code, "IfBlock");
expect(print_if_block(node, DEFAULT_OPTIONS)).toMatchInlineSnapshot(`
"{#if test}
<span>simple if</span>
{/if}"
`);
});
it("correctly prints {#if} block with {:else}", ({ expect }) => {
const code = `
{#if test}
<span>if body</span>
{:else}
<span>else body</span>
{/if}
`;
const node = parse_and_extract_svelte_node<IfBlock>(code, "IfBlock");
expect(print_if_block(node, DEFAULT_OPTIONS)).toMatchInlineSnapshot(`
"{#if test}
<span>if body</span>
{:else}
<span>else body</span>
{/if}"
`);
});
it("correctly prints {#if} block with {:else if}", ({ expect }) => {
const code = `
{#if test1}
<span>if body</span>
{:else if test2}
<span>else if body</span>
{/if}
`;
const node = parse_and_extract_svelte_node<IfBlock>(code, "IfBlock");
expect(print_if_block(node, DEFAULT_OPTIONS)).toMatchInlineSnapshot(`
"{#if test1}
<span>if body</span>
{:else if test2}
<span>else if body</span>
{/if}"
`);
});
it("correctly prints {#if} block with {:else if} and {:else}", ({ expect }) => {
const code = `
{#if test1}
<span>if body</span>
{:else if test2}
<span>else if body</span>
{:else}
<span>else body</span>
{/if}
`;
const node = parse_and_extract_svelte_node<IfBlock>(code, "IfBlock");
expect(print_if_block(node, DEFAULT_OPTIONS)).toMatchInlineSnapshot(`
"{#if test1}
<span>if body</span>
{:else if test2}
<span>else if body</span>
{:else}
<span>else body</span>
{/if}"
`);
});
it("correctly prints {#if} block with multiple {:else if} and {:else}", ({ expect }) => {
const code = `
{#if test1}
<span>if body</span>
{:else if test2}
<span>else if body1</span>
{:else if test3}
<span>else if body2</span>
{:else}
<span>else body</span>
{/if}
`;
const node = parse_and_extract_svelte_node<IfBlock>(code, "IfBlock");
expect(print_if_block(node, DEFAULT_OPTIONS)).toMatchInlineSnapshot(`
"{#if test1}
<span>if body</span>
{:else if test2}
<span>else if body1</span>
{:else if test3}
<span>else if body2</span>
{:else}
<span>else body</span>
{/if}"
`);
});
});
}