Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(minifier): handle dce CallExpression::callee #5752

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)");
}