source: trip-planner-front/node_modules/terser/lib/size.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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