From 8ff013ada14dc75c2ce35db238bebd7794c346f6 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:46:14 +0000 Subject: [PATCH] fix(minifier): handle dce CallExpression::callee (#5752) --- .../src/ast_passes/fold_constants.rs | 6 ++-- .../src/ast_passes/minimize_conditions.rs | 6 ++-- .../tests/ast_passes/fold_constants.rs | 18 ----------- crates/oxc_minifier/tests/ast_passes/mod.rs | 32 +++++++++++++++++++ 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/fold_constants.rs b/crates/oxc_minifier/src/ast_passes/fold_constants.rs index 2c054fef3..646202a77 100644 --- a/crates/oxc_minifier/src/ast_passes/fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/fold_constants.rs @@ -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}, @@ -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 diff --git a/crates/oxc_minifier/src/ast_passes/minimize_conditions.rs b/crates/oxc_minifier/src/ast_passes/minimize_conditions.rs index 31cb1ffea..5c270608a 100644 --- a/crates/oxc_minifier/src/ast_passes/minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/minimize_conditions.rs @@ -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}; @@ -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)) diff --git a/crates/oxc_minifier/tests/ast_passes/fold_constants.rs b/crates/oxc_minifier/tests/ast_passes/fold_constants.rs index 68e54fa19..1a87a94c3 100644 --- a/crates/oxc_minifier/tests/ast_passes/fold_constants.rs +++ b/crates/oxc_minifier/tests/ast_passes/fold_constants.rs @@ -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] diff --git a/crates/oxc_minifier/tests/ast_passes/mod.rs b/crates/oxc_minifier/tests/ast_passes/mod.rs index 0aca536ff..7b193b434 100644 --- a/crates/oxc_minifier/tests/ast_passes/mod.rs +++ b/crates/oxc_minifier/tests/ast_passes/mod.rs @@ -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)"); +}