Skip to content

Commit

Permalink
feat(minifier): remove empty IIFE (#8340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 8, 2025
1 parent 2c2e483 commit e085d66
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
Statement::BlockStatement(s) => Self::try_optimize_block(s, ctx),
Statement::IfStatement(s) => self.try_fold_if(s, ctx),
Statement::ForStatement(s) => self.try_fold_for(s, ctx),
Statement::ExpressionStatement(s) => Self::try_fold_expression_stmt(s, ctx),
Statement::ExpressionStatement(s) => {
Self::try_fold_iife(s, ctx).or_else(|| Self::try_fold_expression_stmt(s, ctx))
}
Statement::TryStatement(s) => Self::try_fold_try(s, ctx),
Statement::LabeledStatement(s) => Self::try_fold_labeled(s, ctx),
_ => None,
Expand Down Expand Up @@ -529,6 +531,19 @@ impl<'a, 'b> PeepholeRemoveDeadCode {

None
}

fn try_fold_iife(e: &ExpressionStatement<'a>, ctx: Ctx<'a, 'b>) -> Option<Statement<'a>> {
let Expression::CallExpression(e) = &e.expression else { return None };
if !e.arguments.is_empty() {
return None;
}
let (params_empty, body_empty) = match &e.callee {
Expression::FunctionExpression(f) => (f.params.is_empty(), f.body.as_ref()?.is_empty()),
Expression::ArrowFunctionExpression(f) => (f.params.is_empty(), f.body.is_empty()),
_ => return None,
};
(params_empty && body_empty).then(|| ctx.ast.statement_empty(e.span))
}
}

/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java>
Expand Down Expand Up @@ -693,16 +708,6 @@ mod test {
fold_same("delete x.y.z()");
}

#[test]
fn test_fold_function() {
fold("() => {}", "");
fold_same("var k = () => {}");
fold_same("var k = function () {}");
// TODO: fold the following...
fold_same("(() => {})()");
fold_same("(function () {})()");
}

#[test]
fn test_fold_sequence_expr() {
fold("('foo', 'bar', 'baz')", "");
Expand Down Expand Up @@ -734,4 +739,30 @@ mod test {
test("if (foo) {}", "foo");
test("if (foo) {} else {}", "foo");
}

#[test]
fn test_fold_iife() {
fold_same("var k = () => {}");
fold_same("var k = function () {}");
// test("var a = (() => {})()", "var a = /* @__PURE__ */ (() => {})();");
test("(() => {})()", "");
// test("(() => a())()", "a();");
// test("(() => { a() })()", "a();");
// test("(() => { return a() })()", "a();");
// test("(() => { let b = a; b() })()", "a();");
// test("(() => { let b = a; return b() })()", "a();");
test("(async () => {})()", "");
test_same("(async () => { a() })()");
// test("(async () => { let b = a; b() })()", "(async () => a())();");
// test("var a = (function() {})()", "var a = /* @__PURE__ */ function() {}();");
test("(function() {})()", "");
test("(function*() {})()", "");
test("(async function() {})()", "");
test_same("(function() { a() })()");
test_same("(function*() { a() })()");
test_same("(async function() { a() })()");
// test("(() => x)()", "x;");
// test("/* @__PURE__ */ (() => x)()", "");
// test("/* @__PURE__ */ (() => x)(y, z)", "y, z;");
}
}

0 comments on commit e085d66

Please sign in to comment.