Skip to content
Open
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
10 changes: 8 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,14 @@ impl Optimizer<'_> {
_ => return None,
};

let lt = left.get_type(self.ctx.expr_ctx);
let rt = right.get_type(self.ctx.expr_ctx);
let lt = left.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
);
let rt = right.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
);
if let (Value::Known(Type::Undefined), Value::Known(Type::Null))
| (Value::Known(Type::Null), Value::Known(Type::Undefined)) = (lt, rt)
{
Expand Down
10 changes: 8 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,11 +1562,17 @@ impl VisitMut for Optimizer<'_> {
self.optimize_bin_and_or(n);

if n.op == op!(bin, "+") {
if let Known(Type::Str) = n.left.get_type(self.ctx.expr_ctx) {
if let Known(Type::Str) = n.left.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
) {
self.optimize_expr_in_str_ctx(&mut n.right);
}

if let Known(Type::Str) = n.right.get_type(self.ctx.expr_ctx) {
if let Known(Type::Str) = n.right.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
) {
self.optimize_expr_in_str_ctx(&mut n.left);
}
}
Expand Down
25 changes: 20 additions & 5 deletions crates/swc_ecma_minifier/src/compress/optimize/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ impl Optimizer<'_> {
}

if e.op == op!("===") {
if let Known(lt) = e.left.get_type(self.ctx.expr_ctx) {
if let Known(rt) = e.right.get_type(self.ctx.expr_ctx) {
if let Known(lt) = e.left.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
) {
if let Known(rt) = e.right.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
) {
if lt == rt {
e.op = op!("==");
self.changed = true;
Expand Down Expand Up @@ -131,7 +137,10 @@ impl Optimizer<'_> {
| Expr::Bin(BinExpr { op: op!("<"), .. })
| Expr::Bin(BinExpr { op: op!(">="), .. })
| Expr::Bin(BinExpr { op: op!(">"), .. }) => {
if let Known(Type::Bool) = arg.get_type(self.ctx.expr_ctx) {
if let Known(Type::Bool) = arg.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
) {
self.changed = true;
report_change!("Optimizing: `!!expr` => `expr`");
*e = *arg.take();
Expand Down Expand Up @@ -187,14 +196,20 @@ impl Optimizer<'_> {
_ => {}
}

let lt = bin.left.get_type(self.ctx.expr_ctx);
let lt = bin.left.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
);
match lt {
// Don't change type
Known(Type::Bool) => {}
_ => return,
}

let rt = bin.right.get_type(self.ctx.expr_ctx);
let rt = bin.right.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
);
match rt {
Known(Type::Bool) => {}
_ => return,
Expand Down
16 changes: 12 additions & 4 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ impl Optimizer<'_> {
}

if let Callee::Expr(callee) = &e.callee {
if callee.is_pure_callee(self.ctx.expr_ctx) {
if callee.is_pure_callee(self.ctx.expr_ctx.unresolved_ctxt) {
if !self.is_skippable_for_seq(a, callee) {
return false;
}
Expand Down Expand Up @@ -1403,7 +1403,7 @@ impl Optimizer<'_> {
false
}
OptChainBase::Call(e) => {
if e.callee.is_pure_callee(self.ctx.expr_ctx) {
if e.callee.is_pure_callee(self.ctx.expr_ctx.unresolved_ctxt) {
if !self.is_skippable_for_seq(a, &e.callee) {
return false;
}
Expand Down Expand Up @@ -2371,7 +2371,12 @@ impl Optimizer<'_> {
Mergable::Drop => return Ok(false),
};

let a_type = a_right.as_deref().map(|a| a.get_type(self.ctx.expr_ctx));
let a_type = a_right.as_deref().map(|a| {
a.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
)
});

if let Some(a_right) = a_right {
if a_right.is_this() || a_right.is_ident_ref_to("arguments") {
Expand Down Expand Up @@ -2487,7 +2492,10 @@ impl Optimizer<'_> {
let Some(a_type) = a_type else {
return Ok(false);
};
let b_type = b.right.get_type(self.ctx.expr_ctx);
let b_type = b.right.get_type(
self.ctx.expr_ctx.unresolved_ctxt,
self.ctx.expr_ctx.remaining_depth,
);

if let Some(a_op) = a_op {
if can_drop_op_for(a_op, b.op, var_type, a_type, b_type) {
Expand Down
20 changes: 8 additions & 12 deletions crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,19 @@ impl Optimizer<'_> {

#[cfg_attr(feature = "debug", tracing::instrument(level = "debug", skip_all))]
pub(super) fn drop_unused_vars(&mut self, name: &mut Pat, init: Option<&mut Expr>) {
if self
.ctx
.bit_ctx
.intersects(BitCtx::IsExported | BitCtx::InAsm)
{
if !self.options.unused && !self.options.side_effects {
return;
}

trace_op!("unused: drop_unused_vars({})", dump(&*name, false));

if !self.options.unused && !self.options.side_effects {
if self.ctx.bit_ctx.intersects(
BitCtx::IsExported
.union(BitCtx::InAsm)
.union(BitCtx::InVarDeclOfForInOrOfLoop),
) {
return;
}

if self.ctx.bit_ctx.contains(BitCtx::InVarDeclOfForInOrOfLoop) {
if !name.is_ident() && init.is_none() {
return;
}

Expand All @@ -135,9 +133,7 @@ impl Optimizer<'_> {
}
}

if !name.is_ident() && init.is_none() {
return;
}
trace_op!("unused: drop_unused_vars({})", dump(&*name, false));

self.take_pat_if_unused(name, init, true);
}
Expand Down
8 changes: 5 additions & 3 deletions crates/swc_ecma_minifier/src/compress/pure/bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl Pure<'_> {
|| is_typeof_unaray(&e.right, &e.left)
|| (self.options.comparisons
&& matches!(
(e.left.get_type(self.expr_ctx), e.right.get_type(self.expr_ctx)),
(e.left.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth), e.right.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth)),
(Value::Known(l), Value::Known(r)) if l == r
));

Expand Down Expand Up @@ -306,8 +306,10 @@ impl Pure<'_> {
right,
..
}) => {
let lt = left.get_type(self.expr_ctx);
let rt = right.get_type(self.expr_ctx);
let lt =
left.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
let rt =
right.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);

if let (Value::Known(Type::Bool), Value::Known(Type::Bool)) = (lt, rt) {
let rb = right.as_pure_bool(self.expr_ctx);
Expand Down
21 changes: 16 additions & 5 deletions crates/swc_ecma_minifier/src/compress/pure/conds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ impl Pure<'_> {

let Expr::Cond(cond) = e else { return };

let lt = cond.cons.get_type(self.expr_ctx);
let lt = cond
.cons
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
if let Value::Known(Type::Bool) = lt {
let lb = cond.cons.as_pure_bool(self.expr_ctx);
if let Value::Known(true) = lb {
Expand Down Expand Up @@ -145,7 +147,9 @@ impl Pure<'_> {
}
}

let rt = cond.alt.get_type(self.expr_ctx);
let rt = cond
.alt
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
if let Value::Known(Type::Bool) = rt {
let rb = cond.alt.as_pure_bool(self.expr_ctx);
if let Value::Known(false) = rb {
Expand Down Expand Up @@ -233,7 +237,10 @@ impl Pure<'_> {
Expr::Lit(Lit::Num(Number { value: 0.0, .. })),
) if *value > 0.0
&& (!cond.test.is_bin()
|| cond.test.get_type(self.expr_ctx) == Value::Known(Type::Bool)) =>
|| cond.test.get_type(
self.expr_ctx.unresolved_ctxt,
self.expr_ctx.remaining_depth,
) == Value::Known(Type::Bool)) =>
{
report_change!("conditionals: `foo ? num : 0` => `num * !!foo`");
self.changed = true;
Expand Down Expand Up @@ -292,8 +299,12 @@ impl Pure<'_> {
return;
}

let lt = bin.left.get_type(self.expr_ctx);
let rt = bin.right.get_type(self.expr_ctx);
let lt = bin
.left
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
let rt = bin
.right
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);

let _lb = bin.left.as_pure_bool(self.expr_ctx);
let rb = bin.right.as_pure_bool(self.expr_ctx);
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ impl Pure<'_> {
)
};
match (
n.left.get_type(self.expr_ctx).opt()?,
n.right.get_type(self.expr_ctx).opt()?,
n.left
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth)
.opt()?,
n.right
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth)
.opt()?,
) {
// Abort if types differ, or one of them is unknown.
(lt, rt) if lt != rt => {}
Expand Down
34 changes: 22 additions & 12 deletions crates/swc_ecma_minifier/src/compress/pure/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,11 @@ impl Pure<'_> {
callee: Callee::Expr(callee),
args,
..
}) if callee.is_one_of_global_ref_to(self.expr_ctx, &["Array", "Object", "RegExp"]) => {
}) if callee.is_one_of_global_ref_to(
self.expr_ctx.unresolved_ctxt,
&["Array", "Object", "RegExp"],
) =>
{
let new_expr = match &**callee {
Expr::Ident(Ident { sym, .. }) if &**sym == "RegExp" => {
self.optimize_regex(args, span)
Expand Down Expand Up @@ -1163,7 +1167,7 @@ impl Pure<'_> {
args,
..
}) if callee.is_one_of_global_ref_to(
self.expr_ctx,
self.expr_ctx.unresolved_ctxt,
&["Boolean", "Number", "String", "Symbol"],
) =>
{
Expand Down Expand Up @@ -1273,7 +1277,7 @@ impl Pure<'_> {
args,
..
}) if callee.is_one_of_global_ref_to(
self.expr_ctx,
self.expr_ctx.unresolved_ctxt,
&[
"Object",
// https://262.ecma-international.org/12.0/#sec-array-constructor
Expand All @@ -1292,7 +1296,7 @@ impl Pure<'_> {
"TypeError",
"URIError",
],
) || (callee.is_global_ref_to(self.expr_ctx, "RegExp")
) || (callee.is_global_ref_to(self.expr_ctx.unresolved_ctxt, "RegExp")
&& can_compress_new_regexp(args.as_deref())) =>
{
self.changed = true;
Expand Down Expand Up @@ -1615,7 +1619,7 @@ impl Pure<'_> {
callee: Callee::Expr(callee),
args,
..
}) if callee.is_pure_callee(self.expr_ctx) => {
}) if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt) => {
report_change!("ignore_return_value: Dropping a pure call (callee is pure)");
self.changed = true;

Expand Down Expand Up @@ -1644,7 +1648,9 @@ impl Pure<'_> {
ctxt,
args,
..
}) if callee.is_pure_callee(self.expr_ctx) || ctxt.has_mark(self.marks.pure) => {
}) if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt)
|| ctxt.has_mark(self.marks.pure) =>
{
report_change!("ignore_return_value: Dropping a pure call");
self.changed = true;

Expand All @@ -1667,7 +1673,7 @@ impl Pure<'_> {
args,
..
}) => {
if callee.is_pure_callee(self.expr_ctx) {
if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt) {
self.changed = true;
report_change!("Dropping pure call as callee is pure");
*e = self
Expand All @@ -1683,7 +1689,7 @@ impl Pure<'_> {
tpl,
..
}) => {
if callee.is_pure_callee(self.expr_ctx) {
if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt) {
self.changed = true;
report_change!("Dropping pure tag tpl as callee is pure");
*e = self
Expand Down Expand Up @@ -2157,7 +2163,7 @@ impl Pure<'_> {
Expr::New(NewExpr {
span, callee, args, ..
}) if callee.is_one_of_global_ref_to(
self.expr_ctx,
self.expr_ctx.unresolved_ctxt,
&[
"Map", "Set", "Array", "Object", "Boolean", "Number", "String",
],
Expand All @@ -2181,7 +2187,7 @@ impl Pure<'_> {
args,
..
}) if callee.is_one_of_global_ref_to(
self.expr_ctx,
self.expr_ctx.unresolved_ctxt,
&["Array", "Object", "Boolean", "Number"],
) =>
{
Expand Down Expand Up @@ -2422,8 +2428,12 @@ impl Pure<'_> {
_ => return,
};

let lt = cond.cons.get_type(self.expr_ctx);
let rt = cond.alt.get_type(self.expr_ctx);
let lt = cond
.cons
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
let rt = cond
.alt
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
match (lt, rt) {
(Known(Type::Bool), Known(Type::Bool)) => {}
_ => return,
Expand Down
Loading
Loading