forked from withastro/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.ts
273 lines (235 loc) · 8.97 KB
/
basic.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { TSXPrefix } from '../utils.js';
test('basic', async () => {
const input = `
---
let value = 'world';
---
<h1 name="value" empty {shorthand} expression={true} literal=\`tags\`>Hello {value}</h1>
<div></div>
`;
const output = `${TSXPrefix}
let value = 'world';
<Fragment>
<h1 name="value" empty shorthand={shorthand} expression={true} literal={\`tags\`}>Hello {value}</h1>
<div></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('named export', async () => {
const input = `
---
let value = 'world';
---
<h1 name="value" empty {shorthand} expression={true} literal=\`tags\`>Hello {value}</h1>
<div></div>
`;
const output = `${TSXPrefix}
let value = 'world';
<Fragment>
<h1 name="value" empty shorthand={shorthand} expression={true} literal={\`tags\`}>Hello {value}</h1>
<div></div>
</Fragment>
export default function Test__AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, {
filename: '/Users/nmoo/test.astro',
sourcemap: 'external',
});
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('moves @attributes to spread', async () => {
const input = `<div @click={() => {}} name="value"></div>`;
const output = `${TSXPrefix}<Fragment>
<div name="value" {...{"@click":(() => {})}}></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('add trailing semicolon to frontmatter', async () => {
const input = `
---
console.log("hello")
---
{hello}
`;
const output = `${TSXPrefix}
console.log("hello")
{};<Fragment>
{hello}
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('add trailing semicolon to frontmatter II', async () => {
const input = `
---
const { hello } = Astro.props
---
<div class={hello}></div>
`;
const output = `${TSXPrefix}
const { hello } = Astro.props
{};<Fragment>
<div class={hello}></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('moves attributes with dots in them to spread', async () => {
const input = `<div x-on:keyup.shift.enter="alert('Astro')" name="value"></div>`;
const output = `${TSXPrefix}<Fragment>
<div name="value" {...{"x-on:keyup.shift.enter":"alert('Astro')"}}></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('moves attributes that starts with : to spread', async () => {
const input = `<div :class="hey" name="value"></div>`;
const output = `${TSXPrefix}<Fragment>
<div name="value" {...{":class":"hey"}}></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test("Don't move attributes to spread unnecessarily", async () => {
const input = `<div 丽dfds_fsfdsfs aria-blarg name="value"></div>`;
const output = `${TSXPrefix}<Fragment>
<div 丽dfds_fsfdsfs aria-blarg name="value"></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('preserves unclosed tags', async () => {
const input = '<components.';
const output = `${TSXPrefix}<Fragment>
<components.
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('template literal attribute', async () => {
const input = '<div class=`${hello}`></div>';
const output = `${TSXPrefix}<Fragment>
<div class={\`\${hello}\`}></div>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('unclosed tags', async () => {
const input = `---
const myMarkdown = await import('../content/post.md');
---
<myMarkdown.`;
const output = `${TSXPrefix}
const myMarkdown = await import('../content/post.md');
<Fragment>
<myMarkdown.
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('unclosed tags II', async () => {
const input = `---
const myMarkdown = await import('../content/post.md');
---
<myMarkdown.
`;
const output = `${TSXPrefix}
const myMarkdown = await import('../content/post.md');
<Fragment>
<myMarkdown.
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('spread object', async () => {
const input = `<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" />`;
const output = `${TSXPrefix}<Fragment>
<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" />
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('spread object II', async () => {
const input = `<MainLayout {...Astro.props}>
</MainLayout>`;
const output = `${TSXPrefix}<Fragment>
<MainLayout {...Astro.props}>
</MainLayout>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('fragment with no name', async () => {
const input = '<>+0123456789</>';
const output = `${TSXPrefix}<Fragment>
<>+0123456789</>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('preserves spaces in tag', async () => {
const input = '<Button ></Button>';
const output = `${TSXPrefix}<Fragment>
<Button ></Button>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('preserves spaces after attributes in tag', async () => {
const input = '<Button a="b" ></Button>';
const output = `${TSXPrefix}<Fragment>
<Button a="b" ></Button>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('preserves spaces in tag', async () => {
const input = '<Button >';
const output = `${TSXPrefix}<Fragment>
<Button ></Button>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('preserves line returns in tag by transforming to space', async () => {
const input = `<Button
>`;
const output = `${TSXPrefix}<Fragment>
<Button ></Button>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test('fragment with leading linebreak', async () => {
const input = `
<>Test123</>`;
const output = `${TSXPrefix}<Fragment>
<>Test123</>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});
test.run();