1 | import {
|
---|
2 | AST_Accessor,
|
---|
3 | AST_Array,
|
---|
4 | AST_Arrow,
|
---|
5 | AST_Await,
|
---|
6 | AST_BigInt,
|
---|
7 | AST_Binary,
|
---|
8 | AST_Block,
|
---|
9 | AST_Break,
|
---|
10 | AST_Call,
|
---|
11 | AST_Case,
|
---|
12 | AST_Class,
|
---|
13 | AST_ClassStaticBlock,
|
---|
14 | AST_ClassPrivateProperty,
|
---|
15 | AST_ClassProperty,
|
---|
16 | AST_ConciseMethod,
|
---|
17 | AST_Conditional,
|
---|
18 | AST_Const,
|
---|
19 | AST_Continue,
|
---|
20 | AST_Debugger,
|
---|
21 | AST_Default,
|
---|
22 | AST_Defun,
|
---|
23 | AST_Destructuring,
|
---|
24 | AST_Directive,
|
---|
25 | AST_Do,
|
---|
26 | AST_Dot,
|
---|
27 | AST_DotHash,
|
---|
28 | AST_EmptyStatement,
|
---|
29 | AST_Expansion,
|
---|
30 | AST_Export,
|
---|
31 | AST_False,
|
---|
32 | AST_For,
|
---|
33 | AST_ForIn,
|
---|
34 | AST_Function,
|
---|
35 | AST_Hole,
|
---|
36 | AST_If,
|
---|
37 | AST_Import,
|
---|
38 | AST_ImportMeta,
|
---|
39 | AST_Infinity,
|
---|
40 | AST_LabeledStatement,
|
---|
41 | AST_Let,
|
---|
42 | AST_NameMapping,
|
---|
43 | AST_NaN,
|
---|
44 | AST_New,
|
---|
45 | AST_NewTarget,
|
---|
46 | AST_Node,
|
---|
47 | AST_Null,
|
---|
48 | AST_Number,
|
---|
49 | AST_Object,
|
---|
50 | AST_ObjectKeyVal,
|
---|
51 | AST_ObjectGetter,
|
---|
52 | AST_ObjectSetter,
|
---|
53 | AST_PrivateGetter,
|
---|
54 | AST_PrivateMethod,
|
---|
55 | AST_PrivateSetter,
|
---|
56 | AST_PrivateIn,
|
---|
57 | AST_RegExp,
|
---|
58 | AST_Return,
|
---|
59 | AST_Sequence,
|
---|
60 | AST_String,
|
---|
61 | AST_Sub,
|
---|
62 | AST_Super,
|
---|
63 | AST_Switch,
|
---|
64 | AST_Symbol,
|
---|
65 | AST_SymbolClassProperty,
|
---|
66 | AST_SymbolExportForeign,
|
---|
67 | AST_SymbolImportForeign,
|
---|
68 | AST_SymbolRef,
|
---|
69 | AST_SymbolDeclaration,
|
---|
70 | AST_TemplateSegment,
|
---|
71 | AST_TemplateString,
|
---|
72 | AST_This,
|
---|
73 | AST_Throw,
|
---|
74 | AST_Toplevel,
|
---|
75 | AST_True,
|
---|
76 | AST_Try,
|
---|
77 | AST_Catch,
|
---|
78 | AST_Finally,
|
---|
79 | AST_Unary,
|
---|
80 | AST_Undefined,
|
---|
81 | AST_Var,
|
---|
82 | AST_VarDef,
|
---|
83 | AST_While,
|
---|
84 | AST_With,
|
---|
85 | AST_Yield,
|
---|
86 | walk_parent
|
---|
87 | } from "./ast.js";
|
---|
88 | import { first_in_statement } from "./utils/first_in_statement.js";
|
---|
89 |
|
---|
90 | let mangle_options = undefined;
|
---|
91 | AST_Node.prototype.size = function (compressor, stack) {
|
---|
92 | mangle_options = compressor && compressor._mangle_options;
|
---|
93 |
|
---|
94 | let size = 0;
|
---|
95 | walk_parent(this, (node, info) => {
|
---|
96 | size += node._size(info);
|
---|
97 |
|
---|
98 | // Braceless arrow functions have fake "return" statements
|
---|
99 | if (node instanceof AST_Arrow && node.is_braceless()) {
|
---|
100 | size += node.body[0].value._size(info);
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 | }, stack || (compressor && compressor.stack));
|
---|
104 |
|
---|
105 | // just to save a bit of memory
|
---|
106 | mangle_options = undefined;
|
---|
107 |
|
---|
108 | return size;
|
---|
109 | };
|
---|
110 |
|
---|
111 | AST_Node.prototype._size = () => 0;
|
---|
112 |
|
---|
113 | AST_Debugger.prototype._size = () => 8;
|
---|
114 |
|
---|
115 | AST_Directive.prototype._size = function () {
|
---|
116 | // TODO string encoding stuff
|
---|
117 | return 2 + this.value.length;
|
---|
118 | };
|
---|
119 |
|
---|
120 | /** Count commas/semicolons necessary to show a list of expressions/statements */
|
---|
121 | const list_overhead = (array) => array.length && array.length - 1;
|
---|
122 |
|
---|
123 | AST_Block.prototype._size = function () {
|
---|
124 | return 2 + list_overhead(this.body);
|
---|
125 | };
|
---|
126 |
|
---|
127 | AST_Toplevel.prototype._size = function() {
|
---|
128 | return list_overhead(this.body);
|
---|
129 | };
|
---|
130 |
|
---|
131 | AST_EmptyStatement.prototype._size = () => 1;
|
---|
132 |
|
---|
133 | AST_LabeledStatement.prototype._size = () => 2; // x:
|
---|
134 |
|
---|
135 | AST_Do.prototype._size = () => 9;
|
---|
136 |
|
---|
137 | AST_While.prototype._size = () => 7;
|
---|
138 |
|
---|
139 | AST_For.prototype._size = () => 8;
|
---|
140 |
|
---|
141 | AST_ForIn.prototype._size = () => 8;
|
---|
142 | // AST_ForOf inherits ^
|
---|
143 |
|
---|
144 | AST_With.prototype._size = () => 6;
|
---|
145 |
|
---|
146 | AST_Expansion.prototype._size = () => 3;
|
---|
147 |
|
---|
148 | const lambda_modifiers = func =>
|
---|
149 | (func.is_generator ? 1 : 0) + (func.async ? 6 : 0);
|
---|
150 |
|
---|
151 | AST_Accessor.prototype._size = function () {
|
---|
152 | return lambda_modifiers(this) + 4 + list_overhead(this.argnames) + list_overhead(this.body);
|
---|
153 | };
|
---|
154 |
|
---|
155 | AST_Function.prototype._size = function (info) {
|
---|
156 | const first = !!first_in_statement(info);
|
---|
157 | return (first * 2) + lambda_modifiers(this) + 12 + list_overhead(this.argnames) + list_overhead(this.body);
|
---|
158 | };
|
---|
159 |
|
---|
160 | AST_Defun.prototype._size = function () {
|
---|
161 | return lambda_modifiers(this) + 13 + list_overhead(this.argnames) + list_overhead(this.body);
|
---|
162 | };
|
---|
163 |
|
---|
164 | AST_Arrow.prototype._size = function () {
|
---|
165 | let args_and_arrow = 2 + list_overhead(this.argnames);
|
---|
166 |
|
---|
167 | if (
|
---|
168 | !(
|
---|
169 | this.argnames.length === 1
|
---|
170 | && this.argnames[0] instanceof AST_Symbol
|
---|
171 | )
|
---|
172 | ) {
|
---|
173 | args_and_arrow += 2; // parens around the args
|
---|
174 | }
|
---|
175 |
|
---|
176 | const body_overhead = this.is_braceless() ? 0 : list_overhead(this.body) + 2;
|
---|
177 |
|
---|
178 | return lambda_modifiers(this) + args_and_arrow + body_overhead;
|
---|
179 | };
|
---|
180 |
|
---|
181 | AST_Destructuring.prototype._size = () => 2;
|
---|
182 |
|
---|
183 | AST_TemplateString.prototype._size = function () {
|
---|
184 | return 2 + (Math.floor(this.segments.length / 2) * 3); /* "${}" */
|
---|
185 | };
|
---|
186 |
|
---|
187 | AST_TemplateSegment.prototype._size = function () {
|
---|
188 | return this.value.length;
|
---|
189 | };
|
---|
190 |
|
---|
191 | AST_Return.prototype._size = function () {
|
---|
192 | return this.value ? 7 : 6;
|
---|
193 | };
|
---|
194 |
|
---|
195 | AST_Throw.prototype._size = () => 6;
|
---|
196 |
|
---|
197 | AST_Break.prototype._size = function () {
|
---|
198 | return this.label ? 6 : 5;
|
---|
199 | };
|
---|
200 |
|
---|
201 | AST_Continue.prototype._size = function () {
|
---|
202 | return this.label ? 9 : 8;
|
---|
203 | };
|
---|
204 |
|
---|
205 | AST_If.prototype._size = () => 4;
|
---|
206 |
|
---|
207 | AST_Switch.prototype._size = function () {
|
---|
208 | return 8 + list_overhead(this.body);
|
---|
209 | };
|
---|
210 |
|
---|
211 | AST_Case.prototype._size = function () {
|
---|
212 | return 5 + list_overhead(this.body);
|
---|
213 | };
|
---|
214 |
|
---|
215 | AST_Default.prototype._size = function () {
|
---|
216 | return 8 + list_overhead(this.body);
|
---|
217 | };
|
---|
218 |
|
---|
219 | AST_Try.prototype._size = () => 3;
|
---|
220 |
|
---|
221 | AST_Catch.prototype._size = function () {
|
---|
222 | let size = 7 + list_overhead(this.body);
|
---|
223 | if (this.argname) {
|
---|
224 | size += 2;
|
---|
225 | }
|
---|
226 | return size;
|
---|
227 | };
|
---|
228 |
|
---|
229 | AST_Finally.prototype._size = function () {
|
---|
230 | return 7 + list_overhead(this.body);
|
---|
231 | };
|
---|
232 |
|
---|
233 | AST_Var.prototype._size = function () {
|
---|
234 | return 4 + list_overhead(this.definitions);
|
---|
235 | };
|
---|
236 |
|
---|
237 | AST_Let.prototype._size = function () {
|
---|
238 | return 4 + list_overhead(this.definitions);
|
---|
239 | };
|
---|
240 |
|
---|
241 | AST_Const.prototype._size = function () {
|
---|
242 | return 6 + list_overhead(this.definitions);
|
---|
243 | };
|
---|
244 |
|
---|
245 | AST_VarDef.prototype._size = function () {
|
---|
246 | return this.value ? 1 : 0;
|
---|
247 | };
|
---|
248 |
|
---|
249 | AST_NameMapping.prototype._size = function () {
|
---|
250 | // foreign name isn't mangled
|
---|
251 | return this.name ? 4 : 0;
|
---|
252 | };
|
---|
253 |
|
---|
254 | AST_Import.prototype._size = function () {
|
---|
255 | // import
|
---|
256 | let size = 6;
|
---|
257 |
|
---|
258 | if (this.imported_name) size += 1;
|
---|
259 |
|
---|
260 | // from
|
---|
261 | if (this.imported_name || this.imported_names) size += 5;
|
---|
262 |
|
---|
263 | // braces, and the commas
|
---|
264 | if (this.imported_names) {
|
---|
265 | size += 2 + list_overhead(this.imported_names);
|
---|
266 | }
|
---|
267 |
|
---|
268 | return size;
|
---|
269 | };
|
---|
270 |
|
---|
271 | AST_ImportMeta.prototype._size = () => 11;
|
---|
272 |
|
---|
273 | AST_Export.prototype._size = function () {
|
---|
274 | let size = 7 + (this.is_default ? 8 : 0);
|
---|
275 |
|
---|
276 | if (this.exported_value) {
|
---|
277 | size += this.exported_value._size();
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (this.exported_names) {
|
---|
281 | // Braces and commas
|
---|
282 | size += 2 + list_overhead(this.exported_names);
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (this.module_name) {
|
---|
286 | // "from "
|
---|
287 | size += 5;
|
---|
288 | }
|
---|
289 |
|
---|
290 | return size;
|
---|
291 | };
|
---|
292 |
|
---|
293 | AST_Call.prototype._size = function () {
|
---|
294 | if (this.optional) {
|
---|
295 | return 4 + list_overhead(this.args);
|
---|
296 | }
|
---|
297 | return 2 + list_overhead(this.args);
|
---|
298 | };
|
---|
299 |
|
---|
300 | AST_New.prototype._size = function () {
|
---|
301 | return 6 + list_overhead(this.args);
|
---|
302 | };
|
---|
303 |
|
---|
304 | AST_Sequence.prototype._size = function () {
|
---|
305 | return list_overhead(this.expressions);
|
---|
306 | };
|
---|
307 |
|
---|
308 | AST_Dot.prototype._size = function () {
|
---|
309 | if (this.optional) {
|
---|
310 | return this.property.length + 2;
|
---|
311 | }
|
---|
312 | return this.property.length + 1;
|
---|
313 | };
|
---|
314 |
|
---|
315 | AST_DotHash.prototype._size = function () {
|
---|
316 | if (this.optional) {
|
---|
317 | return this.property.length + 3;
|
---|
318 | }
|
---|
319 | return this.property.length + 2;
|
---|
320 | };
|
---|
321 |
|
---|
322 | AST_Sub.prototype._size = function () {
|
---|
323 | return this.optional ? 4 : 2;
|
---|
324 | };
|
---|
325 |
|
---|
326 | AST_Unary.prototype._size = function () {
|
---|
327 | if (this.operator === "typeof") return 7;
|
---|
328 | if (this.operator === "void") return 5;
|
---|
329 | return this.operator.length;
|
---|
330 | };
|
---|
331 |
|
---|
332 | AST_Binary.prototype._size = function (info) {
|
---|
333 | if (this.operator === "in") return 4;
|
---|
334 |
|
---|
335 | let size = this.operator.length;
|
---|
336 |
|
---|
337 | if (
|
---|
338 | (this.operator === "+" || this.operator === "-")
|
---|
339 | && this.right instanceof AST_Unary && this.right.operator === this.operator
|
---|
340 | ) {
|
---|
341 | // 1+ +a > needs space between the +
|
---|
342 | size += 1;
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (this.needs_parens(info)) {
|
---|
346 | size += 2;
|
---|
347 | }
|
---|
348 |
|
---|
349 | return size;
|
---|
350 | };
|
---|
351 |
|
---|
352 | AST_Conditional.prototype._size = () => 3;
|
---|
353 |
|
---|
354 | AST_Array.prototype._size = function () {
|
---|
355 | return 2 + list_overhead(this.elements);
|
---|
356 | };
|
---|
357 |
|
---|
358 | AST_Object.prototype._size = function (info) {
|
---|
359 | let base = 2;
|
---|
360 | if (first_in_statement(info)) {
|
---|
361 | base += 2; // parens
|
---|
362 | }
|
---|
363 | return base + list_overhead(this.properties);
|
---|
364 | };
|
---|
365 |
|
---|
366 | /*#__INLINE__*/
|
---|
367 | const key_size = key =>
|
---|
368 | typeof key === "string" ? key.length : 0;
|
---|
369 |
|
---|
370 | AST_ObjectKeyVal.prototype._size = function () {
|
---|
371 | return key_size(this.key) + 1;
|
---|
372 | };
|
---|
373 |
|
---|
374 | /*#__INLINE__*/
|
---|
375 | const static_size = is_static => is_static ? 7 : 0;
|
---|
376 |
|
---|
377 | AST_ObjectGetter.prototype._size = function () {
|
---|
378 | return 5 + static_size(this.static) + key_size(this.key);
|
---|
379 | };
|
---|
380 |
|
---|
381 | AST_ObjectSetter.prototype._size = function () {
|
---|
382 | return 5 + static_size(this.static) + key_size(this.key);
|
---|
383 | };
|
---|
384 |
|
---|
385 | AST_ConciseMethod.prototype._size = function () {
|
---|
386 | return static_size(this.static) + key_size(this.key) + lambda_modifiers(this);
|
---|
387 | };
|
---|
388 |
|
---|
389 | AST_PrivateMethod.prototype._size = function () {
|
---|
390 | return AST_ConciseMethod.prototype._size.call(this) + 1;
|
---|
391 | };
|
---|
392 |
|
---|
393 | AST_PrivateGetter.prototype._size = AST_PrivateSetter.prototype._size = function () {
|
---|
394 | return AST_ConciseMethod.prototype._size.call(this) + 4;
|
---|
395 | };
|
---|
396 |
|
---|
397 | AST_PrivateIn.prototype._size = function () {
|
---|
398 | return 5; // "#", and " in "
|
---|
399 | };
|
---|
400 |
|
---|
401 | AST_Class.prototype._size = function () {
|
---|
402 | return (
|
---|
403 | (this.name ? 8 : 7)
|
---|
404 | + (this.extends ? 8 : 0)
|
---|
405 | );
|
---|
406 | };
|
---|
407 |
|
---|
408 | AST_ClassStaticBlock.prototype._size = function () {
|
---|
409 | // "static{}" + semicolons
|
---|
410 | return 8 + list_overhead(this.body);
|
---|
411 | };
|
---|
412 |
|
---|
413 | AST_ClassProperty.prototype._size = function () {
|
---|
414 | return (
|
---|
415 | static_size(this.static)
|
---|
416 | + (typeof this.key === "string" ? this.key.length + 2 : 0)
|
---|
417 | + (this.value ? 1 : 0)
|
---|
418 | );
|
---|
419 | };
|
---|
420 |
|
---|
421 | AST_ClassPrivateProperty.prototype._size = function () {
|
---|
422 | return AST_ClassProperty.prototype._size.call(this) + 1;
|
---|
423 | };
|
---|
424 |
|
---|
425 | AST_Symbol.prototype._size = function () {
|
---|
426 | if (!(mangle_options && this.thedef && !this.thedef.unmangleable(mangle_options))) {
|
---|
427 | return this.name.length;
|
---|
428 | } else {
|
---|
429 | return 1;
|
---|
430 | }
|
---|
431 | };
|
---|
432 |
|
---|
433 | // TODO take propmangle into account
|
---|
434 | AST_SymbolClassProperty.prototype._size = function () {
|
---|
435 | return this.name.length;
|
---|
436 | };
|
---|
437 |
|
---|
438 | AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function () {
|
---|
439 | if (this.name === "arguments") return 9;
|
---|
440 |
|
---|
441 | return AST_Symbol.prototype._size.call(this);
|
---|
442 | };
|
---|
443 |
|
---|
444 | AST_NewTarget.prototype._size = () => 10;
|
---|
445 |
|
---|
446 | AST_SymbolImportForeign.prototype._size = function () {
|
---|
447 | return this.name.length;
|
---|
448 | };
|
---|
449 |
|
---|
450 | AST_SymbolExportForeign.prototype._size = function () {
|
---|
451 | return this.name.length;
|
---|
452 | };
|
---|
453 |
|
---|
454 | AST_This.prototype._size = () => 4;
|
---|
455 |
|
---|
456 | AST_Super.prototype._size = () => 5;
|
---|
457 |
|
---|
458 | AST_String.prototype._size = function () {
|
---|
459 | return this.value.length + 2;
|
---|
460 | };
|
---|
461 |
|
---|
462 | AST_Number.prototype._size = function () {
|
---|
463 | const { value } = this;
|
---|
464 | if (value === 0) return 1;
|
---|
465 | if (value > 0 && Math.floor(value) === value) {
|
---|
466 | return Math.floor(Math.log10(value) + 1);
|
---|
467 | }
|
---|
468 | return value.toString().length;
|
---|
469 | };
|
---|
470 |
|
---|
471 | AST_BigInt.prototype._size = function () {
|
---|
472 | return this.value.length;
|
---|
473 | };
|
---|
474 |
|
---|
475 | AST_RegExp.prototype._size = function () {
|
---|
476 | return this.value.toString().length;
|
---|
477 | };
|
---|
478 |
|
---|
479 | AST_Null.prototype._size = () => 4;
|
---|
480 |
|
---|
481 | AST_NaN.prototype._size = () => 3;
|
---|
482 |
|
---|
483 | AST_Undefined.prototype._size = () => 6; // "void 0"
|
---|
484 |
|
---|
485 | AST_Hole.prototype._size = () => 0; // comma is taken into account by list_overhead()
|
---|
486 |
|
---|
487 | AST_Infinity.prototype._size = () => 8;
|
---|
488 |
|
---|
489 | AST_True.prototype._size = () => 4;
|
---|
490 |
|
---|
491 | AST_False.prototype._size = () => 5;
|
---|
492 |
|
---|
493 | AST_Await.prototype._size = () => 6;
|
---|
494 |
|
---|
495 | AST_Yield.prototype._size = () => 6;
|
---|