-
-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathtranspilers.spec.ts
More file actions
315 lines (300 loc) · 9.32 KB
/
transpilers.spec.ts
File metadata and controls
315 lines (300 loc) · 9.32 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
// third-party transpiler and swc transpiler tests
// TODO: at the time of writing, other transpiler tests have not been moved into this file.
// Should consolidate them here.
import { context } from './testlib';
import {
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
createExec,
ctxTsNode,
testsDirRequire,
TEST_DIR,
tsSupportsImportAssertions,
tsSupportsReact17JsxFactories,
} from './helpers';
import { createSwcOptions } from '../transpilers/swc';
import * as expect from 'expect';
import { outdent } from 'outdent';
import { join } from 'path';
const test = context(ctxTsNode);
test.suite('swc', (test) => {
test('verify that TS->SWC target mappings suppport all possible values from both TS and SWC', async (t) => {
const swcTranspiler = testsDirRequire(
'ts-node/transpilers/swc-experimental'
) as typeof import('../transpilers/swc');
// Detect when mapping is missing any ts.ScriptTargets
const ts = testsDirRequire('typescript') as typeof import('typescript');
for (const key of Object.keys(ts.ScriptTarget)) {
if (/^\d+$/.test(key)) continue;
if (key === 'JSON') continue;
expect(swcTranspiler.targetMapping.has(ts.ScriptTarget[key as any] as any)).toBe(true);
}
// Detect when mapping is missing any swc targets
// Assuming that tests/package.json declares @swc/core: latest
const swc = testsDirRequire('@swc/core');
let msg: string | undefined = undefined;
try {
swc.transformSync('', { jsc: { target: 'invalid' } });
} catch (e) {
msg = (e as Error).message;
}
expect(msg).toBeDefined();
// Error looks like:
// unknown variant `invalid`, expected one of `es3`, `es5`, `es2015`, `es2016`, `es2017`, `es2018`, `es2019`, `es2020`, `es2021` at line 1 column 28
const match = msg!.match(/unknown variant.*, expected one of (.*) at line/);
expect(match).toBeDefined();
const targets = match![1].split(', ').map((v: string) => v.slice(1, -1));
for (const target of targets) {
expect([...swcTranspiler.targetMapping.values()]).toContain(target);
}
});
test.suite('converts TS config to swc config', (test) => {
test.suite('jsx', (test) => {
const macro = test.macro((jsx: string, runtime?: string, development?: boolean) => [
() => `jsx=${jsx}`,
async (t) => {
const tsNode = t.context.tsNodeUnderTest.create({
compilerOptions: {
jsx,
},
});
const swcOptions = createSwcOptions(tsNode.config.options, undefined, require('@swc/core'), '@swc/core');
expect(swcOptions.tsxOptions.jsc?.transform?.react).toBeDefined();
expect(swcOptions.tsxOptions.jsc?.transform?.react?.development).toBe(development);
expect(swcOptions.tsxOptions.jsc?.transform?.react?.runtime).toBe(runtime);
},
]);
test(macro, 'react', undefined, undefined);
test.suite('react 17 jsx factories', (test) => {
test.if(tsSupportsReact17JsxFactories);
test(macro, 'react-jsx', 'automatic', undefined);
test(macro, 'react-jsxdev', 'automatic', true);
});
});
});
const compileMacro = test.macro((compilerOptions: object, input: string, expectedOutput: string) => [
(title?: string) => title ?? `${JSON.stringify(compilerOptions)}`,
async (t) => {
const code = t.context.tsNodeUnderTest
.create({
swc: true,
skipProject: true,
compilerOptions,
})
.compile(input, 'input.tsx');
expect(code.replace(/\/\/# sourceMappingURL.*/, '').trim()).toBe(expectedOutput);
},
]);
test.suite('transforms various forms of jsx', (test) => {
const input = outdent`
const div = <div></div>;
`;
test(
compileMacro,
{ module: 'esnext', jsx: 'react' },
input,
`const div = /*#__PURE__*/ React.createElement("div", null);`
);
test.suite('react 17 jsx factories', (test) => {
test.if(tsSupportsReact17JsxFactories);
test(
compileMacro,
{ module: 'esnext', jsx: 'react-jsx' },
input,
outdent`
import { jsx as _jsx } from "react/jsx-runtime";
const div = /*#__PURE__*/ _jsx("div", {});
`
);
test(
compileMacro,
{ module: 'esnext', jsx: 'react-jsxdev' },
input,
outdent`
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
const div = /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, {
fileName: "input.tsx",
lineNumber: 1,
columnNumber: 13
}, this);
`
);
});
});
test.suite('preserves import assertions for json imports', (test) => {
test.if(tsSupportsImportAssertions);
test(
'basic json import',
compileMacro,
{ module: 'esnext' },
outdent`
import document from './document.json' assert {type: 'json'};
document;
`,
outdent`
import document from './document.json' assert {
type: 'json'
};
document;
`
);
});
test.suite('useDefineForClassFields', (test) => {
const input = outdent`
class Foo {
bar = 1;
}
`;
const outputNative = outdent`
let Foo = class Foo {
bar = 1;
};
`;
const outputCtorAssignment = outdent`
let Foo = class Foo {
constructor(){
this.bar = 1;
}
};
`;
const outputDefine = outdent`
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
let Foo = class Foo {
constructor(){
_define_property(this, "bar", 1);
}
};
`;
test(
'useDefineForClassFields unset, should default to true and emit native property assignment b/c `next` target',
compileMacro,
{ module: 'esnext', target: 'ESNext' },
input,
outputNative
);
test(
'useDefineForClassFields unset, should default to true and emit native property assignment b/c new target',
compileMacro,
{ module: 'esnext', target: 'ES2022' },
input,
outputNative
);
test(
'useDefineForClassFields unset, should default to false b/c old target',
compileMacro,
{ module: 'esnext', target: 'ES2021' },
input,
outputCtorAssignment
);
test(
'useDefineForClassFields=true, should emit native property assignment b/c new target',
compileMacro,
{
module: 'esnext',
useDefineForClassFields: true,
target: 'ES2022',
},
input,
outputNative
);
test(
'useDefineForClassFields=true, should emit define b/c old target',
compileMacro,
{
module: 'esnext',
useDefineForClassFields: true,
target: 'ES2021',
},
input,
outputDefine
);
test(
'useDefineForClassFields=false, new target, should still emit legacy property assignment in ctor',
compileMacro,
{
module: 'esnext',
useDefineForClassFields: false,
target: 'ES2022',
},
input,
outputCtorAssignment
);
test(
'useDefineForClassFields=false, old target, should emit legacy property assignment in ctor',
compileMacro,
{
module: 'esnext',
useDefineForClassFields: false,
},
input,
outputCtorAssignment
);
});
test.suite('jsx and jsxImportSource', (test) => {
test(
'jsx=react-jsx',
compileMacro,
{
module: 'esnext',
jsx: 'react-jsx',
},
outdent`
<div></div>
`,
outdent`
/*#__PURE__*/ import { jsx as _jsx } from "react/jsx-runtime";
_jsx("div", {});
`
);
test(
'jsx=react-jsx w/custom jsxImportSource',
compileMacro,
{
module: 'esnext',
jsx: 'react-jsx',
jsxImportSource: 'foo',
},
outdent`
<div></div>
`,
outdent`
/*#__PURE__*/ import { jsx as _jsx } from "foo/jsx-runtime";
_jsx("div", {});
`
);
});
test.suite(
'#1996 regression: ts-node gracefully allows swc to not return a sourcemap for type-only files',
(test) => {
// https://github.com/TypeStrong/ts-node/issues/1996
// @swc/core 1.3.51 returned `undefined` instead of sourcemap if the file was empty or only exported types.
// Newer swc versions do not do this. But our typedefs technically allow it.
const exec = createExec({
cwd: join(TEST_DIR, '1996'),
});
test('import empty file w/swc', async (t) => {
const r = await exec(`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} ./index.ts`);
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/#1996 regression test./);
});
test('use custom transpiler which never returns a sourcemap', async (t) => {
const r = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --project tsconfig.custom-transpiler.json ./empty.ts`
);
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/#1996 regression test with custom transpiler./);
});
}
);
});