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 { makePredicate } from "../utils/index.js";
|
---|
45 |
|
---|
46 | // Lists of native methods, useful for `unsafe` option which assumes they exist.
|
---|
47 | // Note: Lots of methods and functions are missing here, in case they aren't pure
|
---|
48 | // or not available in all JS environments.
|
---|
49 |
|
---|
50 | function make_nested_lookup(obj) {
|
---|
51 | const out = new Map();
|
---|
52 | for (var key of Object.keys(obj)) {
|
---|
53 | out.set(key, makePredicate(obj[key]));
|
---|
54 | }
|
---|
55 |
|
---|
56 | const does_have = (global_name, fname) => {
|
---|
57 | const inner_map = out.get(global_name);
|
---|
58 | return inner_map != null && inner_map.has(fname);
|
---|
59 | };
|
---|
60 | return does_have;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Objects which are safe to access without throwing or causing a side effect.
|
---|
64 | // Usually we'd check the `unsafe` option first but these are way too common for that
|
---|
65 | export const pure_prop_access_globals = new Set([
|
---|
66 | "Number",
|
---|
67 | "String",
|
---|
68 | "Array",
|
---|
69 | "Object",
|
---|
70 | "Function",
|
---|
71 | "Promise",
|
---|
72 | ]);
|
---|
73 |
|
---|
74 | const object_methods = [
|
---|
75 | "constructor",
|
---|
76 | "toString",
|
---|
77 | "valueOf",
|
---|
78 | ];
|
---|
79 |
|
---|
80 | export const is_pure_native_method = make_nested_lookup({
|
---|
81 | Array: [
|
---|
82 | "at",
|
---|
83 | "flat",
|
---|
84 | "includes",
|
---|
85 | "indexOf",
|
---|
86 | "join",
|
---|
87 | "lastIndexOf",
|
---|
88 | "slice",
|
---|
89 | ...object_methods,
|
---|
90 | ],
|
---|
91 | Boolean: object_methods,
|
---|
92 | Function: object_methods,
|
---|
93 | Number: [
|
---|
94 | "toExponential",
|
---|
95 | "toFixed",
|
---|
96 | "toPrecision",
|
---|
97 | ...object_methods,
|
---|
98 | ],
|
---|
99 | Object: object_methods,
|
---|
100 | RegExp: [
|
---|
101 | "test",
|
---|
102 | ...object_methods,
|
---|
103 | ],
|
---|
104 | String: [
|
---|
105 | "at",
|
---|
106 | "charAt",
|
---|
107 | "charCodeAt",
|
---|
108 | "charPointAt",
|
---|
109 | "concat",
|
---|
110 | "endsWith",
|
---|
111 | "fromCharCode",
|
---|
112 | "fromCodePoint",
|
---|
113 | "includes",
|
---|
114 | "indexOf",
|
---|
115 | "italics",
|
---|
116 | "lastIndexOf",
|
---|
117 | "localeCompare",
|
---|
118 | "match",
|
---|
119 | "matchAll",
|
---|
120 | "normalize",
|
---|
121 | "padStart",
|
---|
122 | "padEnd",
|
---|
123 | "repeat",
|
---|
124 | "replace",
|
---|
125 | "replaceAll",
|
---|
126 | "search",
|
---|
127 | "slice",
|
---|
128 | "split",
|
---|
129 | "startsWith",
|
---|
130 | "substr",
|
---|
131 | "substring",
|
---|
132 | "repeat",
|
---|
133 | "toLocaleLowerCase",
|
---|
134 | "toLocaleUpperCase",
|
---|
135 | "toLowerCase",
|
---|
136 | "toUpperCase",
|
---|
137 | "trim",
|
---|
138 | "trimEnd",
|
---|
139 | "trimStart",
|
---|
140 | ...object_methods,
|
---|
141 | ],
|
---|
142 | });
|
---|
143 |
|
---|
144 | export const is_pure_native_fn = make_nested_lookup({
|
---|
145 | Array: [
|
---|
146 | "isArray",
|
---|
147 | ],
|
---|
148 | Math: [
|
---|
149 | "abs",
|
---|
150 | "acos",
|
---|
151 | "asin",
|
---|
152 | "atan",
|
---|
153 | "ceil",
|
---|
154 | "cos",
|
---|
155 | "exp",
|
---|
156 | "floor",
|
---|
157 | "log",
|
---|
158 | "round",
|
---|
159 | "sin",
|
---|
160 | "sqrt",
|
---|
161 | "tan",
|
---|
162 | "atan2",
|
---|
163 | "pow",
|
---|
164 | "max",
|
---|
165 | "min",
|
---|
166 | ],
|
---|
167 | Number: [
|
---|
168 | "isFinite",
|
---|
169 | "isNaN",
|
---|
170 | ],
|
---|
171 | Object: [
|
---|
172 | "create",
|
---|
173 | "getOwnPropertyDescriptor",
|
---|
174 | "getOwnPropertyNames",
|
---|
175 | "getPrototypeOf",
|
---|
176 | "isExtensible",
|
---|
177 | "isFrozen",
|
---|
178 | "isSealed",
|
---|
179 | "hasOwn",
|
---|
180 | "keys",
|
---|
181 | ],
|
---|
182 | String: [
|
---|
183 | "fromCharCode",
|
---|
184 | ],
|
---|
185 | });
|
---|
186 |
|
---|
187 | // Known numeric values which come with JS environments
|
---|
188 | export const is_pure_native_value = make_nested_lookup({
|
---|
189 | Math: [
|
---|
190 | "E",
|
---|
191 | "LN10",
|
---|
192 | "LN2",
|
---|
193 | "LOG2E",
|
---|
194 | "LOG10E",
|
---|
195 | "PI",
|
---|
196 | "SQRT1_2",
|
---|
197 | "SQRT2",
|
---|
198 | ],
|
---|
199 | Number: [
|
---|
200 | "MAX_VALUE",
|
---|
201 | "MIN_VALUE",
|
---|
202 | "NaN",
|
---|
203 | "NEGATIVE_INFINITY",
|
---|
204 | "POSITIVE_INFINITY",
|
---|
205 | ],
|
---|
206 | });
|
---|