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
5 changes: 5 additions & 0 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use oxc_syntax::{
identifier::{is_identifier_part, is_identifier_start},
reference::ReferenceId,
};
use oxc_traverse::Ancestor;

use crate::{options::CompressOptions, state::MinifierState, symbol_value::SymbolValue};

Expand Down Expand Up @@ -253,4 +254,8 @@ impl<'a> Ctx<'a, '_> {
chars.next().is_some_and(is_identifier_start)
&& chars.all(|c| is_identifier_part(c) && c != '・' && c != '・')
}

pub fn is_in_async_generator(&self) -> bool {
self.ancestors().any(|ancestor| matches!(ancestor, Ancestor::FunctionBody(body) if *body.r#async() && *body.generator()))
}
}
29 changes: 28 additions & 1 deletion crates/oxc_minifier/src/peephole/minimize_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{iter, ops::ControlFlow};
use oxc_allocator::{Box, TakeIn, Vec};
use oxc_ast::ast::*;
use oxc_ast_visit::Visit;
use oxc_ecmascript::side_effects::MayHaveSideEffects;
use oxc_ecmascript::{
constant_evaluation::{DetermineValueType, ValueType},
side_effects::MayHaveSideEffects,
};
use oxc_semantic::ScopeId;
use oxc_span::{ContentEq, GetSpan};
use oxc_traverse::Ancestor;
Expand Down Expand Up @@ -610,6 +613,30 @@ impl<'a> PeepholeOptimizations {

ctx: &mut Ctx<'a, '_>,
) {
if let Some(argument) = &mut ret_stmt.argument
&& argument.value_type(ctx) == ValueType::Undefined
// `return undefined` has a different semantic in async generator function.
&& !ctx.is_in_async_generator()
{
ctx.state.changed = true;
if argument.may_have_side_effects(ctx) {
if ctx.options().sequences
&& let Some(Statement::ExpressionStatement(prev_expr_stmt)) = result.last_mut()
{
let a = &mut prev_expr_stmt.expression;
prev_expr_stmt.expression = Self::join_sequence(a, argument, ctx);
} else {
result.push(
ctx.ast.statement_expression(argument.span(), argument.take_in(ctx.ast)),
);
}
}
ret_stmt.argument = None;
result.push(Statement::ReturnStatement(ret_stmt));
*is_control_flow_dead = true;
return;
}

if ctx.options().sequences {
if let Some(Statement::ExpressionStatement(prev_expr_stmt)) = result.last_mut() {
if let Some(argument) = &mut ret_stmt.argument {
Expand Down
10 changes: 3 additions & 7 deletions crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,8 @@ impl<'a> PeepholeOptimizations {
return;
}
// `return undefined` has a different semantic in async generator function.
for ancestor in ctx.ancestors() {
if let Ancestor::FunctionBody(func) = ancestor {
if *func.r#async() && *func.generator() {
return;
}
}
if ctx.is_in_async_generator() {
return;
}
stmt.argument = None;
ctx.state.changed = true;
Expand Down Expand Up @@ -1482,7 +1478,7 @@ mod test {
test("function f(){return !1;}", "function f(){return !1}");
test("function f(){return null;}", "function f(){return null}");
test("function f(){return void 0;}", "function f(){}");
test("function f(){return void foo();}", "function f(){return void foo()}");
test("function f(){return void foo();}", "function f(){foo()}");
test("function f(){return undefined;}", "function f(){}");
test("function f(){if(a()){return undefined;}}", "function f(){a()}");
test_same("function a(undefined) { return undefined; }");
Expand Down
2 changes: 1 addition & 1 deletion tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Original | minified | minified | gzip | gzip | Iterations | Fi

6.69 MB | 2.23 MB | 2.31 MB | 461.69 kB | 488.28 kB | 3 | antd.js

10.95 MB | 3.35 MB | 3.49 MB | 860.59 kB | 915.50 kB | 2 | typescript.js
10.95 MB | 3.35 MB | 3.49 MB | 860.58 kB | 915.50 kB | 2 | typescript.js

Loading