1
1
use std:: { borrow:: Borrow , collections:: VecDeque , fmt:: Debug } ;
2
2
use wasm_bindgen:: prelude:: * ;
3
- use rslint_parser:: { ast:: { ArrowExpr , AssignExpr , BreakStmt , CallExpr , ClassDecl , Constructor , ContinueStmt , Expr , ExprOrBlock , ExprStmt , FnDecl , FnExpr , ForInStmt , ForOfStmt , ForStmtInit , GroupingExpr , Literal , Method , NameRef , ObjectPatternProp , ParameterList , Pattern , PropName , ReturnStmt , ThisExpr , UnaryExpr , VarDecl } , parse_text, AstNode , SyntaxKind , SyntaxNode , TextSize } ;
3
+ use rslint_parser:: { ast:: { ArrowExpr , AssignExpr , BracketExpr , BreakStmt , CallExpr , ClassDecl , Constructor , ContinueStmt , DotExpr , Expr , ExprOrBlock , ExprStmt , FnDecl , FnExpr , ForStmtInit , GroupingExpr , Literal , Method , NameRef , ObjectPatternProp , ParameterList , Pattern , PropName , ReturnStmt , ThisExpr , UnaryExpr , VarDecl } , parse_text, AstNode , SyntaxKind , SyntaxNode , TextSize } ;
4
4
5
5
#[ derive( Debug ) ]
6
6
enum InsertionText {
@@ -70,7 +70,7 @@ fn is_block(body: &ExprOrBlock) -> bool {
70
70
71
71
fn make_start_fn_insertion ( offset : TextSize ) -> Insertion {
72
72
Insertion :: new ( offset, r#"
73
- ;const _syntheticPromise = Symbol.for ('@@mongosh.syntheticPromise');
73
+ ;const _syntheticPromise = __SymbolFor ('@@mongosh.syntheticPromise');
74
74
75
75
function _markSyntheticPromise(p) {
76
76
return Object.defineProperty(p, _syntheticPromise, {
@@ -145,7 +145,7 @@ fn fn_end_insertion(body: &ExprOrBlock) -> InsertionList {
145
145
if is_block ( body) {
146
146
offset = offset. checked_sub ( 1 . into ( ) ) . unwrap ( ) ;
147
147
} else {
148
- ret. push_back ( Insertion :: new ( offset, "));" ) ) ;
148
+ ret. push_back ( Insertion :: new ( offset, "), _functionState === 'async' ? _synchronousReturnValue : null );" ) ) ;
149
149
}
150
150
ret. push_back ( make_end_fn_insertion ( offset) ) ;
151
151
if !is_block ( body) {
@@ -361,12 +361,11 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
361
361
let is_returned_expression = ReturnStmt :: can_cast ( as_expr. syntax ( ) . parent ( ) . unwrap ( ) . kind ( ) ) ;
362
362
let is_called_expression = CallExpr :: can_cast ( as_expr. syntax ( ) . parent ( ) . unwrap ( ) . kind ( ) ) ;
363
363
let is_expr_in_async_function = is_in_async_function ( as_expr. syntax ( ) ) ;
364
- let mut is_dot_call_expression = false ;
365
- let mut pushed_insertions = 0 ;
364
+ let is_dot_call_expression = is_called_expression &&
365
+ ( DotExpr :: can_cast ( as_expr . syntax ( ) . kind ( ) ) || BracketExpr :: can_cast ( as_expr . syntax ( ) . kind ( ) ) ) ;
366
366
367
367
if is_returned_expression && !is_expr_in_async_function {
368
- insertions. push_back ( Insertion :: new ( range. start ( ) , "(_synchronousReturnValue = " ) ) ;
369
- pushed_insertions += 1 ;
368
+ insertions. push_back ( Insertion :: new ( range. start ( ) , "(_synchronousReturnValue = (" ) ) ;
370
369
}
371
370
372
371
let is_unary_rhs = UnaryExpr :: can_cast ( as_expr. syntax ( ) . parent ( ) . unwrap ( ) . kind ( ) ) ;
@@ -388,18 +387,17 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
388
387
!is_eval_this_super_reference &&
389
388
!is_literal &&
390
389
!is_label_in_continue_or_break &&
391
- !is_for_in_of_lvalue;
390
+ !is_for_in_of_lvalue &&
391
+ !is_dot_call_expression;
392
392
393
393
if is_named_typeof_rhs {
394
394
insertions. push_back ( Insertion :: new_dynamic ( as_expr. syntax ( ) . parent ( ) . unwrap ( ) . text_range ( ) . start ( ) , [
395
395
"(typeof " , as_expr. syntax ( ) . text ( ) . to_string ( ) . as_str ( ) , " === 'undefined' ? 'undefined' : "
396
396
] . concat ( ) ) ) ;
397
- pushed_insertions += 1 ;
398
397
}
399
398
400
399
if wants_implicit_await_wrapper {
401
400
insertions. push_back ( Insertion :: new ( range. start ( ) , "(_ex = " ) ) ;
402
- pushed_insertions += 1 ;
403
401
}
404
402
405
403
match as_expr {
@@ -423,21 +421,11 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
423
421
insertions. append ( child_insertions) ;
424
422
}
425
423
} ,
426
- Expr :: DotExpr ( _) => {
427
- if is_called_expression {
428
- is_dot_call_expression = true ;
429
- while pushed_insertions > 0 {
430
- pushed_insertions -= 1 ;
431
- insertions. pop_back ( ) ;
432
- }
433
- }
434
- insertions. append ( child_insertions) ;
435
- } ,
436
424
_ => {
437
425
insertions. append ( child_insertions) ;
438
426
} ,
439
427
}
440
- if wants_implicit_await_wrapper && !is_dot_call_expression {
428
+ if wants_implicit_await_wrapper {
441
429
insertions. push_back ( Insertion :: new ( range. end ( ) , ", _isp(_ex) ? await _ex : _ex)" ) ) ;
442
430
}
443
431
if is_named_typeof_rhs {
@@ -446,7 +434,7 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
446
434
if is_returned_expression && !is_expr_in_async_function {
447
435
insertions. push_back ( Insertion :: new (
448
436
range. end ( ) ,
449
- ", _functionState === 'async' ? _synchronousReturnValue : null)"
437
+ ") , _functionState === 'async' ? _synchronousReturnValue : null)"
450
438
) ) ;
451
439
}
452
440
}
@@ -469,7 +457,7 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
469
457
}
470
458
}
471
459
let end = input. len ( ) . try_into ( ) . unwrap ( ) ;
472
- insertions. push_back ( Insertion :: new ( TextSize :: new ( 0 ) , "(() => {" ) ) ;
460
+ insertions. push_back ( Insertion :: new ( TextSize :: new ( 0 ) , "(() => { const __SymbolFor = Symbol.for; " ) ) ;
473
461
insertions. push_back ( make_start_fn_insertion ( TextSize :: new ( 0 ) ) ) ;
474
462
insertions. push_back ( Insertion :: new ( TextSize :: new ( 0 ) , "var _cr;" ) ) ;
475
463
insertions. append ( & mut collected_insertions) ;
0 commit comments