source: frontend/node_modules/terser/lib/size.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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