From 9cacf64f1d2f279b6ca269eaeb40033fe380fd73 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:35:05 +0000 Subject: [PATCH] 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. --- .../src/es2022/class_properties/private.rs | 15 ++++++++++++--- .../transform_conformance/snapshots/babel.snap.md | 10 ++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/private.rs b/crates/oxc_transformer/src/es2022/class_properties/private.rs index ba8ebd27f..3320e9420 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/private.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/private.rs @@ -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) diff --git a/tasks/transform_conformance/snapshots/babel.snap.md b/tasks/transform_conformance/snapshots/babel.snap.md index d6643ba21..bcf589b61 100644 --- a/tasks/transform_conformance/snapshots/babel.snap.md +++ b/tasks/transform_conformance/snapshots/babel.snap.md @@ -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