feat(es/minifier): Improve if_return (#4694)

This commit is contained in:
Donny/강동윤 2022-05-18 17:39:57 +09:00 committed by GitHub
parent 9792cffefa
commit 274648ec26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 13 deletions

View File

@ -7,7 +7,10 @@ use swc_ecma_utils::{ExprExt, ExprFactory, StmtExt, StmtLike};
use super::Optimizer; use super::Optimizer;
use crate::{ use crate::{
compress::{optimize::Ctx, util::negate_cost}, compress::{
optimize::Ctx,
util::{negate, negate_cost},
},
mode::Mode, mode::Mode,
DISABLE_BUGGY_PASSES, DISABLE_BUGGY_PASSES,
}; };
@ -687,6 +690,10 @@ where
*stmts = new; *stmts = new;
} }
/// if (foo) return bar()
/// else baz()
///
/// `else` token can be removed from the code above.
/// if (foo) return bar() /// if (foo) return bar()
/// else baz() /// else baz()
/// ///
@ -716,11 +723,22 @@ where
Ok(stmt) => match stmt { Ok(stmt) => match stmt {
Stmt::If(IfStmt { Stmt::If(IfStmt {
span, span,
test, mut test,
cons, mut cons,
alt: Some(alt), alt: Some(mut alt),
.. ..
}) if cons.terminates() => { }) if cons.terminates() => {
if let (
Stmt::Return(ReturnStmt { arg: None, .. }),
Stmt::Decl(Decl::Fn(..)),
) = (&*cons, &*alt)
{
// I don't know why, but terser behaves differently
negate(&self.expr_ctx, &mut test, true, false);
swap(&mut cons, &mut alt);
}
new_stmts.push(T::from_stmt(Stmt::If(IfStmt { new_stmts.push(T::from_stmt(Stmt::If(IfStmt {
span, span,
test, test,

View File

@ -193,7 +193,7 @@ impl Pure<'_> {
where where
T: StmtLike + ModuleItemExt + Take, T: StmtLike + ModuleItemExt + Take,
{ {
if !self.options.dead_code { if !self.options.dead_code && !self.options.if_return {
return; return;
} }

View File

@ -8,8 +8,6 @@ drop_unused/var_catch_toplevel/input.js
evaluate/issue_2535_1/input.js evaluate/issue_2535_1/input.js
harmony/array_literal_with_spread_4a/input.js harmony/array_literal_with_spread_4a/input.js
inline/dont_inline_funcs_into_default_param_2/input.js inline/dont_inline_funcs_into_default_param_2/input.js
issue_1052/defun_else_if_return/input.js
issue_1052/defun_if_return/input.js
issue_281/inner_var_for_in_1/input.js issue_281/inner_var_for_in_1/input.js
issue_368/collapse/input.js issue_368/collapse/input.js
nullish/conditional_to_nullish_coalescing_2/input.js nullish/conditional_to_nullish_coalescing_2/input.js

View File

@ -271,6 +271,7 @@ conditionals/issue_1645_1/input.js
conditionals/issue_1645_2/input.js conditionals/issue_1645_2/input.js
conditionals/issue_2535_2/input.js conditionals/issue_2535_2/input.js
conditionals/issue_2560/input.js conditionals/issue_2560/input.js
conditionals/issue_2994/input.js
conditionals/no_evaluate/input.js conditionals/no_evaluate/input.js
conditionals/ternary_boolean_alternative/input.js conditionals/ternary_boolean_alternative/input.js
conditionals/ternary_boolean_consequent/input.js conditionals/ternary_boolean_consequent/input.js
@ -711,6 +712,8 @@ issue_1041/const_pragma/input.js
issue_1041/not_const/input.js issue_1041/not_const/input.js
issue_1043/issue_1043/input.js issue_1043/issue_1043/input.js
issue_1044/issue_1044/input.js issue_1044/issue_1044/input.js
issue_1052/defun_else_if_return/input.js
issue_1052/defun_if_return/input.js
issue_1105/Infinity_in_with_scope/input.js issue_1105/Infinity_in_with_scope/input.js
issue_1105/Infinity_not_in_with_scope/input.js issue_1105/Infinity_not_in_with_scope/input.js
issue_1105/with_in_global_scope/input.js issue_1105/with_in_global_scope/input.js

View File

@ -100,7 +100,6 @@ conditionals/ifs_6/input.js
conditionals/ifs_same_consequent/input.js conditionals/ifs_same_consequent/input.js
conditionals/issue_1154/input.js conditionals/issue_1154/input.js
conditionals/issue_2535_1/input.js conditionals/issue_2535_1/input.js
conditionals/issue_2994/input.js
dead_code/dead_code_2_should_warn/input.js dead_code/dead_code_2_should_warn/input.js
dead_code/dead_code_2_should_warn_strict/input.js dead_code/dead_code_2_should_warn_strict/input.js
dead_code/dead_code_const_annotation/input.js dead_code/dead_code_const_annotation/input.js

View File

@ -3,14 +3,13 @@ function f(condition1, condition2, condition3) {
if (condition2) return aValue; if (condition2) return aValue;
{ {
const variable1 = "something"; const variable1 = "something";
if (condition3) { if (!condition3) return;
{
const variable2 = "else"; const variable2 = "else";
return anotherValue; return anotherValue;
} }
return;
} }
} }
} }
let aValue = 2, let aValue = 2, anotherValue = 3;
anotherValue = 3; for(let i = 0; i < 8; ++i)console.log(f(4 & i, 2 & i, 1 & i));
for (let i = 0; i < 8; ++i) console.log(f(4 & i, 2 & i, 1 & i));