@@ -35,7 +35,7 @@ use path_slash::PathExt;
35
35
use serde:: { Deserialize , Serialize } ;
36
36
use swc_core:: {
37
37
common:: {
38
- chain , comments:: SingleThreadedComments , errors:: Handler , pass:: Optional ,
38
+ comments:: SingleThreadedComments , errors:: Handler , pass:: Optional ,
39
39
source_map:: SourceMapGenConfig , sync:: Lrc , FileName , Globals , Mark , SourceMap ,
40
40
} ,
41
41
ecma:: {
@@ -45,17 +45,18 @@ use swc_core::{
45
45
preset_env:: { preset_env, Mode :: Entry , Targets , Version , Versions } ,
46
46
transforms:: {
47
47
base:: {
48
+ assumptions:: Assumptions ,
48
49
fixer:: { fixer, paren_remover} ,
49
50
helpers,
50
51
hygiene:: hygiene,
51
- resolver, Assumptions ,
52
+ resolver,
52
53
} ,
53
54
compat:: reserved_words:: reserved_words,
54
55
optimization:: simplify:: { dead_branch_remover, expr_simplifier} ,
55
56
proposal:: decorators,
56
57
react, typescript,
57
58
} ,
58
- visit:: { as_folder , FoldWith , VisitWith } ,
59
+ visit:: { FoldWith , VisitMutWith , VisitWith } ,
59
60
} ,
60
61
} ;
61
62
use typeof_replacer:: * ;
@@ -231,7 +232,7 @@ pub fn transform(
231
232
232
233
let global_mark = Mark :: fresh ( Mark :: root ( ) ) ;
233
234
let unresolved_mark = Mark :: fresh ( Mark :: root ( ) ) ;
234
- let module = module . fold_with ( & mut chain ! (
235
+ module. mutate ( & mut (
235
236
resolver ( unresolved_mark, global_mark, config. is_type_script ) ,
236
237
// Decorators can use type information, so must run before the TypeScript pass.
237
238
Optional :: new (
@@ -242,7 +243,7 @@ pub fn transform(
242
243
// use_define_for_class_fields is ignored here, uses preset-env assumptions instead
243
244
..Default :: default ( )
244
245
} ) ,
245
- config. decorators
246
+ config. decorators ,
246
247
) ,
247
248
Optional :: new (
248
249
typescript:: tsx (
@@ -256,11 +257,11 @@ pub fn transform(
256
257
unresolved_mark,
257
258
global_mark,
258
259
) ,
259
- config. is_type_script && config. is_jsx
260
+ config. is_type_script && config. is_jsx ,
260
261
) ,
261
262
Optional :: new (
262
263
typescript:: strip ( unresolved_mark, global_mark) ,
263
- config. is_type_script && !config. is_jsx
264
+ config. is_type_script && !config. is_jsx ,
264
265
) ,
265
266
) ) ;
266
267
@@ -276,7 +277,8 @@ pub fn transform(
276
277
} ,
277
278
} ;
278
279
279
- let mut module = module. fold_with ( & mut Optional :: new (
280
+ let mut program = Program :: Module ( module) ;
281
+ program. mutate ( & mut Optional :: new (
280
282
react:: react (
281
283
source_map. clone ( ) ,
282
284
Some ( & comments) ,
@@ -286,6 +288,7 @@ pub fn transform(
286
288
) ,
287
289
config. is_jsx ,
288
290
) ) ;
291
+ let mut module = program. expect_module ( ) ;
289
292
290
293
let mut preset_env_config = swc_core:: ecma:: preset_env:: Config {
291
294
dynamic_import : true ,
@@ -326,111 +329,104 @@ pub fn transform(
326
329
result. is_constant_module = constant_module. is_constant_module ;
327
330
}
328
331
329
- let module = {
330
- let mut passes = chain ! (
331
- Optional :: new(
332
- as_folder( TypeofReplacer :: new( unresolved_mark) ) ,
333
- config. source_type != SourceType :: Script ,
334
- ) ,
335
- // Inline process.env and process.browser,
336
- Optional :: new(
337
- as_folder( EnvReplacer {
338
- replace_env: config. replace_env,
339
- env: & config. env,
340
- is_browser: config. is_browser,
341
- used_env: & mut result. used_env,
342
- source_map: source_map. clone( ) ,
343
- diagnostics: & mut diagnostics,
344
- unresolved_mark
345
- } ) ,
346
- config. source_type != SourceType :: Script
347
- ) ,
348
- paren_remover( Some ( & comments) ) ,
349
- // Simplify expressions and remove dead branches so that we
350
- // don't include dependencies inside conditionals that are always false.
351
- expr_simplifier( unresolved_mark, Default :: default ( ) ) ,
352
- dead_branch_remover( unresolved_mark) ,
353
- // Inline Node fs.readFileSync calls
354
- Optional :: new(
355
- inline_fs(
356
- config. filename. as_str( ) ,
357
- source_map. clone( ) ,
358
- unresolved_mark,
359
- global_mark,
360
- & config. project_root,
361
- & mut fs_deps,
362
- is_module
363
- ) ,
364
- should_inline_fs
365
- ) ,
366
- ) ;
332
+ module. visit_mut_with ( & mut (
333
+ Optional :: new (
334
+ TypeofReplacer :: new ( unresolved_mark) ,
335
+ config. source_type != SourceType :: Script ,
336
+ ) ,
337
+ // Inline process.env and process.browser,
338
+ Optional :: new (
339
+ EnvReplacer {
340
+ replace_env : config. replace_env ,
341
+ env : & config. env ,
342
+ is_browser : config. is_browser ,
343
+ used_env : & mut result. used_env ,
344
+ source_map : source_map. clone ( ) ,
345
+ diagnostics : & mut diagnostics,
346
+ unresolved_mark,
347
+ } ,
348
+ config. source_type != SourceType :: Script ,
349
+ ) ,
350
+ paren_remover ( Some ( & comments) ) ,
351
+ // Simplify expressions and remove dead branches so that we
352
+ // don't include dependencies inside conditionals that are always false.
353
+ expr_simplifier ( unresolved_mark, Default :: default ( ) ) ,
354
+ dead_branch_remover ( unresolved_mark) ,
355
+ ) ) ;
367
356
368
- module. fold_with ( & mut passes)
369
- } ;
357
+ let mut module = module. fold_with ( & mut Optional :: new (
358
+ inline_fs (
359
+ config. filename . as_str ( ) ,
360
+ source_map. clone ( ) ,
361
+ unresolved_mark,
362
+ global_mark,
363
+ & config. project_root ,
364
+ & mut fs_deps,
365
+ is_module,
366
+ ) ,
367
+ should_inline_fs,
368
+ ) ) ;
370
369
371
- let module = module . fold_with (
370
+ module. visit_mut_with (
372
371
// Replace __dirname and __filename with placeholders in Node env
373
372
& mut Optional :: new (
374
- as_folder ( NodeReplacer {
373
+ NodeReplacer {
375
374
source_map : source_map. clone ( ) ,
376
375
items : & mut global_deps,
377
376
global_mark,
378
377
globals : HashMap :: new ( ) ,
379
378
filename : Path :: new ( & config. filename ) ,
380
379
unresolved_mark,
381
380
has_node_replacements : & mut result. has_node_replacements ,
382
- } ) ,
381
+ } ,
383
382
config. node_replacer ,
384
383
) ,
385
384
) ;
386
385
387
- let module = {
388
- let mut passes = chain ! (
389
- // Insert dependencies for node globals
390
- Optional :: new(
391
- as_folder( GlobalReplacer {
392
- source_map: source_map. clone( ) ,
393
- items: & mut global_deps,
394
- global_mark,
395
- globals: IndexMap :: new( ) ,
396
- project_root: Path :: new( & config. project_root) ,
397
- filename: Path :: new( & config. filename) ,
398
- unresolved_mark,
399
- scope_hoist: config. scope_hoist
400
- } ) ,
401
- config. insert_node_globals
402
- ) ,
403
- // Transpile new syntax to older syntax if needed
404
- Optional :: new(
405
- preset_env(
406
- unresolved_mark,
407
- Some ( & comments) ,
408
- preset_env_config,
409
- assumptions,
410
- & mut Default :: default ( ) ,
411
- ) ,
412
- should_run_preset_env,
413
- ) ,
414
- // Inject SWC helpers if needed.
415
- helpers:: inject_helpers( global_mark) ,
416
- ) ;
386
+ module. visit_mut_with (
387
+ // Insert dependencies for node globals
388
+ & mut Optional :: new (
389
+ GlobalReplacer {
390
+ source_map : source_map. clone ( ) ,
391
+ items : & mut global_deps,
392
+ global_mark,
393
+ globals : IndexMap :: new ( ) ,
394
+ project_root : Path :: new ( & config. project_root ) ,
395
+ filename : Path :: new ( & config. filename ) ,
396
+ unresolved_mark,
397
+ scope_hoist : config. scope_hoist ,
398
+ } ,
399
+ config. insert_node_globals ,
400
+ ) ,
401
+ ) ;
417
402
418
- module. fold_with ( & mut passes)
419
- } ;
403
+ let mut program = Program :: Module ( module) ;
404
+ program. mutate ( & mut (
405
+ // Transpile new syntax to older syntax if needed
406
+ Optional :: new (
407
+ preset_env (
408
+ unresolved_mark,
409
+ Some ( & comments) ,
410
+ preset_env_config,
411
+ assumptions,
412
+ & mut Default :: default ( ) ,
413
+ ) ,
414
+ should_run_preset_env,
415
+ ) ,
416
+ // Inject SWC helpers if needed.
417
+ helpers:: inject_helpers ( global_mark) ,
418
+ ) ) ;
419
+ let mut module = program. expect_module ( ) ;
420
420
421
421
// Flush Id=(JsWord, SyntaxContexts) into unique names and reresolve to
422
422
// set global_mark for all nodes, even generated ones.
423
423
// - This will also remove any other other marks (like ignore_mark)
424
424
// This only needs to be done if preset_env ran because all other transforms
425
425
// insert declarations with global_mark (even though they are generated).
426
- let module = if config. scope_hoist && should_run_preset_env {
427
- module. fold_with ( & mut chain ! (
428
- hygiene( ) ,
429
- resolver( unresolved_mark, global_mark, false )
430
- ) )
431
- } else {
426
+ if config. scope_hoist && should_run_preset_env {
432
427
module
433
- } ;
428
+ . visit_mut_with ( & mut ( hygiene ( ) , resolver ( unresolved_mark, global_mark, false ) ) )
429
+ }
434
430
435
431
let ignore_mark = Mark :: fresh ( Mark :: root ( ) ) ;
436
432
let module = module. fold_with (
@@ -468,7 +464,7 @@ pub fn transform(
468
464
diagnostics. extend ( bailouts. iter ( ) . map ( |bailout| bailout. to_diagnostic ( ) ) ) ;
469
465
}
470
466
471
- let module = if config. scope_hoist {
467
+ let mut module = if config. scope_hoist {
472
468
let res = hoist ( module, config. module_id . as_str ( ) , unresolved_mark, & collect) ;
473
469
match res {
474
470
Ok ( ( module, hoist_result, hoist_diagnostics) ) => {
@@ -492,11 +488,7 @@ pub fn transform(
492
488
module
493
489
} ;
494
490
495
- let module = module. fold_with ( & mut chain ! (
496
- reserved_words( ) ,
497
- hygiene( ) ,
498
- fixer( Some ( & comments) ) ,
499
- ) ) ;
491
+ module. visit_mut_with ( & mut ( reserved_words ( ) , hygiene ( ) , fixer ( Some ( & comments) ) ) ) ;
500
492
501
493
result. dependencies . extend ( global_deps) ;
502
494
result. dependencies . extend ( fs_deps) ;
0 commit comments