From ab6142503ff9fdfbd58e531e20989162a9005efb Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 9 Jan 2025 03:17:37 +0000 Subject: [PATCH] fix(transformer/private-methods): no temp var for class when unused private methods (#8360) No temp var is required for class if it contains static private method which is not referenced within class. e.g.: ```js let C = class { static #method() {} }; ``` -> ```js let C = class {}; function _method() {} ``` --- .../src/es2022/class_properties/class.rs | 13 ++++++----- .../snapshots/oxc.snap.md | 3 ++- .../test/fixtures/options.json | 6 +++++ .../test/fixtures/unused-methods/input.js | 15 +++++++++++++ .../test/fixtures/unused-methods/output.js | 22 +++++++++++++++++++ 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/options.json create mode 100644 tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/input.js create mode 100644 tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/output.js diff --git a/crates/oxc_transformer/src/es2022/class_properties/class.rs b/crates/oxc_transformer/src/es2022/class_properties/class.rs index dda0bc4422bcf..521b47bea21db 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/class.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/class.rs @@ -70,11 +70,12 @@ impl<'a> ClassProperties<'a, '_> { let class_scope_id = class.scope_id().get().unwrap(); let has_super_class = class.super_class().is_some(); - // Check if class has any properties or statick blocks, and locate constructor (if class has one) + // Check if class has any properties, private methods, or static blocks. + // Locate constructor (if class has one). let mut instance_prop_count = 0; - let mut has_instance_private_method = false; let mut has_static_prop = false; - let mut has_static_block = false; + let mut has_instance_private_method = false; + let mut has_static_private_method_or_static_block = false; // TODO: Store `FxIndexMap`s in a pool and re-use them let mut private_props = FxIndexMap::default(); let mut constructor = None; @@ -104,7 +105,7 @@ impl<'a> ClassProperties<'a, '_> { ClassElement::StaticBlock(_) => { // Static block only necessitates transforming class if it's being transformed if self.transform_static_blocks { - has_static_block = true; + has_static_private_method_or_static_block = true; continue; } } @@ -115,7 +116,7 @@ impl<'a> ClassProperties<'a, '_> { } } else if let PropertyKey::PrivateIdentifier(ident) = &method.key { if method.r#static { - has_static_prop = true; + has_static_private_method_or_static_block = true; } else { has_instance_private_method = true; } @@ -169,7 +170,7 @@ impl<'a> ClassProperties<'a, '_> { // Exit if nothing to transform if instance_prop_count == 0 && !has_static_prop - && !has_static_block + && !has_static_private_method_or_static_block && !has_instance_private_method { self.classes_stack.push(ClassDetails { diff --git a/tasks/transform_conformance/snapshots/oxc.snap.md b/tasks/transform_conformance/snapshots/oxc.snap.md index 7de2940341909..681931878d8ae 100644 --- a/tasks/transform_conformance/snapshots/oxc.snap.md +++ b/tasks/transform_conformance/snapshots/oxc.snap.md @@ -1,9 +1,10 @@ commit: 54a8389f -Passed: 123/141 +Passed: 124/142 # All Passed: * babel-plugin-transform-class-static-block +* babel-plugin-transform-private-methods * babel-plugin-transform-logical-assignment-operators * babel-plugin-transform-nullish-coalescing-operator * babel-plugin-transform-optional-catch-binding diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/options.json b/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/options.json new file mode 100644 index 0000000000000..fa9cdbc26b5fc --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "transform-class-properties", + "transform-private-methods" + ] +} diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/input.js b/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/input.js new file mode 100644 index 0000000000000..841fe82329894 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/input.js @@ -0,0 +1,15 @@ +class C { + #method() {} +} + +class C2 { + static #method() {} +} + +const C3 = class { + #method() {} +}; + +const C4 = class { + static #method() {} +}; diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/output.js b/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/output.js new file mode 100644 index 0000000000000..87dbdc9c6e68b --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-private-methods/test/fixtures/unused-methods/output.js @@ -0,0 +1,22 @@ +var _Class_brand; + +var _C_brand = new WeakSet(); +class C { + constructor() { + babelHelpers.classPrivateMethodInitSpec(this, _C_brand); + } +} +function _method() {} + +class C2 {} +function _method2() {} + +const C3 = (_Class_brand = new WeakSet(), class { + constructor() { + babelHelpers.classPrivateMethodInitSpec(this, _Class_brand); + } +}); +function _method3() {} + +const C4 = class {}; +function _method4() {}