swc_ecma_minifier/util/
size.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use num_bigint::{BigInt as BigIntValue, Sign};
use swc_common::SyntaxContext;
use swc_ecma_ast::*;

/// Give a rough size for everything
pub trait Size {
    fn size(&self) -> usize;
}

impl Size for Lit {
    fn size(&self) -> usize {
        match self {
            Lit::Str(s) => s.value.len() + 2,
            // would be !0 or !1
            Lit::Bool(_) => 2,
            Lit::Null(_) => 4,
            Lit::Num(num) => num.value.size(),
            Lit::BigInt(i) => i.value.size(),
            Lit::Regex(r) => r.exp.len(),
            Lit::JSXText(s) => s.value.len(),
        }
    }
}

impl Size for UnaryOp {
    fn size(&self) -> usize {
        use UnaryOp::*;
        match self {
            Minus | Plus | Bang | Tilde => 1,
            TypeOf => 7,
            Void => 5,
            Delete => 7,
        }
    }
}

impl Size for UpdateOp {
    fn size(&self) -> usize {
        2
    }
}

impl Size for BinaryOp {
    fn size(&self) -> usize {
        use BinaryOp::*;
        match self {
            Lt | Gt | Add | Sub | Mul | Div | Mod | BitOr | BitXor | BitAnd | EqEq | NotEq
            | LtEq | GtEq | LShift | RShift | LogicalOr | LogicalAnd | Exp | NullishCoalescing
            | EqEqEq | NotEqEq | ZeroFillRShift => self.as_str().len(),

            In => 4,
            InstanceOf => 12,
        }
    }
}

impl Size for AssignOp {
    fn size(&self) -> usize {
        self.as_str().len()
    }
}

impl Size for PrivateName {
    fn size(&self) -> usize {
        // priv name can be mangled
        2
    }
}

// TODO: optimize
impl Size for f64 {
    fn size(&self) -> usize {
        if self.fract() == 0.0 {
            self.log10().ceil() as usize + 1
        } else {
            self.to_string().len()
        }
    }
}

#[allow(clippy::bool_to_int_with_if)]
impl Size for BigIntValue {
    fn size(&self) -> usize {
        let sign = if let Sign::Minus = self.sign() { 1 } else { 0 };
        // bits is bascially log2
        // use this until https://github.com/rust-num/num-bigint/issues/57
        let value = ((self.bits() as f64) / 10.0_f64.log2()).ceil() as usize + 1;
        sign + value + 1 // n
    }
}

/// Give a rough size for everything
pub trait SizeWithCtxt {
    fn size(&self, unresolved: SyntaxContext) -> usize;
}

const TODO: usize = 10000;

impl SizeWithCtxt for Expr {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            Expr::Lit(lit) => lit.size(),
            Expr::Ident(id) => id.size(unresolved),

            Expr::Bin(BinExpr {
                op, left, right, ..
            }) => op.size() + left.size(unresolved) + right.size(unresolved),

            Expr::Unary(UnaryExpr { arg, op, .. }) => op.size() + arg.size(unresolved),

            Expr::Call(CallExpr { callee, args, .. }) => {
                callee.size(unresolved) + args.size(unresolved) + 2
            }

            Expr::New(NewExpr { callee, args, .. }) => {
                args.as_ref().map_or(0, |args| args.size(unresolved) + 2)
                    + 4
                    + callee.size(unresolved)
            }

            Expr::Member(m) => m.obj.size(unresolved) + m.prop.size(unresolved),
            Expr::SuperProp(s) => 6 + s.prop.size(unresolved),
            Expr::Update(e) => e.arg.size(unresolved) + e.op.size(),

            Expr::Assign(AssignExpr {
                op, left, right, ..
            }) => left.size(unresolved) + op.size() + right.size(unresolved),

            Expr::Seq(e) => {
                e.exprs
                    .iter()
                    .map(|v| v.size(unresolved) + 1)
                    .sum::<usize>()
                    - 1
            }

            Expr::This(_) => 4,
            Expr::Array(a) => 2 + a.elems.size(unresolved),
            Expr::Object(o) => 2 + o.props.size(unresolved),

            Expr::Yield(YieldExpr { arg, delegate, .. }) => {
                6 + *delegate as usize + arg.as_ref().map_or(0, |a| a.size(unresolved))
            }
            Expr::Await(a) => 6 + a.arg.size(unresolved),
            Expr::Cond(CondExpr {
                test, cons, alt, ..
            }) => test.size(unresolved) + 1 + cons.size(unresolved) + 1 + alt.size(unresolved),
            Expr::Tpl(t) => t.size(unresolved),
            Expr::TaggedTpl(t) => t.tag.size(unresolved) + t.tpl.size(unresolved),

            Expr::Arrow(ArrowExpr {
                params,
                body,
                is_async,
                ..
            }) => match &**body {
                BlockStmtOrExpr::BlockStmt(_) => TODO,
                BlockStmtOrExpr::Expr(e) => {
                    let p = match &params[..] {
                        [] => 2,
                        [Pat::Ident(_)] => 1,
                        _ => 2 + params.size(unresolved),
                    };
                    let a = if *is_async {
                        5 + usize::from(params.len() != 1)
                    } else {
                        0
                    };

                    p + a + 2 + e.size(unresolved)
                }
            },
            Expr::Fn(_) => TODO,
            Expr::Class(_) => TODO,

            Expr::MetaProp(m) => match m.kind {
                MetaPropKind::NewTarget => 10,
                MetaPropKind::ImportMeta => 11,
            },
            Expr::PrivateName(p) => p.size(),
            Expr::OptChain(p) => match &*p.base {
                OptChainBase::Member(m) => 1 + m.obj.size(unresolved) + m.prop.size(unresolved),
                OptChainBase::Call(c) => {
                    1 + c.callee.size(unresolved) + c.args.size(unresolved) + 2
                }
            },

            Expr::Paren(p) => 2 + p.expr.size(unresolved),
            Expr::Invalid(_) => 0,

            Expr::JSXMember(_) => TODO,
            Expr::JSXNamespacedName(_) => TODO,
            Expr::JSXEmpty(_) => TODO,
            Expr::JSXElement(_) => TODO,
            Expr::JSXFragment(_) => TODO,
            Expr::TsTypeAssertion(_) => TODO,
            Expr::TsConstAssertion(_) => TODO,
            Expr::TsNonNull(_) => TODO,
            Expr::TsAs(_) => TODO,
            Expr::TsInstantiation(_) => TODO,
            Expr::TsSatisfies(_) => TODO,
        }
    }
}

impl SizeWithCtxt for Ident {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        if self.ctxt == unresolved {
            self.sym.len()
        } else {
            1
        }
    }
}

impl SizeWithCtxt for Callee {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            Callee::Super(_) => 5,
            Callee::Import(_) => 6,
            Callee::Expr(e) => e.size(unresolved),
        }
    }
}

impl SizeWithCtxt for ExprOrSpread {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        let mut c = 0;

        if self.spread.is_some() {
            c += 3;
        }

        c += self.expr.size(unresolved);

        c
    }
}

impl SizeWithCtxt for MemberProp {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            MemberProp::Ident(id) => 1 + id.sym.len(),
            MemberProp::PrivateName(priv_name) => 1 + priv_name.size(),
            MemberProp::Computed(c) => 2 + c.expr.size(unresolved),
        }
    }
}

impl SizeWithCtxt for SuperProp {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            SuperProp::Ident(id) => 1 + id.sym.len(),
            SuperProp::Computed(c) => 2 + c.expr.size(unresolved),
        }
    }
}

impl SizeWithCtxt for Pat {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            Pat::Ident(id) => id.size(unresolved),
            Pat::Array(a) => 2 + a.elems.size(unresolved),
            Pat::Rest(r) => 3 + r.arg.size(unresolved),
            Pat::Object(o) => 2 + o.props.size(unresolved),
            Pat::Assign(a) => a.left.size(unresolved) + 1 + a.right.size(unresolved),
            Pat::Invalid(_) => 0,
            Pat::Expr(e) => e.size(unresolved),
        }
    }
}

impl SizeWithCtxt for AssignTarget {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            AssignTarget::Simple(e) => e.size(unresolved),
            AssignTarget::Pat(p) => p.size(unresolved),
        }
    }
}

impl SizeWithCtxt for SimpleAssignTarget {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            SimpleAssignTarget::Ident(e) => {
                if e.ctxt == unresolved {
                    e.sym.len()
                } else {
                    1
                }
            }
            SimpleAssignTarget::Member(e) => e.obj.size(unresolved) + e.prop.size(unresolved),
            SimpleAssignTarget::SuperProp(e) => 6 + e.prop.size(unresolved),
            SimpleAssignTarget::Paren(e) => 2 + e.expr.size(unresolved),
            SimpleAssignTarget::OptChain(e) => match &*e.base {
                OptChainBase::Member(m) => 1 + m.obj.size(unresolved) + m.prop.size(unresolved),
                OptChainBase::Call(c) => {
                    1 + c.callee.size(unresolved) + c.args.size(unresolved) + 2
                }
            },
            SimpleAssignTarget::TsAs(_)
            | SimpleAssignTarget::TsSatisfies(_)
            | SimpleAssignTarget::TsNonNull(_)
            | SimpleAssignTarget::TsTypeAssertion(_)
            | SimpleAssignTarget::TsInstantiation(_)
            | SimpleAssignTarget::Invalid(_) => TODO,
        }
    }
}

impl SizeWithCtxt for AssignTargetPat {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            AssignTargetPat::Array(a) => 2 + a.elems.size(unresolved),
            AssignTargetPat::Object(o) => 2 + o.props.size(unresolved),
            AssignTargetPat::Invalid(_) => unreachable!(),
        }
    }
}

impl SizeWithCtxt for PropName {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            PropName::Ident(id) => id.sym.len(),
            PropName::Str(s) => s.value.len(),
            PropName::Num(n) => n.value.size(),
            PropName::Computed(c) => 2 + c.expr.size(unresolved),
            PropName::BigInt(n) => n.value.size(),
        }
    }
}

impl SizeWithCtxt for ObjectPatProp {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            ObjectPatProp::KeyValue(k) => k.key.size(unresolved) + 1 + k.value.size(unresolved),
            ObjectPatProp::Assign(a) => {
                a.key.sym.len() + a.value.as_ref().map_or(0, |v| v.size(unresolved) + 1)
            }
            ObjectPatProp::Rest(r) => 3 + r.arg.size(unresolved),
        }
    }
}

impl SizeWithCtxt for PropOrSpread {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        match self {
            PropOrSpread::Spread(s) => 3 + s.expr.size(unresolved),
            PropOrSpread::Prop(p) => match &**p {
                Prop::Shorthand(s) => s.sym.len(),
                Prop::KeyValue(KeyValueProp { key, value }) => {
                    key.size(unresolved) + 1 + value.size(unresolved)
                }
                // where is Prop::Assign valid?
                Prop::Assign(_) => TODO,
                Prop::Getter(_) => TODO,
                Prop::Setter(_) => TODO,
                Prop::Method(_) => TODO,
            },
        }
    }
}

impl SizeWithCtxt for Tpl {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        let Self { exprs, quasis, .. } = self;
        let expr_len: usize = exprs.iter().map(|e| e.size(unresolved) + 3).sum();
        let str_len: usize = quasis.iter().map(|q| q.raw.len()).sum();
        2 + expr_len + str_len
    }
}

impl<T: SizeWithCtxt> SizeWithCtxt for Vec<T> {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        let mut c = 0;

        for item in self.iter() {
            c += item.size(unresolved) + 1; // comma
        }

        if !self.is_empty() {
            c -= 1;
        }

        c
    }
}

impl<T: SizeWithCtxt> SizeWithCtxt for Vec<Option<T>> {
    fn size(&self, unresolved: SyntaxContext) -> usize {
        let mut c = 0;

        for item in self.iter() {
            c += 1; // comma
            if let Some(item) = item {
                c += item.size(unresolved); // extra comma
            }
        }

        c -= match self.last() {
            // if empty or last is none, no need to remove dangling comma
            None | Some(None) => 0,
            _ => 1,
        };

        c
    }
}