mirror of
https://github.com/swc-project/swc.git
synced 2024-12-29 16:42:28 +03:00
fix(es/minifier): Make if_return
to not drop required returns (#4847)
This commit is contained in:
parent
866664163e
commit
8365c3d0df
76
crates/swc/tests/fixture/issues-4xxx/4841/input/.swcrc
Normal file
76
crates/swc/tests/fixture/issues-4xxx/4841/input/.swcrc
Normal file
@ -0,0 +1,76 @@
|
||||
{
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "ecmascript",
|
||||
"jsx": false
|
||||
},
|
||||
"target": "es2021",
|
||||
"loose": false,
|
||||
"minify": {
|
||||
"compress": {
|
||||
"arguments": false,
|
||||
"arrows": false,
|
||||
"booleans": false,
|
||||
"booleans_as_integers": false,
|
||||
"collapse_vars": false,
|
||||
"comparisons": false,
|
||||
"computed_props": false,
|
||||
"conditionals": false,
|
||||
"dead_code": false,
|
||||
"defaults": false,
|
||||
"directives": false,
|
||||
"drop_console": false,
|
||||
"drop_debugger": false,
|
||||
"ecma": 5,
|
||||
"evaluate": false,
|
||||
"expression": false,
|
||||
"global_defs": {},
|
||||
"hoist_funs": false,
|
||||
"hoist_props": false,
|
||||
"hoist_vars": false,
|
||||
"ie8": false,
|
||||
"if_return": true,
|
||||
"inline": 0,
|
||||
"join_vars": false,
|
||||
"keep_classnames": false,
|
||||
"keep_fargs": false,
|
||||
"keep_fnames": false,
|
||||
"keep_infinity": false,
|
||||
"loops": false,
|
||||
"module": false,
|
||||
"negate_iife": false,
|
||||
"passes": 1,
|
||||
"properties": false,
|
||||
"pure_funcs": [],
|
||||
"pure_getters": false,
|
||||
"reduce_funcs": false,
|
||||
"reduce_vars": false,
|
||||
"sequences": false,
|
||||
"side_effects": false,
|
||||
"switches": false,
|
||||
"top_retain": [],
|
||||
"toplevel": false,
|
||||
"typeofs": false,
|
||||
"unsafe": false,
|
||||
"unsafe_arrows": false,
|
||||
"unsafe_comps": false,
|
||||
"unsafe_Function": false,
|
||||
"unsafe_math": false,
|
||||
"unsafe_methods": false,
|
||||
"unsafe_proto": false,
|
||||
"unsafe_regexp": false,
|
||||
"unsafe_symbols": false,
|
||||
"unsafe_undefined": false,
|
||||
"unused": false
|
||||
},
|
||||
"mangle": true
|
||||
},
|
||||
"transform": {
|
||||
"legacyDecorator": true
|
||||
},
|
||||
"externalHelpers": true
|
||||
},
|
||||
"module": {
|
||||
"type": "es6"
|
||||
}
|
||||
}
|
11
crates/swc/tests/fixture/issues-4xxx/4841/input/input.js
Normal file
11
crates/swc/tests/fixture/issues-4xxx/4841/input/input.js
Normal file
@ -0,0 +1,11 @@
|
||||
global._processChunk = function _processChunk(chunk, async, newReq) {
|
||||
if (chunk) {
|
||||
if (!async) {
|
||||
return true;
|
||||
}
|
||||
var newReq = this.write();
|
||||
newReq.callback = callback;
|
||||
return;
|
||||
}
|
||||
cb();
|
||||
};
|
10
crates/swc/tests/fixture/issues-4xxx/4841/output/input.js
Normal file
10
crates/swc/tests/fixture/issues-4xxx/4841/output/input.js
Normal file
@ -0,0 +1,10 @@
|
||||
global._processChunk = function d(b, c, a) {
|
||||
if (b) {
|
||||
if (!c) {
|
||||
return true;
|
||||
}
|
||||
var a = this.write();
|
||||
return (a.callback = callback), void 0;
|
||||
}
|
||||
cb();
|
||||
};
|
@ -44,7 +44,7 @@ where
|
||||
pub(super) fn merge_if_returns(
|
||||
&mut self,
|
||||
stmts: &mut Vec<Stmt>,
|
||||
can_work: bool,
|
||||
terminates: bool,
|
||||
is_fn_body: bool,
|
||||
) {
|
||||
if !self.options.if_return {
|
||||
@ -52,11 +52,11 @@ where
|
||||
}
|
||||
|
||||
for stmt in stmts.iter_mut() {
|
||||
self.merge_nested_if_returns(stmt, can_work);
|
||||
self.merge_nested_if_returns(stmt, terminates);
|
||||
}
|
||||
|
||||
if can_work || is_fn_body {
|
||||
self.merge_if_returns_inner(stmts);
|
||||
if terminates || is_fn_body {
|
||||
self.merge_if_returns_inner(stmts, !is_fn_body);
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ where
|
||||
/// return a ? foo() : bar();
|
||||
/// }
|
||||
/// ```
|
||||
fn merge_if_returns_inner(&mut self, stmts: &mut Vec<Stmt>) {
|
||||
fn merge_if_returns_inner(&mut self, stmts: &mut Vec<Stmt>, should_preserve_last_return: bool) {
|
||||
if !self.options.if_return {
|
||||
return;
|
||||
}
|
||||
@ -366,11 +366,12 @@ where
|
||||
if let Some(mut cur) = cur {
|
||||
match &*cur {
|
||||
Expr::Seq(seq)
|
||||
if seq
|
||||
.exprs
|
||||
.last()
|
||||
.map(|v| is_pure_undefined(&self.expr_ctx, v))
|
||||
.unwrap_or(true) =>
|
||||
if !should_preserve_last_return
|
||||
&& seq
|
||||
.exprs
|
||||
.last()
|
||||
.map(|v| is_pure_undefined(&self.expr_ctx, v))
|
||||
.unwrap_or(true) =>
|
||||
{
|
||||
let expr = self.ignore_return_value(&mut cur);
|
||||
|
||||
|
@ -0,0 +1,270 @@
|
||||
TestSnapshot {
|
||||
vars: [
|
||||
(
|
||||
(
|
||||
Atom('_processChunk' type=dynamic),
|
||||
#2,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 0,
|
||||
cond_init: false,
|
||||
declared: false,
|
||||
declared_count: 0,
|
||||
declared_as_fn_param: false,
|
||||
declared_as_fn_expr: true,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 0,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: false,
|
||||
has_property_access: false,
|
||||
has_property_mutation: false,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: false,
|
||||
is_fn_local: false,
|
||||
used_by_nested_fn: false,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: false,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: false,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
(
|
||||
(
|
||||
Atom('async' type=static),
|
||||
#2,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 1,
|
||||
cond_init: false,
|
||||
declared: true,
|
||||
declared_count: 1,
|
||||
declared_as_fn_param: true,
|
||||
declared_as_fn_expr: false,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 1,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: false,
|
||||
has_property_access: false,
|
||||
has_property_mutation: false,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: false,
|
||||
is_fn_local: true,
|
||||
used_by_nested_fn: true,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: true,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: false,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
(
|
||||
(
|
||||
Atom('callback' type=dynamic),
|
||||
#1,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 1,
|
||||
cond_init: false,
|
||||
declared: false,
|
||||
declared_count: 0,
|
||||
declared_as_fn_param: false,
|
||||
declared_as_fn_expr: false,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 1,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: false,
|
||||
has_property_access: false,
|
||||
has_property_mutation: false,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: true,
|
||||
is_fn_local: true,
|
||||
used_by_nested_fn: true,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: true,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: false,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
(
|
||||
(
|
||||
Atom('cb' type=inline),
|
||||
#1,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 1,
|
||||
cond_init: false,
|
||||
declared: false,
|
||||
declared_count: 0,
|
||||
declared_as_fn_param: false,
|
||||
declared_as_fn_expr: false,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 1,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: false,
|
||||
has_property_access: false,
|
||||
has_property_mutation: false,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: true,
|
||||
is_fn_local: true,
|
||||
used_by_nested_fn: true,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: false,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: true,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
(
|
||||
(
|
||||
Atom('chunk' type=inline),
|
||||
#2,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 1,
|
||||
cond_init: false,
|
||||
declared: true,
|
||||
declared_count: 1,
|
||||
declared_as_fn_param: true,
|
||||
declared_as_fn_expr: false,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 1,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: false,
|
||||
has_property_access: false,
|
||||
has_property_mutation: false,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: false,
|
||||
is_fn_local: true,
|
||||
used_by_nested_fn: true,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: false,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: false,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
(
|
||||
(
|
||||
Atom('global' type=static),
|
||||
#1,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 1,
|
||||
cond_init: false,
|
||||
declared: false,
|
||||
declared_count: 0,
|
||||
declared_as_fn_param: false,
|
||||
declared_as_fn_expr: false,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 1,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: true,
|
||||
has_property_access: true,
|
||||
has_property_mutation: true,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: true,
|
||||
is_fn_local: true,
|
||||
used_by_nested_fn: false,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: false,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: false,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
(
|
||||
(
|
||||
Atom('newReq' type=inline),
|
||||
#2,
|
||||
),
|
||||
VarUsageInfo {
|
||||
inline_prevented: false,
|
||||
ref_count: 1,
|
||||
cond_init: true,
|
||||
declared: true,
|
||||
declared_count: 2,
|
||||
declared_as_fn_param: true,
|
||||
declared_as_fn_expr: false,
|
||||
assign_count: 0,
|
||||
mutation_by_call_count: 0,
|
||||
usage_count: 1,
|
||||
reassigned_with_assignment: false,
|
||||
reassigned_with_var_decl: false,
|
||||
mutated: true,
|
||||
has_property_access: true,
|
||||
has_property_mutation: true,
|
||||
accessed_props: {},
|
||||
exported: false,
|
||||
used_above_decl: false,
|
||||
is_fn_local: true,
|
||||
used_by_nested_fn: true,
|
||||
executed_multiple_time: false,
|
||||
used_in_cond: true,
|
||||
var_kind: None,
|
||||
var_initialized: false,
|
||||
declared_as_catch_param: false,
|
||||
no_side_effect_for_member_access: false,
|
||||
used_as_callee: false,
|
||||
used_as_arg: false,
|
||||
pure_fn: false,
|
||||
infects: [],
|
||||
},
|
||||
),
|
||||
],
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
{
|
||||
"arguments": false,
|
||||
"arrows": false,
|
||||
"booleans": false,
|
||||
"booleans_as_integers": false,
|
||||
"collapse_vars": false,
|
||||
"comparisons": false,
|
||||
"computed_props": false,
|
||||
"conditionals": false,
|
||||
"dead_code": false,
|
||||
"defaults": false,
|
||||
"directives": false,
|
||||
"drop_console": false,
|
||||
"drop_debugger": false,
|
||||
"ecma": 5,
|
||||
"evaluate": false,
|
||||
"expression": false,
|
||||
"global_defs": {},
|
||||
"hoist_funs": false,
|
||||
"hoist_props": false,
|
||||
"hoist_vars": false,
|
||||
"ie8": false,
|
||||
"if_return": true,
|
||||
"inline": 0,
|
||||
"join_vars": false,
|
||||
"keep_classnames": false,
|
||||
"keep_fargs": false,
|
||||
"keep_fnames": false,
|
||||
"keep_infinity": false,
|
||||
"loops": false,
|
||||
"module": false,
|
||||
"negate_iife": false,
|
||||
"passes": 1,
|
||||
"properties": false,
|
||||
"pure_funcs": [],
|
||||
"pure_getters": false,
|
||||
"reduce_funcs": false,
|
||||
"reduce_vars": false,
|
||||
"sequences": false,
|
||||
"side_effects": false,
|
||||
"switches": false,
|
||||
"top_retain": [],
|
||||
"toplevel": false,
|
||||
"typeofs": false,
|
||||
"unsafe": false,
|
||||
"unsafe_arrows": false,
|
||||
"unsafe_comps": false,
|
||||
"unsafe_Function": false,
|
||||
"unsafe_math": false,
|
||||
"unsafe_methods": false,
|
||||
"unsafe_proto": false,
|
||||
"unsafe_regexp": false,
|
||||
"unsafe_symbols": false,
|
||||
"unsafe_undefined": false,
|
||||
"unused": false
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
global._processChunk = function _processChunk(chunk, async, newReq) {
|
||||
if (chunk) {
|
||||
if (!async) {
|
||||
return true;
|
||||
}
|
||||
var newReq = this.write();
|
||||
newReq.callback = callback;
|
||||
return;
|
||||
}
|
||||
cb();
|
||||
};
|
@ -0,0 +1,10 @@
|
||||
global._processChunk = function _processChunk(chunk, async, newReq) {
|
||||
if (chunk) {
|
||||
if (!async) {
|
||||
return true;
|
||||
}
|
||||
var newReq = this.write();
|
||||
return (newReq.callback = callback), void 0;
|
||||
}
|
||||
cb();
|
||||
};
|
@ -0,0 +1,11 @@
|
||||
global._processChunk = function d(b, c, a) {
|
||||
if (b) {
|
||||
if (!c) {
|
||||
return true;
|
||||
}
|
||||
var a = this.write();
|
||||
a.callback = callback;
|
||||
return;
|
||||
}
|
||||
cb();
|
||||
};
|
Loading…
Reference in New Issue
Block a user