1- use resast:: prelude:: * ;
1+ use resast:: { prelude:: * , SourceText } ;
22use ressa:: Parser ;
33use resw:: Writer ;
44use std:: {
5+ borrow:: Cow ,
56 fs:: { read_to_string, File } ,
67 io:: BufWriter ,
78} ;
@@ -22,15 +23,18 @@ fn main() {
2223 }
2324}
2425
25- fn map_part < ' a > ( args : Vec < Expr < ' a > > , part : ProgramPart < ' a > ) -> ProgramPart < ' a > {
26+ fn map_part < ' a > (
27+ args : Vec < Expr < Cow < ' a , str > > > ,
28+ part : ProgramPart < Cow < ' a , str > > ,
29+ ) -> ProgramPart < Cow < ' a , str > > {
2630 match part {
2731 ProgramPart :: Decl ( decl) => ProgramPart :: Decl ( map_decl ( args, decl) ) ,
2832 ProgramPart :: Stmt ( stmt) => ProgramPart :: Stmt ( map_stmt ( args, stmt) ) ,
2933 ProgramPart :: Dir ( _) => part,
3034 }
3135}
3236
33- fn map_decl < ' a > ( mut args : Vec < Expr < ' a > > , decl : Decl < ' a > ) -> Decl < ' a > {
37+ fn map_decl < ' a > ( mut args : Vec < Expr < Cow < ' a , str > > > , decl : Decl < Cow < ' a , str > > ) -> Decl < Cow < ' a , str > > {
3438 match decl {
3539 Decl :: Func ( f) => Decl :: Func ( map_func ( args, f) ) ,
3640 Decl :: Class ( class) => Decl :: Class ( map_class ( args, class) ) ,
@@ -52,14 +56,14 @@ fn map_decl<'a>(mut args: Vec<Expr<'a>>, decl: Decl<'a>) -> Decl<'a> {
5256 }
5357}
5458
55- fn map_stmt < ' a > ( args : Vec < Expr < ' a > > , stmt : Stmt < ' a > ) -> Stmt < ' a > {
59+ fn map_stmt < ' a > ( args : Vec < Expr < Cow < ' a , str > > > , stmt : Stmt < Cow < ' a , str > > ) -> Stmt < Cow < ' a , str > > {
5660 match stmt {
5761 Stmt :: Expr ( expr) => Stmt :: Expr ( map_expr ( args, expr) ) ,
5862 _ => stmt. clone ( ) ,
5963 }
6064}
6165
62- fn map_expr < ' a > ( mut args : Vec < Expr < ' a > > , expr : Expr < ' a > ) -> Expr < ' a > {
66+ fn map_expr < ' a > ( mut args : Vec < Expr < Cow < ' a , str > > > , expr : Expr < Cow < ' a , str > > ) -> Expr < Cow < ' a , str > > {
6367 match expr {
6468 Expr :: Func ( f) => Expr :: Func ( map_func ( args, f) ) ,
6569 Expr :: Class ( c) => Expr :: Class ( map_class ( args, c) ) ,
@@ -75,7 +79,10 @@ fn map_expr<'a>(mut args: Vec<Expr<'a>>, expr: Expr<'a>) -> Expr<'a> {
7579 }
7680}
7781
78- fn map_func < ' a > ( mut args : Vec < Expr < ' a > > , mut func : Func < ' a > ) -> Func < ' a > {
82+ fn map_func < ' a > (
83+ mut args : Vec < Expr < Cow < ' a , str > > > ,
84+ mut func : Func < Cow < ' a , str > > ,
85+ ) -> Func < Cow < ' a , str > > {
7986 if let Some ( ref id) = & func. id {
8087 args. push ( ident_to_string_lit ( id) ) ;
8188 }
@@ -99,7 +106,10 @@ fn map_func<'a>(mut args: Vec<Expr<'a>>, mut func: Func<'a>) -> Func<'a> {
99106 func
100107}
101108
102- fn map_class < ' a > ( mut args : Vec < Expr < ' a > > , mut class : Class < ' a > ) -> Class < ' a > {
109+ fn map_class < ' a > (
110+ mut args : Vec < Expr < Cow < ' a , str > > > ,
111+ mut class : Class < Cow < ' a , str > > ,
112+ ) -> Class < Cow < ' a , str > > {
103113 if let Some ( ref id) = class. id {
104114 args. push ( ident_to_string_lit ( id) )
105115 }
@@ -111,26 +121,29 @@ fn map_class<'a>(mut args: Vec<Expr<'a>>, mut class: Class<'a>) -> Class<'a> {
111121 class
112122}
113123
114- fn map_class_prop < ' a > ( mut args : Vec < Expr < ' a > > , mut prop : Prop < ' a > ) -> Prop < ' a > {
124+ fn map_class_prop < ' a > (
125+ mut args : Vec < Expr < Cow < ' a , str > > > ,
126+ mut prop : Prop < Cow < ' a , str > > ,
127+ ) -> Prop < Cow < ' a , str > > {
115128 match prop. kind {
116129 PropKind :: Ctor => {
117130 args. insert (
118131 args. len ( ) . saturating_sub ( 1 ) ,
119- Expr :: Lit ( Lit :: String ( StringLit :: single_from ( "new" ) ) ) ,
132+ Expr :: Lit ( Lit :: String ( StringLit :: single_from ( "new" . into ( ) ) ) ) ,
120133 ) ;
121134 }
122135 PropKind :: Get => {
123- args. push ( Expr :: Lit ( Lit :: String ( StringLit :: single_from ( "get" ) ) ) ) ;
136+ args. push ( Expr :: Lit ( Lit :: String ( StringLit :: single_from ( "get" . into ( ) ) ) ) ) ;
124137 }
125138 PropKind :: Set => {
126- args. push ( Expr :: Lit ( Lit :: String ( StringLit :: single_from ( "set" ) ) ) ) ;
139+ args. push ( Expr :: Lit ( Lit :: String ( StringLit :: single_from ( "set" . into ( ) ) ) ) ) ;
127140 }
128141 _ => ( ) ,
129142 } ;
130143 match & prop. key {
131144 PropKey :: Expr ( ref expr) => match expr {
132145 Expr :: Ident ( ref i) => {
133- if i. name != "constructor" {
146+ if i. name . as_ref ( ) != "constructor" {
134147 args. push ( ident_to_string_lit ( i) ) ;
135148 }
136149 }
@@ -141,9 +154,9 @@ fn map_class_prop<'a>(mut args: Vec<Expr<'a>>, mut prop: Prop<'a>) -> Prop<'a> {
141154 args. push ( Expr :: Lit ( l. clone ( ) ) )
142155 }
143156 Lit :: Null => {
144- args. push ( Expr :: Lit ( Lit :: String ( StringLit :: Single (
157+ args. push ( Expr :: Lit ( Lit :: String ( StringLit :: Single ( SourceText (
145158 :: std:: borrow:: Cow :: Owned ( String :: from ( "null" ) ) ,
146- ) ) ) ) ;
159+ ) ) ) ) ) ;
147160 }
148161 _ => ( ) ,
149162 } ,
@@ -158,7 +171,10 @@ fn map_class_prop<'a>(mut args: Vec<Expr<'a>>, mut prop: Prop<'a>) -> Prop<'a> {
158171 prop
159172}
160173
161- fn map_arrow_func < ' a > ( mut args : Vec < Expr < ' a > > , mut f : ArrowFuncExpr < ' a > ) -> ArrowFuncExpr < ' a > {
174+ fn map_arrow_func < ' a > (
175+ mut args : Vec < Expr < Cow < ' a , str > > > ,
176+ mut f : ArrowFuncExpr < Cow < ' a , str > > ,
177+ ) -> ArrowFuncExpr < Cow < ' a , str > > {
162178 args. extend ( extract_idents_from_args ( & f. params ) ) ;
163179 match & mut f. body {
164180 ArrowFuncBody :: FuncBody ( ref mut body) => {
@@ -174,7 +190,7 @@ fn map_arrow_func<'a>(mut args: Vec<Expr<'a>>, mut f: ArrowFuncExpr<'a>) -> Arro
174190 f
175191}
176192
177- fn assign_left_to_string_lit < ' a > ( left : & AssignLeft < ' a > ) -> Option < Expr < ' a > > {
193+ fn assign_left_to_string_lit < ' a > ( left : & AssignLeft < Cow < ' a , str > > ) -> Option < Expr < Cow < ' a , str > > > {
178194 match left {
179195 AssignLeft :: Expr ( expr) => expr_to_string_lit ( expr) ,
180196 AssignLeft :: Pat ( pat) => match pat {
@@ -184,7 +200,7 @@ fn assign_left_to_string_lit<'a>(left: &AssignLeft<'a>) -> Option<Expr<'a>> {
184200 }
185201}
186202
187- fn extract_idents_from_args < ' a > ( args : & [ FuncArg < ' a > ] ) -> Vec < Expr < ' a > > {
203+ fn extract_idents_from_args < ' a > ( args : & [ FuncArg < Cow < ' a , str > > ] ) -> Vec < Expr < Cow < ' a , str > > > {
188204 let mut ret = vec ! [ ] ;
189205 for arg in args {
190206 match arg {
@@ -195,14 +211,14 @@ fn extract_idents_from_args<'a>(args: &[FuncArg<'a>]) -> Vec<Expr<'a>> {
195211 ret. into_iter ( ) . filter_map ( |e| e) . collect ( )
196212}
197213
198- fn extract_ident_from_expr < ' a > ( expr : & Expr < ' a > ) -> Option < Expr < ' a > > {
214+ fn extract_ident_from_expr < ' a > ( expr : & Expr < Cow < ' a , str > > ) -> Option < Expr < Cow < ' a , str > > > {
199215 match expr {
200216 Expr :: Ident ( ident) => Some ( Expr :: Ident ( ident. clone ( ) ) ) ,
201217 _ => None ,
202218 }
203219}
204220
205- fn extract_idents_from_pat < ' a > ( pat : & Pat < ' a > ) -> Vec < Option < Expr < ' a > > > {
221+ fn extract_idents_from_pat < ' a > ( pat : & Pat < Cow < ' a , str > > ) -> Vec < Option < Expr < Cow < ' a , str > > > > {
206222 match pat {
207223 Pat :: Ident ( i) => {
208224 vec ! [ Some ( Expr :: Ident ( i. clone( ) ) ) ]
@@ -239,14 +255,14 @@ fn extract_idents_from_pat<'a>(pat: &Pat<'a>) -> Vec<Option<Expr<'a>>> {
239255 }
240256}
241257
242- fn expr_to_string_lit < ' a > ( e : & Expr < ' a > ) -> Option < Expr < ' a > > {
258+ fn expr_to_string_lit < ' a > ( e : & Expr < Cow < ' a , str > > ) -> Option < Expr < Cow < ' a , str > > > {
243259 let inner = expr_to_string ( e) ?;
244- Some ( Expr :: Lit ( Lit :: String ( StringLit :: Single (
260+ Some ( Expr :: Lit ( Lit :: String ( StringLit :: Single ( SourceText (
245261 :: std:: borrow:: Cow :: Owned ( inner) ,
246- ) ) ) )
262+ ) ) ) ) )
247263}
248264
249- fn expr_to_string ( expr : & Expr ) -> Option < String > {
265+ fn expr_to_string < ' a > ( expr : & Expr < Cow < ' a , str > > ) -> Option < String > {
250266 match expr {
251267 Expr :: Ident ( ref ident) => Some ( ident. name . to_string ( ) ) ,
252268 Expr :: This => Some ( "this" . to_string ( ) ) ,
@@ -266,7 +282,13 @@ fn expr_to_string(expr: &Expr) -> Option<String> {
266282 Expr :: Lit ( lit) => match lit {
267283 Lit :: String ( s) => Some ( s. clone_inner ( ) . to_string ( ) ) ,
268284 Lit :: Number ( n) => Some ( n. to_string ( ) ) ,
269- Lit :: RegEx ( r) => Some ( format ! ( "/{}/{}" , r. pattern, r. flags) ) ,
285+ Lit :: RegEx ( r) => {
286+ if let Some ( flags) = & r. flags {
287+ Some ( format ! ( "/{}/{}" , r. pattern, flags) )
288+ } else {
289+ Some ( format ! ( "/{}/" , r. pattern) )
290+ }
291+ }
270292 Lit :: Boolean ( b) => Some ( b. to_string ( ) ) ,
271293 Lit :: Null => Some ( "null" . to_string ( ) ) ,
272294 _ => None ,
@@ -275,24 +297,27 @@ fn expr_to_string(expr: &Expr) -> Option<String> {
275297 }
276298}
277299
278- fn ident_to_string_lit < ' a > ( i : & Ident < ' a > ) -> Expr < ' a > {
300+ fn ident_to_string_lit < ' a > ( i : & Ident < Cow < ' a , str > > ) -> Expr < Cow < ' a , str > > {
279301 Expr :: Lit ( Lit :: String ( StringLit :: Single ( i. name . clone ( ) ) ) )
280302}
281303
282- fn insert_expr_into_func < ' a > ( expr : ProgramPart < ' a > , func : & mut Func < ' a > ) {
304+ fn insert_expr_into_func < ' a > ( expr : ProgramPart < Cow < ' a , str > > , func : & mut Func < Cow < ' a , str > > ) {
283305 insert_expr_into_func_body ( expr, & mut func. body ) ;
284306}
285307
286- fn insert_expr_into_func_body < ' a > ( expr : ProgramPart < ' a > , body : & mut FuncBody < ' a > ) {
308+ fn insert_expr_into_func_body < ' a > (
309+ expr : ProgramPart < Cow < ' a , str > > ,
310+ body : & mut FuncBody < Cow < ' a , str > > ,
311+ ) {
287312 body. 0 . insert ( 0 , expr) ;
288313}
289314
290- pub fn console_log < ' a > ( args : Vec < Expr < ' a > > ) -> ProgramPart < ' a > {
315+ pub fn console_log < ' a > ( args : Vec < Expr < Cow < ' a , str > > > ) -> ProgramPart < Cow < ' a , str > > {
291316 ProgramPart :: Stmt ( Stmt :: Expr ( Expr :: Call ( CallExpr {
292317 callee : Box :: new ( Expr :: Member ( MemberExpr {
293318 computed : false ,
294- object : Box :: new ( Expr :: ident_from ( "console" ) ) ,
295- property : Box :: new ( Expr :: ident_from ( "log" ) ) ,
319+ object : Box :: new ( Expr :: Ident ( Cow :: Borrowed ( "console" ) . into ( ) ) ) ,
320+ property : Box :: new ( Expr :: Ident ( Cow :: Borrowed ( "log" ) . into ( ) ) ) ,
296321 } ) ) ,
297322 arguments : args,
298323 } ) ) )
0 commit comments