refactor(transformer/class-properties): transform the remaining PrivateFieldExpression in ChainExpression first (#7763)

We need to transform the inner PrivateFieldExpression so that the variable name matches Babel's output as closely as possible.
This commit is contained in:
Dunqing 2024-12-10 10:35:05 +00:00
parent f42dbdf56b
commit 9cacf64f1d
2 changed files with 14 additions and 11 deletions

View file

@ -1096,6 +1096,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
let is_optional = field_expr.optional;
let object = &mut field_expr.object;
let left = if is_optional {
Some(self.transform_expression_to_wrap_nullish_check(object, ctx))
} else {
@ -1192,9 +1193,17 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
object: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let owned_object = ctx.ast.move_expression(object.get_inner_expression_mut());
let owned_object =
Self::ensure_optional_expression_wrapped_by_chain_expression(owned_object, ctx);
let mut owned_object = ctx.ast.move_expression(object.get_inner_expression_mut());
let owned_object = if let Some(result) =
self.transform_chain_element_recursively(&mut owned_object, ctx)
{
// If the `object` contains PrivateFieldExpression, we need to transform it first.
Self::wrap_conditional_check(result, owned_object, ctx)
} else {
Self::ensure_optional_expression_wrapped_by_chain_expression(owned_object, ctx)
};
let (assignment, reference1, reference2) = self.duplicate_object_twice(owned_object, ctx);
*object = reference1;
self.wrap_nullish_check(assignment, reference2, ctx)

View file

@ -1,6 +1,6 @@
commit: 54a8389f
Passed: 480/846
Passed: 482/846
# All Passed:
* babel-plugin-transform-class-static-block
@ -276,7 +276,7 @@ x Output mismatch
x Output mismatch
# babel-plugin-transform-class-properties (153/264)
# babel-plugin-transform-class-properties (155/264)
* assumption-constantSuper/complex-super-class/input.js
x Output mismatch
@ -488,9 +488,6 @@ x Output mismatch
* private/optional-chain-optional-member-call-with-transform/input.js
x Output mismatch
* private/optional-chain-optional-property/input.js
x Output mismatch
* private/optional-chain-optional-property-with-transform/input.js
x Output mismatch
@ -675,9 +672,6 @@ x Output mismatch
* private-loose/optional-chain-optional-member-call-with-transform/input.js
x Output mismatch
* private-loose/optional-chain-optional-property/input.js
x Output mismatch
* private-loose/optional-chain-optional-property-with-transform/input.js
x Output mismatch