Skip to content

Commit 29993d0

Browse files
committed
fixup: handle bracketed call expressions
1 parent 3404916 commit 29993d0

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

packages/async-rewriter3/src/lib.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{borrow::Borrow, collections::VecDeque, fmt::Debug};
22
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};
44

55
#[derive(Debug)]
66
enum InsertionText {
@@ -70,7 +70,7 @@ fn is_block(body: &ExprOrBlock) -> bool {
7070

7171
fn make_start_fn_insertion(offset: TextSize) -> Insertion {
7272
Insertion::new(offset, r#"
73-
;const _syntheticPromise = Symbol.for('@@mongosh.syntheticPromise');
73+
;const _syntheticPromise = __SymbolFor('@@mongosh.syntheticPromise');
7474
7575
function _markSyntheticPromise(p) {
7676
return Object.defineProperty(p, _syntheticPromise, {
@@ -145,7 +145,7 @@ fn fn_end_insertion(body: &ExprOrBlock) -> InsertionList {
145145
if is_block(body) {
146146
offset = offset.checked_sub(1.into()).unwrap();
147147
} else {
148-
ret.push_back(Insertion::new(offset, "));"));
148+
ret.push_back(Insertion::new(offset, "), _functionState === 'async' ? _synchronousReturnValue : null);"));
149149
}
150150
ret.push_back(make_end_fn_insertion(offset));
151151
if !is_block(body) {
@@ -361,12 +361,11 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
361361
let is_returned_expression = ReturnStmt::can_cast(as_expr.syntax().parent().unwrap().kind());
362362
let is_called_expression = CallExpr::can_cast(as_expr.syntax().parent().unwrap().kind());
363363
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()));
366366

367367
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 = ("));
370369
}
371370

372371
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 {
388387
!is_eval_this_super_reference &&
389388
!is_literal &&
390389
!is_label_in_continue_or_break &&
391-
!is_for_in_of_lvalue;
390+
!is_for_in_of_lvalue &&
391+
!is_dot_call_expression;
392392

393393
if is_named_typeof_rhs {
394394
insertions.push_back(Insertion::new_dynamic(as_expr.syntax().parent().unwrap().text_range().start(), [
395395
"(typeof ", as_expr.syntax().text().to_string().as_str(), " === 'undefined' ? 'undefined' : "
396396
].concat()));
397-
pushed_insertions += 1;
398397
}
399398

400399
if wants_implicit_await_wrapper {
401400
insertions.push_back(Insertion::new(range.start(), "(_ex = "));
402-
pushed_insertions += 1;
403401
}
404402

405403
match as_expr {
@@ -423,21 +421,11 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
423421
insertions.append(child_insertions);
424422
}
425423
},
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-
},
436424
_ => {
437425
insertions.append(child_insertions);
438426
},
439427
}
440-
if wants_implicit_await_wrapper && !is_dot_call_expression {
428+
if wants_implicit_await_wrapper {
441429
insertions.push_back(Insertion::new(range.end(), ", _isp(_ex) ? await _ex : _ex)"));
442430
}
443431
if is_named_typeof_rhs {
@@ -446,7 +434,7 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
446434
if is_returned_expression && !is_expr_in_async_function {
447435
insertions.push_back(Insertion::new(
448436
range.end(),
449-
", _functionState === 'async' ? _synchronousReturnValue : null)"
437+
"), _functionState === 'async' ? _synchronousReturnValue : null)"
450438
));
451439
}
452440
}
@@ -469,7 +457,7 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
469457
}
470458
}
471459
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;"));
473461
insertions.push_back(make_start_fn_insertion(TextSize::new(0)));
474462
insertions.push_back(Insertion::new(TextSize::new(0), "var _cr;"));
475463
insertions.append(&mut collected_insertions);

packages/e2e-tests/test/e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ describe('e2e', function () {
697697
expect(result).to.include('{ _id: 1, value: 4 }');
698698
});
699699

700-
it('rewrites async properly for common libraries', async function () {
700+
it.only('rewrites async properly for common libraries', async function () {
701701
this.timeout(120_000);
702702
await shell.executeLine(`use ${dbName}`);
703703
await shell.executeLine(

0 commit comments

Comments
 (0)