-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathjsx.spec.ts
More file actions
375 lines (352 loc) · 10.7 KB
/
jsx.spec.ts
File metadata and controls
375 lines (352 loc) · 10.7 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import * as util from "../util";
import { TestBuilder } from "../util";
import { JsxEmit } from "typescript";
import { unsupportedJsxEmit } from "../../src/transpilation/diagnostics";
import { unsupportedNodeKind } from "../../src/transformation/utils/diagnostics";
// language=TypeScript
const reactLib = `
export class Component {
static isClass = true
}
namespace React {
const isLua = typeof Component === "object"
export function createElement(
type: any,
props?: any,
...children: any[]
) {
let typeStr: string
if (isLua) {
typeStr =
typeof type === "function" ? "<< function >>" :
typeof type === "object" ? \`<< class \${type.name} >>\` :
type
} else {
typeStr = typeof type === "function" ? (type.isClass
? \`<< class \${type.name} >>\`
: "<< function >>")
: type
}
return {
type: typeStr,
props,
children
};
}
export class Fragment extends Component {
}
}
export default React;
`;
// language=TypeScript
const jsxTypings = `declare namespace JSX {
interface IntrinsicElements {
a: any
b: any
foo: any
bar: any
div: any
"with-dash": any
}
}
`;
function testJsx(...args: [string] | [TemplateStringsArray, ...any[]]): TestBuilder {
return util
.testFunction(...args)
.setOptions({
jsx: JsxEmit.React,
})
.setMainFileName("main.tsx")
.addExtraFile("react.ts", reactLib)
.addExtraFile("jsx.d.ts", jsxTypings)
.setTsHeader('import React, { Component } from "./react";');
}
describe("jsx", () => {
test("element", () => {
testJsx`
return <foo></foo>
`.expectToMatchJsResult();
});
test("self closing element", () => {
testJsx`
return <foo />
`.expectToMatchJsResult();
});
test("element with dash name", () => {
testJsx`
return <with-dash />
`.expectToMatchJsResult();
});
test("custom element", () => {
testJsx`
function Foo() {}
return <Foo />
`.expectToMatchJsResult();
});
test("fragment", () => {
testJsx`
return <></>
`.expectToMatchJsResult();
});
test("fragment with children", () => {
testJsx`
return <><a foo="bar" /><b bar="foo" /></>
`.expectToMatchJsResult();
});
test("esoteric component names", () => {
testJsx`
class _Foo extends Component {}
return <_Foo />
`.expectToMatchJsResult();
testJsx`
class $ extends Component {}
return <$ />
`.expectToMatchJsResult();
testJsx`
class é extends Component {}
return <é />
`.expectToMatchJsResult();
});
test("nested elements", () => {
testJsx`
return <foo><bar></bar></foo>
`.expectToMatchJsResult();
});
test("many nested elements", () => {
testJsx`
return <foo><a><b><bar></bar></b></a></foo>
`.expectToMatchJsResult();
});
test("interpolated children", () => {
testJsx`
const x = 3
return <foo>{x}</foo>
`.expectToMatchJsResult();
});
test("string prop", () => {
testJsx`
return <a foo="bar" />
`.expectToMatchJsResult();
});
test("value prop", () => {
testJsx`
const x = 5
return <a foo={x} />
`.expectToMatchJsResult();
});
test("quoted prop", () => {
testJsx`
return <a foo-bar={true} />
`.expectToMatchJsResult();
});
test("shorthand prop", () => {
testJsx`
return <a foo />
`.expectToMatchJsResult();
});
test("spaces in jsxText", () => {
testJsx`
return <a>this
<b/> is some<a/>multiline
text <a/> thing.
<b/>
</a>
`.expectToMatchJsResult();
});
test("multiline string jsxText", () => {
testJsx`
return <a>
foo bar
baz
</a>
`.expectToMatchJsResult();
});
test("access tag value", () => {
testJsx`
const a = { b(){} };
return <a.b c='d' />
`.expectToMatchJsResult();
testJsx`
const a = { b: { c: { d(){} } } };
return <a.b.c.d c='d'/>
`.expectToMatchJsResult();
});
test("spread props", () => {
testJsx`
const x = {c: "d", e: "f"}
return <a {...x} />
`.expectToMatchJsResult();
testJsx`
const x = {c: "d", e: "no"}
return <a a="b" {...x} e="f" />
`.expectToMatchJsResult();
});
test("comment children", () => {
testJsx`
return <a>
{/* comment */}
{/* another comment */}
</a>
`.expectToMatchJsResult();
testJsx`
return <a>
<b/>
{/* comment */}
<b/>
</a>
`.expectToMatchJsResult();
});
test("multiline string prop value", () => {
testJsx`
return <div value="This is a
multi-line string."
/>
`.expectToMatchJsResult();
testJsx`
return <div value="
This is a longer
multi-line string.
"
/>
`.expectToMatchJsResult();
});
test("prop strings with entities", () => {
testJsx`
return <div value="a>b" />
`.expectToMatchJsResult();
});
test("jsxText with entities", () => {
testJsx`
return <a> 9+10<21 </a>
`.expectToMatchJsResult();
});
test("Spread children", () => {
// doesn't actually "spread" (typescript's current behavior)
testJsx`
const children = [<a/>, <b/>]
return <foo>{...children}</foo>
`.expectToMatchJsResult();
});
test("complex", () => {
testJsx`
const x = 3
const props = {one: "two", three: 4}
return <div a="b">
<a c="d" r={x}/>
<b baz-bar {...props}> the {x}marks the spot </b>
a
{/** bar */}
and
<> <a>2</a></>
<b/>
</div>
`.expectToMatchJsResult();
});
// language=TypeScript
const customJsxLib = `export namespace MyLib {
export function myCreate(
type: any,
props: any,
...children: any[]
) {
return { type: typeof type, props, children, myThing: true };
}
export function MyFragment() {
}
}
`;
test("custom JSX factory", () => {
testJsx`
return <a><b>c</b></a>
`
.setTsHeader('import { MyLib } from "./myJsx";')
.setOptions({ jsxFactory: "MyLib.myCreate" })
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
testJsx`
return <a><b>c</b></a>
`
.setTsHeader('import { MyLib } from "./myJsx";const myCreate2 = MyLib.myCreate;')
.setOptions({ jsxFactory: "myCreate2" })
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
});
test("custom JSX factory with noImplicitSelf", () => {
testJsx`
return <a><b>c</b></a>
`
.setTsHeader(
`function createElement(tag: string | Function, props: { [key: string]: string | boolean }, ...children: any[]) {
return { tag, children };
}`
)
.setOptions({ jsxFactory: "createElement", noImplicitSelf: true })
.expectToMatchJsResult();
});
test("custom fragment factory", () => {
testJsx`
return <><b>c</b></>
`
.setTsHeader('import { MyLib } from "./myJsx";')
.setOptions({ jsxFactory: "MyLib.myCreate", jsxFragmentFactory: "MyLib.MyFragment" })
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
testJsx`
return <><b>c</b></>
`
.setTsHeader('import { MyLib } from "./myJsx";function MyFragment2(){};')
.setOptions({ jsxFactory: "MyLib.myCreate", jsxFragmentFactory: "MyFragment2" })
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
});
test("custom JSX pragma", () => {
testJsx`
return <a><b>c</b></a>
`
.setTsHeader('/** @jsx MyLib.myCreate */\nimport { MyLib } from "./myJsx";')
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
testJsx`
return <a><b>c</b></a>
`
.setTsHeader('/** @jsx myCreate2 */import { MyLib } from "./myJsx";const myCreate2 = MyLib.myCreate;')
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
});
test("custom fragment pragma", () => {
testJsx`
return <><b>c</b></>
`
.setTsHeader(
'/** @jsx MyLib.myCreate */\n/** @jsxFrag MyLib.MyFragment */\nimport { MyLib } from "./myJsx";'
)
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
testJsx`
return <><b>c</b></>
`
.setTsHeader(
"/** @jsx MyLib.myCreate */\n/** @jsxFrag MyFragment2 */\n" +
'import { MyLib } from "./myJsx";function MyFragment2(){};'
)
.setOptions({ jsxFactory: "MyLib.myCreate", jsxFragmentFactory: "MyFragment" })
.addExtraFile("myJsx.ts", customJsxLib)
.expectToMatchJsResult();
});
test("forward declare components", () => {
testJsx`
const foo = <Foo />
function Foo(){}
return foo
`.expectToMatchJsResult();
});
test("invalid jsx config", () => {
testJsx(`
return <a/>
`)
.setOptions({
jsx: JsxEmit.Preserve,
})
.expectToHaveDiagnostics([unsupportedJsxEmit.code, unsupportedNodeKind.code]);
});
});