lc-test3 1.1.10 → 1.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/p.js +751 -556
- package/package.json +1 -1
package/p.js
CHANGED
|
@@ -1,556 +1,751 @@
|
|
|
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
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
1
|
+
/**
|
|
2
|
+
* SWC ��������װ
|
|
3
|
+
*
|
|
4
|
+
* SWC ���� Rust ��д�ij��� JavaScript/TypeScript ������
|
|
5
|
+
* �� Babel �� 20-70 ��
|
|
6
|
+
*
|
|
7
|
+
* ��ģ���ṩ��
|
|
8
|
+
* 1. SWC WASM ��ʼ��
|
|
9
|
+
* 2. SWC AST �� Babel AST ��ת��
|
|
10
|
+
* 3. ���˵� Babel �ļ��ݻ���
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as t from '@babel/types';
|
|
14
|
+
import { parse as babelParse, ParserOptions } from '@babel/parser';
|
|
15
|
+
|
|
16
|
+
// SWC WASM �
|
|
17
|
+
let swcModule: typeof import('@swc/wasm-web') | null = null;
|
|
18
|
+
let swcInitPromise: Promise<void> | null = null;
|
|
19
|
+
let swcAvailable = false;
|
|
20
|
+
|
|
21
|
+
// Babel �������ã������ã�
|
|
22
|
+
const babelParserConfig: ParserOptions = {
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
plugins: [
|
|
25
|
+
'jsx',
|
|
26
|
+
'doExpressions',
|
|
27
|
+
'objectRestSpread',
|
|
28
|
+
'decorators-legacy',
|
|
29
|
+
'classProperties',
|
|
30
|
+
'asyncGenerators',
|
|
31
|
+
'functionBind',
|
|
32
|
+
'dynamicImport',
|
|
33
|
+
'optionalChaining',
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// SWC ��������
|
|
38
|
+
const swcParserConfig = {
|
|
39
|
+
syntax: 'typescript' as const,
|
|
40
|
+
tsx: true,
|
|
41
|
+
decorators: true,
|
|
42
|
+
dynamicImport: true,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const permissionCommentInJSXPattern = /\/\*\*[\s\S]*?RESOURCESTREE-START[\s\S]*?RESOURCESTREE-END[\s\S]*?\*\/(?=\s*<)/g;
|
|
46
|
+
|
|
47
|
+
function sanitizePermissionCommentsForJSX(code: string) {
|
|
48
|
+
return code.replace(permissionCommentInJSXPattern, (comment) => {
|
|
49
|
+
const inner = comment
|
|
50
|
+
.replace(/^\/\*\*?/, '')
|
|
51
|
+
.replace(/\*\/$/, '');
|
|
52
|
+
return `{/*${inner}*/}`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ����ͳ��
|
|
57
|
+
const swcStats = {
|
|
58
|
+
swcParseCount: 0,
|
|
59
|
+
swcTotalTime: 0,
|
|
60
|
+
babelFallbackCount: 0,
|
|
61
|
+
babelTotalTime: 0,
|
|
62
|
+
conversionCount: 0,
|
|
63
|
+
conversionTotalTime: 0,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* ��ʼ�� SWC WASM ģ��
|
|
68
|
+
*/
|
|
69
|
+
export async function initSWC(): Promise<boolean> {
|
|
70
|
+
if (swcAvailable) return true;
|
|
71
|
+
if (swcInitPromise) {
|
|
72
|
+
await swcInitPromise;
|
|
73
|
+
return swcAvailable;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
swcInitPromise = (async () => {
|
|
77
|
+
try {
|
|
78
|
+
// ��̬���� SWC WASM
|
|
79
|
+
const swc = await import('@swc/wasm-web');
|
|
80
|
+
|
|
81
|
+
// ��ʼ�� WASM����Ҫ���� .wasm �ļ���
|
|
82
|
+
await swc.default();
|
|
83
|
+
|
|
84
|
+
swcModule = swc;
|
|
85
|
+
swcAvailable = true;
|
|
86
|
+
console.log('%c[SWC] ? WASM ģ���ʼ���ɹ�', 'color: #4CAF50; font-weight: bold;');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.warn('[SWC] ?? WASM ģ�����ʧ�ܣ���ʹ�� Babel ��Ϊ����:', error);
|
|
89
|
+
swcAvailable = false;
|
|
90
|
+
}
|
|
91
|
+
})();
|
|
92
|
+
|
|
93
|
+
await swcInitPromise;
|
|
94
|
+
return swcAvailable;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* ��� SWC �Ƿ����
|
|
99
|
+
*/
|
|
100
|
+
export function isSWCAvailable(): boolean {
|
|
101
|
+
return swcAvailable;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* SWC AST �ڵ�����ӳ�䵽 Babel ����
|
|
106
|
+
*/
|
|
107
|
+
const SWC_TO_BABEL_TYPE: Record<string, string> = {
|
|
108
|
+
'Module': 'File',
|
|
109
|
+
'Script': 'File',
|
|
110
|
+
'ImportDeclaration': 'ImportDeclaration',
|
|
111
|
+
'ExportDeclaration': 'ExportDeclaration',
|
|
112
|
+
'ExportDefaultDeclaration': 'ExportDefaultDeclaration',
|
|
113
|
+
'ExportDefaultExpression': 'ExportDefaultDeclaration',
|
|
114
|
+
'ExportNamedDeclaration': 'ExportNamedDeclaration',
|
|
115
|
+
'ExportAllDeclaration': 'ExportAllDeclaration',
|
|
116
|
+
'VariableDeclaration': 'VariableDeclaration',
|
|
117
|
+
'VariableDeclarator': 'VariableDeclarator',
|
|
118
|
+
'FunctionDeclaration': 'FunctionDeclaration',
|
|
119
|
+
'FunctionExpression': 'FunctionExpression',
|
|
120
|
+
'ArrowFunctionExpression': 'ArrowFunctionExpression',
|
|
121
|
+
'ClassDeclaration': 'ClassDeclaration',
|
|
122
|
+
'ClassExpression': 'ClassExpression',
|
|
123
|
+
'ClassMethod': 'ClassMethod',
|
|
124
|
+
'ClassPrivateMethod': 'ClassPrivateMethod',
|
|
125
|
+
'ClassProperty': 'ClassProperty',
|
|
126
|
+
'ClassPrivateProperty': 'ClassPrivateProperty',
|
|
127
|
+
'Constructor': 'ClassMethod',
|
|
128
|
+
'PrivateName': 'PrivateName',
|
|
129
|
+
'Identifier': 'Identifier',
|
|
130
|
+
'StringLiteral': 'StringLiteral',
|
|
131
|
+
'NumericLiteral': 'NumericLiteral',
|
|
132
|
+
'BooleanLiteral': 'BooleanLiteral',
|
|
133
|
+
'NullLiteral': 'NullLiteral',
|
|
134
|
+
'RegExpLiteral': 'RegExpLiteral',
|
|
135
|
+
'TemplateLiteral': 'TemplateLiteral',
|
|
136
|
+
'ArrayExpression': 'ArrayExpression',
|
|
137
|
+
'ObjectExpression': 'ObjectExpression',
|
|
138
|
+
'CallExpression': 'CallExpression',
|
|
139
|
+
'MemberExpression': 'MemberExpression',
|
|
140
|
+
'BinaryExpression': 'BinaryExpression',
|
|
141
|
+
'UnaryExpression': 'UnaryExpression',
|
|
142
|
+
'ConditionalExpression': 'ConditionalExpression',
|
|
143
|
+
'AssignmentExpression': 'AssignmentExpression',
|
|
144
|
+
'LogicalExpression': 'LogicalExpression',
|
|
145
|
+
'SequenceExpression': 'SequenceExpression',
|
|
146
|
+
'NewExpression': 'NewExpression',
|
|
147
|
+
'ThisExpression': 'ThisExpression',
|
|
148
|
+
'SpreadElement': 'SpreadElement',
|
|
149
|
+
'RestElement': 'RestElement',
|
|
150
|
+
'ObjectPattern': 'ObjectPattern',
|
|
151
|
+
'ArrayPattern': 'ArrayPattern',
|
|
152
|
+
'AssignmentPattern': 'AssignmentPattern',
|
|
153
|
+
'BlockStatement': 'BlockStatement',
|
|
154
|
+
'ReturnStatement': 'ReturnStatement',
|
|
155
|
+
'IfStatement': 'IfStatement',
|
|
156
|
+
'SwitchStatement': 'SwitchStatement',
|
|
157
|
+
'SwitchCase': 'SwitchCase',
|
|
158
|
+
'ForStatement': 'ForStatement',
|
|
159
|
+
'ForInStatement': 'ForInStatement',
|
|
160
|
+
'ForOfStatement': 'ForOfStatement',
|
|
161
|
+
'WhileStatement': 'WhileStatement',
|
|
162
|
+
'DoWhileStatement': 'DoWhileStatement',
|
|
163
|
+
'TryStatement': 'TryStatement',
|
|
164
|
+
'CatchClause': 'CatchClause',
|
|
165
|
+
'ThrowStatement': 'ThrowStatement',
|
|
166
|
+
'BreakStatement': 'BreakStatement',
|
|
167
|
+
'ContinueStatement': 'ContinueStatement',
|
|
168
|
+
'ExpressionStatement': 'ExpressionStatement',
|
|
169
|
+
'EmptyStatement': 'EmptyStatement',
|
|
170
|
+
'DebuggerStatement': 'DebuggerStatement',
|
|
171
|
+
'LabeledStatement': 'LabeledStatement',
|
|
172
|
+
'WithStatement': 'WithStatement',
|
|
173
|
+
// JSX
|
|
174
|
+
'JSXElement': 'JSXElement',
|
|
175
|
+
'JSXFragment': 'JSXFragment',
|
|
176
|
+
'JSXOpeningElement': 'JSXOpeningElement',
|
|
177
|
+
'JSXClosingElement': 'JSXClosingElement',
|
|
178
|
+
'JSXOpeningFragment': 'JSXOpeningFragment',
|
|
179
|
+
'JSXClosingFragment': 'JSXClosingFragment',
|
|
180
|
+
'JSXAttribute': 'JSXAttribute',
|
|
181
|
+
'JSXSpreadAttribute': 'JSXSpreadAttribute',
|
|
182
|
+
'JSXExpressionContainer': 'JSXExpressionContainer',
|
|
183
|
+
'JSXText': 'JSXText',
|
|
184
|
+
'JSXIdentifier': 'JSXIdentifier',
|
|
185
|
+
'JSXMemberExpression': 'JSXMemberExpression',
|
|
186
|
+
'JSXNamespacedName': 'JSXNamespacedName',
|
|
187
|
+
'JSXEmptyExpression': 'JSXEmptyExpression',
|
|
188
|
+
// ����
|
|
189
|
+
'AwaitExpression': 'AwaitExpression',
|
|
190
|
+
'YieldExpression': 'YieldExpression',
|
|
191
|
+
'TaggedTemplateExpression': 'TaggedTemplateExpression',
|
|
192
|
+
'TemplateElement': 'TemplateElement',
|
|
193
|
+
'OptionalMemberExpression': 'OptionalMemberExpression',
|
|
194
|
+
'OptionalCallExpression': 'OptionalCallExpression',
|
|
195
|
+
'ParenthesisExpression': 'ParenthesizedExpression',
|
|
196
|
+
// Property ������Ҫ�����
|
|
197
|
+
'KeyValueProperty': 'ObjectProperty',
|
|
198
|
+
'AssignmentProperty': 'ObjectProperty',
|
|
199
|
+
'GetterProperty': 'ObjectMethod',
|
|
200
|
+
'SetterProperty': 'ObjectMethod',
|
|
201
|
+
'MethodProperty': 'ObjectMethod',
|
|
202
|
+
'SpreadProperty': 'SpreadElement',
|
|
203
|
+
'ShorthandProperty': 'ObjectProperty',
|
|
204
|
+
// Import/Export specifiers
|
|
205
|
+
'ImportDefaultSpecifier': 'ImportDefaultSpecifier',
|
|
206
|
+
'ImportSpecifier': 'ImportSpecifier',
|
|
207
|
+
'ImportNamespaceSpecifier': 'ImportNamespaceSpecifier',
|
|
208
|
+
'ExportSpecifier': 'ExportSpecifier',
|
|
209
|
+
'ExportDefaultSpecifier': 'ExportDefaultSpecifier',
|
|
210
|
+
'ExportNamespaceSpecifier': 'ExportNamespaceSpecifier',
|
|
211
|
+
'Computed': 'Computed',
|
|
212
|
+
'SuperPropExpression': 'MemberExpression',
|
|
213
|
+
'AssignmentPatternProperty': 'ObjectProperty',
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* �淶�����������б�
|
|
218
|
+
*/
|
|
219
|
+
function normalizeParams(params: any): any[] {
|
|
220
|
+
if (Array.isArray(params)) {
|
|
221
|
+
return params.map((p) => p?.pat || p);
|
|
222
|
+
}
|
|
223
|
+
if (params && Array.isArray(params.params)) {
|
|
224
|
+
return params.params.map((p: any) => p?.pat || p);
|
|
225
|
+
}
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* �淶����Ա���ԣ����� MemberExpression.property Ϊ MemberExpression ��������ʧ��
|
|
231
|
+
*/
|
|
232
|
+
function normalizeMemberProperty(prop: any, computedFlag?: boolean): { property: any; computed: boolean } {
|
|
233
|
+
if (!prop) {
|
|
234
|
+
return { property: prop, computed: Boolean(computedFlag) };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (prop.type === 'Computed') {
|
|
238
|
+
return { property: convertSWCNode(prop.expression), computed: true };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const converted = convertSWCNode(prop);
|
|
242
|
+
const needsComputed = converted?.type === 'MemberExpression';
|
|
243
|
+
return { property: converted, computed: Boolean(computedFlag) || needsComputed };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* �ݹ�ת�� SWC AST �ڵ㵽 Babel AST ��ʽ
|
|
248
|
+
*/
|
|
249
|
+
function convertSWCNode(node: any): any {
|
|
250
|
+
if (!node || typeof node !== 'object') {
|
|
251
|
+
return node;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ��������
|
|
255
|
+
if (Array.isArray(node)) {
|
|
256
|
+
return node.map(convertSWCNode);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ��ȡ�ڵ�����
|
|
260
|
+
const swcType = node.type;
|
|
261
|
+
if (!swcType) {
|
|
262
|
+
// ���� SWC ExprOrSpread �ṹ��{ spread: null|{...}, expression: {...} }
|
|
263
|
+
if ('expression' in node && 'spread' in node) {
|
|
264
|
+
if (node.spread) {
|
|
265
|
+
return {
|
|
266
|
+
type: 'SpreadElement',
|
|
267
|
+
argument: convertSWCNode(node.expression),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
return convertSWCNode(node.expression);
|
|
271
|
+
}
|
|
272
|
+
// ������ AST �ڵ�Ķ���
|
|
273
|
+
const result: any = {};
|
|
274
|
+
for (const key of Object.keys(node)) {
|
|
275
|
+
result[key] = convertSWCNode(node[key]);
|
|
276
|
+
}
|
|
277
|
+
return result;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ӳ������
|
|
281
|
+
const babelType = SWC_TO_BABEL_TYPE[swcType] || swcType;
|
|
282
|
+
|
|
283
|
+
// �����½ڵ�
|
|
284
|
+
const babelNode: any = {
|
|
285
|
+
type: babelType,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// �����������
|
|
289
|
+
switch (swcType) {
|
|
290
|
+
case 'SuperPropExpression':
|
|
291
|
+
const superProp = normalizeMemberProperty(node.property, node.computed);
|
|
292
|
+
return {
|
|
293
|
+
type: 'MemberExpression',
|
|
294
|
+
object: { type: 'Super' },
|
|
295
|
+
property: superProp.property,
|
|
296
|
+
computed: superProp.computed,
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
case 'AssignmentPatternProperty':
|
|
300
|
+
return {
|
|
301
|
+
type: 'ObjectProperty',
|
|
302
|
+
key: convertSWCNode(node.key),
|
|
303
|
+
value: convertSWCNode(node.value),
|
|
304
|
+
computed: node.key?.type === 'Computed' || node.computed || false,
|
|
305
|
+
shorthand: false,
|
|
306
|
+
};
|
|
307
|
+
case 'SuperPropExpression':
|
|
308
|
+
return {
|
|
309
|
+
type: 'MemberExpression',
|
|
310
|
+
object: { type: 'Super' },
|
|
311
|
+
property: convertSWCNode(node.property),
|
|
312
|
+
computed: node.property?.type === 'Computed' || node.computed || false,
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
case 'AssignmentPatternProperty':
|
|
316
|
+
return {
|
|
317
|
+
type: 'ObjectProperty',
|
|
318
|
+
key: convertSWCNode(node.key),
|
|
319
|
+
value: convertSWCNode(node.value),
|
|
320
|
+
computed: node.key?.type === 'Computed' || node.computed || false,
|
|
321
|
+
shorthand: false,
|
|
322
|
+
};
|
|
323
|
+
case 'Computed':
|
|
324
|
+
// SWC computed property wrapper
|
|
325
|
+
return convertSWCNode(node.expression);
|
|
326
|
+
case 'Program':
|
|
327
|
+
return {
|
|
328
|
+
type: 'Program',
|
|
329
|
+
sourceType: node.sourceType || 'module',
|
|
330
|
+
body: convertSWCNode(node.body || node.stmts || []),
|
|
331
|
+
directives: [],
|
|
332
|
+
};
|
|
333
|
+
case 'Module':
|
|
334
|
+
case 'Script':
|
|
335
|
+
// ת��Ϊ Babel �� File �ṹ
|
|
336
|
+
return {
|
|
337
|
+
type: 'File',
|
|
338
|
+
program: {
|
|
339
|
+
type: 'Program',
|
|
340
|
+
sourceType: 'module',
|
|
341
|
+
body: convertSWCNode(node.body || node.stmts || []),
|
|
342
|
+
directives: [],
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
case 'ExportDefaultExpression':
|
|
347
|
+
return {
|
|
348
|
+
type: 'ExportDefaultDeclaration',
|
|
349
|
+
declaration: convertSWCNode(node.expression || node.expr || node.decl),
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
case 'StringLiteral':
|
|
353
|
+
babelNode.value = node.value;
|
|
354
|
+
babelNode.raw = node.raw;
|
|
355
|
+
break;
|
|
356
|
+
|
|
357
|
+
case 'TemplateLiteral':
|
|
358
|
+
babelNode.expressions = convertSWCNode(node.expressions || node.exprs || []);
|
|
359
|
+
babelNode.quasis = convertSWCNode(node.quasis || []);
|
|
360
|
+
if (!babelNode.quasis || babelNode.quasis.length === 0) {
|
|
361
|
+
babelNode.quasis = [
|
|
362
|
+
{
|
|
363
|
+
type: 'TemplateElement',
|
|
364
|
+
value: { raw: '', cooked: '' },
|
|
365
|
+
tail: true,
|
|
366
|
+
},
|
|
367
|
+
];
|
|
368
|
+
}
|
|
369
|
+
if (Array.isArray(babelNode.quasis) && Array.isArray(babelNode.expressions)) {
|
|
370
|
+
if (babelNode.quasis.length === babelNode.expressions.length) {
|
|
371
|
+
babelNode.quasis.push({
|
|
372
|
+
type: 'TemplateElement',
|
|
373
|
+
value: { raw: '', cooked: '' },
|
|
374
|
+
tail: true,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
const last = babelNode.quasis[babelNode.quasis.length - 1];
|
|
378
|
+
if (last) last.tail = true;
|
|
379
|
+
}
|
|
380
|
+
break;
|
|
381
|
+
|
|
382
|
+
case 'NumericLiteral':
|
|
383
|
+
babelNode.value = node.value;
|
|
384
|
+
babelNode.raw = String(node.value);
|
|
385
|
+
break;
|
|
386
|
+
|
|
387
|
+
case 'BooleanLiteral':
|
|
388
|
+
babelNode.value = node.value;
|
|
389
|
+
break;
|
|
390
|
+
|
|
391
|
+
case 'Identifier':
|
|
392
|
+
babelNode.name = node.value;
|
|
393
|
+
break;
|
|
394
|
+
|
|
395
|
+
case 'PrivateName':
|
|
396
|
+
return {
|
|
397
|
+
type: 'PrivateName',
|
|
398
|
+
id: convertSWCNode(node.id || { type: 'Identifier', value: node.name }),
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
case 'KeyValueProperty':
|
|
402
|
+
case 'ShorthandProperty':
|
|
403
|
+
babelNode.type = 'ObjectProperty';
|
|
404
|
+
const keyNode = node.key;
|
|
405
|
+
const isComputedKey = keyNode?.type === 'Computed';
|
|
406
|
+
const normalizedKey = normalizeMemberProperty(isComputedKey ? keyNode.expression : keyNode, node.computed);
|
|
407
|
+
babelNode.key = normalizedKey.property;
|
|
408
|
+
babelNode.value = convertSWCNode(node.value);
|
|
409
|
+
babelNode.computed = normalizedKey.computed || isComputedKey || node.computed || false;
|
|
410
|
+
babelNode.shorthand = swcType === 'ShorthandProperty';
|
|
411
|
+
babelNode.method = false;
|
|
412
|
+
break;
|
|
413
|
+
|
|
414
|
+
case 'GetterProperty':
|
|
415
|
+
case 'SetterProperty':
|
|
416
|
+
case 'MethodProperty':
|
|
417
|
+
babelNode.type = 'ObjectMethod';
|
|
418
|
+
const objMethodKey = node.key;
|
|
419
|
+
const objMethodComputed = objMethodKey?.type === 'Computed';
|
|
420
|
+
const normalizedObjMethodKey = normalizeMemberProperty(objMethodComputed ? objMethodKey.expression : objMethodKey, node.computed);
|
|
421
|
+
babelNode.key = normalizedObjMethodKey.property;
|
|
422
|
+
babelNode.kind = swcType === 'GetterProperty' ? 'get' :
|
|
423
|
+
swcType === 'SetterProperty' ? 'set' : 'method';
|
|
424
|
+
babelNode.params = convertSWCNode(normalizeParams(node.params));
|
|
425
|
+
babelNode.body = convertSWCNode(node.body);
|
|
426
|
+
babelNode.computed = normalizedObjMethodKey.computed || objMethodComputed || node.computed || false;
|
|
427
|
+
babelNode.generator = node.generator || false;
|
|
428
|
+
babelNode.async = node.async || false;
|
|
429
|
+
break;
|
|
430
|
+
|
|
431
|
+
case 'ImportDeclaration':
|
|
432
|
+
babelNode.source = convertSWCNode(node.source);
|
|
433
|
+
babelNode.specifiers = convertSWCNode(node.specifiers || []);
|
|
434
|
+
babelNode.importKind = node.typeOnly ? 'type' : 'value';
|
|
435
|
+
break;
|
|
436
|
+
case 'BlockStatement':
|
|
437
|
+
babelNode.body = convertSWCNode(node.body || node.stmts || []);
|
|
438
|
+
break;
|
|
439
|
+
|
|
440
|
+
case 'ReturnStatement':
|
|
441
|
+
babelNode.argument = convertSWCNode(node.argument ?? node.value ?? node.expr ?? null);
|
|
442
|
+
break;
|
|
443
|
+
|
|
444
|
+
case 'ExpressionStatement':
|
|
445
|
+
babelNode.expression = convertSWCNode(node.expression ?? node.expr);
|
|
446
|
+
break;
|
|
447
|
+
|
|
448
|
+
case 'ImportSpecifier':
|
|
449
|
+
babelNode.imported = convertSWCNode(node.imported || node.local);
|
|
450
|
+
babelNode.local = convertSWCNode(node.local);
|
|
451
|
+
break;
|
|
452
|
+
|
|
453
|
+
case 'ImportDefaultSpecifier':
|
|
454
|
+
babelNode.local = convertSWCNode(node.local);
|
|
455
|
+
break;
|
|
456
|
+
|
|
457
|
+
case 'ImportNamespaceSpecifier':
|
|
458
|
+
babelNode.local = convertSWCNode(node.local);
|
|
459
|
+
break;
|
|
460
|
+
|
|
461
|
+
case 'ExportNamedDeclaration':
|
|
462
|
+
babelNode.declaration = convertSWCNode(node.declaration);
|
|
463
|
+
babelNode.specifiers = convertSWCNode(node.specifiers || []);
|
|
464
|
+
babelNode.source = convertSWCNode(node.source);
|
|
465
|
+
break;
|
|
466
|
+
|
|
467
|
+
case 'ExportDefaultDeclaration':
|
|
468
|
+
babelNode.declaration = convertSWCNode(node.decl || node.declaration);
|
|
469
|
+
break;
|
|
470
|
+
|
|
471
|
+
case 'VariableDeclaration':
|
|
472
|
+
babelNode.kind = node.kind;
|
|
473
|
+
babelNode.declarations = convertSWCNode(node.declarations);
|
|
474
|
+
break;
|
|
475
|
+
|
|
476
|
+
case 'VariableDeclarator':
|
|
477
|
+
babelNode.id = convertSWCNode(node.id);
|
|
478
|
+
babelNode.init = convertSWCNode(node.init);
|
|
479
|
+
break;
|
|
480
|
+
|
|
481
|
+
case 'FunctionDeclaration':
|
|
482
|
+
case 'FunctionExpression':
|
|
483
|
+
babelNode.id = convertSWCNode(node.identifier);
|
|
484
|
+
babelNode.params = convertSWCNode(normalizeParams(node.params));
|
|
485
|
+
babelNode.body = convertSWCNode(node.body);
|
|
486
|
+
babelNode.generator = node.generator || false;
|
|
487
|
+
babelNode.async = node.async || false;
|
|
488
|
+
break;
|
|
489
|
+
|
|
490
|
+
case 'ArrowFunctionExpression':
|
|
491
|
+
babelNode.params = convertSWCNode(normalizeParams(node.params));
|
|
492
|
+
babelNode.body = convertSWCNode(node.body);
|
|
493
|
+
babelNode.async = node.async || false;
|
|
494
|
+
babelNode.expression = node.body?.type !== 'BlockStatement';
|
|
495
|
+
break;
|
|
496
|
+
|
|
497
|
+
case 'ClassDeclaration':
|
|
498
|
+
case 'ClassExpression':
|
|
499
|
+
babelNode.id = convertSWCNode(node.identifier || node.id);
|
|
500
|
+
babelNode.superClass = convertSWCNode(node.superClass);
|
|
501
|
+
babelNode.decorators = convertSWCNode(node.decorators || []);
|
|
502
|
+
babelNode.body = {
|
|
503
|
+
type: 'ClassBody',
|
|
504
|
+
body: convertSWCNode(node.body?.body || node.body || []),
|
|
505
|
+
};
|
|
506
|
+
break;
|
|
507
|
+
|
|
508
|
+
case 'ClassMethod':
|
|
509
|
+
case 'ClassPrivateMethod':
|
|
510
|
+
case 'Constructor':
|
|
511
|
+
const classMethodKey = node.key;
|
|
512
|
+
const classMethodComputed = classMethodKey?.type === 'Computed';
|
|
513
|
+
const normalizedClassMethodKey = normalizeMemberProperty(classMethodComputed ? classMethodKey.expression : classMethodKey, node.computed);
|
|
514
|
+
babelNode.key = normalizedClassMethodKey.property;
|
|
515
|
+
babelNode.kind = swcType === 'Constructor' ? 'constructor' : (node.kind || 'method');
|
|
516
|
+
babelNode.params = convertSWCNode(normalizeParams(node.function?.params || node.params));
|
|
517
|
+
babelNode.body = convertSWCNode(node.function?.body || node.body);
|
|
518
|
+
babelNode.computed = normalizedClassMethodKey.computed || classMethodComputed || node.computed || false;
|
|
519
|
+
babelNode.static = node.isStatic || node.static || false;
|
|
520
|
+
babelNode.async = node.function?.isAsync || node.async || false;
|
|
521
|
+
babelNode.generator = node.function?.isGenerator || node.generator || false;
|
|
522
|
+
break;
|
|
523
|
+
|
|
524
|
+
case 'ClassProperty':
|
|
525
|
+
case 'ClassPrivateProperty':
|
|
526
|
+
const classPropKey = node.key;
|
|
527
|
+
const classPropComputed = classPropKey?.type === 'Computed';
|
|
528
|
+
const normalizedClassPropKey = normalizeMemberProperty(classPropComputed ? classPropKey.expression : classPropKey, node.computed);
|
|
529
|
+
babelNode.key = normalizedClassPropKey.property;
|
|
530
|
+
babelNode.value = convertSWCNode(node.value);
|
|
531
|
+
babelNode.computed = normalizedClassPropKey.computed || classPropComputed || node.computed || false;
|
|
532
|
+
babelNode.static = node.isStatic || node.static || false;
|
|
533
|
+
break;
|
|
534
|
+
|
|
535
|
+
case 'CallExpression':
|
|
536
|
+
babelNode.callee = convertSWCNode(node.callee);
|
|
537
|
+
babelNode.arguments = convertSWCNode(node.arguments || []);
|
|
538
|
+
break;
|
|
539
|
+
|
|
540
|
+
case 'OptionalChainingExpression': {
|
|
541
|
+
const base = node.base || node.expression || node.expr || node.argument;
|
|
542
|
+
const converted = convertSWCNode(base);
|
|
543
|
+
|
|
544
|
+
if (converted && (converted.type === 'MemberExpression' || converted.type === 'OptionalMemberExpression')) {
|
|
545
|
+
converted.type = 'OptionalMemberExpression';
|
|
546
|
+
converted.optional = true;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (converted && (converted.type === 'CallExpression' || converted.type === 'OptionalCallExpression')) {
|
|
550
|
+
converted.type = 'OptionalCallExpression';
|
|
551
|
+
converted.optional = true;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
return converted;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
case 'MemberExpression':
|
|
558
|
+
babelNode.object = convertSWCNode(node.object);
|
|
559
|
+
const memberProp = normalizeMemberProperty(node.property, node.computed);
|
|
560
|
+
babelNode.property = memberProp.property;
|
|
561
|
+
babelNode.computed = memberProp.computed;
|
|
562
|
+
babelNode.optional = node.optional || false;
|
|
563
|
+
break;
|
|
564
|
+
case 'NewExpression':
|
|
565
|
+
babelNode.callee = convertSWCNode(node.callee);
|
|
566
|
+
babelNode.arguments = convertSWCNode(node.arguments || []);
|
|
567
|
+
break;
|
|
568
|
+
case 'OptionalCallExpression':
|
|
569
|
+
babelNode.callee = convertSWCNode(node.callee);
|
|
570
|
+
babelNode.arguments = convertSWCNode(node.arguments || []);
|
|
571
|
+
babelNode.optional = true;
|
|
572
|
+
break;
|
|
573
|
+
|
|
574
|
+
case 'JSXElement':
|
|
575
|
+
babelNode.openingElement = convertSWCNode(node.opening);
|
|
576
|
+
babelNode.closingElement = convertSWCNode(node.closing);
|
|
577
|
+
babelNode.children = convertSWCNode(node.children || []);
|
|
578
|
+
babelNode.selfClosing = !node.closing;
|
|
579
|
+
break;
|
|
580
|
+
|
|
581
|
+
case 'JSXOpeningElement':
|
|
582
|
+
babelNode.name = convertSWCNode(node.name);
|
|
583
|
+
babelNode.attributes = convertSWCNode(node.attributes || []);
|
|
584
|
+
babelNode.selfClosing = node.selfClosing || false;
|
|
585
|
+
break;
|
|
586
|
+
|
|
587
|
+
case 'JSXClosingElement':
|
|
588
|
+
babelNode.name = convertSWCNode(node.name);
|
|
589
|
+
break;
|
|
590
|
+
|
|
591
|
+
case 'JSXAttribute':
|
|
592
|
+
babelNode.name = convertSWCNode(node.name);
|
|
593
|
+
babelNode.value = convertSWCNode(node.value);
|
|
594
|
+
break;
|
|
595
|
+
|
|
596
|
+
case 'JSXIdentifier':
|
|
597
|
+
babelNode.name = node.value;
|
|
598
|
+
break;
|
|
599
|
+
|
|
600
|
+
case 'JSXText':
|
|
601
|
+
babelNode.value = node.value;
|
|
602
|
+
babelNode.raw = node.raw || node.value;
|
|
603
|
+
break;
|
|
604
|
+
|
|
605
|
+
case 'TemplateElement':
|
|
606
|
+
return {
|
|
607
|
+
type: 'TemplateElement',
|
|
608
|
+
value: {
|
|
609
|
+
raw: node.raw ?? node.value?.raw ?? node.cooked ?? '',
|
|
610
|
+
cooked: node.cooked ?? node.value?.cooked ?? node.raw ?? '',
|
|
611
|
+
},
|
|
612
|
+
tail: node.tail ?? false,
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
case 'JSXExpressionContainer':
|
|
616
|
+
babelNode.expression = convertSWCNode(node.expression);
|
|
617
|
+
break;
|
|
618
|
+
|
|
619
|
+
case 'JSXFragment':
|
|
620
|
+
babelNode.openingFragment = { type: 'JSXOpeningFragment' };
|
|
621
|
+
babelNode.closingFragment = { type: 'JSXClosingFragment' };
|
|
622
|
+
babelNode.children = convertSWCNode(node.children || []);
|
|
623
|
+
break;
|
|
624
|
+
|
|
625
|
+
default:
|
|
626
|
+
// ͨ�ô������ݹ�ת����������
|
|
627
|
+
for (const key of Object.keys(node)) {
|
|
628
|
+
if (key === 'type' || key === 'span' || key === 'ctxt') continue;
|
|
629
|
+
babelNode[key] = convertSWCNode(node[key]);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
return babelNode;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* �� SWC AST ת��Ϊ Babel ���ݸ�ʽ
|
|
638
|
+
*/
|
|
639
|
+
export function convertSWCToBabel(swcAst: any): t.File {
|
|
640
|
+
const startTime = performance.now();
|
|
641
|
+
const result = convertSWCNode(swcAst) as t.File;
|
|
642
|
+
const duration = performance.now() - startTime;
|
|
643
|
+
|
|
644
|
+
swcStats.conversionCount++;
|
|
645
|
+
swcStats.conversionTotalTime += duration;
|
|
646
|
+
|
|
647
|
+
return result;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* ʹ�� SWC ��������
|
|
652
|
+
* ��� SWC �����ã����˵� Babel
|
|
653
|
+
*/
|
|
654
|
+
export function parseWithSWC(code: string): t.File {
|
|
655
|
+
const safeCode = sanitizePermissionCommentsForJSX(code);
|
|
656
|
+
if (!swcAvailable || !swcModule) {
|
|
657
|
+
// ���˵� Babel
|
|
658
|
+
const startTime = performance.now();
|
|
659
|
+
const ast = babelParse(safeCode, babelParserConfig);
|
|
660
|
+
swcStats.babelFallbackCount++;
|
|
661
|
+
swcStats.babelTotalTime += performance.now() - startTime;
|
|
662
|
+
return ast;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
try {
|
|
666
|
+
// ʹ�� SWC ����
|
|
667
|
+
const startTime = performance.now();
|
|
668
|
+
const swcAst = swcModule.parseSync(safeCode, swcParserConfig);
|
|
669
|
+
const parseTime = performance.now() - startTime;
|
|
670
|
+
|
|
671
|
+
swcStats.swcParseCount++;
|
|
672
|
+
swcStats.swcTotalTime += parseTime;
|
|
673
|
+
|
|
674
|
+
// ת��Ϊ Babel ��ʽ
|
|
675
|
+
return convertSWCToBabel(swcAst);
|
|
676
|
+
} catch (error) {
|
|
677
|
+
// ����ʧ�ܣ����˵� Babel
|
|
678
|
+
console.warn('[SWC] ����ʧ�ܣ����˵� Babel:', error);
|
|
679
|
+
const startTime = performance.now();
|
|
680
|
+
const ast = babelParse(safeCode, babelParserConfig);
|
|
681
|
+
swcStats.babelFallbackCount++;
|
|
682
|
+
swcStats.babelTotalTime += performance.now() - startTime;
|
|
683
|
+
return ast;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* �첽ʹ�� SWC ��������
|
|
689
|
+
*/
|
|
690
|
+
export async function parseWithSWCAsync(code: string): Promise<t.File> {
|
|
691
|
+
// ȷ�� SWC �ѳ�ʼ��
|
|
692
|
+
await initSWC();
|
|
693
|
+
return parseWithSWC(code);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* ��ȡ SWC ����ͳ��
|
|
698
|
+
*/
|
|
699
|
+
export function getSWCStats() {
|
|
700
|
+
return {
|
|
701
|
+
...swcStats,
|
|
702
|
+
swcAvailable,
|
|
703
|
+
swcAverageParseTime: swcStats.swcParseCount > 0
|
|
704
|
+
? swcStats.swcTotalTime / swcStats.swcParseCount
|
|
705
|
+
: 0,
|
|
706
|
+
babelAverageTime: swcStats.babelFallbackCount > 0
|
|
707
|
+
? swcStats.babelTotalTime / swcStats.babelFallbackCount
|
|
708
|
+
: 0,
|
|
709
|
+
conversionAverageTime: swcStats.conversionCount > 0
|
|
710
|
+
? swcStats.conversionTotalTime / swcStats.conversionCount
|
|
711
|
+
: 0,
|
|
712
|
+
speedup: swcStats.babelFallbackCount > 0 && swcStats.swcParseCount > 0
|
|
713
|
+
? (swcStats.babelTotalTime / swcStats.babelFallbackCount) /
|
|
714
|
+
((swcStats.swcTotalTime + swcStats.conversionTotalTime) / swcStats.swcParseCount)
|
|
715
|
+
: 0,
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* ��ӡ SWC ���ܱ���
|
|
721
|
+
*/
|
|
722
|
+
export function printSWCReport(): void {
|
|
723
|
+
const stats = getSWCStats();
|
|
724
|
+
|
|
725
|
+
console.log('%c=== SWC Parser Report ===', 'color: #FF5722; font-weight: bold;');
|
|
726
|
+
console.log(`SWC ����: ${stats.swcAvailable ? '?' : '?'}`);
|
|
727
|
+
console.log(`%cSWC ��������: ${stats.swcParseCount}`, 'color: #4CAF50;');
|
|
728
|
+
console.log(`%cSWC �ܺ�ʱ: ${stats.swcTotalTime.toFixed(2)}ms`, 'color: #4CAF50;');
|
|
729
|
+
console.log(`%cSWC ƽ����ʱ: ${stats.swcAverageParseTime.toFixed(2)}ms`, 'color: #4CAF50;');
|
|
730
|
+
console.log(`%cBabel ���˴���: ${stats.babelFallbackCount}`, 'color: #FF9800;');
|
|
731
|
+
console.log(`%cBabel �ܺ�ʱ: ${stats.babelTotalTime.toFixed(2)}ms`, 'color: #FF9800;');
|
|
732
|
+
console.log(`%cBabel ƽ����ʱ: ${stats.babelAverageTime.toFixed(2)}ms`, 'color: #FF9800;');
|
|
733
|
+
console.log(`AST ת������: ${stats.conversionCount}`);
|
|
734
|
+
console.log(`AST ת��ƽ����ʱ: ${stats.conversionAverageTime.toFixed(2)}ms`);
|
|
735
|
+
if (stats.speedup > 0) {
|
|
736
|
+
console.log(`%c���ٱ�: ${stats.speedup.toFixed(1)}x`, 'color: #E91E63; font-weight: bold;');
|
|
737
|
+
}
|
|
738
|
+
console.log('%c=========================', 'color: #FF5722; font-weight: bold;');
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// ��¶��ȫ�֣��������
|
|
742
|
+
if (typeof window !== 'undefined') {
|
|
743
|
+
(window as any).__SWC_PARSER__ = {
|
|
744
|
+
initSWC,
|
|
745
|
+
isSWCAvailable,
|
|
746
|
+
parseWithSWC,
|
|
747
|
+
parseWithSWCAsync,
|
|
748
|
+
getSWCStats,
|
|
749
|
+
printSWCReport,
|
|
750
|
+
};
|
|
751
|
+
}
|