3
3
// Should consolidate them here.
4
4
5
5
import { context } from './testlib' ;
6
- import { ctxTsNode , testsDirRequire , tsSupportsImportAssertions , tsSupportsReact17JsxFactories } from './helpers' ;
6
+ import {
7
+ CMD_TS_NODE_WITHOUT_PROJECT_FLAG ,
8
+ createExec ,
9
+ ctxTsNode ,
10
+ testsDirRequire ,
11
+ TEST_DIR ,
12
+ tsSupportsImportAssertions ,
13
+ tsSupportsReact17JsxFactories ,
14
+ } from './helpers' ;
7
15
import { createSwcOptions } from '../transpilers/swc' ;
8
16
import * as expect from 'expect' ;
9
17
import { outdent } from 'outdent' ;
18
+ import { join } from 'path' ;
10
19
11
20
const test = context ( ctxTsNode ) ;
12
21
@@ -78,10 +87,7 @@ test.suite('swc', (test) => {
78
87
. create ( {
79
88
swc : true ,
80
89
skipProject : true ,
81
- compilerOptions : {
82
- module : 'esnext' ,
83
- ...compilerOptions ,
84
- } ,
90
+ compilerOptions,
85
91
} )
86
92
. compile ( input , 'input.tsx' ) ;
87
93
expect ( code . replace ( / \/ \/ # s o u r c e M a p p i n g U R L .* / , '' ) . trim ( ) ) . toBe ( expectedOutput ) ;
@@ -93,12 +99,17 @@ test.suite('swc', (test) => {
93
99
const div = <div></div>;
94
100
` ;
95
101
96
- test ( compileMacro , { jsx : 'react' } , input , `const div = /*#__PURE__*/ React.createElement("div", null);` ) ;
102
+ test (
103
+ compileMacro ,
104
+ { module : 'esnext' , jsx : 'react' } ,
105
+ input ,
106
+ `const div = /*#__PURE__*/ React.createElement("div", null);`
107
+ ) ;
97
108
test . suite ( 'react 17 jsx factories' , ( test ) => {
98
109
test . if ( tsSupportsReact17JsxFactories ) ;
99
110
test (
100
111
compileMacro ,
101
- { jsx : 'react-jsx' } ,
112
+ { module : 'esnext' , jsx : 'react-jsx' } ,
102
113
input ,
103
114
outdent `
104
115
import { jsx as _jsx } from "react/jsx-runtime";
@@ -107,7 +118,7 @@ test.suite('swc', (test) => {
107
118
) ;
108
119
test (
109
120
compileMacro ,
110
- { jsx : 'react-jsxdev' } ,
121
+ { module : 'esnext' , jsx : 'react-jsxdev' } ,
111
122
input ,
112
123
outdent `
113
124
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
@@ -139,4 +150,166 @@ test.suite('swc', (test) => {
139
150
`
140
151
) ;
141
152
} ) ;
153
+
154
+ test . suite ( 'useDefineForClassFields' , ( test ) => {
155
+ const input = outdent `
156
+ class Foo {
157
+ bar = 1;
158
+ }
159
+ ` ;
160
+ const outputNative = outdent `
161
+ let Foo = class Foo {
162
+ bar = 1;
163
+ };
164
+ ` ;
165
+ const outputCtorAssignment = outdent `
166
+ let Foo = class Foo {
167
+ constructor(){
168
+ this.bar = 1;
169
+ }
170
+ };
171
+ ` ;
172
+ const outputDefine = outdent `
173
+ function _define_property(obj, key, value) {
174
+ if (key in obj) {
175
+ Object.defineProperty(obj, key, {
176
+ value: value,
177
+ enumerable: true,
178
+ configurable: true,
179
+ writable: true
180
+ });
181
+ } else {
182
+ obj[key] = value;
183
+ }
184
+ return obj;
185
+ }
186
+ let Foo = class Foo {
187
+ constructor(){
188
+ _define_property(this, "bar", 1);
189
+ }
190
+ };
191
+ ` ;
192
+ test (
193
+ 'useDefineForClassFields unset, should default to true and emit native property assignment b/c `next` target' ,
194
+ compileMacro ,
195
+ { module : 'esnext' , target : 'ESNext' } ,
196
+ input ,
197
+ outputNative
198
+ ) ;
199
+ test (
200
+ 'useDefineForClassFields unset, should default to true and emit native property assignment b/c new target' ,
201
+ compileMacro ,
202
+ { module : 'esnext' , target : 'ES2022' } ,
203
+ input ,
204
+ outputNative
205
+ ) ;
206
+ test (
207
+ 'useDefineForClassFields unset, should default to false b/c old target' ,
208
+ compileMacro ,
209
+ { module : 'esnext' , target : 'ES2021' } ,
210
+ input ,
211
+ outputCtorAssignment
212
+ ) ;
213
+ test (
214
+ 'useDefineForClassFields=true, should emit native property assignment b/c new target' ,
215
+ compileMacro ,
216
+ {
217
+ module : 'esnext' ,
218
+ useDefineForClassFields : true ,
219
+ target : 'ES2022' ,
220
+ } ,
221
+ input ,
222
+ outputNative
223
+ ) ;
224
+ test (
225
+ 'useDefineForClassFields=true, should emit define b/c old target' ,
226
+ compileMacro ,
227
+ {
228
+ module : 'esnext' ,
229
+ useDefineForClassFields : true ,
230
+ target : 'ES2021' ,
231
+ } ,
232
+ input ,
233
+ outputDefine
234
+ ) ;
235
+ test (
236
+ 'useDefineForClassFields=false, new target, should still emit legacy property assignment in ctor' ,
237
+ compileMacro ,
238
+ {
239
+ module : 'esnext' ,
240
+ useDefineForClassFields : false ,
241
+ target : 'ES2022' ,
242
+ } ,
243
+ input ,
244
+ outputCtorAssignment
245
+ ) ;
246
+ test (
247
+ 'useDefineForClassFields=false, old target, should emit legacy property assignment in ctor' ,
248
+ compileMacro ,
249
+ {
250
+ module : 'esnext' ,
251
+ useDefineForClassFields : false ,
252
+ } ,
253
+ input ,
254
+ outputCtorAssignment
255
+ ) ;
256
+ } ) ;
257
+
258
+ test . suite ( 'jsx and jsxImportSource' , ( test ) => {
259
+ test (
260
+ 'jsx=react-jsx' ,
261
+ compileMacro ,
262
+ {
263
+ module : 'esnext' ,
264
+ jsx : 'react-jsx' ,
265
+ } ,
266
+ outdent `
267
+ <div></div>
268
+ ` ,
269
+ outdent `
270
+ /*#__PURE__*/ import { jsx as _jsx } from "react/jsx-runtime";
271
+ _jsx("div", {});
272
+ `
273
+ ) ;
274
+ test (
275
+ 'jsx=react-jsx w/custom jsxImportSource' ,
276
+ compileMacro ,
277
+ {
278
+ module : 'esnext' ,
279
+ jsx : 'react-jsx' ,
280
+ jsxImportSource : 'foo' ,
281
+ } ,
282
+ outdent `
283
+ <div></div>
284
+ ` ,
285
+ outdent `
286
+ /*#__PURE__*/ import { jsx as _jsx } from "foo/jsx-runtime";
287
+ _jsx("div", {});
288
+ `
289
+ ) ;
290
+ } ) ;
291
+
292
+ test . suite (
293
+ '#1996 regression: ts-node gracefully allows swc to not return a sourcemap for type-only files' ,
294
+ ( test ) => {
295
+ // https://github.com/TypeStrong/ts-node/issues/1996
296
+ // @swc /core 1.3.51 returned `undefined` instead of sourcemap if the file was empty or only exported types.
297
+ // Newer swc versions do not do this. But our typedefs technically allow it.
298
+ const exec = createExec ( {
299
+ cwd : join ( TEST_DIR , '1996' ) ,
300
+ } ) ;
301
+ test ( 'import empty file w/swc' , async ( t ) => {
302
+ const r = await exec ( `${ CMD_TS_NODE_WITHOUT_PROJECT_FLAG } ./index.ts` ) ;
303
+ expect ( r . err ) . toBe ( null ) ;
304
+ expect ( r . stdout ) . toMatch ( / # 1 9 9 6 r e g r e s s i o n t e s t ./ ) ;
305
+ } ) ;
306
+ test ( 'use custom transpiler which never returns a sourcemap' , async ( t ) => {
307
+ const r = await exec (
308
+ `${ CMD_TS_NODE_WITHOUT_PROJECT_FLAG } --project tsconfig.custom-transpiler.json ./empty.ts`
309
+ ) ;
310
+ expect ( r . err ) . toBe ( null ) ;
311
+ expect ( r . stdout ) . toMatch ( / # 1 9 9 6 r e g r e s s i o n t e s t w i t h c u s t o m t r a n s p i l e r ./ ) ;
312
+ } ) ;
313
+ }
314
+ ) ;
142
315
} ) ;
0 commit comments