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

This commit is contained in:
Boshen 2024-09-13 10:46:14 +00:00
parent c9fea5dd16
commit 8ff013ada1
4 changed files with 40 additions and 22 deletions

View file

@ -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

View file

@ -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))

View file

@ -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]

View file

@ -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)");
}