Skip to content

Commit f52b1db

Browse files
committed
fix(transformer/class-properties): output is not the same with Babel when callee has optional (#7748)
Part of #7749. It is just an output mismatch problem, not affecting runtime behavior.
1 parent cf2ee06 commit f52b1db

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

  • crates/oxc_transformer/src/es2022/class_properties

crates/oxc_transformer/src/es2022/class_properties/private.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,22 +1299,37 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
12991299
) -> Expression<'a> {
13001300
let Expression::CallExpression(call) = expr else { unreachable!() };
13011301

1302-
let callee = &mut call.callee;
13031302
// `Foo?.bar()?.zoo?.()`
1304-
// ^^^^^^^^^^^^^^^^^ callee is a member expression
13051303
// ^^^^^^^^^^^ object
1306-
let object = callee.to_member_expression_mut().object_mut();
1304+
// ^^^^^^^^^^^^^^^^^ callee is a member expression
1305+
let callee = &mut call.callee;
1306+
let callee_member = callee.to_member_expression_mut();
1307+
let is_optional_callee = callee_member.optional();
1308+
let object = callee_member.object_mut();
13071309

13081310
let context = if let Some(result) = self.transform_chain_element_recursively(object, ctx) {
1309-
// `o?.Foo.#self.getSelf?.().#m?.();` -> `(_ref = o === null || o === void 0 ? void 0 : (_babelHelpers$assertC =
1310-
// babelHelpers.assertClassBrand(Foo, o.Foo, _self)._).getSelf)`
1311-
// ^^^^^^^^^^^^^^^^^^^^^^ to make sure get `getSelf` call has a proper context, we need to assign
1312-
// the parent of callee (i.e `o?.Foo.#self`) to a temp variable,
1313-
// and then use it as a first argument of `_ref.call`.
1314-
let (assignment, context) = self.duplicate_object(ctx.ast.move_expression(object), ctx);
1315-
*object = assignment;
1316-
*callee = Self::wrap_conditional_check(result, ctx.ast.move_expression(callee), ctx);
1317-
context
1311+
if is_optional_callee {
1312+
// `o?.Foo.#self?.getSelf?.().#x;` -> `(_ref$getSelf = (_ref2 = _ref = o === null || o === void 0 ?
1313+
// ^^ is optional void 0 : babelHelpers.assertClassBrand(Foo, o.Foo, _self)._)`
1314+
*object =
1315+
Self::wrap_conditional_check(result, ctx.ast.move_expression(object), ctx);
1316+
let (assignment, context) =
1317+
self.duplicate_object(ctx.ast.move_expression(object), ctx);
1318+
*object = assignment;
1319+
context
1320+
} else {
1321+
// `o?.Foo.#self.getSelf?.().#m?.();` -> `(_ref = o === null || o === void 0 ? void 0 : (_babelHelpers$assertC =
1322+
// babelHelpers.assertClassBrand(Foo, o.Foo, _self)._).getSelf)`
1323+
// ^^^^^^^^^^^^^^^^^^^^^^ to make sure get `getSelf` call has a proper context, we need to assign
1324+
// the parent of callee (i.e `o?.Foo.#self`) to a temp variable,
1325+
// and then use it as a first argument of `_ref.call`.
1326+
let (assignment, context) =
1327+
self.duplicate_object(ctx.ast.move_expression(object), ctx);
1328+
*object = assignment;
1329+
*callee =
1330+
Self::wrap_conditional_check(result, ctx.ast.move_expression(callee), ctx);
1331+
context
1332+
}
13181333
} else {
13191334
// `Foo?.bar()?.zoo?.().#x;` -> `(_Foo$bar$zoo = (_Foo$bar = Foo?.bar())?.zoo)`
13201335
// ^^^^^^^^^^^^^^^^ this is a optional function call, to make sure it has a proper context,

0 commit comments

Comments
 (0)