@@ -959,8 +959,13 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
959
959
if matches ! ( element, ChainElement :: PrivateFieldExpression ( _) ) {
960
960
// The PrivateFieldExpression must be transformed, so we can convert it to a normal expression here.
961
961
let mut chain_expr = Self :: convert_chain_expression_to_expression ( expr, ctx) ;
962
- let result =
963
- self . transform_private_field_expression_of_chain_expression ( & mut chain_expr, ctx) ;
962
+ let result = self
963
+ . transform_private_field_expression_of_chain_expression ( & mut chain_expr, ctx)
964
+ . unwrap_or_else ( || {
965
+ unreachable ! (
966
+ "The ChainExpression must contains at least one optional expression, so it never be `None` here."
967
+ )
968
+ } ) ;
964
969
Some ( ( result, chain_expr) )
965
970
} else if let Some ( result) = self . transform_chain_expression_element ( element, ctx) {
966
971
let chain_expr = Self :: convert_chain_expression_to_expression ( expr, ctx) ;
@@ -1003,7 +1008,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
1003
1008
) -> Option < Expression < ' a > > {
1004
1009
match expr {
1005
1010
Expression :: PrivateFieldExpression ( _) => {
1006
- Some ( self . transform_private_field_expression_of_chain_expression ( expr, ctx) )
1011
+ self . transform_private_field_expression_of_chain_expression ( expr, ctx)
1007
1012
}
1008
1013
match_member_expression ! ( Expression ) => self
1009
1014
. transform_member_expression_of_chain_expression (
@@ -1086,17 +1091,20 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
1086
1091
Some ( result)
1087
1092
}
1088
1093
1094
+ /// Transform private field expression of chain expression.
1095
+ ///
1096
+ /// Returns `None` if the `expr` doesn't contain any optional expression.
1089
1097
fn transform_private_field_expression_of_chain_expression (
1090
1098
& mut self ,
1091
1099
expr : & mut Expression < ' a > ,
1092
1100
ctx : & mut TraverseCtx < ' a > ,
1093
- ) -> Expression < ' a > {
1101
+ ) -> Option < Expression < ' a > > {
1094
1102
let Expression :: PrivateFieldExpression ( field_expr) = expr else { unreachable ! ( ) } ;
1095
1103
1096
1104
let is_optional = field_expr. optional ;
1097
1105
let object = & mut field_expr. object ;
1098
1106
1099
- let left = if is_optional {
1107
+ let result = if is_optional {
1100
1108
Some ( self . transform_expression_to_wrap_nullish_check ( object, ctx) )
1101
1109
} else {
1102
1110
self . transform_first_optional_expression ( object, ctx)
@@ -1110,13 +1118,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
1110
1118
self . transform_private_field_expression ( expr, ctx) ;
1111
1119
}
1112
1120
1113
- left. unwrap_or_else ( || {
1114
- // `o.Foo.#x?.self` -> `(_babelHelpers$assertC = expr === null || _babelHelpers$assertC === void 0 ?
1115
- // void 0 : _babelHelpers$assertC?.self;`
1116
- // `expr` is `o.Foo.#x` that has transformed to `babelHelpers.assertClassBrand(Foo, o.Foo, _x)._)` by
1117
- // `self.transform_private_field_expression`.
1118
- self . transform_expression_to_wrap_nullish_check ( expr, ctx)
1119
- } )
1121
+ result
1120
1122
}
1121
1123
1122
1124
fn transform_member_expression_of_chain_expression (
@@ -1145,6 +1147,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
1145
1147
ctx : & mut TraverseCtx < ' a > ,
1146
1148
) -> Option < Expression < ' a > > {
1147
1149
let is_optional = call_expr. optional ;
1150
+
1148
1151
let callee = call_expr. callee . get_inner_expression_mut ( ) ;
1149
1152
if matches ! ( callee, Expression :: PrivateFieldExpression ( _) ) {
1150
1153
let result = self . transform_first_optional_expression ( callee, ctx) ;
@@ -1157,9 +1160,8 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
1157
1160
return result;
1158
1161
}
1159
1162
1160
- let result = self . transform_chain_element_recursively ( callee, ctx) ?;
1161
1163
if !is_optional {
1162
- return Some ( result ) ;
1164
+ return self . transform_chain_element_recursively ( callee , ctx ) ;
1163
1165
}
1164
1166
1165
1167
// `o?.Foo.#self.getSelf?.()?.self.#m();`
@@ -1168,6 +1170,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
1168
1170
// then use it as a first argument of `getSelf` call.
1169
1171
//
1170
1172
// TODO(improve-on-babel): Consider remove this logic, because it seems no runtime behavior change.
1173
+ let result = self . transform_chain_element_recursively ( callee, ctx) ?;
1171
1174
let object = callee. to_member_expression_mut ( ) . object_mut ( ) ;
1172
1175
let ( assignment, context) = self . duplicate_object ( ctx. ast . move_expression ( object) , ctx) ;
1173
1176
* object = assignment;
0 commit comments