| 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 { AST_Array, AST_Dot, AST_New, AST_Number, AST_SymbolRef } from "../ast.js";
|
|---|
| 45 | import { makePredicate } from "../utils/index.js";
|
|---|
| 46 | import { is_undeclared_ref } from "./inference.js";
|
|---|
| 47 |
|
|---|
| 48 | // Lists of native methods, useful for `unsafe` option which assumes they exist.
|
|---|
| 49 | // Note: Lots of methods and functions are missing here, in case they aren't pure
|
|---|
| 50 | // or not available in all JS environments.
|
|---|
| 51 |
|
|---|
| 52 | const make_nested_lookup = (feature_callback) => (compressor) => {
|
|---|
| 53 | const obj = feature_callback(feature_variables(compressor));
|
|---|
| 54 |
|
|---|
| 55 | const out = new Map();
|
|---|
| 56 | for (var key of Object.keys(obj)) {
|
|---|
| 57 | if (obj[key]) {
|
|---|
| 58 | out.set(key, makePredicate(remove_false(obj[key])));
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | const does_have = (global_name, fname) => {
|
|---|
| 63 | const inner_map = out.get(global_name);
|
|---|
| 64 | return inner_map != null && inner_map.has(fname);
|
|---|
| 65 | };
|
|---|
| 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;
|
|---|
| 87 | }
|
|---|
| 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 |
|
|---|
| 135 | // Objects which are safe to access without throwing or causing a side effect.
|
|---|
| 136 | // Usually we'd check the `unsafe` option first but these are way too common for that
|
|---|
| 137 | export const pure_prop_access_globals = new Set([
|
|---|
| 138 | "Number",
|
|---|
| 139 | "String",
|
|---|
| 140 | "Array",
|
|---|
| 141 | "Object",
|
|---|
| 142 | "Function",
|
|---|
| 143 | "Promise",
|
|---|
| 144 | ]);
|
|---|
| 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 |
|
|---|
| 218 | const object_methods = [
|
|---|
| 219 | "constructor",
|
|---|
| 220 | "toString",
|
|---|
| 221 | "valueOf",
|
|---|
| 222 | ];
|
|---|
| 223 |
|
|---|
| 224 | // eslint-disable-next-line no-unused-vars
|
|---|
| 225 | export const is_pure_native_method = make_nested_lookup(({ sloppy, es }) => ({
|
|---|
| 226 | Array: [
|
|---|
| 227 | es >= 2022 && "at",
|
|---|
| 228 | es >= 2019 && "flat",
|
|---|
| 229 | es >= 2016 && "includes",
|
|---|
| 230 | "indexOf",
|
|---|
| 231 | "join",
|
|---|
| 232 | "lastIndexOf",
|
|---|
| 233 | "slice",
|
|---|
| 234 | ...object_methods,
|
|---|
| 235 | ],
|
|---|
| 236 | Boolean: object_methods,
|
|---|
| 237 | Function: object_methods,
|
|---|
| 238 | Number: [
|
|---|
| 239 | "toExponential",
|
|---|
| 240 | "toFixed",
|
|---|
| 241 | "toPrecision",
|
|---|
| 242 | ...object_methods,
|
|---|
| 243 | ],
|
|---|
| 244 | Object: object_methods,
|
|---|
| 245 | RegExp: [
|
|---|
| 246 | "test",
|
|---|
| 247 | ...object_methods,
|
|---|
| 248 | ],
|
|---|
| 249 | String: [
|
|---|
| 250 | es >= 2022 && "at",
|
|---|
| 251 | "charAt",
|
|---|
| 252 | "charCodeAt",
|
|---|
| 253 | es >= 2015 && "codePointAt",
|
|---|
| 254 | "concat",
|
|---|
| 255 | es >= 2025 && "endsWith",
|
|---|
| 256 | es >= 2015 && "includes",
|
|---|
| 257 | "indexOf",
|
|---|
| 258 | "italics",
|
|---|
| 259 | "lastIndexOf",
|
|---|
| 260 | es >= 2020 && "localeCompare",
|
|---|
| 261 | "match",
|
|---|
| 262 | es >= 2020 && "matchAll",
|
|---|
| 263 | es >= 2015 && "normalize",
|
|---|
| 264 | es >= 2017 && "padStart",
|
|---|
| 265 | es >= 2017 && "padEnd",
|
|---|
| 266 | es >= 2015 && sloppy && "repeat",
|
|---|
| 267 | "replace",
|
|---|
| 268 | es >= 2021 && "replaceAll",
|
|---|
| 269 | "search",
|
|---|
| 270 | "slice",
|
|---|
| 271 | "split",
|
|---|
| 272 | es >= 2015 && "startsWith",
|
|---|
| 273 | "substr",
|
|---|
| 274 | "substring",
|
|---|
| 275 | es >= 2015 && "repeat",
|
|---|
| 276 | "toLocaleLowerCase",
|
|---|
| 277 | "toLocaleUpperCase",
|
|---|
| 278 | "toLowerCase",
|
|---|
| 279 | "toUpperCase",
|
|---|
| 280 | "trim",
|
|---|
| 281 | es >= 2019 && "trimEnd",
|
|---|
| 282 | es >= 2019 && "trimStart",
|
|---|
| 283 | es >= 2019 && "trimLeft",
|
|---|
| 284 | es >= 2019 && "trimRight",
|
|---|
| 285 | ...object_methods,
|
|---|
| 286 | ],
|
|---|
| 287 | }));
|
|---|
| 288 |
|
|---|
| 289 | // eslint-disable-next-line no-unused-vars
|
|---|
| 290 | export const is_pure_native_static_fn = make_nested_lookup(({ sloppy, es }) => ({
|
|---|
| 291 | Array: [
|
|---|
| 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",
|
|---|
| 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"],
|
|---|
| 318 | Math: [
|
|---|
| 319 | "abs",
|
|---|
| 320 | "acos",
|
|---|
| 321 | es >= 2015 && "acosh",
|
|---|
| 322 | "asin",
|
|---|
| 323 | es >= 2015 && "asinh",
|
|---|
| 324 | "atan",
|
|---|
| 325 | "atan2",
|
|---|
| 326 | es >= 2015 && "atanh",
|
|---|
| 327 | es >= 2015 && "cbrt",
|
|---|
| 328 | "ceil",
|
|---|
| 329 | es >= 2015 && "clz32",
|
|---|
| 330 | "cos",
|
|---|
| 331 | es >= 2015 && "cosh",
|
|---|
| 332 | "exp",
|
|---|
| 333 | es >= 2015 && "expm1",
|
|---|
| 334 | "floor",
|
|---|
| 335 | es >= 2026 && "f16round",
|
|---|
| 336 | es >= 2015 && "fround",
|
|---|
| 337 | es >= 2015 && "hypot",
|
|---|
| 338 | es >= 2015 && "imul",
|
|---|
| 339 | "log",
|
|---|
| 340 | es >= 2015 && "log10",
|
|---|
| 341 | es >= 2015 && "log1p",
|
|---|
| 342 | es >= 2015 && "log2",
|
|---|
| 343 | "max",
|
|---|
| 344 | "min",
|
|---|
| 345 | "pow",
|
|---|
| 346 | "round",
|
|---|
| 347 | es >= 2015 && "sign",
|
|---|
| 348 | "sin",
|
|---|
| 349 | es >= 2015 && "sinh",
|
|---|
| 350 | "sqrt",
|
|---|
| 351 | "tan",
|
|---|
| 352 | es >= 2015 && "tanh",
|
|---|
| 353 | es >= 2015 && "trunc",
|
|---|
| 354 | ],
|
|---|
| 355 | Number: [
|
|---|
| 356 | es >= 2015 && "isFinite",
|
|---|
| 357 | es >= 2015 && "isInteger",
|
|---|
| 358 | es >= 2015 && "isSafeInteger",
|
|---|
| 359 | es >= 2015 && "isNaN",
|
|---|
| 360 | es >= 2015 && "parseFloat",
|
|---|
| 361 | es >= 2015 && "parseInt",
|
|---|
| 362 | ],
|
|---|
| 363 | Object: [
|
|---|
| 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",
|
|---|
| 372 | "isExtensible",
|
|---|
| 373 | "isFrozen",
|
|---|
| 374 | "isSealed",
|
|---|
| 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",
|
|---|
| 390 | ],
|
|---|
| 391 | String: [
|
|---|
| 392 | "fromCharCode",
|
|---|
| 393 | sloppy && es >= 2025 && "fromCodePoint",
|
|---|
| 394 | ],
|
|---|
| 395 | Uint16Array: ["of"],
|
|---|
| 396 | Uint32Array: ["of"],
|
|---|
| 397 | Uint8Array: ["of"],
|
|---|
| 398 | Uint8ClampedArray: ["of"],
|
|---|
| 399 | }));
|
|---|
| 400 |
|
|---|
| 401 | // Known numeric values which come with JS environments
|
|---|
| 402 | // eslint-disable-next-line no-unused-vars
|
|---|
| 403 | export const is_pure_native_static_property = make_nested_lookup(({ sloppy, es }) => ({
|
|---|
| 404 | Math: [
|
|---|
| 405 | "E",
|
|---|
| 406 | "LN10",
|
|---|
| 407 | "LN2",
|
|---|
| 408 | "LOG2E",
|
|---|
| 409 | "LOG10E",
|
|---|
| 410 | "PI",
|
|---|
| 411 | "SQRT1_2",
|
|---|
| 412 | "SQRT2",
|
|---|
| 413 | ],
|
|---|
| 414 | Number: [
|
|---|
| 415 | es >= 2015 && "EPSILON",
|
|---|
| 416 | es >= 2015 && "MAX_SAFE_VALUE",
|
|---|
| 417 | "MAX_VALUE",
|
|---|
| 418 | es >= 2015 && "MIN_SAFE_VALUE",
|
|---|
| 419 | "MIN_VALUE",
|
|---|
| 420 | "NaN",
|
|---|
| 421 | "NEGATIVE_INFINITY",
|
|---|
| 422 | "POSITIVE_INFINITY",
|
|---|
| 423 | ],
|
|---|
| 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 | }
|
|---|