terser 5.46.2 → 5.47.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.
- package/CHANGELOG.md +5 -0
- package/README.md +4 -0
- package/dist/bundle.min.js +416 -54
- package/lib/compress/evaluate.js +3 -4
- package/lib/compress/index.js +10 -2
- package/lib/compress/inference.js +5 -9
- package/lib/compress/native-objects.js +372 -40
- package/lib/utils/index.js +1 -1
- package/package.json +1 -1
- package/tools/domprops.js +29 -0
- package/tools/terser.d.ts +3 -1
- package/lib/compress/import-export.js +0 -121
package/lib/compress/evaluate.js
CHANGED
|
@@ -75,7 +75,6 @@ import {
|
|
|
75
75
|
AST_With,
|
|
76
76
|
} from "../ast.js";
|
|
77
77
|
import { is_undeclared_ref} from "./inference.js";
|
|
78
|
-
import { is_pure_native_value, is_pure_native_fn, is_pure_native_method } from "./native-objects.js";
|
|
79
78
|
|
|
80
79
|
// methods to evaluate a constant expression
|
|
81
80
|
|
|
@@ -439,7 +438,7 @@ def_eval(AST_PropAccess, function (compressor, depth, ast_chain) {
|
|
|
439
438
|
if (first_arg == null || first_arg.thedef && first_arg.thedef.undeclared) {
|
|
440
439
|
return this.clone();
|
|
441
440
|
}
|
|
442
|
-
if (!
|
|
441
|
+
if (!compressor.is_pure_native_static_property(exp.name, key))
|
|
443
442
|
return this;
|
|
444
443
|
obj = global_objs[exp.name];
|
|
445
444
|
} else {
|
|
@@ -495,14 +494,14 @@ def_eval(AST_Call, function (compressor, depth, ast_chain) {
|
|
|
495
494
|
if ((first_arg == null || first_arg.thedef && first_arg.thedef.undeclared)) {
|
|
496
495
|
return this.clone();
|
|
497
496
|
}
|
|
498
|
-
if (!
|
|
497
|
+
if (!compressor.is_pure_native_static_fn(e.name, key)) return this;
|
|
499
498
|
val = global_objs[e.name];
|
|
500
499
|
} else {
|
|
501
500
|
val = e._eval(compressor, depth + 1, /* don't pass ast_chain (exponential work) */);
|
|
502
501
|
|
|
503
502
|
if (val === e || !val)
|
|
504
503
|
return this;
|
|
505
|
-
if (!is_pure_native_method(val.constructor.name, key))
|
|
504
|
+
if (!compressor.is_pure_native_method(val.constructor.name, key))
|
|
506
505
|
return this;
|
|
507
506
|
}
|
|
508
507
|
var args = [];
|
package/lib/compress/index.js
CHANGED
|
@@ -215,6 +215,7 @@ import {
|
|
|
215
215
|
import { tighten_body, extract_from_unreachable_code } from "./tighten-body.js";
|
|
216
216
|
import { inline_into_symbolref, inline_into_call } from "./inline.js";
|
|
217
217
|
import "./global-defs.js";
|
|
218
|
+
import { is_pure_native_fn, is_pure_native_method, is_pure_native_static_fn, is_pure_native_static_property, pure_access_globals } from "./native-objects.js";
|
|
218
219
|
|
|
219
220
|
class Compressor extends TreeWalker {
|
|
220
221
|
constructor(options, { false_by_default = false, mangle_options = false }) {
|
|
@@ -235,6 +236,8 @@ class Compressor extends TreeWalker {
|
|
|
235
236
|
drop_console : false,
|
|
236
237
|
drop_debugger : !false_by_default,
|
|
237
238
|
ecma : 5,
|
|
239
|
+
builtins_ecma : 5,
|
|
240
|
+
builtins_pure : false,
|
|
238
241
|
evaluate : !false_by_default,
|
|
239
242
|
expression : false,
|
|
240
243
|
global_defs : false,
|
|
@@ -330,6 +333,12 @@ class Compressor extends TreeWalker {
|
|
|
330
333
|
this._mangle_options = mangle_options
|
|
331
334
|
? format_mangler_options(mangle_options)
|
|
332
335
|
: mangle_options;
|
|
336
|
+
|
|
337
|
+
this.pure_access_globals = pure_access_globals(this);
|
|
338
|
+
this.is_pure_native_fn = is_pure_native_fn(this);
|
|
339
|
+
this.is_pure_native_method = is_pure_native_method(this);
|
|
340
|
+
this.is_pure_native_static_fn = is_pure_native_static_fn(this);
|
|
341
|
+
this.is_pure_native_static_property = is_pure_native_static_property(this);
|
|
333
342
|
}
|
|
334
343
|
|
|
335
344
|
mangle_options() {
|
|
@@ -664,10 +673,9 @@ function find_variable(compressor, name) {
|
|
|
664
673
|
return scope.find_variable(name);
|
|
665
674
|
}
|
|
666
675
|
|
|
667
|
-
var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
|
|
668
676
|
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
|
|
669
677
|
return !this.definition().undeclared
|
|
670
|
-
|| compressor.option("unsafe") &&
|
|
678
|
+
|| (compressor.option("unsafe") || compressor.option("builtins_pure")) && compressor.pure_access_globals(this.name);
|
|
671
679
|
});
|
|
672
680
|
|
|
673
681
|
/* -----[ optimizers ]----- */
|
|
@@ -128,7 +128,7 @@ import {
|
|
|
128
128
|
import { make_sequence, best_of_expression, read_property, requires_sequence_to_maintain_binding } from "./common.js";
|
|
129
129
|
|
|
130
130
|
import { INLINED, UNDEFINED, has_flag } from "./compressor-flags.js";
|
|
131
|
-
import {
|
|
131
|
+
import { is_pure_builtin_call, pure_prop_access_globals } from "./native-objects.js";
|
|
132
132
|
|
|
133
133
|
// Functions and methods to infer certain facts about expressions
|
|
134
134
|
// It's not always possible to be 100% sure about something just by static analysis,
|
|
@@ -969,13 +969,9 @@ AST_Call.DEFMETHOD("is_callee_pure", function(compressor) {
|
|
|
969
969
|
return false;
|
|
970
970
|
}
|
|
971
971
|
if (is_undeclared_ref(expr) && global_pure_fns.has(expr.name)) return true;
|
|
972
|
-
if (
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
&& is_pure_native_fn(expr.expression.name, expr.property)
|
|
976
|
-
) {
|
|
977
|
-
return true;
|
|
978
|
-
}
|
|
972
|
+
if (is_pure_builtin_call(compressor, this)) return true;
|
|
973
|
+
} else if (compressor.option("builtins_pure")) {
|
|
974
|
+
if (is_pure_builtin_call(compressor, this)) return true;
|
|
979
975
|
}
|
|
980
976
|
if ((this instanceof AST_New) && compressor.option("pure_new")) {
|
|
981
977
|
return true;
|
|
@@ -1006,7 +1002,7 @@ AST_Dot.DEFMETHOD("is_call_pure", function(compressor) {
|
|
|
1006
1002
|
} else if (!this.may_throw_on_access(compressor)) {
|
|
1007
1003
|
native_obj = "Object";
|
|
1008
1004
|
}
|
|
1009
|
-
return native_obj != null && is_pure_native_method(native_obj, this.property);
|
|
1005
|
+
return native_obj != null && compressor.is_pure_native_method(native_obj, this.property);
|
|
1010
1006
|
});
|
|
1011
1007
|
|
|
1012
1008
|
// tell me if a statement aborts
|
|
@@ -41,16 +41,22 @@
|
|
|
41
41
|
|
|
42
42
|
***********************************************************************/
|
|
43
43
|
|
|
44
|
+
import { AST_Array, AST_Dot, AST_New, AST_Number, AST_SymbolRef } from "../ast.js";
|
|
44
45
|
import { makePredicate } from "../utils/index.js";
|
|
46
|
+
import { is_undeclared_ref } from "./inference.js";
|
|
45
47
|
|
|
46
48
|
// Lists of native methods, useful for `unsafe` option which assumes they exist.
|
|
47
49
|
// Note: Lots of methods and functions are missing here, in case they aren't pure
|
|
48
50
|
// or not available in all JS environments.
|
|
49
51
|
|
|
50
|
-
|
|
52
|
+
const make_nested_lookup = (feature_callback) => (compressor) => {
|
|
53
|
+
const obj = feature_callback(feature_variables(compressor));
|
|
54
|
+
|
|
51
55
|
const out = new Map();
|
|
52
56
|
for (var key of Object.keys(obj)) {
|
|
53
|
-
|
|
57
|
+
if (obj[key]) {
|
|
58
|
+
out.set(key, makePredicate(remove_false(obj[key])));
|
|
59
|
+
}
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
const does_have = (global_name, fname) => {
|
|
@@ -58,8 +64,74 @@ function make_nested_lookup(obj) {
|
|
|
58
64
|
return inner_map != null && inner_map.has(fname);
|
|
59
65
|
};
|
|
60
66
|
return does_have;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const make_lookup = (feature_callback) => (compressor) => {
|
|
70
|
+
const obj = feature_callback(feature_variables(compressor));
|
|
71
|
+
|
|
72
|
+
const predicate = makePredicate(remove_false(obj));
|
|
73
|
+
const does_have = (global_name) => {
|
|
74
|
+
return predicate.has(global_name);
|
|
75
|
+
};
|
|
76
|
+
return does_have;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function remove_false(arr) {
|
|
80
|
+
for (let i = 0; i < arr.length; i++) {
|
|
81
|
+
if (arr[i] === false) {
|
|
82
|
+
arr.splice(i, 1);
|
|
83
|
+
i--;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return arr;
|
|
61
87
|
}
|
|
62
88
|
|
|
89
|
+
/** Generate the object with arguments seen below */
|
|
90
|
+
function feature_variables(compressor) {
|
|
91
|
+
return {
|
|
92
|
+
sloppy: compressor.option("unsafe"),
|
|
93
|
+
es: compressor.option("builtins_ecma"),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// eslint-disable-next-line no-unused-vars
|
|
98
|
+
export const pure_access_globals = make_lookup(({ sloppy, es }) => [
|
|
99
|
+
"Array",
|
|
100
|
+
"Boolean",
|
|
101
|
+
"clearInterval",
|
|
102
|
+
"clearTimeout",
|
|
103
|
+
"console",
|
|
104
|
+
"Date",
|
|
105
|
+
"decodeURI",
|
|
106
|
+
"decodeURIComponent",
|
|
107
|
+
"encodeURI",
|
|
108
|
+
"encodeURIComponent",
|
|
109
|
+
"Error",
|
|
110
|
+
"escape",
|
|
111
|
+
"eval",
|
|
112
|
+
"EvalError",
|
|
113
|
+
"Function",
|
|
114
|
+
es >= 2020 && "globalThis",
|
|
115
|
+
"isFinite",
|
|
116
|
+
"isNaN",
|
|
117
|
+
"JSON",
|
|
118
|
+
"Math",
|
|
119
|
+
"Number",
|
|
120
|
+
"parseFloat",
|
|
121
|
+
"parseInt",
|
|
122
|
+
"RangeError",
|
|
123
|
+
"ReferenceError",
|
|
124
|
+
"RegExp",
|
|
125
|
+
"Object",
|
|
126
|
+
"setInterval",
|
|
127
|
+
"setTimeout",
|
|
128
|
+
"String",
|
|
129
|
+
"SyntaxError",
|
|
130
|
+
"TypeError",
|
|
131
|
+
"unescape",
|
|
132
|
+
"URIError",
|
|
133
|
+
]);
|
|
134
|
+
|
|
63
135
|
// Objects which are safe to access without throwing or causing a side effect.
|
|
64
136
|
// Usually we'd check the `unsafe` option first but these are way too common for that
|
|
65
137
|
export const pure_prop_access_globals = new Set([
|
|
@@ -71,17 +143,90 @@ export const pure_prop_access_globals = new Set([
|
|
|
71
143
|
"Promise",
|
|
72
144
|
]);
|
|
73
145
|
|
|
146
|
+
// eslint-disable-next-line no-unused-vars
|
|
147
|
+
export const is_pure_native_fn = make_lookup(({ sloppy, es }) => [
|
|
148
|
+
sloppy && es >= 2021 && "AggregateError",
|
|
149
|
+
"Array",
|
|
150
|
+
"ArrayBuffer",
|
|
151
|
+
es >= 2020 && "BigInt",
|
|
152
|
+
es >= 2020 && "BigInt64Array",
|
|
153
|
+
es >= 2020 && "BigUint64Array",
|
|
154
|
+
"Boolean",
|
|
155
|
+
"Date",
|
|
156
|
+
sloppy && "decodeURI",
|
|
157
|
+
sloppy && "decodeURIComponent",
|
|
158
|
+
sloppy && "encodeURI",
|
|
159
|
+
sloppy && "encodeURIComponent",
|
|
160
|
+
"Error",
|
|
161
|
+
"escape",
|
|
162
|
+
"EvalError",
|
|
163
|
+
es >= 2021 && "FinalizationRegistry",
|
|
164
|
+
es >= 2026 && "Float16Array",
|
|
165
|
+
"Float32Array",
|
|
166
|
+
"Float64Array",
|
|
167
|
+
"Int16Array",
|
|
168
|
+
"Int32Array",
|
|
169
|
+
"Int8Array",
|
|
170
|
+
"isFinite",
|
|
171
|
+
"isNaN",
|
|
172
|
+
es >= 2026 && "Iterator",
|
|
173
|
+
es >= 2015 && "Map",
|
|
174
|
+
"Number",
|
|
175
|
+
"parseFloat",
|
|
176
|
+
"parseInt",
|
|
177
|
+
es >= 2015 && "Promise",
|
|
178
|
+
es >= 2015 && "Proxy",
|
|
179
|
+
"RangeError",
|
|
180
|
+
"ReferenceError",
|
|
181
|
+
sloppy && "RegExp",
|
|
182
|
+
es >= 2015 && "Set",
|
|
183
|
+
"String",
|
|
184
|
+
es >= 2015 && "Symbol",
|
|
185
|
+
"SyntaxError",
|
|
186
|
+
"TypeError",
|
|
187
|
+
"Uint16Array",
|
|
188
|
+
"Uint32Array",
|
|
189
|
+
"Uint8Array",
|
|
190
|
+
"Uint8ClampedArray",
|
|
191
|
+
sloppy && "unescape",
|
|
192
|
+
"URIError",
|
|
193
|
+
sloppy && es >= 2015 && "WeakMap",
|
|
194
|
+
sloppy && es >= 2021 && "WeakRef",
|
|
195
|
+
sloppy && es >= 2015 && "WeakSet",
|
|
196
|
+
]);
|
|
197
|
+
|
|
198
|
+
const arg1_is_iterable = new Set([
|
|
199
|
+
"Map",
|
|
200
|
+
"Set",
|
|
201
|
+
"WeakMap",
|
|
202
|
+
"WeakSet",
|
|
203
|
+
]);
|
|
204
|
+
const arg1_is_range_or_iterable = new Set([
|
|
205
|
+
"ArrayBuffer",
|
|
206
|
+
"Float32Array",
|
|
207
|
+
"Float64Array",
|
|
208
|
+
"Int16Array",
|
|
209
|
+
"Int32Array",
|
|
210
|
+
"Int8Array",
|
|
211
|
+
"Uint16Array",
|
|
212
|
+
"Uint32Array",
|
|
213
|
+
"Uint8Array",
|
|
214
|
+
"Uint8ClampedArray",
|
|
215
|
+
]);
|
|
216
|
+
const lone_arg_is_range = new Set(["Array"]);
|
|
217
|
+
|
|
74
218
|
const object_methods = [
|
|
75
219
|
"constructor",
|
|
76
220
|
"toString",
|
|
77
221
|
"valueOf",
|
|
78
222
|
];
|
|
79
223
|
|
|
80
|
-
|
|
224
|
+
// eslint-disable-next-line no-unused-vars
|
|
225
|
+
export const is_pure_native_method = make_nested_lookup(({ sloppy, es }) => ({
|
|
81
226
|
Array: [
|
|
82
|
-
"at",
|
|
83
|
-
"flat",
|
|
84
|
-
"includes",
|
|
227
|
+
es >= 2022 && "at",
|
|
228
|
+
es >= 2019 && "flat",
|
|
229
|
+
es >= 2016 && "includes",
|
|
85
230
|
"indexOf",
|
|
86
231
|
"join",
|
|
87
232
|
"lastIndexOf",
|
|
@@ -102,90 +247,160 @@ export const is_pure_native_method = make_nested_lookup({
|
|
|
102
247
|
...object_methods,
|
|
103
248
|
],
|
|
104
249
|
String: [
|
|
105
|
-
"at",
|
|
250
|
+
es >= 2022 && "at",
|
|
106
251
|
"charAt",
|
|
107
252
|
"charCodeAt",
|
|
108
|
-
"
|
|
253
|
+
es >= 2015 && "codePointAt",
|
|
109
254
|
"concat",
|
|
110
|
-
"endsWith",
|
|
111
|
-
"
|
|
112
|
-
"fromCodePoint",
|
|
113
|
-
"includes",
|
|
255
|
+
es >= 2025 && "endsWith",
|
|
256
|
+
es >= 2015 && "includes",
|
|
114
257
|
"indexOf",
|
|
115
258
|
"italics",
|
|
116
259
|
"lastIndexOf",
|
|
117
|
-
"localeCompare",
|
|
260
|
+
es >= 2020 && "localeCompare",
|
|
118
261
|
"match",
|
|
119
|
-
"matchAll",
|
|
120
|
-
"normalize",
|
|
121
|
-
"padStart",
|
|
122
|
-
"padEnd",
|
|
123
|
-
"repeat",
|
|
262
|
+
es >= 2020 && "matchAll",
|
|
263
|
+
es >= 2015 && "normalize",
|
|
264
|
+
es >= 2017 && "padStart",
|
|
265
|
+
es >= 2017 && "padEnd",
|
|
266
|
+
es >= 2015 && sloppy && "repeat",
|
|
124
267
|
"replace",
|
|
125
|
-
"replaceAll",
|
|
268
|
+
es >= 2021 && "replaceAll",
|
|
126
269
|
"search",
|
|
127
270
|
"slice",
|
|
128
271
|
"split",
|
|
129
|
-
"startsWith",
|
|
272
|
+
es >= 2015 && "startsWith",
|
|
130
273
|
"substr",
|
|
131
274
|
"substring",
|
|
132
|
-
"repeat",
|
|
275
|
+
es >= 2015 && "repeat",
|
|
133
276
|
"toLocaleLowerCase",
|
|
134
277
|
"toLocaleUpperCase",
|
|
135
278
|
"toLowerCase",
|
|
136
279
|
"toUpperCase",
|
|
137
280
|
"trim",
|
|
138
|
-
"trimEnd",
|
|
139
|
-
"trimStart",
|
|
281
|
+
es >= 2019 && "trimEnd",
|
|
282
|
+
es >= 2019 && "trimStart",
|
|
283
|
+
es >= 2019 && "trimLeft",
|
|
284
|
+
es >= 2019 && "trimRight",
|
|
140
285
|
...object_methods,
|
|
141
286
|
],
|
|
142
|
-
});
|
|
287
|
+
}));
|
|
143
288
|
|
|
144
|
-
|
|
289
|
+
// eslint-disable-next-line no-unused-vars
|
|
290
|
+
export const is_pure_native_static_fn = make_nested_lookup(({ sloppy, es }) => ({
|
|
145
291
|
Array: [
|
|
146
292
|
"isArray",
|
|
293
|
+
es >= 2015 && "of",
|
|
294
|
+
],
|
|
295
|
+
ArrayBuffer: [
|
|
296
|
+
"isView",
|
|
297
|
+
],
|
|
298
|
+
BigInt: es >= 2020 && [
|
|
299
|
+
sloppy && "asIntN",
|
|
300
|
+
sloppy && "asUintN",
|
|
301
|
+
],
|
|
302
|
+
BigInt64Array: sloppy && es >= 2020 && ["of"],
|
|
303
|
+
BigUint64Array: sloppy && es >= 2020 && ["of"],
|
|
304
|
+
Date: [
|
|
305
|
+
"now",
|
|
306
|
+
"parse",
|
|
307
|
+
"UTC",
|
|
147
308
|
],
|
|
309
|
+
Error: [
|
|
310
|
+
es >= 2026 && "isError",
|
|
311
|
+
],
|
|
312
|
+
Float16Array: sloppy && es >= 2026 && ["of"],
|
|
313
|
+
Float32Array: sloppy && ["of"],
|
|
314
|
+
Float64Array: sloppy && ["of"],
|
|
315
|
+
Int16Array: sloppy && ["of"],
|
|
316
|
+
Int32Array: sloppy && ["of"],
|
|
317
|
+
Int8Array: sloppy && ["of"],
|
|
148
318
|
Math: [
|
|
149
319
|
"abs",
|
|
150
320
|
"acos",
|
|
321
|
+
es >= 2015 && "acosh",
|
|
151
322
|
"asin",
|
|
323
|
+
es >= 2015 && "asinh",
|
|
152
324
|
"atan",
|
|
325
|
+
"atan2",
|
|
326
|
+
es >= 2015 && "atanh",
|
|
327
|
+
es >= 2015 && "cbrt",
|
|
153
328
|
"ceil",
|
|
329
|
+
es >= 2015 && "clz32",
|
|
154
330
|
"cos",
|
|
331
|
+
es >= 2015 && "cosh",
|
|
155
332
|
"exp",
|
|
333
|
+
es >= 2015 && "expm1",
|
|
156
334
|
"floor",
|
|
335
|
+
es >= 2026 && "f16round",
|
|
336
|
+
es >= 2015 && "fround",
|
|
337
|
+
es >= 2015 && "hypot",
|
|
338
|
+
es >= 2015 && "imul",
|
|
157
339
|
"log",
|
|
340
|
+
es >= 2015 && "log10",
|
|
341
|
+
es >= 2015 && "log1p",
|
|
342
|
+
es >= 2015 && "log2",
|
|
343
|
+
"max",
|
|
344
|
+
"min",
|
|
345
|
+
"pow",
|
|
158
346
|
"round",
|
|
347
|
+
es >= 2015 && "sign",
|
|
159
348
|
"sin",
|
|
349
|
+
es >= 2015 && "sinh",
|
|
160
350
|
"sqrt",
|
|
161
351
|
"tan",
|
|
162
|
-
"
|
|
163
|
-
"
|
|
164
|
-
"max",
|
|
165
|
-
"min",
|
|
352
|
+
es >= 2015 && "tanh",
|
|
353
|
+
es >= 2015 && "trunc",
|
|
166
354
|
],
|
|
167
355
|
Number: [
|
|
168
|
-
"isFinite",
|
|
169
|
-
"
|
|
356
|
+
es >= 2015 && "isFinite",
|
|
357
|
+
es >= 2015 && "isInteger",
|
|
358
|
+
es >= 2015 && "isSafeInteger",
|
|
359
|
+
es >= 2015 && "isNaN",
|
|
360
|
+
es >= 2015 && "parseFloat",
|
|
361
|
+
es >= 2015 && "parseInt",
|
|
170
362
|
],
|
|
171
363
|
Object: [
|
|
172
|
-
"create",
|
|
173
|
-
"getOwnPropertyDescriptor",
|
|
174
|
-
"
|
|
175
|
-
"
|
|
364
|
+
sloppy && "create",
|
|
365
|
+
sloppy && "getOwnPropertyDescriptor",
|
|
366
|
+
es >= 2017 && sloppy && "getOwnPropertyDescriptors",
|
|
367
|
+
sloppy && "getOwnPropertyNames",
|
|
368
|
+
es >= 2015 && sloppy && "getOwnPropertySymbols",
|
|
369
|
+
sloppy && "getPrototypeOf",
|
|
370
|
+
es >= 2022 && sloppy && "hasOwn",
|
|
371
|
+
es >= 2015 && "is",
|
|
176
372
|
"isExtensible",
|
|
177
373
|
"isFrozen",
|
|
178
374
|
"isSealed",
|
|
179
|
-
"
|
|
180
|
-
|
|
375
|
+
es >= 2015 && sloppy && "keys",
|
|
376
|
+
],
|
|
377
|
+
Promise: es >= 2015 && [
|
|
378
|
+
es >= 2024 && "withResolvers",
|
|
379
|
+
],
|
|
380
|
+
Proxy: es >= 2015 && [
|
|
381
|
+
sloppy && "revocable",
|
|
382
|
+
],
|
|
383
|
+
Reflect: es >= 2015 && [
|
|
384
|
+
sloppy && "has",
|
|
385
|
+
sloppy && "isExtensible",
|
|
386
|
+
sloppy && "ownKeys",
|
|
387
|
+
],
|
|
388
|
+
RegExp: [
|
|
389
|
+
es >= 2026 && sloppy && "escape",
|
|
181
390
|
],
|
|
182
391
|
String: [
|
|
183
392
|
"fromCharCode",
|
|
393
|
+
sloppy && es >= 2025 && "fromCodePoint",
|
|
184
394
|
],
|
|
185
|
-
|
|
395
|
+
Uint16Array: ["of"],
|
|
396
|
+
Uint32Array: ["of"],
|
|
397
|
+
Uint8Array: ["of"],
|
|
398
|
+
Uint8ClampedArray: ["of"],
|
|
399
|
+
}));
|
|
186
400
|
|
|
187
401
|
// Known numeric values which come with JS environments
|
|
188
|
-
|
|
402
|
+
// eslint-disable-next-line no-unused-vars
|
|
403
|
+
export const is_pure_native_static_property = make_nested_lookup(({ sloppy, es }) => ({
|
|
189
404
|
Math: [
|
|
190
405
|
"E",
|
|
191
406
|
"LN10",
|
|
@@ -197,10 +412,127 @@ export const is_pure_native_value = make_nested_lookup({
|
|
|
197
412
|
"SQRT2",
|
|
198
413
|
],
|
|
199
414
|
Number: [
|
|
415
|
+
es >= 2015 && "EPSILON",
|
|
416
|
+
es >= 2015 && "MAX_SAFE_VALUE",
|
|
200
417
|
"MAX_VALUE",
|
|
418
|
+
es >= 2015 && "MIN_SAFE_VALUE",
|
|
201
419
|
"MIN_VALUE",
|
|
202
420
|
"NaN",
|
|
203
421
|
"NEGATIVE_INFINITY",
|
|
204
422
|
"POSITIVE_INFINITY",
|
|
205
423
|
],
|
|
206
|
-
|
|
424
|
+
RegExp: [
|
|
425
|
+
"$_",
|
|
426
|
+
"$0",
|
|
427
|
+
"$1",
|
|
428
|
+
"$2",
|
|
429
|
+
"$3",
|
|
430
|
+
"$4",
|
|
431
|
+
"$5",
|
|
432
|
+
"$6",
|
|
433
|
+
"$7",
|
|
434
|
+
"$8",
|
|
435
|
+
"$9",
|
|
436
|
+
"input",
|
|
437
|
+
"lastMatch",
|
|
438
|
+
"lastParen",
|
|
439
|
+
"leftContext",
|
|
440
|
+
"rightContext",
|
|
441
|
+
],
|
|
442
|
+
}));
|
|
443
|
+
|
|
444
|
+
const re_uppercase_first_letter = /^[A-Z]/;
|
|
445
|
+
export function is_pure_builtin_call(compressor, call) {
|
|
446
|
+
let builtin = "";
|
|
447
|
+
let method = "";
|
|
448
|
+
|
|
449
|
+
let exp = call.expression;
|
|
450
|
+
if (is_undeclared_ref(exp)) {
|
|
451
|
+
builtin = exp.name;
|
|
452
|
+
} else if (exp instanceof AST_Dot) {
|
|
453
|
+
method = exp.property;
|
|
454
|
+
|
|
455
|
+
exp = exp.expression;
|
|
456
|
+
if (is_undeclared_ref(exp)) {
|
|
457
|
+
if (
|
|
458
|
+
// globalThis.pureFunc()
|
|
459
|
+
exp.name === "globalThis"
|
|
460
|
+
&& compressor.option("builtins_ecma") >= 2020
|
|
461
|
+
) {
|
|
462
|
+
builtin = method;
|
|
463
|
+
method = "";
|
|
464
|
+
} else {
|
|
465
|
+
// SomeBuiltin.pureFunc()
|
|
466
|
+
builtin = exp.name;
|
|
467
|
+
}
|
|
468
|
+
} else if (exp instanceof AST_Dot) {
|
|
469
|
+
if (
|
|
470
|
+
is_undeclared_ref(exp.expression)
|
|
471
|
+
&& exp.expression.name === "globalThis"
|
|
472
|
+
&& compressor.option("builtins_ecma") >= 2020
|
|
473
|
+
) {
|
|
474
|
+
// globalThis.SomeBuiltin.pureFunc()
|
|
475
|
+
builtin = exp.property;
|
|
476
|
+
} else {
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
} else {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
} else {
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (!method) {
|
|
487
|
+
if (compressor.is_pure_native_fn(builtin)) {
|
|
488
|
+
// some require `new`, others throw if you use it
|
|
489
|
+
const is_new = call instanceof AST_New;
|
|
490
|
+
const should_be_new = re_uppercase_first_letter.test(builtin); // true of all `is_pure_native_fn`
|
|
491
|
+
if (is_new !== should_be_new) return false;
|
|
492
|
+
|
|
493
|
+
if (!is_builtin_pure_with_these_args(builtin, call.args)) {
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return false;
|
|
501
|
+
} else {
|
|
502
|
+
return compressor.is_pure_native_static_fn(builtin, method);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Some builtins are listed above but their purity is subject to some conditions */
|
|
507
|
+
function is_builtin_pure_with_these_args(builtin, args) {
|
|
508
|
+
// all the builtins we deal with here are ok with getting 0 args
|
|
509
|
+
if (args.length === 0) return true;
|
|
510
|
+
|
|
511
|
+
let arg1 = args[0];
|
|
512
|
+
if (arg1 instanceof AST_SymbolRef) {
|
|
513
|
+
arg1 = arg1.fixed_value();
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (lone_arg_is_range.has(builtin)) { // new Array(number)
|
|
517
|
+
const arg_valid = args.length > 1
|
|
518
|
+
|| arg1 instanceof AST_Number
|
|
519
|
+
&& arg1.value >= 0 && arg1.value <= 0xffffffff;
|
|
520
|
+
// TODO: or, we are asked to ignore TypeError
|
|
521
|
+
if (!arg_valid) return false;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (arg1_is_range_or_iterable.has(builtin)) { // new Float32Array(number | Array)
|
|
525
|
+
const arg_valid = args.length === 0
|
|
526
|
+
|| arg1 instanceof AST_Array
|
|
527
|
+
|| arg1 instanceof AST_Number
|
|
528
|
+
&& arg1.value >= 0 && arg1.value <= 0xffffffff;
|
|
529
|
+
if (!arg_valid) return false;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (arg1_is_iterable.has(builtin)) { // new Set(iterable)
|
|
533
|
+
const arg_valid = args.length === 0 || arg1 instanceof AST_Array;
|
|
534
|
+
if (!arg_valid) return false;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return true;
|
|
538
|
+
}
|
package/lib/utils/index.js
CHANGED
|
@@ -79,7 +79,7 @@ function defaults(args, defs, croak) {
|
|
|
79
79
|
for (const i in defs) if (HOP(defs, i)) {
|
|
80
80
|
if (!args || !HOP(args, i)) {
|
|
81
81
|
ret[i] = defs[i];
|
|
82
|
-
} else if (i === "ecma") {
|
|
82
|
+
} else if (i === "ecma" || i === "builtins_ecma") {
|
|
83
83
|
let ecma = args[i] | 0;
|
|
84
84
|
if (ecma > 5 && ecma < 2015) ecma += 2009;
|
|
85
85
|
ret[i] = ecma;
|