terser 3.14.0 → 3.17.0

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.

Potentially problematic release.


This version of terser might be problematic. Click here for more details.

package/lib/propmangle.js DELETED
@@ -1,267 +0,0 @@
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
- "use strict";
45
-
46
- function find_builtins(reserved) {
47
- reserved.push.apply(reserved, domprops);
48
-
49
- // Compatibility fix for some standard defined globals not defined on every js environment
50
- var new_globals = ["Symbol", "Map", "Promise", "Proxy", "Reflect", "Set", "WeakMap", "WeakSet"];
51
- var objects = {};
52
- var global_ref = typeof global === "object" ? global : self;
53
-
54
- new_globals.forEach(function (new_global) {
55
- objects[new_global] = global_ref[new_global] || new Function();
56
- });
57
-
58
- // NaN will be included due to Number.NaN
59
- [
60
- "null",
61
- "true",
62
- "false",
63
- "Infinity",
64
- "-Infinity",
65
- "undefined",
66
- ].forEach(add);
67
- [ Object, Array, Function, Number,
68
- String, Boolean, Error, Math,
69
- Date, RegExp, objects.Symbol, ArrayBuffer,
70
- DataView, decodeURI, decodeURIComponent,
71
- encodeURI, encodeURIComponent, eval, EvalError,
72
- Float32Array, Float64Array, Int8Array, Int16Array,
73
- Int32Array, isFinite, isNaN, JSON, objects.Map, parseFloat,
74
- parseInt, objects.Promise, objects.Proxy, RangeError, ReferenceError,
75
- objects.Reflect, objects.Set, SyntaxError, TypeError, Uint8Array,
76
- Uint8ClampedArray, Uint16Array, Uint32Array, URIError,
77
- objects.WeakMap, objects.WeakSet
78
- ].forEach(function(ctor) {
79
- Object.getOwnPropertyNames(ctor).map(add);
80
- if (ctor.prototype) {
81
- Object.getOwnPropertyNames(ctor.prototype).map(add);
82
- }
83
- });
84
- function add(name) {
85
- push_uniq(reserved, name);
86
- }
87
- }
88
-
89
- function reserve_quoted_keys(ast, reserved) {
90
- function add(name) {
91
- push_uniq(reserved, name);
92
- }
93
-
94
- ast.walk(new TreeWalker(function(node) {
95
- if (node instanceof AST_ObjectKeyVal && node.quote) {
96
- add(node.key);
97
- } else if (node instanceof AST_ObjectProperty && node.quote) {
98
- add(node.key.name);
99
- } else if (node instanceof AST_Sub) {
100
- addStrings(node.property, add);
101
- }
102
- }));
103
- }
104
-
105
- function addStrings(node, add) {
106
- node.walk(new TreeWalker(function(node) {
107
- if (node instanceof AST_Sequence) {
108
- addStrings(node.tail_node(), add);
109
- } else if (node instanceof AST_String) {
110
- add(node.value);
111
- } else if (node instanceof AST_Conditional) {
112
- addStrings(node.consequent, add);
113
- addStrings(node.alternative, add);
114
- }
115
- return true;
116
- }));
117
- }
118
-
119
- function mangle_properties(ast, options) {
120
- options = defaults(options, {
121
- builtins: false,
122
- cache: null,
123
- debug: false,
124
- keep_quoted: false,
125
- only_cache: false,
126
- regex: null,
127
- reserved: null,
128
- }, true);
129
-
130
- var reserved = options.reserved;
131
- if (!Array.isArray(reserved)) reserved = [reserved];
132
- if (!options.builtins) find_builtins(reserved);
133
-
134
- var cname = -1;
135
- var cache;
136
- if (options.cache) {
137
- cache = options.cache.props;
138
- cache.each(function(mangled_name) {
139
- push_uniq(reserved, mangled_name);
140
- });
141
- } else {
142
- cache = new Dictionary();
143
- }
144
-
145
- var regex = options.regex;
146
-
147
- // note debug is either false (disabled), or a string of the debug suffix to use (enabled).
148
- // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
149
- // the same as passing an empty string.
150
- var debug = options.debug !== false;
151
- var debug_name_suffix;
152
- if (debug) {
153
- debug_name_suffix = (options.debug === true ? "" : options.debug);
154
- }
155
-
156
- var names_to_mangle = [];
157
- var unmangleable = [];
158
-
159
- // step 1: find candidates to mangle
160
- ast.walk(new TreeWalker(function(node) {
161
- if (node instanceof AST_ObjectKeyVal) {
162
- if (typeof node.key == "string") {
163
- add(node.key);
164
- }
165
- } else if (node instanceof AST_ObjectProperty) {
166
- // setter or getter, since KeyVal is handled above
167
- add(node.key.name);
168
- } else if (node instanceof AST_Dot) {
169
- add(node.property);
170
- } else if (node instanceof AST_Sub) {
171
- addStrings(node.property, add);
172
- } else if (node instanceof AST_Call
173
- && node.expression.print_to_string() == "Object.defineProperty") {
174
- addStrings(node.args[1], add);
175
- }
176
- }));
177
-
178
- // step 2: transform the tree, renaming properties
179
- return ast.transform(new TreeTransformer(function(node) {
180
- if (node instanceof AST_ObjectKeyVal) {
181
- if (typeof node.key == "string") {
182
- node.key = mangle(node.key);
183
- }
184
- } else if (node instanceof AST_ObjectProperty) {
185
- // setter or getter
186
- node.key.name = mangle(node.key.name);
187
- } else if (node instanceof AST_Dot) {
188
- node.property = mangle(node.property);
189
- } else if (!options.keep_quoted && node instanceof AST_Sub) {
190
- node.property = mangleStrings(node.property);
191
- } else if (node instanceof AST_Call
192
- && node.expression.print_to_string() == "Object.defineProperty") {
193
- node.args[1] = mangleStrings(node.args[1]);
194
- }
195
- }));
196
-
197
- // only function declarations after this line
198
-
199
- function can_mangle(name) {
200
- if (unmangleable.indexOf(name) >= 0) return false;
201
- if (reserved.indexOf(name) >= 0) return false;
202
- if (options.only_cache) {
203
- return cache.has(name);
204
- }
205
- if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
206
- return true;
207
- }
208
-
209
- function should_mangle(name) {
210
- if (regex && !regex.test(name)) return false;
211
- if (reserved.indexOf(name) >= 0) return false;
212
- return cache.has(name)
213
- || names_to_mangle.indexOf(name) >= 0;
214
- }
215
-
216
- function add(name) {
217
- if (can_mangle(name))
218
- push_uniq(names_to_mangle, name);
219
-
220
- if (!should_mangle(name)) {
221
- push_uniq(unmangleable, name);
222
- }
223
- }
224
-
225
- function mangle(name) {
226
- if (!should_mangle(name)) {
227
- return name;
228
- }
229
-
230
- var mangled = cache.get(name);
231
- if (!mangled) {
232
- if (debug) {
233
- // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_.
234
- var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_";
235
-
236
- if (can_mangle(debug_mangled)) {
237
- mangled = debug_mangled;
238
- }
239
- }
240
-
241
- // either debug mode is off, or it is on and we could not use the mangled name
242
- if (!mangled) {
243
- do {
244
- mangled = base54(++cname);
245
- } while (!can_mangle(mangled));
246
- }
247
-
248
- cache.set(name, mangled);
249
- }
250
- return mangled;
251
- }
252
-
253
- function mangleStrings(node) {
254
- return node.transform(new TreeTransformer(function(node) {
255
- if (node instanceof AST_Sequence) {
256
- var last = node.expressions.length - 1;
257
- node.expressions[last] = mangleStrings(node.expressions[last]);
258
- } else if (node instanceof AST_String) {
259
- node.value = mangle(node.value);
260
- } else if (node instanceof AST_Conditional) {
261
- node.consequent = mangleStrings(node.consequent);
262
- node.alternative = mangleStrings(node.alternative);
263
- }
264
- return node;
265
- }));
266
- }
267
- }