@@ -163,18 +163,18 @@ fn fn_end_insertion(body: &ExprOrBlock) -> InsertionList {
163163}
164164
165165fn is_in_async_function ( node : & SyntaxNode ) -> bool {
166- return node. parent ( ) . map_or ( false , |parent : SyntaxNode | {
166+ node. parent ( ) . map_or ( false , |parent : SyntaxNode | {
167167 if FnExpr :: can_cast ( parent. kind ( ) ) {
168- return FnExpr :: cast ( parent) . unwrap ( ) . async_token ( ) . is_some ( ) ;
168+ return FnExpr :: cast ( parent) . map_or ( false , |e| e . async_token ( ) . is_some ( ) ) ;
169169 }
170170 if FnDecl :: can_cast ( parent. kind ( ) ) {
171- return FnDecl :: cast ( parent) . unwrap ( ) . async_token ( ) . is_some ( ) ;
171+ return FnDecl :: cast ( parent) . map_or ( false , |e| e . async_token ( ) . is_some ( ) ) ;
172172 }
173173 if ArrowExpr :: can_cast ( parent. kind ( ) ) {
174- return ArrowExpr :: cast ( parent) . unwrap ( ) . async_token ( ) . is_some ( ) ;
174+ return ArrowExpr :: cast ( parent) . map_or ( false , |e| e . async_token ( ) . is_some ( ) ) ;
175175 }
176176 if Method :: can_cast ( parent. kind ( ) ) {
177- return Method :: cast ( parent) . unwrap ( ) . async_token ( ) . is_some ( ) ;
177+ return Method :: cast ( parent) . map_or ( false , |e| e . async_token ( ) . is_some ( ) ) ;
178178 }
179179 if Constructor :: can_cast ( parent. kind ( ) ) {
180180 return false ;
@@ -186,18 +186,18 @@ fn is_in_async_function(node: &SyntaxNode) -> bool {
186186 return false ;
187187 }
188188 assert ! ( !is_function_node( & parent) ) ;
189- return is_in_async_function ( & parent)
190- } ) ;
189+ is_in_async_function ( & parent)
190+ } )
191191}
192192
193193fn is_function_node ( node : & SyntaxNode ) -> bool {
194- return FnExpr :: can_cast ( node. kind ( ) ) ||
194+ FnExpr :: can_cast ( node. kind ( ) ) ||
195195 FnDecl :: can_cast ( node. kind ( ) ) ||
196196 ArrowExpr :: can_cast ( node. kind ( ) ) ||
197197 Method :: can_cast ( node. kind ( ) ) ||
198198 Constructor :: can_cast ( node. kind ( ) ) ||
199199 ClassExpr :: can_cast ( node. kind ( ) ) ||
200- ClassDecl :: can_cast ( node. kind ( ) ) ;
200+ ClassDecl :: can_cast ( node. kind ( ) )
201201}
202202
203203fn add_all_variables_from_declaration ( patterns : impl Iterator < Item = impl Borrow < Pattern > > ) -> InsertionList {
@@ -254,38 +254,31 @@ fn add_all_variables_from_declaration(patterns: impl Iterator<Item = impl Borrow
254254
255255fn is_name_ref ( syntax_node : & SyntaxNode , names : Option < & ' static [ & ' static str ] > ) -> bool {
256256 if !NameRef :: can_cast ( syntax_node. kind ( ) ) {
257- if GroupingExpr :: can_cast ( syntax_node. kind ( ) ) {
258- let inner = GroupingExpr :: cast ( syntax_node. clone ( ) ) . unwrap ( ) . inner ( ) ;
259- if inner. is_none ( ) {
260- return false ;
261- }
262- return is_name_ref ( inner. unwrap ( ) . syntax ( ) , names) ;
263- }
264- return false ;
265- }
266- if names. is_none ( ) {
267- return true ;
257+ let inner = GroupingExpr :: cast ( syntax_node. clone ( ) ) . and_then ( |e| e. inner ( ) ) ;
258+ return inner. map_or ( false , |e| is_name_ref ( e. syntax ( ) , names) ) ;
268259 }
269- return names. unwrap ( ) . iter ( ) . any ( |t| * t == syntax_node. text ( ) . to_string ( ) . as_str ( ) )
260+ names. map_or ( true , |names| names . iter ( ) . any ( |t| * t == syntax_node. text ( ) . to_string ( ) . as_str ( ) ) )
270261}
271262
272- fn collect_insertions ( node : & SyntaxNode , nesting_depth : u32 ) -> InsertionList {
263+ fn collect_insertions ( node : & SyntaxNode , nesting_depth : u32 , with_debug_tags : bool ) -> Result < InsertionList , & ' static str > {
273264 let has_function_parent = nesting_depth > 0 ;
274265 let mut insertions = InsertionList :: new ( ) ;
275266 for child in node. children ( ) {
276267 let range = child. text_range ( ) ;
277- let child_insertions = & mut collect_insertions ( & child, nesting_depth + if is_function_node ( node) { 1 } else { 0 } ) ;
268+ let child_insertions = & mut collect_insertions (
269+ & child,
270+ nesting_depth + if is_function_node ( node) { 1 } else { 0 } , with_debug_tags) ?;
278271 {
279272 let kind = child. kind ( ) ;
280273 if kind != SyntaxKind :: TEMPLATE_ELEMENT {
281274 insertions. push_back ( Insertion :: new_dynamic ( range. start ( ) ,
282- [ " /*" , format ! ( "{kind:#?}" ) . as_str ( ) , " */ "] . concat ( )
275+ format ! ( " /* {kind:#?}*/ " )
283276 ) ) ;
284277 }
285278 }
286279 if FnDecl :: can_cast ( child. kind ( ) ) {
287- let as_fn = FnDecl :: cast ( child) . unwrap ( ) ;
288- let body = ExprOrBlock :: Block ( as_fn. body ( ) . unwrap ( ) ) ;
280+ let as_fn = FnDecl :: cast ( child) . ok_or ( "bad FnDecl" ) ? ;
281+ let body = ExprOrBlock :: Block ( as_fn. body ( ) . ok_or ( "bad FnDecl without body" ) ? ) ;
289282 if !has_function_parent {
290283 match as_fn. ident_token ( ) . or ( as_fn. name ( ) . and_then ( |n| n. ident_token ( ) ) ) {
291284 None => {
@@ -294,7 +287,7 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
294287 Some ( name) => {
295288 insertions. push_back ( Insertion :: new ( name. text_range ( ) . end ( ) , "__" ) ) ;
296289 insertions. push_back ( Insertion :: new_dynamic ( range. end ( ) ,
297- [ ";\n _cr = " , name. text ( ) , " = " , name. text ( ) , " __;\n "] . concat ( )
290+ format ! ( ";\n _cr = { name} = { name} __;\n " , name = name . text ( ) )
298291 ) ) ;
299292 insertions. add_variable ( name. to_string ( ) ) ;
300293 }
@@ -310,8 +303,8 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
310303 continue ;
311304 }
312305 if Method :: can_cast ( child. kind ( ) ) {
313- let as_fn = Method :: cast ( child) . unwrap ( ) ;
314- let body = ExprOrBlock :: Block ( as_fn. body ( ) . unwrap ( ) ) ;
306+ let as_fn = Method :: cast ( child) . ok_or ( "bad Method" ) ? ;
307+ let body = ExprOrBlock :: Block ( as_fn. body ( ) . ok_or ( "bad Method without body" ) ? ) ;
315308 if as_fn. async_token ( ) . is_none ( ) {
316309 insertions. append ( & mut fn_start_insertion ( & body) ) ;
317310 insertions. append ( child_insertions) ;
@@ -322,20 +315,20 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
322315 continue ;
323316 }
324317 if Constructor :: can_cast ( child. kind ( ) ) {
325- let as_fn = Constructor :: cast ( child) . unwrap ( ) ;
326- let body = ExprOrBlock :: Block ( as_fn. body ( ) . unwrap ( ) ) ;
318+ let as_fn = Constructor :: cast ( child) . ok_or ( "bad Constructor" ) ? ;
319+ let body = ExprOrBlock :: Block ( as_fn. body ( ) . ok_or ( "bad Constructor without body" ) ? ) ;
327320 insertions. append ( & mut fn_start_insertion ( & body) ) ;
328321 insertions. append ( child_insertions) ;
329322 insertions. append ( & mut fn_end_insertion ( & body) ) ;
330323 continue ;
331324 }
332325 if ClassDecl :: can_cast ( child. kind ( ) ) && !has_function_parent {
333- let as_class_decl = ClassDecl :: cast ( child) . unwrap ( ) ;
326+ let as_class_decl = ClassDecl :: cast ( child) . ok_or ( "bad ClassDecl" ) ? ;
334327 match as_class_decl. name ( ) {
335328 None => { } ,
336329 Some ( name) => {
337330 insertions. push_back ( Insertion :: new_dynamic ( range. start ( ) ,
338- [ "_cr = " , name. text ( ) . as_str ( ) , " = " ] . concat ( )
331+ format ! ( "_cr = {name} = " , name = name . text ( ) )
339332 ) ) ;
340333 insertions. push_back ( Insertion :: new ( range. end ( ) ,
341334 ";"
@@ -349,25 +342,27 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
349342 if VarDecl :: can_cast ( child. kind ( ) ) &&
350343 !child. parent ( ) . map_or ( false , |p| ForStmtInit :: can_cast ( p. kind ( ) ) ) &&
351344 !has_function_parent {
352- let as_var_decl = VarDecl :: cast ( child) . unwrap ( ) ;
345+ let as_var_decl = VarDecl :: cast ( child) . ok_or ( "bad VarDecl" ) ? ;
353346 let declarator_range =
354347 as_var_decl. const_token ( ) . map ( |t| t. text_range ( ) )
355348 . or ( as_var_decl. let_token ( ) . map ( |t| t. text_range ( ) ) )
356349 . or ( as_var_decl. var_token ( ) . map ( |t| t. text_range ( ) ) ) ;
357350
358- if declarator_range . is_some ( ) {
359- insertions. push_back ( Insertion :: new ( declarator_range . unwrap ( ) . start ( ) , "/*" ) ) ;
360- insertions. push_back ( Insertion :: new ( declarator_range . unwrap ( ) . end ( ) , "*/;(" ) ) ;
351+ if let Some ( r ) = declarator_range {
352+ insertions. push_back ( Insertion :: new ( r . start ( ) , "/*" ) ) ;
353+ insertions. push_back ( Insertion :: new ( r . end ( ) , "*/;(" ) ) ;
361354 insertions. append ( & mut add_all_variables_from_declaration ( as_var_decl. declared ( ) . filter_map ( |d| d. pattern ( ) ) ) ) ;
362355 }
363356 insertions. append ( child_insertions) ;
364357 if declarator_range. is_some ( ) {
365- insertions. push_back ( Insertion :: new ( as_var_decl. declared ( ) . map ( |d| d. range ( ) . end ( ) ) . max ( ) . unwrap ( ) , ")" ) ) ;
358+ insertions. push_back ( Insertion :: new (
359+ as_var_decl. declared ( ) . map ( |d| d. range ( ) . end ( ) ) . max ( ) . unwrap ( ) ,
360+ ")" ) ) ;
366361 }
367362 continue ;
368363 }
369364 if ExprStmt :: can_cast ( child. kind ( ) ) {
370- let as_expr_stmt = ExprStmt :: cast ( child) . unwrap ( ) ;
365+ let as_expr_stmt = ExprStmt :: cast ( child) . ok_or ( "bad ExprStmt" ) ? ;
371366 let expr_range = as_expr_stmt. expr ( ) . map ( |e| e. syntax ( ) . text_range ( ) ) ;
372367 if let Some ( start) = expr_range. map ( |r| r. start ( ) ) {
373368 insertions. push_back ( Insertion :: new ( start, ";" ) ) ;
@@ -438,9 +433,9 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
438433 !is_function_node ( as_expr. syntax ( ) ) ;
439434
440435 if is_named_typeof_rhs {
441- insertions. push_back ( Insertion :: new_dynamic ( as_expr. syntax ( ) . parent ( ) . unwrap ( ) . text_range ( ) . start ( ) , [
442- "(typeof " , as_expr . syntax ( ) . text ( ) . to_string ( ) . as_str ( ) , " === 'undefined' ? 'undefined' : "
443- ] . concat ( ) ) ) ;
436+ insertions. push_back ( Insertion :: new_dynamic ( as_expr. syntax ( ) . parent ( ) . unwrap ( ) . text_range ( ) . start ( ) ,
437+ format ! ( "(typeof {original} === 'undefined' ? 'undefined' : " , original = as_expr . syntax ( ) . text ( ) )
438+ ) ) ;
444439 }
445440
446441 if wants_implicit_await_wrapper {
@@ -487,20 +482,20 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
487482 }
488483 }
489484 }
490- return insertions;
485+ return Ok ( insertions) ;
491486}
492487
493488#[ wasm_bindgen]
494- pub fn async_rewrite ( input : String , with_debug_tags : bool ) -> String {
489+ pub fn async_rewrite ( input : String , with_debug_tags : bool ) -> Result < String , String > {
495490 let parsed = parse_text ( input. as_str ( ) , 0 ) ;
496491 let mut insertions = InsertionList :: new ( ) ;
497- let mut collected_insertions = collect_insertions ( & parsed. syntax ( ) , 0 ) ;
492+ let mut collected_insertions = collect_insertions ( & parsed. syntax ( ) , 0 , with_debug_tags ) ? ;
498493 {
499494 let vars = & collected_insertions. vars ;
500495 for var in vars {
501- insertions. push_back ( Insertion :: new_dynamic ( TextSize :: new ( 0 ) , [
502- "var " , var . as_str ( ) , ";"
503- ] . concat ( ) ) ) ;
496+ insertions. push_back ( Insertion :: new_dynamic (
497+ TextSize :: new ( 0 ) ,
498+ format ! ( "var {};" , var ) ) ) ;
504499 }
505500 }
506501 let end = input. len ( ) . try_into ( ) . unwrap ( ) ;
@@ -536,17 +531,18 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
536531 }
537532
538533 if with_debug_tags {
539- debug_tag = [
540- "/*i" , insertion. original_ordering . unwrap ( ) . to_string ( ) . as_str ( ) , "@" ,
541- u32:: from ( insertion. offset ) . to_string ( ) . as_str ( ) ,
534+ debug_tag = format ! (
535+ "/*i{}@{}{}" ,
536+ insertion. original_ordering. unwrap( ) ,
537+ u32 :: from( insertion. offset) . to_string( ) ,
542538 if text. contains( "/*" ) { "" } else { "*/" }
543- ] . concat ( ) ;
539+ ) ;
544540 }
545541 result. push_str ( debug_tag. as_str ( ) ) ;
546542 result. push_str ( text) ;
547543 result. push_str ( debug_tag. as_str ( ) ) ;
548544 }
549545 result. push_str ( & input[ previous_offset..] ) ;
550546
551- result
547+ Ok ( result)
552548}
0 commit comments