@@ -163,18 +163,18 @@ fn fn_end_insertion(body: &ExprOrBlock) -> InsertionList {
163
163
}
164
164
165
165
fn is_in_async_function ( node : & SyntaxNode ) -> bool {
166
- return node. parent ( ) . map_or ( false , |parent : SyntaxNode | {
166
+ node. parent ( ) . map_or ( false , |parent : SyntaxNode | {
167
167
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 ( ) ) ;
169
169
}
170
170
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 ( ) ) ;
172
172
}
173
173
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 ( ) ) ;
175
175
}
176
176
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 ( ) ) ;
178
178
}
179
179
if Constructor :: can_cast ( parent. kind ( ) ) {
180
180
return false ;
@@ -186,18 +186,18 @@ fn is_in_async_function(node: &SyntaxNode) -> bool {
186
186
return false ;
187
187
}
188
188
assert ! ( !is_function_node( & parent) ) ;
189
- return is_in_async_function ( & parent)
190
- } ) ;
189
+ is_in_async_function ( & parent)
190
+ } )
191
191
}
192
192
193
193
fn is_function_node ( node : & SyntaxNode ) -> bool {
194
- return FnExpr :: can_cast ( node. kind ( ) ) ||
194
+ FnExpr :: can_cast ( node. kind ( ) ) ||
195
195
FnDecl :: can_cast ( node. kind ( ) ) ||
196
196
ArrowExpr :: can_cast ( node. kind ( ) ) ||
197
197
Method :: can_cast ( node. kind ( ) ) ||
198
198
Constructor :: can_cast ( node. kind ( ) ) ||
199
199
ClassExpr :: can_cast ( node. kind ( ) ) ||
200
- ClassDecl :: can_cast ( node. kind ( ) ) ;
200
+ ClassDecl :: can_cast ( node. kind ( ) )
201
201
}
202
202
203
203
fn 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
254
254
255
255
fn is_name_ref ( syntax_node : & SyntaxNode , names : Option < & ' static [ & ' static str ] > ) -> bool {
256
256
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) ) ;
268
259
}
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 ( ) ) )
270
261
}
271
262
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 > {
273
264
let has_function_parent = nesting_depth > 0 ;
274
265
let mut insertions = InsertionList :: new ( ) ;
275
266
for child in node. children ( ) {
276
267
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) ?;
278
271
{
279
272
let kind = child. kind ( ) ;
280
273
if kind != SyntaxKind :: TEMPLATE_ELEMENT {
281
274
insertions. push_back ( Insertion :: new_dynamic ( range. start ( ) ,
282
- [ " /*" , format ! ( "{kind:#?}" ) . as_str ( ) , " */ "] . concat ( )
275
+ format ! ( " /* {kind:#?}*/ " )
283
276
) ) ;
284
277
}
285
278
}
286
279
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" ) ? ) ;
289
282
if !has_function_parent {
290
283
match as_fn. ident_token ( ) . or ( as_fn. name ( ) . and_then ( |n| n. ident_token ( ) ) ) {
291
284
None => {
@@ -294,7 +287,7 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
294
287
Some ( name) => {
295
288
insertions. push_back ( Insertion :: new ( name. text_range ( ) . end ( ) , "__" ) ) ;
296
289
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 ( ) )
298
291
) ) ;
299
292
insertions. add_variable ( name. to_string ( ) ) ;
300
293
}
@@ -310,8 +303,8 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
310
303
continue ;
311
304
}
312
305
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" ) ? ) ;
315
308
if as_fn. async_token ( ) . is_none ( ) {
316
309
insertions. append ( & mut fn_start_insertion ( & body) ) ;
317
310
insertions. append ( child_insertions) ;
@@ -322,20 +315,20 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
322
315
continue ;
323
316
}
324
317
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" ) ? ) ;
327
320
insertions. append ( & mut fn_start_insertion ( & body) ) ;
328
321
insertions. append ( child_insertions) ;
329
322
insertions. append ( & mut fn_end_insertion ( & body) ) ;
330
323
continue ;
331
324
}
332
325
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" ) ? ;
334
327
match as_class_decl. name ( ) {
335
328
None => { } ,
336
329
Some ( name) => {
337
330
insertions. push_back ( Insertion :: new_dynamic ( range. start ( ) ,
338
- [ "_cr = " , name. text ( ) . as_str ( ) , " = " ] . concat ( )
331
+ format ! ( "_cr = {name} = " , name = name . text ( ) )
339
332
) ) ;
340
333
insertions. push_back ( Insertion :: new ( range. end ( ) ,
341
334
";"
@@ -349,25 +342,27 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
349
342
if VarDecl :: can_cast ( child. kind ( ) ) &&
350
343
!child. parent ( ) . map_or ( false , |p| ForStmtInit :: can_cast ( p. kind ( ) ) ) &&
351
344
!has_function_parent {
352
- let as_var_decl = VarDecl :: cast ( child) . unwrap ( ) ;
345
+ let as_var_decl = VarDecl :: cast ( child) . ok_or ( "bad VarDecl" ) ? ;
353
346
let declarator_range =
354
347
as_var_decl. const_token ( ) . map ( |t| t. text_range ( ) )
355
348
. or ( as_var_decl. let_token ( ) . map ( |t| t. text_range ( ) ) )
356
349
. or ( as_var_decl. var_token ( ) . map ( |t| t. text_range ( ) ) ) ;
357
350
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 ( ) , "*/;(" ) ) ;
361
354
insertions. append ( & mut add_all_variables_from_declaration ( as_var_decl. declared ( ) . filter_map ( |d| d. pattern ( ) ) ) ) ;
362
355
}
363
356
insertions. append ( child_insertions) ;
364
357
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
+ ")" ) ) ;
366
361
}
367
362
continue ;
368
363
}
369
364
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" ) ? ;
371
366
let expr_range = as_expr_stmt. expr ( ) . map ( |e| e. syntax ( ) . text_range ( ) ) ;
372
367
if let Some ( start) = expr_range. map ( |r| r. start ( ) ) {
373
368
insertions. push_back ( Insertion :: new ( start, ";" ) ) ;
@@ -438,9 +433,9 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
438
433
!is_function_node ( as_expr. syntax ( ) ) ;
439
434
440
435
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
+ ) ) ;
444
439
}
445
440
446
441
if wants_implicit_await_wrapper {
@@ -487,20 +482,20 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
487
482
}
488
483
}
489
484
}
490
- return insertions;
485
+ return Ok ( insertions) ;
491
486
}
492
487
493
488
#[ 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 > {
495
490
let parsed = parse_text ( input. as_str ( ) , 0 ) ;
496
491
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 ) ? ;
498
493
{
499
494
let vars = & collected_insertions. vars ;
500
495
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 ) ) ) ;
504
499
}
505
500
}
506
501
let end = input. len ( ) . try_into ( ) . unwrap ( ) ;
@@ -536,17 +531,18 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
536
531
}
537
532
538
533
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( ) ,
542
538
if text. contains( "/*" ) { "" } else { "*/" }
543
- ] . concat ( ) ;
539
+ ) ;
544
540
}
545
541
result. push_str ( debug_tag. as_str ( ) ) ;
546
542
result. push_str ( text) ;
547
543
result. push_str ( debug_tag. as_str ( ) ) ;
548
544
}
549
545
result. push_str ( & input[ previous_offset..] ) ;
550
546
551
- result
547
+ Ok ( result)
552
548
}
0 commit comments