|
| 1 | +use oxc_ast::ast::*; |
| 2 | +use oxc_ecmascript::side_effects::MayHaveSideEffects; |
| 3 | + |
| 4 | +use crate::ctx::Ctx; |
| 5 | + |
| 6 | +use super::PeepholeOptimizations; |
| 7 | + |
| 8 | +impl<'a> PeepholeOptimizations { |
| 9 | + /// Remove unused private fields and methods from class body |
| 10 | + /// |
| 11 | + /// This function uses the private member usage collected during the main traverse |
| 12 | + /// to remove unused private fields and methods from the class body. |
| 13 | + pub fn remove_unused_private_members(body: &mut ClassBody<'a>, ctx: &mut Ctx<'a, '_>) { |
| 14 | + let old_len = body.body.len(); |
| 15 | + body.body.retain(|element| match element { |
| 16 | + ClassElement::PropertyDefinition(prop) => { |
| 17 | + let PropertyKey::PrivateIdentifier(private_id) = &prop.key else { |
| 18 | + return true; |
| 19 | + }; |
| 20 | + if ctx |
| 21 | + .state |
| 22 | + .class_symbols_stack |
| 23 | + .is_private_member_used_in_current_class(&private_id.name) |
| 24 | + { |
| 25 | + return true; |
| 26 | + } |
| 27 | + prop.value.as_ref().is_some_and(|value| value.may_have_side_effects(ctx)) |
| 28 | + } |
| 29 | + ClassElement::MethodDefinition(method) => { |
| 30 | + let PropertyKey::PrivateIdentifier(private_id) = &method.key else { |
| 31 | + return true; |
| 32 | + }; |
| 33 | + ctx.state |
| 34 | + .class_symbols_stack |
| 35 | + .is_private_member_used_in_current_class(&private_id.name) |
| 36 | + } |
| 37 | + ClassElement::AccessorProperty(accessor) => { |
| 38 | + let PropertyKey::PrivateIdentifier(private_id) = &accessor.key else { |
| 39 | + return true; |
| 40 | + }; |
| 41 | + if ctx |
| 42 | + .state |
| 43 | + .class_symbols_stack |
| 44 | + .is_private_member_used_in_current_class(&private_id.name) |
| 45 | + { |
| 46 | + return true; |
| 47 | + } |
| 48 | + accessor.value.as_ref().is_some_and(|value| value.may_have_side_effects(ctx)) |
| 49 | + } |
| 50 | + ClassElement::StaticBlock(_) => true, |
| 51 | + ClassElement::TSIndexSignature(_) => { |
| 52 | + unreachable!("TypeScript syntax should be transformed away") |
| 53 | + } |
| 54 | + }); |
| 55 | + if body.body.len() != old_len { |
| 56 | + ctx.state.changed = true; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod test { |
| 63 | + use crate::tester::{test, test_same}; |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_remove_unused_private_fields() { |
| 67 | + test( |
| 68 | + "class C { #unused = 1; #used = 2; method() { return this.#used; } } new C();", |
| 69 | + "class C { #used = 2; method() { return this.#used; } } new C();", |
| 70 | + ); |
| 71 | + test( |
| 72 | + "class C { #unused = 1; #used = 2; method(foo) { return #used in foo; } } new C();", |
| 73 | + "class C { #used = 2; method(foo) { return #used in foo; } } new C();", |
| 74 | + ); |
| 75 | + test( |
| 76 | + "class C { #unused; #used; method() { return this.#used; } } new C();", |
| 77 | + "class C { #used; method() { return this.#used; } } new C();", |
| 78 | + ); |
| 79 | + test("class C { #a = 1; #b = 2; #c = 3; } new C();", "class C { } new C();"); |
| 80 | + test( |
| 81 | + "class C { static #unused = 1; static #used = 2; static method() { return C.#used; } }", |
| 82 | + "class C { static #used = 2; static method() { return C.#used; } }", |
| 83 | + ); |
| 84 | + test( |
| 85 | + "class C { public = 1; #unused = 2; #used = 3; method() { return this.public + this.#used; } } new C();", |
| 86 | + "class C { public = 1; #used = 3; method() { return this.public + this.#used; } } new C();", |
| 87 | + ); |
| 88 | + test_same("class C { #unused = foo(); method() { return 1; } } new C();"); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_remove_unused_private_methods() { |
| 93 | + test( |
| 94 | + "class C { #unusedMethod() { return 1; } #usedMethod() { return 2; } method() { return this.#usedMethod(); } } new C();", |
| 95 | + "class C { #usedMethod() { return 2; } method() { return this.#usedMethod(); } } new C();", |
| 96 | + ); |
| 97 | + test("class C { #a() {} #b() {} #c() {} } new C();", "class C { } new C();"); |
| 98 | + test( |
| 99 | + "class C { static #unusedMethod() { return 1; } static #usedMethod() { return 2; } static method() { return C.#usedMethod(); } }", |
| 100 | + "class C { static #usedMethod() { return 2; } static method() { return C.#usedMethod(); } }", |
| 101 | + ); |
| 102 | + test_same( |
| 103 | + "class C { #helper() { return 1; } method() { return this.#helper(); } } new C();", |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_remove_unused_private_accessors() { |
| 109 | + test( |
| 110 | + "class C { accessor #unused = 1; accessor #used = 2; method() { return this.#used; } } new C();", |
| 111 | + "class C { accessor #used = 2; method() { return this.#used; } } new C();", |
| 112 | + ); |
| 113 | + test_same("class C { accessor #unused = foo(); method() { return 1; } } new C();"); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_nested_classes() { |
| 118 | + test( |
| 119 | + r"class Outer { |
| 120 | + #shared = 1; |
| 121 | + #unusedOuter = 2; |
| 122 | +
|
| 123 | + method() { |
| 124 | + return this.#shared; |
| 125 | + } |
| 126 | +
|
| 127 | + getInner() { |
| 128 | + return class Inner { |
| 129 | + #shared = 3; |
| 130 | + #unusedInner = 4; |
| 131 | +
|
| 132 | + method() { |
| 133 | + return this.#shared; |
| 134 | + } |
| 135 | + }; |
| 136 | + } |
| 137 | + } new Outer();", |
| 138 | + r"class Outer { |
| 139 | + #shared = 1; |
| 140 | +
|
| 141 | + method() { |
| 142 | + return this.#shared; |
| 143 | + } |
| 144 | +
|
| 145 | + getInner() { |
| 146 | + return class { |
| 147 | + #shared = 3; |
| 148 | +
|
| 149 | + method() { |
| 150 | + return this.#shared; |
| 151 | + } |
| 152 | + }; |
| 153 | + } |
| 154 | + } new Outer();", |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments