@@ -7,7 +7,7 @@ pub mod oxc {
77 parser:: Parser ,
88 semantic:: SemanticBuilder ,
99 span:: SourceType ,
10- transformer:: { JsxOptions , TransformOptions , Transformer , TypeScriptOptions } ,
10+ transformer:: { TransformOptions , Transformer } ,
1111 } ;
1212
1313 pub fn transform ( path : & Path , source_text : & str ) -> ( Allocator , String ) {
@@ -16,11 +16,7 @@ pub mod oxc {
1616 let printed = {
1717 let ret = Parser :: new ( & allocator, source_text, source_type) . parse ( ) ;
1818 let mut program = ret. program ;
19- let transform_options = TransformOptions {
20- typescript : TypeScriptOptions :: default ( ) ,
21- jsx : JsxOptions :: default ( ) ,
22- ..TransformOptions :: default ( )
23- } ;
19+ let transform_options = TransformOptions :: from_target ( "es2015" ) . unwrap ( ) ;
2420 let ( symbols, scopes) = SemanticBuilder :: new ( )
2521 . build ( & program)
2622 . semantic
@@ -38,16 +34,18 @@ pub mod oxc {
3834pub mod swc {
3935 use std:: { path:: Path , sync:: Arc } ;
4036
41- use swc:: { Compiler , PrintArgs , SwcComments } ;
37+ use swc:: { try_with_handler , Compiler , PrintArgs , SwcComments } ;
4238 use swc_common:: { source_map:: SourceMap , sync:: Lrc , Mark , GLOBALS } ;
43- use swc_ecma_ast:: Program ;
4439 use swc_ecma_parser:: { EsSyntax , Parser , StringInput , Syntax , TsSyntax } ;
45- use swc_ecma_transforms:: resolver;
40+ use swc_ecma_transforms:: {
41+ compat:: { es2016, es2017, es2018, es2019, es2020, es2021, es2022} ,
42+ resolver,
43+ } ;
4644 use swc_ecma_transforms_react:: { react, Options , Runtime } ;
4745 use swc_ecma_transforms_typescript:: strip;
4846 use swc_ecma_visit:: VisitMutWith ;
4947
50- pub fn transform ( path : & Path , source_text : & str ) -> ( Program , String ) {
48+ pub fn transform ( path : & Path , source_text : & str ) -> String {
5149 let cm = Lrc :: new ( SourceMap :: new ( swc_common:: FilePathMapping :: empty ( ) ) ) ;
5250 let compiler = Compiler :: new ( Arc :: clone ( & cm) ) ;
5351 let comments = SwcComments :: default ( ) ;
@@ -61,40 +59,50 @@ pub mod swc {
6159 } ;
6260
6361 GLOBALS . set ( & Default :: default ( ) , || {
64- let input = StringInput :: new ( source_text, Default :: default ( ) , Default :: default ( ) ) ;
65- let mut program = Parser :: new ( syntax, input, Some ( & comments) )
66- . parse_program ( )
67- . unwrap ( ) ;
68-
69- let top_level_mark = Mark :: new ( ) ;
70- let unresolved_mark = Mark :: new ( ) ;
62+ try_with_handler ( cm. clone ( ) , Default :: default ( ) , |_handler| {
63+ let input = StringInput :: new ( source_text, Default :: default ( ) , Default :: default ( ) ) ;
64+ let mut program = Parser :: new ( syntax, input, Some ( & comments) )
65+ . parse_program ( )
66+ . unwrap ( ) ;
7167
72- program. visit_mut_with ( & mut resolver (
73- unresolved_mark,
74- top_level_mark,
75- syntax. typescript ( ) ,
76- ) ) ;
68+ let top_level_mark = Mark :: new ( ) ;
69+ let unresolved_mark = Mark :: new ( ) ;
7770
78- let mut ast_pass = (
79- strip ( unresolved_mark, top_level_mark) ,
80- react (
81- Arc :: clone ( & cm) ,
82- Some ( comments) ,
83- Options {
84- runtime : Some ( Runtime :: Automatic ) ,
85- ..Options :: default ( )
86- } ,
87- top_level_mark,
71+ program. visit_mut_with ( & mut resolver (
8872 unresolved_mark,
89- ) ,
90- ) ;
91- let program = program. apply ( & mut ast_pass) ;
73+ top_level_mark,
74+ syntax. typescript ( ) ,
75+ ) ) ;
76+
77+ let mut ast_pass = (
78+ strip ( unresolved_mark, top_level_mark) ,
79+ react (
80+ Arc :: clone ( & cm) ,
81+ Some ( comments) ,
82+ Options {
83+ runtime : Some ( Runtime :: Automatic ) ,
84+ ..Options :: default ( )
85+ } ,
86+ top_level_mark,
87+ unresolved_mark,
88+ ) ,
89+ es2022 ( Default :: default ( ) , unresolved_mark) ,
90+ es2021 ( ) ,
91+ es2020 ( Default :: default ( ) , unresolved_mark) ,
92+ es2019 ( ) ,
93+ es2018 ( Default :: default ( ) ) ,
94+ es2017 ( Default :: default ( ) , unresolved_mark) ,
95+ es2016 ( ) ,
96+ ) ;
97+ let program = program. apply ( & mut ast_pass) ;
9298
93- let printed = compiler
94- . print ( & program, PrintArgs :: default ( ) )
95- . expect ( "print failed" ) ;
99+ let printed = compiler
100+ . print ( & program, PrintArgs :: default ( ) )
101+ . expect ( "print failed" ) ;
96102
97- ( program, printed. code )
103+ Ok ( printed. code )
104+ } )
105+ . unwrap ( )
98106 } )
99107 }
100108}
0 commit comments