mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-14 22:29:17 +00:00
feat(minifier): fold undefined argument in non delegating yield (#27324)
add missing optimization for `yield undefined`, `undefined` is substituted for `void 0` if its a global --------- Co-authored-by: Cameron <cameron.clark@hey.com>
This commit is contained in:
1 parent
b98722887c
commit
61003c727b
3 files changed
+26
-2
No files matched your search
@@ -590,7 +590,11 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
|
||||
Self::substitute_unary_plus(expr, ctx);
|
||||
Self::fold_sequence_expression(expr, ctx);
|
||||
}
|
||||
Expression::YieldExpression(_) | Expression::AwaitExpression(_) => {
|
||||
Expression::YieldExpression(e) => {
|
||||
Self::substitute_yield_expression(e);
|
||||
Self::fold_sequence_expression(expr, ctx);
|
||||
}
|
||||
Expression::AwaitExpression(_) => {
|
||||
Self::fold_sequence_expression(expr, ctx);
|
||||
}
|
||||
Expression::StaticMemberExpression(_) => {
|
||||
|
||||
@@ -1049,6 +1049,14 @@ impl<'a> PeepholeOptimizations {
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove `void 0` from a non-delegating `yield`.
|
||||
/// `yield void 0` -> `yield`
|
||||
pub fn substitute_yield_expression(expr: &mut YieldExpression<'a>) {
|
||||
if !expr.delegate && expr.argument.as_ref().is_some_and(Expression::is_void_0) {
|
||||
expr.argument = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn compress_variable_declarator(
|
||||
decl: &mut VariableDeclarator<'a>,
|
||||
kind: VariableDeclarationKind,
|
||||
|
||||
@@ -1115,7 +1115,7 @@ fn test_flatten_array_spread_elements() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fold_sequence_expression() {
|
||||
fn test_fold_sequence_expression() {
|
||||
test("(a(), b) + c", "a(), b + c");
|
||||
test("(a(), b, c) + d", "a(), b, c + d");
|
||||
|
||||
@@ -1138,3 +1138,15 @@ fn fold_sequence_expression() {
|
||||
"async function a() { c(1), d(2), await 3 }",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_substitute_yield_expression() {
|
||||
test("function* a() { yield 2 }", "function* a() { yield 2; }");
|
||||
test("function* a() { yield void 0; }", "function* a() { yield; }");
|
||||
test("function* a() { yield undefined; }", "function* a() { yield; }");
|
||||
test("function* a() { yield fn(); }", "function* a() { yield fn(); }");
|
||||
test_same("function* a() { yield* void 0; }");
|
||||
test("function* a() { yield* undefined; }", "function* a() { yield* void 0; }");
|
||||
test("function* a() { yield* fn(); }", "function* a() { yield* fn(); }");
|
||||
test_same("function* a(undefined) { yield undefined; }");
|
||||
}
|
||||
Reference in new issue
Block a user