fix(es/compat): Transform curried function call with optional chaining (#7313)

**Related issue:**

 - Closes #7312
This commit is contained in:
Fy 2023-04-24 13:23:53 +08:00 committed by GitHub
parent 5400648de8
commit 66b5b110bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 91 additions and 10 deletions

View File

@ -1 +1 @@
(myValue.children?.children)[0].value = 'My Value'; (myValue.children?.children[0]).value = 'My Value';

View File

@ -1 +1 @@
(foo?.bar.bar.bar?.baz).baz.baz.foo.baz = 1; (foo?.bar.bar.bar?.baz.baz.baz).foo.baz = 1;

View File

@ -1,14 +1,14 @@
//// [deleteChain.ts] //// [deleteChain.ts]
var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _o4_b_c_d1, _o4_b1, _o4_b_c_d2, _o4_b2, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o6_b_c_d, _o6_b, _o6_b_c_d1, _o6_b1; var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _ref, _o4_b1, _o4_b_c_d1, _o4_b2, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o6_b_c_d, _o6_b, _o6_b_c_d1, _o6_b1;
o1 === null || o1 === void 0 ? void 0 : delete o1.b; o1 === null || o1 === void 0 ? void 0 : delete o1.b;
delete (o1 === null || o1 === void 0 ? void 0 : o1.b); delete (o1 === null || o1 === void 0 ? void 0 : o1.b);
o2 === null || o2 === void 0 ? void 0 : delete o2.b.c; o2 === null || o2 === void 0 ? void 0 : delete o2.b.c;
o2 === null || o2 === void 0 ? void 0 : delete o2.b.c; delete (o2 === null || o2 === void 0 ? void 0 : o2.b.c);
(_o3_b = o3.b) === null || _o3_b === void 0 ? void 0 : delete _o3_b.c; (_o3_b = o3.b) === null || _o3_b === void 0 ? void 0 : delete _o3_b.c;
delete ((_o3_b1 = o3.b) === null || _o3_b1 === void 0 ? void 0 : _o3_b1.c); delete ((_o3_b1 = o3.b) === null || _o3_b1 === void 0 ? void 0 : _o3_b1.c);
(_o4_b_c_d = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b.c.d) === null || _o4_b_c_d === void 0 ? void 0 : delete _o4_b_c_d.e; (_o4_b_c_d = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b.c.d) === null || _o4_b_c_d === void 0 ? void 0 : delete _o4_b_c_d.e;
(_o4_b_c_d1 = (_o4_b1 = o4.b) === null || _o4_b1 === void 0 ? void 0 : _o4_b1.c.d) === null || _o4_b_c_d1 === void 0 ? void 0 : delete _o4_b_c_d1.e; (_ref = (_o4_b1 = o4.b) === null || _o4_b1 === void 0 ? void 0 : _o4_b1.c.d) === null || _ref === void 0 ? void 0 : delete _ref.e;
delete ((_o4_b_c_d2 = (_o4_b2 = o4.b) === null || _o4_b2 === void 0 ? void 0 : _o4_b2.c.d) === null || _o4_b_c_d2 === void 0 ? void 0 : _o4_b_c_d2.e); delete ((_o4_b_c_d1 = (_o4_b2 = o4.b) === null || _o4_b2 === void 0 ? void 0 : _o4_b2.c.d) === null || _o4_b_c_d1 === void 0 ? void 0 : _o4_b_c_d1.e);
(_o5_b_c_d = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5).c.d) === null || _o5_b_c_d === void 0 ? void 0 : delete _o5_b_c_d.e; (_o5_b_c_d = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5).c.d) === null || _o5_b_c_d === void 0 ? void 0 : delete _o5_b_c_d.e;
delete ((_o5_b_c_d1 = (_o5_b1 = o5.b) === null || _o5_b1 === void 0 ? void 0 : _o5_b1.call(o5).c.d) === null || _o5_b_c_d1 === void 0 ? void 0 : _o5_b_c_d1.e); delete ((_o5_b_c_d1 = (_o5_b1 = o5.b) === null || _o5_b1 === void 0 ? void 0 : _o5_b1.call(o5).c.d) === null || _o5_b_c_d1 === void 0 ? void 0 : _o5_b_c_d1.e);
(_o6_b_c_d = (_o6_b = o6.b) === null || _o6_b === void 0 ? void 0 : _o6_b["c"].d) === null || _o6_b_c_d === void 0 ? void 0 : delete _o6_b_c_d["e"]; (_o6_b_c_d = (_o6_b = o6.b) === null || _o6_b === void 0 ? void 0 : _o6_b["c"].d) === null || _o6_b_c_d === void 0 ? void 0 : delete _o6_b_c_d["e"];

View File

@ -1,3 +1,3 @@
//// [deleteChain.ts] //// [deleteChain.ts]
var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _o4_b_c_d1, _o4_b1, _o4_b_c_d2, _o4_b2, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o6_b_c_d, _o6_b, _o6_b_c_d1, _o6_b1; var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _ref, _o4_b1, _o4_b_c_d1, _o4_b2, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o6_b_c_d, _o6_b, _o6_b_c_d1, _o6_b1;
null == o1 || delete o1.b, delete (null == o1 ? void 0 : o1.b), null == o2 || delete o2.b.c, null == o2 || delete o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || delete _o3_b.c, delete (null === (_o3_b1 = o3.b) || void 0 === _o3_b1 ? void 0 : _o3_b1.c), null === (_o4_b_c_d = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _o4_b_c_d || delete _o4_b_c_d.e, null === (_o4_b_c_d1 = null === (_o4_b1 = o4.b) || void 0 === _o4_b1 ? void 0 : _o4_b1.c.d) || void 0 === _o4_b_c_d1 || delete _o4_b_c_d1.e, delete (null === (_o4_b_c_d2 = null === (_o4_b2 = o4.b) || void 0 === _o4_b2 ? void 0 : _o4_b2.c.d) || void 0 === _o4_b_c_d2 ? void 0 : _o4_b_c_d2.e), null === (_o5_b_c_d = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _o5_b_c_d || delete _o5_b_c_d.e, delete (null === (_o5_b_c_d1 = null === (_o5_b1 = o5.b) || void 0 === _o5_b1 ? void 0 : _o5_b1.call(o5).c.d) || void 0 === _o5_b_c_d1 ? void 0 : _o5_b_c_d1.e), null === (_o6_b_c_d = null === (_o6_b = o6.b) || void 0 === _o6_b ? void 0 : _o6_b.c.d) || void 0 === _o6_b_c_d || delete _o6_b_c_d.e, delete (null === (_o6_b_c_d1 = null === (_o6_b1 = o6.b) || void 0 === _o6_b1 ? void 0 : _o6_b1.c.d) || void 0 === _o6_b_c_d1 ? void 0 : _o6_b_c_d1.e); null == o1 || delete o1.b, delete (null == o1 ? void 0 : o1.b), null == o2 || delete o2.b.c, delete (null == o2 ? void 0 : o2.b.c), null === (_o3_b = o3.b) || void 0 === _o3_b || delete _o3_b.c, delete (null === (_o3_b1 = o3.b) || void 0 === _o3_b1 ? void 0 : _o3_b1.c), null === (_o4_b_c_d = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _o4_b_c_d || delete _o4_b_c_d.e, null === (_ref = null === (_o4_b1 = o4.b) || void 0 === _o4_b1 ? void 0 : _o4_b1.c.d) || void 0 === _ref || delete _ref.e, delete (null === (_o4_b_c_d1 = null === (_o4_b2 = o4.b) || void 0 === _o4_b2 ? void 0 : _o4_b2.c.d) || void 0 === _o4_b_c_d1 ? void 0 : _o4_b_c_d1.e), null === (_o5_b_c_d = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _o5_b_c_d || delete _o5_b_c_d.e, delete (null === (_o5_b_c_d1 = null === (_o5_b1 = o5.b) || void 0 === _o5_b1 ? void 0 : _o5_b1.call(o5).c.d) || void 0 === _o5_b_c_d1 ? void 0 : _o5_b_c_d1.e), null === (_o6_b_c_d = null === (_o6_b = o6.b) || void 0 === _o6_b ? void 0 : _o6_b.c.d) || void 0 === _o6_b_c_d || delete _o6_b_c_d.e, delete (null === (_o6_b_c_d1 = null === (_o6_b1 = o6.b) || void 0 === _o6_b1 ? void 0 : _o6_b1.c.d) || void 0 === _o6_b_c_d1 ? void 0 : _o6_b_c_d1.e);

View File

@ -2,4 +2,4 @@
o1(o1 !== null && o1 !== void 0 ? o1 : 1); o1(o1 !== null && o1 !== void 0 ? o1 : 1);
(o2 === null || o2 === void 0 ? void 0 : o2.b)(o1 !== null && o1 !== void 0 ? o1 : 1); (o2 === null || o2 === void 0 ? void 0 : o2.b)(o1 !== null && o1 !== void 0 ? o1 : 1);
(o3 === null || o3 === void 0 ? void 0 : o3.b())(o1 !== null && o1 !== void 0 ? o1 : 1); (o3 === null || o3 === void 0 ? void 0 : o3.b())(o1 !== null && o1 !== void 0 ? o1 : 1);
o4 === null || o4 === void 0 ? void 0 : o4.b().c(o1 !== null && o1 !== void 0 ? o1 : 1); (o4 === null || o4 === void 0 ? void 0 : o4.b().c)(o1 !== null && o1 !== void 0 ? o1 : 1);

View File

@ -1,2 +1,2 @@
//// [parentheses.ts] //// [parentheses.ts]
o1(null != o1 ? o1 : 1), (null == o2 ? void 0 : o2.b)(null != o1 ? o1 : 1), (null == o3 ? void 0 : o3.b())(null != o1 ? o1 : 1), null == o4 || o4.b().c(null != o1 ? o1 : 1); o1(null != o1 ? o1 : 1), (null == o2 ? void 0 : o2.b)(null != o1 ? o1 : 1), (null == o3 ? void 0 : o3.b())(null != o1 ? o1 : 1), (null == o4 ? void 0 : o4.b().c)(null != o1 ? o1 : 1);

View File

@ -1000,6 +1000,11 @@ impl Fixer<'_> {
return; return;
} }
// `(a?.b.c)() !== a?.b.c()`
if Self::contain_opt_chain(expr) {
return;
}
let expr_span = expr.span(); let expr_span = expr.span();
let paren_span = *paren_span; let paren_span = *paren_span;
self.unwrap_expr(expr); self.unwrap_expr(expr);
@ -1011,6 +1016,17 @@ impl Fixer<'_> {
} }
} }
fn contain_opt_chain(e: &Expr) -> bool {
match e {
Expr::Member(member) => Self::contain_opt_chain(&member.obj),
Expr::OptChain(_) => true,
Expr::Call(CallExpr { callee, .. }) if callee.is_expr() => {
Self::contain_opt_chain(callee.as_expr().unwrap())
}
_ => false,
}
}
fn handle_expr_stmt(&mut self, expr: &mut Expr) { fn handle_expr_stmt(&mut self, expr: &mut Expr) {
match expr { match expr {
// It's important for arrow pass to work properly. // It's important for arrow pass to work properly.

View File

@ -235,6 +235,27 @@ impl OptChaining {
})), })),
}; };
} }
Callee::Expr(callee) if callee.is_call() => {
let mut callee = callee.take().call().unwrap();
let callee = self.handle_call(&mut callee);
return match callee {
Ok(expr) => Ok(CondExpr {
span: e.span,
alt: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: expr.alt.as_callee(),
args: e.args.take(),
type_args: e.type_args.take(),
})),
..expr
}),
Err(callee) => Err(Expr::Call(CallExpr {
callee: callee.as_callee(),
..e.take()
})),
};
}
_ => {} _ => {}
} }

View File

@ -105,6 +105,47 @@ foo == null ? void 0 : foo.bar == null ? void 0 : foo.bar(foo.bar, true);
"# "#
); );
// parentheses
test!(
syntax(),
|_| tr(Default::default()),
parentheses,
r#"
(o1)(o1 ?? 1);
(o2?.b)(o1 ?? 1);
(o3?.b())(o1 ?? 1);
(o4?.b().c)(o1 ?? 1);
"#,
r#"
o1(o1 ?? 1);
(o2 === null || o2 === void 0 ? void 0 : o2.b)(o1 ?? 1);
(o3 === null || o3 === void 0 ? void 0 : o3.b())(o1 ?? 1);
(o4 === null || o4 === void 0 ? void 0 : o4.b().c)(o1 ?? 1);
"#
);
// curried_function_call
test!(
syntax(),
|_| tr(Default::default()),
curried_function_call,
r#"
a?.b()();
a?.b()()();
a?.b()().c;
a?.b()()?.c;
a?.b()()?.c()();
"#,
r#"
var _a_b, _a_b1;
a === null || a === void 0 ? void 0 : a.b()();
a === null || a === void 0 ? void 0 : a.b()()();
a === null || a === void 0 ? void 0 : a.b()().c;
(_a_b = a === null || a === void 0 ? void 0 : a.b()()) === null || _a_b === void 0 ? void 0 : _a_b.c;
(_a_b1 = a === null || a === void 0 ? void 0 : a.b()()) === null || _a_b1 === void 0 ? void 0 : _a_b1.c()();
"#
);
// indirect_eval_call // indirect_eval_call
test!( test!(
syntax(), syntax(),

View File

@ -0,0 +1,3 @@
var a = undefined;
a?.a()()?.a()();