Skip to content
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
3 changes: 2 additions & 1 deletion crates/oxc_minifier/src/peephole/remove_unused_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl<'a> PeepholeOptimizations {

fn remove_unused_new_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
let Expression::NewExpression(new_expr) = e else { return false };
if new_expr.pure && ctx.annotations() {
if (new_expr.pure && ctx.annotations()) || ctx.manual_pure_functions(&new_expr.callee) {
let mut exprs =
Self::fold_arguments_into_needed_expressions(&mut new_expr.arguments, ctx);
if exprs.is_empty() {
Expand Down Expand Up @@ -536,6 +536,7 @@ impl<'a> PeepholeOptimizations {

let is_pure = {
(call_expr.pure && ctx.annotations())
|| ctx.manual_pure_functions(&call_expr.callee)
|| (if let Expression::Identifier(id) = &call_expr.callee
&& let Some(symbol_id) =
ctx.scoping().get_reference(id.reference_id()).symbol_id()
Expand Down
59 changes: 17 additions & 42 deletions crates/oxc_minifier/tests/peephole/manual_pure_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ mod terser_tests {
use super::test;

#[test]
#[ignore = "FIXME"]
fn array() {
// `a / b` and `c / b` are kept because they might throw error
test(
"
var a;
Expand All @@ -31,13 +31,12 @@ mod terser_tests {
Math.floor(c / b);
}
",
"export function f(b) {}",
"var a; export function f(b) { a / b, c / b; }",
&["Math.floor"],
);
}

#[test]
#[ignore = "FIXME"]
fn side_effects() {
test(
"
Expand All @@ -56,7 +55,6 @@ mod terser_tests {
}

#[test]
#[ignore = "FIXME"]
fn unused() {
test(
"
Expand All @@ -70,8 +68,7 @@ mod terser_tests {
",
"
export function foo() {
side_effects();
return pure(3);
return side_effects(), pure(3);
}
",
&["pure"],
Expand All @@ -98,8 +95,8 @@ mod terser_tests {
}

#[test]
#[ignore = "FIXME"]
fn conditional() {
// Bitwise operators like `2 & b()` are kept because they might throw
test(
"
pure(1 | a() ? 2 & b() : 7 ^ c());
Expand All @@ -111,22 +108,14 @@ mod terser_tests {
pure(3 ? 4 : 7 ^ c());
pure(3 ? 4 : 5);
",
"
1 | a() ? b() : c(),
1 | a() && b(),
1 | a() || c(),
a(),
3 ? b() : c(),
3 && b(),
3 || c()
",
"1 | a() ? 2 & b() : 7 ^ c(), 1 | a() && 2 & b(), 1 | a() || 7 ^ c(), 1 | a(), 2 & b(), 2 & b()",
&["pure"],
);
}

#[test]
#[ignore = "FIXME"]
fn relational() {
// `in` and `instanceof` operators can throw
test(
r#"
foo() in foo();
Expand All @@ -139,20 +128,14 @@ mod terser_tests {
"bar" === bar();
"bar" >= "bar";
"#,
"
bar(),
bar(),
bar(), bar(),
bar(),
bar()
",
"foo() in foo(), foo() instanceof bar(), bar(), bar(), bar(), bar(), bar();",
&["foo"],
);
}

#[test]
#[ignore = "FIXME"]
fn arithmetic() {
// Arithmetic/bitwise operators might throw
test(
r#"
foo() + foo();
Expand All @@ -165,19 +148,12 @@ mod terser_tests {
"bar" << bar();
"bar" >>> "bar";
"#,
"
bar(),
bar(),
bar(), bar(),
bar(),
bar()
",
"foo() + foo(), foo() - bar(), foo() * 'bar', bar() / foo(), bar() & bar(), bar() | 'bar', 'bar' >> foo(), 'bar' << bar();",
&["foo"],
);
}

#[test]
#[ignore = "FIXME"]
fn boolean_and() {
// Test logical AND with pure function calls
test(
Expand All @@ -192,13 +168,13 @@ mod terser_tests {
"bar" && bar();
"bar" && "bar";
"#,
r#"
"
foo() && bar(),
bar(),
bar() && bar(),
bar(),
"bar" && bar()
"#,
bar()
",
&["foo"],
);
}
Expand Down Expand Up @@ -249,8 +225,8 @@ mod terser_tests {
}

#[test]
#[ignore = "FIXME"]
fn unary() {
// ~ is kept because it may throw
test(
r#"
typeof foo();
Expand Down Expand Up @@ -284,7 +260,8 @@ mod terser_tests {
--a[foo()],
--a[bar()],
--a.bar,
bar()
~foo(),
~bar()
",
&["foo"],
);
Expand Down Expand Up @@ -314,7 +291,6 @@ mod terser_tests {
}

#[test]
#[ignore = "FIXME"]
fn issue_3065_3() {
test(
r#"
Expand All @@ -328,15 +304,14 @@ mod terser_tests {
"#,
r#"
(function() {
console.log("PASS");
return console.log("PASS"), "FAIL";
})();
"#,
&["debug"],
);
}

#[test]
#[ignore = "FIXME"]
fn issue_3065_4() {
test(
r#"
Expand All @@ -350,7 +325,7 @@ mod terser_tests {
"#,
r#"
(function() {
console.log("PASS");
return console.log("PASS"), "FAIL";
})();
"#,
&["debug"],
Expand Down
Loading