Skip to content

Commit 95d4311

Browse files
committed
feat(ecmascript): check side effects inside static blocks (#13404)
Now that we have `MayHaveSideEffects` for Statements, we can check the static block.
1 parent 2d0414d commit 95d4311

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

crates/oxc_ecmascript/src/side_effects/expressions.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,9 @@ impl<'a> MayHaveSideEffects<'a> for Class<'a> {
412412
impl<'a> MayHaveSideEffects<'a> for ClassElement<'a> {
413413
fn may_have_side_effects(&self, ctx: &impl MayHaveSideEffectsContext<'a>) -> bool {
414414
match self {
415-
// TODO: check side effects inside the block
416-
ClassElement::StaticBlock(block) => !block.body.is_empty(),
415+
ClassElement::StaticBlock(block) => {
416+
block.body.iter().any(|stmt| stmt.may_have_side_effects(ctx))
417+
}
417418
ClassElement::MethodDefinition(e) => {
418419
!e.decorators.is_empty() || e.key.may_have_side_effects(ctx)
419420
}

crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ fn closure_compiler_tests() {
387387

388388
// CLASS_STATIC_BLOCK
389389
test("(class C { static {} })", false);
390-
// test("(class C { static { [1]; } })", false);
391-
test("(class C { static { let x; } })", true);
392-
test("(class C { static { const x =1 ; } })", true);
393-
test("(class C { static { var x; } })", true);
390+
test("(class C { static { [1]; } })", false);
391+
test("(class C { static { let x; } })", false);
392+
test("(class C { static { const x =1 ; } })", false);
393+
test("(class C { static { var x; } })", false);
394394
test("(class C { static { this.x = 1; } })", true);
395-
test("(class C { static { function f() { } } })", true);
396-
// test("(class C { static { (function () {} )} })", false);
397-
// test("(class C { static { ()=>{} } })", false);
395+
test("(class C { static { function f() { } } })", false);
396+
test("(class C { static { (function () {} )} })", false);
397+
test("(class C { static { ()=>{} } })", false);
398398

399399
// SUPER calls
400400
test("super()", true);
@@ -715,6 +715,7 @@ fn test_class_expression() {
715715
test("(class extends foo() {})", true);
716716
test("(class extends (() => {}) {})", true);
717717
test("(class { static {} })", false);
718+
test("(class { static { 1; } })", false);
718719
test("(class { static { foo(); } })", true);
719720
test("(class { a() {} })", false);
720721
test("(class { [1]() {} })", false);

0 commit comments

Comments
 (0)