mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(minifier): handle dce CallExpression::callee (#5752)
This commit is contained in:
parent
c9fea5dd16
commit
8ff013ada1
4 changed files with 40 additions and 22 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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)");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue