Skip to content

Commit

Permalink
fix(minifier): handle dce CallExpression::callee (#5752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 13, 2024
1 parent c9fea5d commit 8ff013a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
6 changes: 4 additions & 2 deletions crates/oxc_minifier/src/ast_passes/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_syntax::{
number::NumberBase,
operator::{BinaryOperator, LogicalOperator, UnaryOperator},
};
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

use crate::{
node_util::{is_exact_int64, IsLiteralValue, MayHaveSideEffects, NodeUtil, NumberValue},
Expand Down Expand Up @@ -609,7 +609,9 @@ impl<'a> FoldConstants {
} else if !left.may_have_side_effects() {
let parent = ctx.ancestry.parent();
// Bail `let o = { f() { assert.ok(this !== o); } }; (true && o.f)(); (true && o.f)``;`
if parent.is_tagged_template_expression() || parent.is_call_expression() {
if parent.is_tagged_template_expression()
|| matches!(parent, Ancestor::CallExpressionCallee(_))
{
return None;
}
// (FALSE || x) => x
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_minifier/src/ast_passes/minimize_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

use crate::{node_util::NodeUtil, tri::Tri, CompressorPass};

Expand Down Expand Up @@ -44,7 +44,9 @@ impl<'a> MinimizeConditions {
Tri::True => {
// Bail `let o = { f() { assert.ok(this !== o); } }; (true ? o.f : false)(); (true ? o.f : false)``;`
let parent = ctx.ancestry.parent();
if parent.is_tagged_template_expression() || parent.is_call_expression() {
if parent.is_tagged_template_expression()
|| matches!(parent, Ancestor::CallExpressionCallee(_))
{
return None;
}
Some(ctx.ast.move_expression(&mut expr.consequent))
Expand Down
18 changes: 0 additions & 18 deletions crates/oxc_minifier/tests/ast_passes/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ fn test_same(source_text: &str) {
test(source_text, source_text);
}

// Oxc

#[test]
fn cjs() {
// Bail `cjs-module-lexer`.
test_same("0 && (module.exports = { version });");
}

#[test] // https://github.com/oxc-project/oxc/issues/4341
fn tagged_template() {
test_same("(1, o.f)()");
test_same("(1, o.f)``");
test_same("(true && o.f)()");
test_same("(true && o.f)``");
test_same("(true ? o.f : false)()");
test_same("(true ? o.f : false)``");
}

// Google Closure Compiler

#[test]
Expand Down
32 changes: 32 additions & 0 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,35 @@ mod minimize_conditions;
mod remove_syntax;
mod reorder_constant_expression;
mod substitute_alternate_syntax;

// Oxc Integration Tests

use crate::CompressOptions;

fn test(source_text: &str, expected: &str) {
let options = CompressOptions::default();
crate::test(source_text, expected, options);
}

fn test_same(source_text: &str) {
test(source_text, source_text);
}

#[test]
fn cjs() {
// Bail `cjs-module-lexer`.
test_same("0 && (module.exports = { version });");
}

#[test] // https://github.com/oxc-project/oxc/issues/4341
fn tagged_template() {
test_same("(1, o.f)()");
test_same("(1, o.f)``");
test_same("(!0 && o.f)()");
test_same("(!0 && o.f)``");
test_same("(!0 ? o.f : !1)()");
test_same("(!0 ? o.f : !1)``");

test("foo(true && o.f)", "foo(o.f)");
test("foo(true ? o.f : false)", "foo(o.f)");
}

0 comments on commit 8ff013a

Please sign in to comment.