terser 5.6.0 → 5.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ast.js CHANGED
@@ -41,8 +41,6 @@
41
41
 
42
42
  ***********************************************************************/
43
43
 
44
- "use strict";
45
-
46
44
  import {
47
45
  HOP,
48
46
  MAP,
@@ -214,8 +212,6 @@ function walk_body(node, visitor) {
214
212
  function clone_block_scope(deep) {
215
213
  var clone = this._clone(deep);
216
214
  if (this.block_scope) {
217
- // TODO this is sometimes undefined during compression.
218
- // But it should always have a value!
219
215
  clone.block_scope = this.block_scope.clone();
220
216
  }
221
217
  return clone;
@@ -399,7 +395,6 @@ var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent
399
395
  $documentation: "Base class for all statements introducing a lexical scope",
400
396
  $propdoc: {
401
397
  variables: "[Map/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
402
- functions: "[Map/S] like `variables`, but only lists function declarations",
403
398
  uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
404
399
  uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
405
400
  parent_scope: "[AST_Scope?/S] link to the parent scope",
@@ -422,7 +417,6 @@ var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent
422
417
  });
423
418
  } else {
424
419
  if (this.variables) node.variables = new Map(this.variables);
425
- if (this.functions) node.functions = new Map(this.functions);
426
420
  if (this.enclosed) node.enclosed = this.enclosed.slice();
427
421
  if (this._block_scope) node._block_scope = this._block_scope;
428
422
  }
@@ -522,6 +516,21 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments is_generator as
522
516
 
523
517
  if (this.name) push(this.name);
524
518
  },
519
+ is_braceless() {
520
+ return this.body[0] instanceof AST_Return && this.body[0].value;
521
+ },
522
+ // Default args and expansion don't count, so .argnames.length doesn't cut it
523
+ length_property() {
524
+ let length = 0;
525
+
526
+ for (const arg of this.argnames) {
527
+ if (arg instanceof AST_SymbolFunarg || arg instanceof AST_Destructuring) {
528
+ length++;
529
+ }
530
+ }
531
+
532
+ return length;
533
+ }
525
534
  }, AST_Scope);
526
535
 
527
536
  var AST_Accessor = DEFNODE("Accessor", null, {
package/lib/cli.js CHANGED
@@ -264,7 +264,6 @@ export async function run_cli({ program, packageJson, fs, path }) {
264
264
  case "enclosed":
265
265
  return value.length ? value.map(symdef) : undefined;
266
266
  case "variables":
267
- case "functions":
268
267
  case "globals":
269
268
  return value.size ? collect_from_map(value, symdef) : undefined;
270
269
  }
@@ -277,7 +276,6 @@ export async function run_cli({ program, packageJson, fs, path }) {
277
276
  };
278
277
  if (value.block_scope) {
279
278
  result.variables = value.block_scope.variables;
280
- result.functions = value.block_scope.functions;
281
279
  result.enclosed = value.block_scope.enclosed;
282
280
  }
283
281
  value.CTOR.PROPS.forEach(function(prop) {
@@ -0,0 +1,296 @@
1
+ /***********************************************************************
2
+
3
+ A JavaScript tokenizer / parser / beautifier / compressor.
4
+ https://github.com/mishoo/UglifyJS2
5
+
6
+ -------------------------------- (C) ---------------------------------
7
+
8
+ Author: Mihai Bazon
9
+ <mihai.bazon@gmail.com>
10
+ http://mihai.bazon.net/blog
11
+
12
+ Distributed under the BSD license:
13
+
14
+ Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
15
+
16
+ Redistribution and use in source and binary forms, with or without
17
+ modification, are permitted provided that the following conditions
18
+ are met:
19
+
20
+ * Redistributions of source code must retain the above
21
+ copyright notice, this list of conditions and the following
22
+ disclaimer.
23
+
24
+ * Redistributions in binary form must reproduce the above
25
+ copyright notice, this list of conditions and the following
26
+ disclaimer in the documentation and/or other materials
27
+ provided with the distribution.
28
+
29
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
+ SUCH DAMAGE.
41
+
42
+ ***********************************************************************/
43
+
44
+ import {
45
+ AST_Array,
46
+ AST_Arrow,
47
+ AST_BlockStatement,
48
+ AST_Call,
49
+ AST_Class,
50
+ AST_Const,
51
+ AST_Constant,
52
+ AST_DefClass,
53
+ AST_Defun,
54
+ AST_EmptyStatement,
55
+ AST_Export,
56
+ AST_False,
57
+ AST_Function,
58
+ AST_Import,
59
+ AST_Infinity,
60
+ AST_LabeledStatement,
61
+ AST_Lambda,
62
+ AST_Let,
63
+ AST_LoopControl,
64
+ AST_NaN,
65
+ AST_Node,
66
+ AST_Null,
67
+ AST_Number,
68
+ AST_Object,
69
+ AST_ObjectKeyVal,
70
+ AST_PropAccess,
71
+ AST_RegExp,
72
+ AST_Scope,
73
+ AST_Sequence,
74
+ AST_SimpleStatement,
75
+ AST_Statement,
76
+ AST_String,
77
+ AST_SymbolRef,
78
+ AST_True,
79
+ AST_UnaryPrefix,
80
+ AST_Undefined,
81
+
82
+ TreeWalker,
83
+ } from "../ast.js";
84
+ import { make_node, regexp_source_fix, string_template, makePredicate } from "../utils/index.js";
85
+ import { first_in_statement } from "../utils/first_in_statement.js";
86
+
87
+ export function merge_sequence(array, node) {
88
+ if (node instanceof AST_Sequence) {
89
+ array.push(...node.expressions);
90
+ } else {
91
+ array.push(node);
92
+ }
93
+ return array;
94
+ }
95
+
96
+ export function make_sequence(orig, expressions) {
97
+ if (expressions.length == 1) return expressions[0];
98
+ if (expressions.length == 0) throw new Error("trying to create a sequence with length zero!");
99
+ return make_node(AST_Sequence, orig, {
100
+ expressions: expressions.reduce(merge_sequence, [])
101
+ });
102
+ }
103
+
104
+ export function make_node_from_constant(val, orig) {
105
+ switch (typeof val) {
106
+ case "string":
107
+ return make_node(AST_String, orig, {
108
+ value: val
109
+ });
110
+ case "number":
111
+ if (isNaN(val)) return make_node(AST_NaN, orig);
112
+ if (isFinite(val)) {
113
+ return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, {
114
+ operator: "-",
115
+ expression: make_node(AST_Number, orig, { value: -val })
116
+ }) : make_node(AST_Number, orig, { value: val });
117
+ }
118
+ return val < 0 ? make_node(AST_UnaryPrefix, orig, {
119
+ operator: "-",
120
+ expression: make_node(AST_Infinity, orig)
121
+ }) : make_node(AST_Infinity, orig);
122
+ case "boolean":
123
+ return make_node(val ? AST_True : AST_False, orig);
124
+ case "undefined":
125
+ return make_node(AST_Undefined, orig);
126
+ default:
127
+ if (val === null) {
128
+ return make_node(AST_Null, orig, { value: null });
129
+ }
130
+ if (val instanceof RegExp) {
131
+ return make_node(AST_RegExp, orig, {
132
+ value: {
133
+ source: regexp_source_fix(val.source),
134
+ flags: val.flags
135
+ }
136
+ });
137
+ }
138
+ throw new Error(string_template("Can't handle constant of type: {type}", {
139
+ type: typeof val
140
+ }));
141
+ }
142
+ }
143
+
144
+ export function best_of_expression(ast1, ast2) {
145
+ return ast1.size() > ast2.size() ? ast2 : ast1;
146
+ }
147
+
148
+ export function best_of_statement(ast1, ast2) {
149
+ return best_of_expression(
150
+ make_node(AST_SimpleStatement, ast1, {
151
+ body: ast1
152
+ }),
153
+ make_node(AST_SimpleStatement, ast2, {
154
+ body: ast2
155
+ })
156
+ ).body;
157
+ }
158
+
159
+ /** Find which node is smaller, and return that */
160
+ export function best_of(compressor, ast1, ast2) {
161
+ if (first_in_statement(compressor)) {
162
+ return best_of_statement(ast1, ast2);
163
+ } else {
164
+ return best_of_expression(ast1, ast2);
165
+ }
166
+ }
167
+
168
+ /** Simplify an object property's key, if possible */
169
+ export function get_simple_key(key) {
170
+ if (key instanceof AST_Constant) {
171
+ return key.getValue();
172
+ }
173
+ if (key instanceof AST_UnaryPrefix
174
+ && key.operator == "void"
175
+ && key.expression instanceof AST_Constant) {
176
+ return;
177
+ }
178
+ return key;
179
+ }
180
+
181
+ export function read_property(obj, key) {
182
+ key = get_simple_key(key);
183
+ if (key instanceof AST_Node) return;
184
+
185
+ var value;
186
+ if (obj instanceof AST_Array) {
187
+ var elements = obj.elements;
188
+ if (key == "length") return make_node_from_constant(elements.length, obj);
189
+ if (typeof key == "number" && key in elements) value = elements[key];
190
+ } else if (obj instanceof AST_Object) {
191
+ key = "" + key;
192
+ var props = obj.properties;
193
+ for (var i = props.length; --i >= 0;) {
194
+ var prop = props[i];
195
+ if (!(prop instanceof AST_ObjectKeyVal)) return;
196
+ if (!value && props[i].key === key) value = props[i].value;
197
+ }
198
+ }
199
+
200
+ return value instanceof AST_SymbolRef && value.fixed_value() || value;
201
+ }
202
+
203
+ export function has_break_or_continue(loop, parent) {
204
+ var found = false;
205
+ var tw = new TreeWalker(function(node) {
206
+ if (found || node instanceof AST_Scope) return true;
207
+ if (node instanceof AST_LoopControl && tw.loopcontrol_target(node) === loop) {
208
+ return found = true;
209
+ }
210
+ });
211
+ if (parent instanceof AST_LabeledStatement) tw.push(parent);
212
+ tw.push(loop);
213
+ loop.body.walk(tw);
214
+ return found;
215
+ }
216
+
217
+ // we shouldn't compress (1,func)(something) to
218
+ // func(something) because that changes the meaning of
219
+ // the func (becomes lexical instead of global).
220
+ export function maintain_this_binding(parent, orig, val) {
221
+ if (
222
+ parent instanceof AST_UnaryPrefix && parent.operator == "delete"
223
+ || parent instanceof AST_Call && parent.expression === orig
224
+ && (
225
+ val instanceof AST_PropAccess
226
+ || val instanceof AST_SymbolRef && val.name == "eval"
227
+ )
228
+ ) {
229
+ const zero = make_node(AST_Number, orig, { value: 0 });
230
+ return make_sequence(orig, [ zero, val ]);
231
+ } else {
232
+ return val;
233
+ }
234
+ }
235
+
236
+ export function is_func_expr(node) {
237
+ return node instanceof AST_Arrow || node instanceof AST_Function;
238
+ }
239
+
240
+ export function is_iife_call(node) {
241
+ // Used to determine whether the node can benefit from negation.
242
+ // Not the case with arrow functions (you need an extra set of parens).
243
+ if (node.TYPE != "Call") return false;
244
+ return node.expression instanceof AST_Function || is_iife_call(node.expression);
245
+ }
246
+
247
+ export const identifier_atom = makePredicate("Infinity NaN undefined");
248
+ export function is_identifier_atom(node) {
249
+ return node instanceof AST_Infinity
250
+ || node instanceof AST_NaN
251
+ || node instanceof AST_Undefined;
252
+ }
253
+
254
+ /** Check if this is a SymbolRef node which has one def of a certain AST type */
255
+ export function is_ref_of(ref, type) {
256
+ if (!(ref instanceof AST_SymbolRef)) return false;
257
+ var orig = ref.definition().orig;
258
+ for (var i = orig.length; --i >= 0;) {
259
+ if (orig[i] instanceof type) return true;
260
+ }
261
+ }
262
+
263
+ // Can we turn { block contents... } into just the block contents ?
264
+ // Not if one of these is inside.
265
+ export function can_be_evicted_from_block(node) {
266
+ return !(
267
+ node instanceof AST_DefClass ||
268
+ node instanceof AST_Defun ||
269
+ node instanceof AST_Let ||
270
+ node instanceof AST_Const ||
271
+ node instanceof AST_Export ||
272
+ node instanceof AST_Import
273
+ );
274
+ }
275
+
276
+ export function as_statement_array(thing) {
277
+ if (thing === null) return [];
278
+ if (thing instanceof AST_BlockStatement) return thing.body;
279
+ if (thing instanceof AST_EmptyStatement) return [];
280
+ if (thing instanceof AST_Statement) return [ thing ];
281
+ throw new Error("Can't convert thing to statement array");
282
+ }
283
+
284
+ /** Check if a ref refers to the name of a function/class it's defined within */
285
+ export function is_recursive_ref(compressor, def) {
286
+ var node;
287
+ for (var i = 0; node = compressor.parent(i); i++) {
288
+ if (node instanceof AST_Lambda || node instanceof AST_Class) {
289
+ var name = node.name;
290
+ if (name && name.definition() === def) {
291
+ return true;
292
+ }
293
+ }
294
+ }
295
+ return false;
296
+ }
@@ -0,0 +1,63 @@
1
+ /***********************************************************************
2
+
3
+ A JavaScript tokenizer / parser / beautifier / compressor.
4
+ https://github.com/mishoo/UglifyJS2
5
+
6
+ -------------------------------- (C) ---------------------------------
7
+
8
+ Author: Mihai Bazon
9
+ <mihai.bazon@gmail.com>
10
+ http://mihai.bazon.net/blog
11
+
12
+ Distributed under the BSD license:
13
+
14
+ Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
15
+
16
+ Redistribution and use in source and binary forms, with or without
17
+ modification, are permitted provided that the following conditions
18
+ are met:
19
+
20
+ * Redistributions of source code must retain the above
21
+ copyright notice, this list of conditions and the following
22
+ disclaimer.
23
+
24
+ * Redistributions in binary form must reproduce the above
25
+ copyright notice, this list of conditions and the following
26
+ disclaimer in the documentation and/or other materials
27
+ provided with the distribution.
28
+
29
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
+ SUCH DAMAGE.
41
+
42
+ ***********************************************************************/
43
+
44
+ // bitfield flags to be stored in node.flags.
45
+ // These are set and unset during compression, and store information in the node without requiring multiple fields.
46
+ export const UNUSED = 0b00000001;
47
+ export const TRUTHY = 0b00000010;
48
+ export const FALSY = 0b00000100;
49
+ export const UNDEFINED = 0b00001000;
50
+ export const INLINED = 0b00010000;
51
+
52
+ // Nodes to which values are ever written. Used when keep_assign is part of the unused option string.
53
+ export const WRITE_ONLY = 0b00100000;
54
+
55
+ // information specific to a single compression pass
56
+ export const SQUEEZED = 0b0000000100000000;
57
+ export const OPTIMIZED = 0b0000001000000000;
58
+ export const TOP = 0b0000010000000000;
59
+ export const CLEAR_BETWEEN_PASSES = SQUEEZED | OPTIMIZED | TOP;
60
+
61
+ export const has_flag = (node, flag) => node.flags & flag;
62
+ export const set_flag = (node, flag) => { node.flags |= flag; };
63
+ export const clear_flag = (node, flag) => { node.flags &= ~flag; };