Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Inline before cloning #4338

Merged
merged 10 commits into from
Apr 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions crates/swc_ecma_minifier/src/compress/optimize/hoist_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ where
return;
}

if let Some(Expr::Object(init)) = n.init.as_deref() {
for prop in &init.props {
if let Some(init) = n.init.as_deref() {
self.mode.store(name.to_id(), init);
}

if let Some(Expr::Object(init)) = n.init.as_deref_mut() {
for prop in &mut init.props {
let prop = match prop {
PropOrSpread::Spread(_) => continue,
PropOrSpread::Prop(prop) => prop,
};

if let Prop::KeyValue(p) = &**prop {
if let Prop::KeyValue(p) = &mut **prop {
self.vars.inline_with_multi_replacer(&mut p.value);

let value = match &*p.value {
Expr::Lit(..) => p.value.clone(),
Expr::Fn(..) | Expr::Arrow(..) => {
Expand All @@ -117,7 +123,6 @@ where
);
self.simple_props
.insert((name.to_id(), s.value.clone()), value);
self.mode.store(name.to_id(), n.init.as_deref().unwrap());
}
PropName::Ident(i) => {
trace_op!(
Expand All @@ -126,7 +131,6 @@ where
);
self.simple_props
.insert((name.to_id(), i.sym.clone()), value);
self.mode.store(name.to_id(), n.init.as_deref().unwrap());
}
_ => {}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ where
return;
}

self.vars.inline_with_multi_replacer(init);

// TODO: Check for side effect between original decl position and inlined
// position

Expand Down Expand Up @@ -435,6 +437,8 @@ where
// Inline very simple functions.
match decl {
Decl::Fn(f) if self.options.inline >= 2 && f.ident.sym != *"arguments" => {
self.vars.inline_with_multi_replacer(&mut f.function.body);

match &f.function.body {
Some(body) => {
if !UsageFinder::find(&i, body)
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/pure/dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Pure<'_> {
}
}

/// Returns [Some] if the whole statement sohuld be replaced
/// Returns [Some] if the whole statement should be replaced
fn drop_useless_continue_inner(
&mut self,
label: Option<Ident>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
TestSnapshot {
vars: [
(
(
Atom('String' 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: false,
has_property_access: true,
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: false,
used_as_arg: false,
pure_fn: false,
infects: [],
},
),
(
(
Atom('check' type=inline),
#2,
),
VarUsageInfo {
inline_prevented: false,
ref_count: 1,
cond_init: false,
declared: true,
declared_count: 1,
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: false,
used_as_arg: false,
pure_fn: false,
infects: [],
},
),
(
(
Atom('code' type=inline),
#3,
),
VarUsageInfo {
inline_prevented: false,
ref_count: 2,
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: 1,
usage_count: 2,
reassigned_with_assignment: false,
reassigned_with_var_decl: false,
mutated: true,
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: true,
pure_fn: false,
infects: [],
},
),
(
(
Atom('regex' 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: true,
has_property_mutation: false,
accessed_props: {},
exported: false,
used_above_decl: false,
is_fn_local: false,
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('regexCheck' type=dynamic),
#1,
),
VarUsageInfo {
inline_prevented: true,
ref_count: 0,
cond_init: false,
declared: true,
declared_count: 1,
declared_as_fn_param: false,
declared_as_fn_expr: false,
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: true,
used_by_nested_fn: false,
executed_multiple_time: false,
used_in_cond: false,
var_kind: None,
var_initialized: true,
declared_as_catch_param: false,
no_side_effect_for_member_access: false,
used_as_callee: false,
used_as_arg: false,
pure_fn: false,
infects: [],
},
),
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Create a code check from a regex.
*
* @param {RegExp} regex
* @returns {(code: Code) => code is number}
*/
export function regexCheck(regex) {
return check

/**
* Check whether a code matches the bound regex.
*
* @param {Code} code Character code
* @returns {code is number} Whether the character code matches the bound regex
*/
function check(code) {
return code !== null && regex.test(String.fromCharCode(code))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function regexCheck(regex) {
return function(code) {
return null !== code && regex.test(String.fromCharCode(code));
};
}