Skip to content

Commit 102148d

Browse files
committed
refact(es): Remove useless helper(again)
1 parent 32c9894 commit 102148d

5 files changed

Lines changed: 31 additions & 164 deletions

File tree

crates/swc_ecma_minifier/src/compress/pure/dead_code.rs

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use par_iter::prelude::*;
22
use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP};
33
use swc_ecma_ast::*;
4-
use swc_ecma_utils::{extract_var_ids, ExprCtx, ExprExt, StmtExt, StmtLike, Value};
4+
use swc_ecma_utils::{extract_var_ids, ExprCtx, ExprExt, StmtExt, StmtLike, Type, Value};
55
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
66

77
use super::Pure;
@@ -11,62 +11,6 @@ use crate::{
1111
util::{make_bool, ModuleItemExt},
1212
};
1313

14-
/// Returns true if completing `expr` always produces a value accepted by
15-
/// RequireObjectCoercible.
16-
///
17-
/// This deliberately recognizes only expression forms whose result is known
18-
/// from syntax. Calls and identifier references are excluded because their
19-
/// values can be nullish even when evaluating them has no observable effects.
20-
fn is_definitely_non_nullish(expr: &Expr) -> bool {
21-
match expr {
22-
Expr::Paren(ParenExpr { expr, .. }) => is_definitely_non_nullish(expr),
23-
24-
Expr::Lit(Lit::Null(..))
25-
| Expr::Unary(UnaryExpr {
26-
op: op!("void"), ..
27-
}) => false,
28-
29-
Expr::Lit(..)
30-
| Expr::Array(..)
31-
| Expr::Arrow(..)
32-
| Expr::Class(..)
33-
| Expr::Fn(..)
34-
| Expr::New(..)
35-
| Expr::Object(..)
36-
| Expr::Tpl(..)
37-
| Expr::Unary(..)
38-
| Expr::Update(..) => true,
39-
40-
Expr::Assign(AssignExpr {
41-
op: op!("="),
42-
right,
43-
..
44-
}) => is_definitely_non_nullish(right),
45-
46-
Expr::Bin(BinExpr {
47-
op: op!("&&") | op!("||") | op!("??"),
48-
left,
49-
right,
50-
..
51-
}) => is_definitely_non_nullish(left) && is_definitely_non_nullish(right),
52-
53-
// All other binary operators produce a primitive value if their
54-
// operands complete evaluation. Replacing the outer assignment retains
55-
// that operand evaluation and any exception it may produce.
56-
Expr::Bin(..) => true,
57-
58-
Expr::Cond(CondExpr { cons, alt, .. }) => {
59-
is_definitely_non_nullish(cons) && is_definitely_non_nullish(alt)
60-
}
61-
62-
Expr::Seq(SeqExpr { exprs, .. }) => exprs
63-
.last()
64-
.is_some_and(|expr| is_definitely_non_nullish(expr)),
65-
66-
_ => false,
67-
}
68-
}
69-
7014
/// Methods related to option `dead_code`.
7115
impl Pure<'_> {
7216
pub(super) fn simplify_assign_expr(&mut self, e: &mut Expr) {
@@ -96,7 +40,11 @@ impl Pure<'_> {
9640
..
9741
}) if match &*left {
9842
AssignTargetPat::Object(obj) => {
99-
obj.props.is_empty() && is_definitely_non_nullish(right)
43+
obj.props.is_empty()
44+
&& !matches!(
45+
right.get_type(self.expr_ctx),
46+
Value::Known(Type::Null | Type::Undefined) | Value::Unknown
47+
)
10048
}
10149
_ => false,
10250
} =>

crates/swc_ecma_minifier/src/compress/pure/misc.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::Pure;
1717
use crate::{
1818
compress::{
1919
pure::{strings::convert_str_value_to_tpl_raw, Ctx},
20-
util::{eval_to_undefined, is_pure_undefined},
20+
util::is_pure_undefined,
2121
},
2222
usage_analyzer::util::is_global_var_with_pure_property_access,
2323
};
@@ -61,45 +61,6 @@ fn can_compress_new_regexp(args: Option<&[ExprOrSpread]>) -> bool {
6161
}
6262
}
6363

64-
fn may_evaluate_to_nullish(expr_ctx: ExprCtx, expr: &Expr) -> bool {
65-
if is_pure_undefined(expr_ctx, expr) || matches!(expr, Expr::Lit(Lit::Null(..))) {
66-
return true;
67-
}
68-
69-
match expr {
70-
Expr::Paren(ParenExpr { expr, .. }) => may_evaluate_to_nullish(expr_ctx, expr),
71-
Expr::Seq(SeqExpr { exprs, .. }) => match exprs.last() {
72-
Some(last) => may_evaluate_to_nullish(expr_ctx, last),
73-
None => false,
74-
},
75-
Expr::Cond(CondExpr { cons, alt, .. }) => {
76-
may_evaluate_to_nullish(expr_ctx, cons) || may_evaluate_to_nullish(expr_ctx, alt)
77-
}
78-
_ => matches!(
79-
expr.get_type(expr_ctx),
80-
Value::Known(Type::Undefined | Type::Null) | Value::Unknown
81-
),
82-
}
83-
}
84-
85-
/// Returns true if evaluating `expr` always produces a nullish value.
86-
///
87-
/// Unlike [`is_pure_undefined`], this accepts expressions with effects because
88-
/// callers can preserve those effects separately.
89-
fn eval_to_nullish(expr_ctx: ExprCtx, expr: &Expr) -> bool {
90-
match expr {
91-
Expr::Paren(ParenExpr { expr, .. }) => eval_to_nullish(expr_ctx, expr),
92-
Expr::Seq(SeqExpr { exprs, .. }) => exprs
93-
.last()
94-
.is_some_and(|last| eval_to_nullish(expr_ctx, last)),
95-
Expr::Cond(CondExpr { cons, alt, .. }) => {
96-
eval_to_nullish(expr_ctx, cons) && eval_to_nullish(expr_ctx, alt)
97-
}
98-
Expr::Lit(Lit::Null(..)) => true,
99-
_ => eval_to_undefined(expr_ctx, expr),
100-
}
101-
}
102-
10364
fn collect_exprs_from_object(obj: &mut ObjectLit) -> Vec<Box<Expr>> {
10465
let mut exprs = Vec::new();
10566

@@ -885,9 +846,10 @@ impl Pure<'_> {
885846
if !self.options.unsafe_passes
886847
&& groups.iter().any(|group| match group {
887848
GroupType::Literals(_) => false,
888-
GroupType::Expression(expr) => {
889-
may_evaluate_to_nullish(self.expr_ctx, &expr.expr)
890-
}
849+
GroupType::Expression(expr) => matches!(
850+
&expr.expr.get_type(self.expr_ctx,),
851+
Value::Known(Type::Null | Type::Undefined) | Value::Unknown
852+
),
891853
})
892854
{
893855
return None;
@@ -2339,8 +2301,10 @@ impl Pure<'_> {
23392301
.and_then(|arg| arg.first())
23402302
.map(|arg| {
23412303
arg.spread.is_none()
2342-
&& (eval_to_nullish(self.expr_ctx, &arg.expr)
2343-
|| is_valid_map_set_init(&arg.expr, self.expr_ctx, callee))
2304+
&& (matches!(
2305+
&arg.expr.get_type(self.expr_ctx,),
2306+
Value::Known(Type::Null | Type::Undefined)
2307+
) || is_valid_map_set_init(&arg.expr, self.expr_ctx, callee))
23442308
})
23452309
.unwrap_or(true) =>
23462310
{

crates/swc_ecma_minifier/tests/projects-size.snapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
| File | Original Size | Compressed Size | Gzipped Size |
22
| --- | --- | --- | --- |
3-
| angular-1.2.5.js | 757.44 KiB | 101.90 KiB | 37.18 KiB |
3+
| angular-1.2.5.js | 757.44 KiB | 102.00 KiB | 37.21 KiB |
44
| backbone-1.1.0.js | 59.77 KiB | 18.29 KiB | 6.29 KiB |
55
| jquery-1.9.1.js | 309.61 KiB | 90.76 KiB | 32.09 KiB |
66
| jquery.mobile-1.4.2.js | 534.38 KiB | 191.24 KiB | 52.60 KiB |

crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use swc_ecma_ast::*;
99
use swc_ecma_transforms_base::perf::{cpu_count, Parallel, ParallelExt};
1010
use swc_ecma_utils::{
1111
extract_var_ids, is_literal, prepend_stmt, ExprCtx, ExprExt, ExprFactory, Hoister, IsEmpty,
12-
StmtExt, StmtLike, Value::Known,
12+
StmtExt, StmtLike, Type,
13+
Value::{self, Known},
1314
};
1415
use swc_ecma_visit::{
1516
noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith,
@@ -62,58 +63,6 @@ struct Remover {
6263
expr_ctx: ExprCtx,
6364
}
6465

65-
/// Returns true if completing `expr` always produces a value accepted by
66-
/// RequireObjectCoercible.
67-
///
68-
/// Empty object destructuring still throws for nullish values, so this must
69-
/// remain conservative when removing the destructuring assignment.
70-
fn is_definitely_non_nullish(expr: &Expr) -> bool {
71-
match expr {
72-
Expr::Paren(ParenExpr { expr, .. }) => is_definitely_non_nullish(expr),
73-
74-
Expr::Lit(Lit::Null(..))
75-
| Expr::Unary(UnaryExpr {
76-
op: op!("void"), ..
77-
}) => false,
78-
79-
Expr::Lit(..)
80-
| Expr::Array(..)
81-
| Expr::Arrow(..)
82-
| Expr::Class(..)
83-
| Expr::Fn(..)
84-
| Expr::New(..)
85-
| Expr::Object(..)
86-
| Expr::Tpl(..)
87-
| Expr::Unary(..)
88-
| Expr::Update(..) => true,
89-
90-
Expr::Assign(AssignExpr {
91-
op: op!("="),
92-
right,
93-
..
94-
}) => is_definitely_non_nullish(right),
95-
96-
Expr::Bin(BinExpr {
97-
op: op!("&&") | op!("||") | op!("??"),
98-
left,
99-
right,
100-
..
101-
}) => is_definitely_non_nullish(left) && is_definitely_non_nullish(right),
102-
103-
Expr::Bin(..) => true,
104-
105-
Expr::Cond(CondExpr { cons, alt, .. }) => {
106-
is_definitely_non_nullish(cons) && is_definitely_non_nullish(alt)
107-
}
108-
109-
Expr::Seq(SeqExpr { exprs, .. }) => exprs
110-
.last()
111-
.is_some_and(|expr| is_definitely_non_nullish(expr)),
112-
113-
_ => false,
114-
}
115-
}
116-
11766
impl Parallel for Remover {
11867
fn create(&self) -> Self {
11968
Self { ..*self }
@@ -189,7 +138,11 @@ impl VisitMut for Remover {
189138
..
190139
}) if match &*left {
191140
AssignTargetPat::Object(obj) => {
192-
obj.props.is_empty() && is_definitely_non_nullish(right)
141+
obj.props.is_empty()
142+
&& !matches!(
143+
right.get_type(self.expr_ctx),
144+
Value::Known(Type::Null | Type::Undefined) | Value::Unknown
145+
)
193146
}
194147
_ => false,
195148
} =>

crates/swc_ecma_utils/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,9 +3346,11 @@ fn get_type(expr: &Expr, ctx: ExprCtx) -> Value<Type> {
33463346
..
33473347
}) if &**length == "length" => match &**obj {
33483348
Expr::Array(ArrayLit { .. }) | Expr::Lit(Lit::Str(..)) => Known(Type::Num),
3349-
Expr::Ident(Ident { sym: arguments, .. }) if &**arguments == "arguments" => {
3350-
Known(Type::Num)
3351-
}
3349+
Expr::Ident(Ident {
3350+
sym: arguments,
3351+
ctxt,
3352+
..
3353+
}) if &**arguments == "arguments" && *ctxt == ctx.unresolved_ctxt => Known(Type::Num),
33523354
_ => Unknown,
33533355
},
33543356

@@ -3421,9 +3423,9 @@ fn get_type(expr: &Expr, ctx: ExprCtx) -> Value<Type> {
34213423
Unknown
34223424
}
34233425

3424-
Expr::Ident(Ident { ref sym, .. }) => Known(match &**sym {
3425-
"undefined" => UndefinedType,
3426-
"NaN" | "Infinity" => NumberType,
3426+
Expr::Ident(Ident { sym, ctxt, .. }) => Known(match &**sym {
3427+
"undefined" if *ctxt == ctx.unresolved_ctxt => UndefinedType,
3428+
"NaN" | "Infinity" if *ctxt == ctx.unresolved_ctxt => NumberType,
34273429
_ => return Unknown,
34283430
}),
34293431

0 commit comments

Comments
 (0)