1- use swc_common:: util:: take:: Take ;
2- use swc_ecma_ast:: { CallExpr , Expr , Lit , Pass , Regex } ;
3- use swc_ecma_compiler:: compat:: { CompatCompiler , TransformOptions } ;
4- use swc_ecma_utils:: { quote_ident, ExprFactory } ;
5- use swc_ecma_visit:: { noop_visit_mut_type, visit_mut_pass, VisitMut , VisitMutWith } ;
1+ use swc_ecma_ast:: Pass ;
2+ use swc_ecma_compiler:: { TransformOptions , Transformer } ;
63
74pub fn regexp ( config : Config ) -> impl Pass {
85 let mut transform_options = TransformOptions :: default ( ) ;
@@ -16,7 +13,7 @@ pub fn regexp(config: Config) -> impl Pass {
1613 transform_options. env . regexp . unicode_flag = config. unicode_regex ;
1714 transform_options. env . regexp . set_notation = config. unicode_sets_regex ;
1815
19- visit_mut_pass ( CompatCompiler :: new ( & transform_options) )
16+ Transformer :: new ( "" . as_ref ( ) , & transform_options)
2017}
2118
2219#[ derive( Default , Clone , Copy ) ]
@@ -38,47 +35,3 @@ pub struct Config {
3835 // [RegExp.prototype.unicodeSets](https://github.com/tc39/proposal-regexp-v-flag)
3936 pub unicode_sets_regex : bool ,
4037}
41-
42- struct RegExp {
43- config : Config ,
44- }
45-
46- impl VisitMut for RegExp {
47- noop_visit_mut_type ! ( fail) ;
48-
49- fn visit_mut_expr ( & mut self , expr : & mut Expr ) {
50- expr. visit_mut_children_with ( self ) ;
51-
52- if let Expr :: Lit ( Lit :: Regex ( regex) ) = expr {
53- if ( self . config . dot_all_regex && regex. flags . contains ( 's' ) )
54- || ( self . config . sticky_regex && regex. flags . contains ( 'y' ) )
55- || ( self . config . unicode_regex && regex. flags . contains ( 'u' ) )
56- || ( self . config . unicode_sets_regex && regex. flags . contains ( 'v' ) )
57- || ( self . config . has_indices && regex. flags . contains ( 'd' ) )
58- || ( self . config . named_capturing_groups_regex && regex. exp . contains ( "(?<" ) )
59- || ( self . config . lookbehind_assertion && regex. exp . contains ( "(?<=" )
60- || regex. exp . contains ( "(?<!" ) )
61- || ( self . config . unicode_property_regex
62- && ( regex. exp . contains ( "\\ p{" ) || regex. exp . contains ( "\\ P{" ) ) )
63- {
64- let Regex { exp, flags, span } = regex. take ( ) ;
65-
66- let exp: Expr = exp. into ( ) ;
67- let mut args = vec ! [ exp. into( ) ] ;
68-
69- if !flags. is_empty ( ) {
70- let flags: Expr = flags. into ( ) ;
71- args. push ( flags. into ( ) ) ;
72- }
73-
74- * expr = CallExpr {
75- span,
76- callee : quote_ident ! ( "RegExp" ) . as_callee ( ) ,
77- args,
78- ..Default :: default ( )
79- }
80- . into ( )
81- }
82- }
83- }
84- }
0 commit comments