webfuscator 0.9.4

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.
Files changed (289) hide show
  1. package/DISCLOSURE +9 -0
  2. package/LICENSE +21 -0
  3. package/README.md +83 -0
  4. package/dist/analysis/constant.d.ts +11 -0
  5. package/dist/analysis/constant.d.ts.map +1 -0
  6. package/dist/analysis/constant.js +261 -0
  7. package/dist/analysis/constant.js.map +1 -0
  8. package/dist/analysis/document-order.d.ts +5 -0
  9. package/dist/analysis/document-order.d.ts.map +1 -0
  10. package/dist/analysis/document-order.js +124 -0
  11. package/dist/analysis/document-order.js.map +1 -0
  12. package/dist/analysis/inlineability.d.ts +13 -0
  13. package/dist/analysis/inlineability.d.ts.map +1 -0
  14. package/dist/analysis/inlineability.js +270 -0
  15. package/dist/analysis/inlineability.js.map +1 -0
  16. package/dist/analysis/purity.d.ts +5 -0
  17. package/dist/analysis/purity.d.ts.map +1 -0
  18. package/dist/analysis/purity.js +281 -0
  19. package/dist/analysis/purity.js.map +1 -0
  20. package/dist/index.d.ts +3 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +2 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/obfuscator.d.ts +4 -0
  25. package/dist/obfuscator.d.ts.map +1 -0
  26. package/dist/obfuscator.js +222 -0
  27. package/dist/obfuscator.js.map +1 -0
  28. package/dist/options.d.ts +73 -0
  29. package/dist/options.d.ts.map +1 -0
  30. package/dist/options.js +2 -0
  31. package/dist/options.js.map +1 -0
  32. package/dist/preparation/dot-to-bracket.d.ts +16 -0
  33. package/dist/preparation/dot-to-bracket.d.ts.map +1 -0
  34. package/dist/preparation/dot-to-bracket.js +36 -0
  35. package/dist/preparation/dot-to-bracket.js.map +1 -0
  36. package/dist/preparation/expand-destructuring.d.ts +26 -0
  37. package/dist/preparation/expand-destructuring.d.ts.map +1 -0
  38. package/dist/preparation/expand-destructuring.js +154 -0
  39. package/dist/preparation/expand-destructuring.js.map +1 -0
  40. package/dist/preparation/expand-template-literals.d.ts +23 -0
  41. package/dist/preparation/expand-template-literals.d.ts.map +1 -0
  42. package/dist/preparation/expand-template-literals.js +78 -0
  43. package/dist/preparation/expand-template-literals.js.map +1 -0
  44. package/dist/preparation/rewrite-class-fields.d.ts +43 -0
  45. package/dist/preparation/rewrite-class-fields.d.ts.map +1 -0
  46. package/dist/preparation/rewrite-class-fields.js +270 -0
  47. package/dist/preparation/rewrite-class-fields.js.map +1 -0
  48. package/dist/preparation/split-sequence-expressions.d.ts +15 -0
  49. package/dist/preparation/split-sequence-expressions.d.ts.map +1 -0
  50. package/dist/preparation/split-sequence-expressions.js +36 -0
  51. package/dist/preparation/split-sequence-expressions.js.map +1 -0
  52. package/dist/preparation/split-variable-declarations.d.ts +20 -0
  53. package/dist/preparation/split-variable-declarations.d.ts.map +1 -0
  54. package/dist/preparation/split-variable-declarations.js +36 -0
  55. package/dist/preparation/split-variable-declarations.js.map +1 -0
  56. package/dist/preparation/stringify-object-keys.d.ts +21 -0
  57. package/dist/preparation/stringify-object-keys.d.ts.map +1 -0
  58. package/dist/preparation/stringify-object-keys.js +96 -0
  59. package/dist/preparation/stringify-object-keys.js.map +1 -0
  60. package/dist/preparation/wrap-single-statements.d.ts +24 -0
  61. package/dist/preparation/wrap-single-statements.d.ts.map +1 -0
  62. package/dist/preparation/wrap-single-statements.js +53 -0
  63. package/dist/preparation/wrap-single-statements.js.map +1 -0
  64. package/dist/transforms/arguments-to-parameters.d.ts +20 -0
  65. package/dist/transforms/arguments-to-parameters.d.ts.map +1 -0
  66. package/dist/transforms/arguments-to-parameters.js +162 -0
  67. package/dist/transforms/arguments-to-parameters.js.map +1 -0
  68. package/dist/transforms/arrow-to-function.d.ts +46 -0
  69. package/dist/transforms/arrow-to-function.d.ts.map +1 -0
  70. package/dist/transforms/arrow-to-function.js +203 -0
  71. package/dist/transforms/arrow-to-function.js.map +1 -0
  72. package/dist/transforms/collapse-single-use-temps.d.ts +16 -0
  73. package/dist/transforms/collapse-single-use-temps.d.ts.map +1 -0
  74. package/dist/transforms/collapse-single-use-temps.js +805 -0
  75. package/dist/transforms/collapse-single-use-temps.js.map +1 -0
  76. package/dist/transforms/const-let-to-var.d.ts +21 -0
  77. package/dist/transforms/const-let-to-var.d.ts.map +1 -0
  78. package/dist/transforms/const-let-to-var.js +217 -0
  79. package/dist/transforms/const-let-to-var.js.map +1 -0
  80. package/dist/transforms/const-to-let.d.ts +17 -0
  81. package/dist/transforms/const-to-let.d.ts.map +1 -0
  82. package/dist/transforms/const-to-let.js +33 -0
  83. package/dist/transforms/const-to-let.js.map +1 -0
  84. package/dist/transforms/do-while-to-while.d.ts +26 -0
  85. package/dist/transforms/do-while-to-while.d.ts.map +1 -0
  86. package/dist/transforms/do-while-to-while.js +54 -0
  87. package/dist/transforms/do-while-to-while.js.map +1 -0
  88. package/dist/transforms/drop-console.d.ts +27 -0
  89. package/dist/transforms/drop-console.d.ts.map +1 -0
  90. package/dist/transforms/drop-console.js +107 -0
  91. package/dist/transforms/drop-console.js.map +1 -0
  92. package/dist/transforms/drop-debugger.d.ts +18 -0
  93. package/dist/transforms/drop-debugger.d.ts.map +1 -0
  94. package/dist/transforms/drop-debugger.js +34 -0
  95. package/dist/transforms/drop-debugger.js.map +1 -0
  96. package/dist/transforms/expand-binary-assignment.d.ts +26 -0
  97. package/dist/transforms/expand-binary-assignment.d.ts.map +1 -0
  98. package/dist/transforms/expand-binary-assignment.js +78 -0
  99. package/dist/transforms/expand-binary-assignment.js.map +1 -0
  100. package/dist/transforms/expand-logical-assignment.d.ts +22 -0
  101. package/dist/transforms/expand-logical-assignment.d.ts.map +1 -0
  102. package/dist/transforms/expand-logical-assignment.js +73 -0
  103. package/dist/transforms/expand-logical-assignment.js.map +1 -0
  104. package/dist/transforms/extract-object-properties.d.ts +21 -0
  105. package/dist/transforms/extract-object-properties.d.ts.map +1 -0
  106. package/dist/transforms/extract-object-properties.js +340 -0
  107. package/dist/transforms/extract-object-properties.js.map +1 -0
  108. package/dist/transforms/fold-builtin-methods.d.ts +23 -0
  109. package/dist/transforms/fold-builtin-methods.d.ts.map +1 -0
  110. package/dist/transforms/fold-builtin-methods.js +839 -0
  111. package/dist/transforms/fold-builtin-methods.js.map +1 -0
  112. package/dist/transforms/fold-constants.d.ts +16 -0
  113. package/dist/transforms/fold-constants.d.ts.map +1 -0
  114. package/dist/transforms/fold-constants.js +421 -0
  115. package/dist/transforms/fold-constants.js.map +1 -0
  116. package/dist/transforms/for-to-while.d.ts +29 -0
  117. package/dist/transforms/for-to-while.d.ts.map +1 -0
  118. package/dist/transforms/for-to-while.js +91 -0
  119. package/dist/transforms/for-to-while.js.map +1 -0
  120. package/dist/transforms/function-declaration-to-expression.d.ts +28 -0
  121. package/dist/transforms/function-declaration-to-expression.d.ts.map +1 -0
  122. package/dist/transforms/function-declaration-to-expression.js +95 -0
  123. package/dist/transforms/function-declaration-to-expression.js.map +1 -0
  124. package/dist/transforms/inline-functions.d.ts +16 -0
  125. package/dist/transforms/inline-functions.d.ts.map +1 -0
  126. package/dist/transforms/inline-functions.js +896 -0
  127. package/dist/transforms/inline-functions.js.map +1 -0
  128. package/dist/transforms/logical-to-ternary.d.ts +20 -0
  129. package/dist/transforms/logical-to-ternary.d.ts.map +1 -0
  130. package/dist/transforms/logical-to-ternary.js +78 -0
  131. package/dist/transforms/logical-to-ternary.js.map +1 -0
  132. package/dist/transforms/mangle-properties.d.ts +27 -0
  133. package/dist/transforms/mangle-properties.d.ts.map +1 -0
  134. package/dist/transforms/mangle-properties.js +703 -0
  135. package/dist/transforms/mangle-properties.js.map +1 -0
  136. package/dist/transforms/nullish-coalescing-to-ternary.d.ts +18 -0
  137. package/dist/transforms/nullish-coalescing-to-ternary.d.ts.map +1 -0
  138. package/dist/transforms/nullish-coalescing-to-ternary.js +61 -0
  139. package/dist/transforms/nullish-coalescing-to-ternary.js.map +1 -0
  140. package/dist/transforms/numbers-to-strings.d.ts +19 -0
  141. package/dist/transforms/numbers-to-strings.d.ts.map +1 -0
  142. package/dist/transforms/numbers-to-strings.js +51 -0
  143. package/dist/transforms/numbers-to-strings.js.map +1 -0
  144. package/dist/transforms/object-method-to-property.d.ts +34 -0
  145. package/dist/transforms/object-method-to-property.d.ts.map +1 -0
  146. package/dist/transforms/object-method-to-property.js +78 -0
  147. package/dist/transforms/object-method-to-property.js.map +1 -0
  148. package/dist/transforms/optional-chaining-to-ternary.d.ts +19 -0
  149. package/dist/transforms/optional-chaining-to-ternary.d.ts.map +1 -0
  150. package/dist/transforms/optional-chaining-to-ternary.js +206 -0
  151. package/dist/transforms/optional-chaining-to-ternary.js.map +1 -0
  152. package/dist/transforms/pack-declarations-into-parameters.d.ts +22 -0
  153. package/dist/transforms/pack-declarations-into-parameters.d.ts.map +1 -0
  154. package/dist/transforms/pack-declarations-into-parameters.js +208 -0
  155. package/dist/transforms/pack-declarations-into-parameters.js.map +1 -0
  156. package/dist/transforms/remove-anonymous-function-names.d.ts +17 -0
  157. package/dist/transforms/remove-anonymous-function-names.d.ts.map +1 -0
  158. package/dist/transforms/remove-anonymous-function-names.js +72 -0
  159. package/dist/transforms/remove-anonymous-function-names.js.map +1 -0
  160. package/dist/transforms/remove-unreachable-code.d.ts +21 -0
  161. package/dist/transforms/remove-unreachable-code.d.ts.map +1 -0
  162. package/dist/transforms/remove-unreachable-code.js +103 -0
  163. package/dist/transforms/remove-unreachable-code.js.map +1 -0
  164. package/dist/transforms/remove-unused-code.d.ts +18 -0
  165. package/dist/transforms/remove-unused-code.d.ts.map +1 -0
  166. package/dist/transforms/remove-unused-code.js +249 -0
  167. package/dist/transforms/remove-unused-code.js.map +1 -0
  168. package/dist/transforms/rename-identifiers.d.ts +24 -0
  169. package/dist/transforms/rename-identifiers.d.ts.map +1 -0
  170. package/dist/transforms/rename-identifiers.js +264 -0
  171. package/dist/transforms/rename-identifiers.js.map +1 -0
  172. package/dist/transforms/specials-to-strings.d.ts +30 -0
  173. package/dist/transforms/specials-to-strings.d.ts.map +1 -0
  174. package/dist/transforms/specials-to-strings.js +101 -0
  175. package/dist/transforms/specials-to-strings.js.map +1 -0
  176. package/dist/transforms/switch-to-if.d.ts +28 -0
  177. package/dist/transforms/switch-to-if.d.ts.map +1 -0
  178. package/dist/transforms/switch-to-if.js +279 -0
  179. package/dist/transforms/switch-to-if.js.map +1 -0
  180. package/dist/transforms/ternary-to-if.d.ts +20 -0
  181. package/dist/transforms/ternary-to-if.d.ts.map +1 -0
  182. package/dist/transforms/ternary-to-if.js +315 -0
  183. package/dist/transforms/ternary-to-if.js.map +1 -0
  184. package/dist/transforms/update-to-assignment.d.ts +26 -0
  185. package/dist/transforms/update-to-assignment.d.ts.map +1 -0
  186. package/dist/transforms/update-to-assignment.js +120 -0
  187. package/dist/transforms/update-to-assignment.js.map +1 -0
  188. package/dist/transforms/yodify-conditions.d.ts +22 -0
  189. package/dist/transforms/yodify-conditions.d.ts.map +1 -0
  190. package/dist/transforms/yodify-conditions.js +91 -0
  191. package/dist/transforms/yodify-conditions.js.map +1 -0
  192. package/dist/utils/ast.d.ts +10 -0
  193. package/dist/utils/ast.d.ts.map +1 -0
  194. package/dist/utils/ast.js +112 -0
  195. package/dist/utils/ast.js.map +1 -0
  196. package/dist/utils/change-tracking.d.ts +8 -0
  197. package/dist/utils/change-tracking.d.ts.map +1 -0
  198. package/dist/utils/change-tracking.js +8 -0
  199. package/dist/utils/change-tracking.js.map +1 -0
  200. package/dist/utils/dom-properties.d.ts +2 -0
  201. package/dist/utils/dom-properties.d.ts.map +1 -0
  202. package/dist/utils/dom-properties.js +9252 -0
  203. package/dist/utils/dom-properties.js.map +1 -0
  204. package/dist/utils/evaluation-order.d.ts +4 -0
  205. package/dist/utils/evaluation-order.d.ts.map +1 -0
  206. package/dist/utils/evaluation-order.js +152 -0
  207. package/dist/utils/evaluation-order.js.map +1 -0
  208. package/dist/utils/literal.d.ts +5 -0
  209. package/dist/utils/literal.d.ts.map +1 -0
  210. package/dist/utils/literal.js +90 -0
  211. package/dist/utils/literal.js.map +1 -0
  212. package/dist/utils/logical-lowering.d.ts +4 -0
  213. package/dist/utils/logical-lowering.d.ts.map +1 -0
  214. package/dist/utils/logical-lowering.js +104 -0
  215. package/dist/utils/logical-lowering.js.map +1 -0
  216. package/dist/utils/loop-lowering.d.ts +23 -0
  217. package/dist/utils/loop-lowering.d.ts.map +1 -0
  218. package/dist/utils/loop-lowering.js +88 -0
  219. package/dist/utils/loop-lowering.js.map +1 -0
  220. package/dist/utils/paths.d.ts +9 -0
  221. package/dist/utils/paths.d.ts.map +1 -0
  222. package/dist/utils/paths.js +90 -0
  223. package/dist/utils/paths.js.map +1 -0
  224. package/dist/utils/random.d.ts +3 -0
  225. package/dist/utils/random.d.ts.map +1 -0
  226. package/dist/utils/random.js +22 -0
  227. package/dist/utils/random.js.map +1 -0
  228. package/dist/utils/string-generator.d.ts +37 -0
  229. package/dist/utils/string-generator.d.ts.map +1 -0
  230. package/dist/utils/string-generator.js +175 -0
  231. package/dist/utils/string-generator.js.map +1 -0
  232. package/package.json +76 -0
  233. package/src/analysis/constant.ts +310 -0
  234. package/src/analysis/document-order.ts +141 -0
  235. package/src/analysis/inlineability.ts +336 -0
  236. package/src/analysis/purity.ts +315 -0
  237. package/src/index.ts +2 -0
  238. package/src/obfuscator.ts +271 -0
  239. package/src/options.ts +114 -0
  240. package/src/preparation/dot-to-bracket.ts +41 -0
  241. package/src/preparation/expand-destructuring.ts +211 -0
  242. package/src/preparation/expand-template-literals.ts +92 -0
  243. package/src/preparation/rewrite-class-fields.ts +320 -0
  244. package/src/preparation/split-sequence-expressions.ts +40 -0
  245. package/src/preparation/split-variable-declarations.ts +39 -0
  246. package/src/preparation/stringify-object-keys.ts +107 -0
  247. package/src/preparation/wrap-single-statements.ts +63 -0
  248. package/src/transforms/arguments-to-parameters.ts +187 -0
  249. package/src/transforms/arrow-to-function.ts +225 -0
  250. package/src/transforms/collapse-single-use-temps.ts +957 -0
  251. package/src/transforms/const-let-to-var.ts +246 -0
  252. package/src/transforms/const-to-let.ts +38 -0
  253. package/src/transforms/do-while-to-while.ts +69 -0
  254. package/src/transforms/drop-console.ts +119 -0
  255. package/src/transforms/drop-debugger.ts +38 -0
  256. package/src/transforms/expand-binary-assignment.ts +94 -0
  257. package/src/transforms/expand-logical-assignment.ts +84 -0
  258. package/src/transforms/extract-object-properties.ts +404 -0
  259. package/src/transforms/fold-builtin-methods.ts +949 -0
  260. package/src/transforms/fold-constants.ts +462 -0
  261. package/src/transforms/for-to-while.ts +109 -0
  262. package/src/transforms/function-declaration-to-expression.ts +108 -0
  263. package/src/transforms/inline-functions.ts +1074 -0
  264. package/src/transforms/logical-to-ternary.ts +92 -0
  265. package/src/transforms/mangle-properties.ts +819 -0
  266. package/src/transforms/nullish-coalescing-to-ternary.ts +77 -0
  267. package/src/transforms/numbers-to-strings.ts +61 -0
  268. package/src/transforms/object-method-to-property.ts +86 -0
  269. package/src/transforms/optional-chaining-to-ternary.ts +225 -0
  270. package/src/transforms/pack-declarations-into-parameters.ts +233 -0
  271. package/src/transforms/remove-anonymous-function-names.ts +84 -0
  272. package/src/transforms/remove-unreachable-code.ts +115 -0
  273. package/src/transforms/remove-unused-code.ts +296 -0
  274. package/src/transforms/rename-identifiers.ts +289 -0
  275. package/src/transforms/specials-to-strings.ts +116 -0
  276. package/src/transforms/switch-to-if.ts +339 -0
  277. package/src/transforms/ternary-to-if.ts +387 -0
  278. package/src/transforms/update-to-assignment.ts +132 -0
  279. package/src/transforms/yodify-conditions.ts +103 -0
  280. package/src/utils/ast.ts +129 -0
  281. package/src/utils/change-tracking.ts +15 -0
  282. package/src/utils/dom-properties.ts +9252 -0
  283. package/src/utils/evaluation-order.ts +178 -0
  284. package/src/utils/literal.ts +96 -0
  285. package/src/utils/logical-lowering.ts +132 -0
  286. package/src/utils/loop-lowering.ts +109 -0
  287. package/src/utils/paths.ts +103 -0
  288. package/src/utils/random.ts +22 -0
  289. package/src/utils/string-generator.ts +206 -0
@@ -0,0 +1,703 @@
1
+ import traverse from '@babel/traverse';
2
+ import * as t from '@babel/types';
3
+ import { evaluateConstant, isOpaque } from '../analysis/constant.js';
4
+ import { isProtoKey } from '../utils/ast.js';
5
+ import { DOM_PROPERTIES } from '../utils/dom-properties.js';
6
+ import { mulberry32 } from '../utils/random.js';
7
+ import { StringGenerator } from '../utils/string-generator.js';
8
+ /**
9
+ * Renames statically known public properties through one program-wide mapping.
10
+ * Property selection follows Terser, with extra collision checks for names
11
+ * that this pipeline must leave unchanged.
12
+ *
13
+ * This pass runs before shape preparation, while Babel still distinguishes
14
+ * `obj.name` from `obj["name"]` and class fields still carry their source keys.
15
+ *
16
+ * @example
17
+ * // ◀️ before
18
+ * var counter = {
19
+ * current_: 1,
20
+ * increment_() { return ++this.current_; }
21
+ * };
22
+ * log(counter.increment_());
23
+ *
24
+ * // ▶️ after
25
+ * var counter = {
26
+ * a: 1,
27
+ * b() { return ++this.a; }
28
+ * };
29
+ * log(counter.b());
30
+ */
31
+ export function mangleProperties(ast, ctx) {
32
+ const state = new PropertyManglerState(ctx);
33
+ traverse(ast, prepareVisitor, undefined, state);
34
+ traverse(ast, collectVisitor, undefined, state);
35
+ state.finalizeCandidates();
36
+ traverse(ast, rewriteVisitor, undefined, state);
37
+ return state.changed;
38
+ }
39
+ const MANGLE_PROPERTY_ANNOTATION = '__MANGLE_PROP__';
40
+ const PROPERTY_KEY_ANNOTATION = '__KEY__';
41
+ const NUMERIC_PROPERTY_RE = /^-?[0-9]+(?:\.[0-9]+)?(?:e[+-][0-9]+)?$/u;
42
+ const SYNTAX_SENSITIVE_GENERATED_NAMES = new Set(['__proto__', 'constructor', 'prototype']);
43
+ const MAX_GENERATOR_ATTEMPTS = 100_000;
44
+ class PropertyManglerState {
45
+ changed = false;
46
+ annotated = new Set();
47
+ candidates = new Set();
48
+ cache;
49
+ keepQuoted;
50
+ onlyAnnotated;
51
+ onlyCache;
52
+ reserved;
53
+ rewrittenKeyAnnotations = new WeakSet();
54
+ undeclared;
55
+ blockedCacheSources = new Set();
56
+ debugSuffix;
57
+ emittedNames = new Set();
58
+ nameGenerator;
59
+ regex;
60
+ skippedUndeclaredNames = new Set();
61
+ unavailableOutputs = new Set();
62
+ nameIndex = 0;
63
+ constructor(ctx) {
64
+ const options = ctx.mangleProperties ?? {};
65
+ this.cache = options.cache ?? new Map();
66
+ this.keepQuoted = options.keepQuoted ?? false;
67
+ this.onlyAnnotated = options.onlyAnnotated ?? false;
68
+ this.onlyCache = options.onlyCache ?? false;
69
+ this.reserved = new Set(options.reserved);
70
+ this.undeclared = options.undeclared ?? false;
71
+ this.debugSuffix = debugSuffix(options.debug);
72
+ this.regex = compileRegex(options.regex);
73
+ if (!options.builtins) {
74
+ for (const name of builtinProperties()) {
75
+ this.reserved.add(name);
76
+ }
77
+ }
78
+ const cachedNames = new Set();
79
+ for (const [name, mangled] of this.cache) {
80
+ if (typeof name !== 'string' || typeof mangled !== 'string') {
81
+ throw new TypeError('mangleProperties.cache must map strings to strings');
82
+ }
83
+ if (SYNTAX_SENSITIVE_GENERATED_NAMES.has(mangled)) {
84
+ throw new TypeError(`mangleProperties.cache cannot emit ${JSON.stringify(mangled)}`);
85
+ }
86
+ if (cachedNames.has(mangled)) {
87
+ throw new TypeError(`mangleProperties.cache contains duplicate output ${JSON.stringify(mangled)}`);
88
+ }
89
+ cachedNames.add(mangled);
90
+ this.emittedNames.add(mangled);
91
+ }
92
+ if (options.nameGenerator) {
93
+ this.nameGenerator = options.nameGenerator;
94
+ }
95
+ else {
96
+ const generator = new StringGenerator(ctx.stringGeneratorMode, mulberry32(ctx.seed));
97
+ this.nameGenerator = (index) => generator.at(index);
98
+ }
99
+ }
100
+ addCandidate(name) {
101
+ if (this.canSelect(name)) {
102
+ this.candidates.add(name);
103
+ }
104
+ if (!this.shouldMangle(name)) {
105
+ this.unavailableOutputs.add(name);
106
+ }
107
+ }
108
+ retainName(name) {
109
+ this.unavailableOutputs.add(name);
110
+ }
111
+ reserveName(name) {
112
+ this.reserved.add(name);
113
+ this.unavailableOutputs.add(name);
114
+ }
115
+ skipUndeclaredName(name) {
116
+ this.skippedUndeclaredNames.add(name);
117
+ }
118
+ finalizeCandidates() {
119
+ for (const name of this.skippedUndeclaredNames) {
120
+ if (!this.shouldMangle(name)) {
121
+ this.unavailableOutputs.add(name);
122
+ }
123
+ }
124
+ const sourceByOutput = new Map();
125
+ for (const [source, output] of this.cache) {
126
+ if (source === output || !this.shouldMangle(source)) {
127
+ continue;
128
+ }
129
+ sourceByOutput.set(output, source);
130
+ }
131
+ // A blocked endpoint in a cached rename chain makes every predecessor
132
+ // unsafe too, since its output would collide with the retained source.
133
+ const pending = [...this.unavailableOutputs];
134
+ for (const output of pending) {
135
+ const source = sourceByOutput.get(output);
136
+ if (source === undefined || this.blockedCacheSources.has(source)) {
137
+ continue;
138
+ }
139
+ this.blockedCacheSources.add(source);
140
+ if (!this.unavailableOutputs.has(source)) {
141
+ this.unavailableOutputs.add(source);
142
+ pending.push(source);
143
+ }
144
+ }
145
+ }
146
+ mangle(name) {
147
+ if (!this.shouldMangle(name)) {
148
+ return name;
149
+ }
150
+ const cached = this.cache.get(name);
151
+ if (cached !== undefined || this.cache.has(name)) {
152
+ return cached;
153
+ }
154
+ let mangled = this.debugName(name);
155
+ if (mangled === null) {
156
+ mangled = this.nextGeneratedName();
157
+ }
158
+ this.cache.set(name, mangled);
159
+ this.emittedNames.add(mangled);
160
+ return mangled;
161
+ }
162
+ canSelect(name) {
163
+ if (this.reserved.has(name)) {
164
+ return false;
165
+ }
166
+ if (this.onlyCache) {
167
+ return this.cache.has(name);
168
+ }
169
+ return !NUMERIC_PROPERTY_RE.test(name);
170
+ }
171
+ debugName(name) {
172
+ if (this.debugSuffix === null) {
173
+ return null;
174
+ }
175
+ const candidate = `_$${name}$${this.debugSuffix}_`;
176
+ return this.isAvailableOutput(candidate) ? candidate : null;
177
+ }
178
+ matchesRegex(name) {
179
+ if (this.regex === null) {
180
+ return true;
181
+ }
182
+ this.regex.lastIndex = 0;
183
+ return this.regex.test(name);
184
+ }
185
+ nextGeneratedName() {
186
+ for (let attempt = 0; attempt < MAX_GENERATOR_ATTEMPTS; attempt++) {
187
+ const candidate = this.nameGenerator(this.nameIndex++);
188
+ if (typeof candidate !== 'string') {
189
+ throw new TypeError('mangleProperties.nameGenerator must return a string');
190
+ }
191
+ if (this.isAvailableOutput(candidate)) {
192
+ return candidate;
193
+ }
194
+ }
195
+ throw new Error('mangleProperties.nameGenerator did not produce an available name');
196
+ }
197
+ shouldMangle(name) {
198
+ if (this.blockedCacheSources.has(name)) {
199
+ return false;
200
+ }
201
+ if (this.onlyAnnotated && !this.annotated.has(name)) {
202
+ return false;
203
+ }
204
+ if (!this.matchesRegex(name) && !this.annotated.has(name)) {
205
+ return false;
206
+ }
207
+ if (this.reserved.has(name)) {
208
+ return false;
209
+ }
210
+ return this.cache.has(name) || this.candidates.has(name);
211
+ }
212
+ isAvailableOutput(name) {
213
+ return (!SYNTAX_SENSITIVE_GENERATED_NAMES.has(name) &&
214
+ !NUMERIC_PROPERTY_RE.test(name) &&
215
+ !this.emittedNames.has(name) &&
216
+ !this.reserved.has(name) &&
217
+ !this.unavailableOutputs.has(name));
218
+ }
219
+ }
220
+ const prepareVisitor = {
221
+ enter(path, state) {
222
+ if (isKeyedProperty(path.node)) {
223
+ prepareKeyedProperty(path, state);
224
+ return;
225
+ }
226
+ if (t.isMemberExpression(path.node) || t.isOptionalMemberExpression(path.node)) {
227
+ prepareMember(path, state);
228
+ }
229
+ },
230
+ };
231
+ const collectVisitor = {
232
+ enter(path, state) {
233
+ if (isKeyedProperty(path.node)) {
234
+ collectKeyedProperty(path, state);
235
+ return;
236
+ }
237
+ if (t.isMemberExpression(path.node) || t.isOptionalMemberExpression(path.node)) {
238
+ collectMember(path, state);
239
+ return;
240
+ }
241
+ if (t.isBinaryExpression(path.node) && path.node.operator === 'in') {
242
+ if (t.isExpression(path.node.left)) {
243
+ collectTerminalNames(path.node.left, path.scope, path, state);
244
+ }
245
+ return;
246
+ }
247
+ if ((t.isCallExpression(path.node) || t.isOptionalCallExpression(path.node)) &&
248
+ isObjectDefinePropertyCall(path)) {
249
+ const [, key] = path.node.arguments;
250
+ if (key && t.isExpression(key)) {
251
+ collectTerminalNames(key, path.scope, path, state);
252
+ }
253
+ return;
254
+ }
255
+ if (t.isStringLiteral(path.node) && hasAnnotation(path.node, PROPERTY_KEY_ANNOTATION)) {
256
+ state.addCandidate(path.node.value);
257
+ }
258
+ },
259
+ };
260
+ const rewriteVisitor = {
261
+ enter(path, state) {
262
+ if (isKeyedProperty(path.node)) {
263
+ rewriteKeyedProperty(path, state);
264
+ return;
265
+ }
266
+ if (t.isMemberExpression(path.node) || t.isOptionalMemberExpression(path.node)) {
267
+ rewriteMember(path, state);
268
+ return;
269
+ }
270
+ if (t.isBinaryExpression(path.node) && path.node.operator === 'in') {
271
+ if (t.isExpression(path.node.left)) {
272
+ path.node.left = rewriteTerminalNames(path.node.left, path.scope, path, state);
273
+ }
274
+ return;
275
+ }
276
+ if ((t.isCallExpression(path.node) || t.isOptionalCallExpression(path.node)) &&
277
+ isObjectDefinePropertyCall(path)) {
278
+ const [, key] = path.node.arguments;
279
+ if (key && t.isExpression(key)) {
280
+ path.node.arguments[1] = rewriteTerminalNames(key, path.scope, path, state);
281
+ }
282
+ return;
283
+ }
284
+ if (t.isStringLiteral(path.node) &&
285
+ hasAnnotation(path.node, PROPERTY_KEY_ANNOTATION) &&
286
+ !state.rewrittenKeyAnnotations.has(path.node)) {
287
+ rewriteString(path.node, state);
288
+ }
289
+ },
290
+ };
291
+ function prepareKeyedProperty(path, state) {
292
+ const { node } = path;
293
+ if (isSemanticKey(node)) {
294
+ const name = plainKeyName(node.key);
295
+ if (name !== null) {
296
+ state.reserveName(name);
297
+ }
298
+ }
299
+ if (state.keepQuoted) {
300
+ protectQuotedKey(path, state);
301
+ }
302
+ if (hasAnnotation(node, MANGLE_PROPERTY_ANNOTATION) ||
303
+ hasAnnotation(node.key, MANGLE_PROPERTY_ANNOTATION)) {
304
+ addAnnotatedKey(path, state);
305
+ }
306
+ }
307
+ function prepareMember(path, state) {
308
+ const { node } = path;
309
+ if (state.keepQuoted && node.computed && t.isExpression(node.property)) {
310
+ protectTerminalNames(node.property, path.scope, path, state);
311
+ }
312
+ if (memberHasMangleAnnotation(path)) {
313
+ addAnnotatedMember(path, state);
314
+ }
315
+ }
316
+ function protectQuotedKey(path, state) {
317
+ const { node } = path;
318
+ if (node.computed) {
319
+ if (t.isExpression(node.key)) {
320
+ protectTerminalNames(node.key, path.scope, path, state);
321
+ }
322
+ }
323
+ else if (t.isStringLiteral(node.key)) {
324
+ protectName(node.key.value, state);
325
+ }
326
+ }
327
+ function protectTerminalNames(node, scope, referencePath, state) {
328
+ forEachTerminalName(node, scope, referencePath, (name) => protectName(name, state));
329
+ }
330
+ function protectName(name, state) {
331
+ if (state.keepQuoted === true) {
332
+ state.reserveName(name);
333
+ }
334
+ else {
335
+ state.retainName(name);
336
+ }
337
+ }
338
+ function addAnnotatedKey(path, state) {
339
+ const { node } = path;
340
+ if (node.computed) {
341
+ if (t.isExpression(node.key)) {
342
+ forEachTerminalName(node.key, path.scope, path, (name) => state.annotated.add(name));
343
+ }
344
+ return;
345
+ }
346
+ const name = plainKeyName(node.key);
347
+ if (name !== null) {
348
+ state.annotated.add(name);
349
+ }
350
+ }
351
+ function addAnnotatedMember(path, state) {
352
+ const { node } = path;
353
+ if (node.computed) {
354
+ if (t.isExpression(node.property)) {
355
+ forEachTerminalName(node.property, path.scope, path, (name) => state.annotated.add(name));
356
+ }
357
+ }
358
+ else if (t.isIdentifier(node.property)) {
359
+ state.annotated.add(node.property.name);
360
+ }
361
+ }
362
+ function collectKeyedProperty(path, state) {
363
+ const { node } = path;
364
+ if (node.computed) {
365
+ if (!state.keepQuoted && t.isExpression(node.key)) {
366
+ collectTerminalNames(node.key, path.scope, path, state);
367
+ }
368
+ return;
369
+ }
370
+ if (state.keepQuoted && t.isStringLiteral(node.key)) {
371
+ return;
372
+ }
373
+ const name = plainKeyName(node.key);
374
+ if (name !== null) {
375
+ state.addCandidate(name);
376
+ }
377
+ }
378
+ function collectMember(path, state) {
379
+ const { node } = path;
380
+ if (node.computed) {
381
+ if (!state.keepQuoted && t.isExpression(node.property)) {
382
+ collectTerminalNames(node.property, path.scope, path, state);
383
+ }
384
+ return;
385
+ }
386
+ if (!state.undeclared && memberRootIsUndeclared(path)) {
387
+ if (t.isIdentifier(node.property)) {
388
+ state.skipUndeclaredName(node.property.name);
389
+ }
390
+ return;
391
+ }
392
+ if (t.isIdentifier(node.property)) {
393
+ state.addCandidate(node.property.name);
394
+ }
395
+ }
396
+ function collectTerminalNames(node, scope, referencePath, state) {
397
+ forEachTerminalName(node, scope, referencePath, (name) => state.addCandidate(name));
398
+ }
399
+ function rewriteKeyedProperty(path, state) {
400
+ const { node } = path;
401
+ if (node.computed) {
402
+ if (!state.keepQuoted && t.isExpression(node.key)) {
403
+ node.key = rewriteTerminalNames(node.key, path.scope, path, state);
404
+ }
405
+ return;
406
+ }
407
+ if (state.keepQuoted && t.isStringLiteral(node.key)) {
408
+ return;
409
+ }
410
+ const name = plainKeyName(node.key);
411
+ if (name === null) {
412
+ return;
413
+ }
414
+ const mangled = state.mangle(name);
415
+ if (mangled === name) {
416
+ return;
417
+ }
418
+ node.key = t.inheritsComments(t.stringLiteral(mangled), node.key);
419
+ node.computed = false;
420
+ if (t.isObjectProperty(node)) {
421
+ node.shorthand = false;
422
+ }
423
+ state.changed = true;
424
+ }
425
+ function rewriteMember(path, state) {
426
+ const { node } = path;
427
+ if (node.computed) {
428
+ if (!state.keepQuoted && t.isExpression(node.property)) {
429
+ node.property = rewriteTerminalNames(node.property, path.scope, path, state);
430
+ }
431
+ return;
432
+ }
433
+ if (!t.isIdentifier(node.property)) {
434
+ return;
435
+ }
436
+ const mangled = state.mangle(node.property.name);
437
+ if (mangled === node.property.name) {
438
+ return;
439
+ }
440
+ // Babel narrows members by `computed`, so both fields need widening.
441
+ const computed = node;
442
+ computed.property = t.inheritsComments(t.stringLiteral(mangled), node.property);
443
+ computed.computed = true;
444
+ state.changed = true;
445
+ }
446
+ function rewriteString(node, state) {
447
+ const mangled = state.mangle(node.value);
448
+ if (mangled !== node.value) {
449
+ node.value = mangled;
450
+ state.changed = true;
451
+ }
452
+ }
453
+ function forEachTerminalName(node, scope, referencePath, visit) {
454
+ const name = evaluatedPropertyName(node, scope, referencePath);
455
+ if (name !== null) {
456
+ visit(name);
457
+ return;
458
+ }
459
+ if (t.isSequenceExpression(node)) {
460
+ forEachTerminalName(node.expressions.at(-1), scope, referencePath, visit);
461
+ }
462
+ else if (t.isConditionalExpression(node)) {
463
+ forEachTerminalName(node.consequent, scope, referencePath, visit);
464
+ forEachTerminalName(node.alternate, scope, referencePath, visit);
465
+ }
466
+ else if (t.isLogicalExpression(node)) {
467
+ forEachTerminalName(node.right, scope, referencePath, visit);
468
+ }
469
+ else if (t.isParenthesizedExpression(node)) {
470
+ forEachTerminalName(node.expression, scope, referencePath, visit);
471
+ }
472
+ }
473
+ function rewriteTerminalNames(node, scope, referencePath, state) {
474
+ const name = evaluatedPropertyName(node, scope, referencePath);
475
+ if (name !== null) {
476
+ const mangled = state.mangle(name);
477
+ if (mangled === name) {
478
+ return node;
479
+ }
480
+ state.changed = true;
481
+ if (t.isStringLiteral(node)) {
482
+ state.rewrittenKeyAnnotations.add(node);
483
+ node.value = mangled;
484
+ return node;
485
+ }
486
+ return t.inheritsComments(t.stringLiteral(mangled), node);
487
+ }
488
+ if (t.isSequenceExpression(node)) {
489
+ const last = node.expressions.length - 1;
490
+ node.expressions[last] = rewriteTerminalNames(node.expressions[last], scope, referencePath, state);
491
+ }
492
+ else if (t.isConditionalExpression(node)) {
493
+ node.consequent = rewriteTerminalNames(node.consequent, scope, referencePath, state);
494
+ node.alternate = rewriteTerminalNames(node.alternate, scope, referencePath, state);
495
+ }
496
+ else if (t.isLogicalExpression(node)) {
497
+ node.right = rewriteTerminalNames(node.right, scope, referencePath, state);
498
+ }
499
+ else if (t.isParenthesizedExpression(node)) {
500
+ node.expression = rewriteTerminalNames(node.expression, scope, referencePath, state);
501
+ }
502
+ return node;
503
+ }
504
+ function evaluatedPropertyName(node, scope, referencePath) {
505
+ const result = evaluateConstant(node, scope, referencePath);
506
+ if (!result.known || isOpaque(result.value) || typeof result.value === 'symbol') {
507
+ return null;
508
+ }
509
+ return String(result.value);
510
+ }
511
+ function memberRootIsUndeclared(path) {
512
+ const root = memberRootIdentifier(path.node);
513
+ return root !== null && path.scope.getBinding(root.name) === undefined;
514
+ }
515
+ function memberRootIdentifier(node) {
516
+ let root = node.object;
517
+ while (true) {
518
+ if (t.isMemberExpression(root) || t.isOptionalMemberExpression(root)) {
519
+ root = root.object;
520
+ }
521
+ else if (t.isCallExpression(root) || t.isOptionalCallExpression(root)) {
522
+ root = root.callee;
523
+ }
524
+ else if (t.isNewExpression(root)) {
525
+ root = root.callee;
526
+ }
527
+ else if (t.isTaggedTemplateExpression(root)) {
528
+ root = root.tag;
529
+ }
530
+ else if (t.isParenthesizedExpression(root)) {
531
+ root = root.expression;
532
+ }
533
+ else {
534
+ break;
535
+ }
536
+ }
537
+ return t.isIdentifier(root) ? root : null;
538
+ }
539
+ function memberHasMangleAnnotation(path) {
540
+ if (hasAnnotation(path.node, MANGLE_PROPERTY_ANNOTATION) ||
541
+ hasAnnotation(path.node.object, MANGLE_PROPERTY_ANNOTATION) ||
542
+ hasAnnotation(path.node.property, MANGLE_PROPERTY_ANNOTATION)) {
543
+ return true;
544
+ }
545
+ let current = path;
546
+ while (current.parentPath) {
547
+ const parent = current.parentPath;
548
+ if (parent.isMemberExpression() || parent.isOptionalMemberExpression()) {
549
+ return false;
550
+ }
551
+ if (parent.isExpressionStatement() && parent.node.expression === current.node) {
552
+ return hasAnnotation(parent.node, MANGLE_PROPERTY_ANNOTATION);
553
+ }
554
+ if (!isTransparentAnnotationParent(parent, current.node)) {
555
+ return false;
556
+ }
557
+ current = parent;
558
+ }
559
+ return false;
560
+ }
561
+ function isTransparentAnnotationParent(path, child) {
562
+ const { node } = path;
563
+ return ((t.isAssignmentExpression(node) && node.left === child) ||
564
+ (t.isCallExpression(node) && node.callee === child) ||
565
+ (t.isOptionalCallExpression(node) && node.callee === child) ||
566
+ (t.isParenthesizedExpression(node) && node.expression === child) ||
567
+ (t.isUnaryExpression(node) && node.argument === child) ||
568
+ (t.isUpdateExpression(node) && node.argument === child));
569
+ }
570
+ function hasAnnotation(node, annotation) {
571
+ return (node.leadingComments?.some((comment) => comment.value.includes(`@${annotation}`) || comment.value.includes(`#${annotation}`)) ?? false);
572
+ }
573
+ function isObjectDefinePropertyCall(path) {
574
+ const { node } = path;
575
+ const { callee } = node;
576
+ if ((!t.isMemberExpression(callee) && !t.isOptionalMemberExpression(callee)) ||
577
+ !t.isIdentifier(callee.object, { name: 'Object' })) {
578
+ return false;
579
+ }
580
+ if (!callee.computed) {
581
+ return t.isIdentifier(callee.property, { name: 'defineProperty' });
582
+ }
583
+ return (t.isExpression(callee.property) &&
584
+ evaluatedPropertyName(callee.property, path.scope, path) === 'defineProperty');
585
+ }
586
+ function isSemanticKey(node) {
587
+ return ((t.isClassMethod(node) && node.kind === 'constructor') ||
588
+ (t.isObjectProperty(node) && !node.computed && !node.shorthand && isProtoKey(node.key)));
589
+ }
590
+ function isKeyedProperty(node) {
591
+ return (t.isClassAccessorProperty(node) ||
592
+ t.isClassMethod(node) ||
593
+ t.isClassProperty(node) ||
594
+ t.isObjectMethod(node) ||
595
+ t.isObjectProperty(node));
596
+ }
597
+ function plainKeyName(key) {
598
+ if (t.isIdentifier(key)) {
599
+ return key.name;
600
+ }
601
+ if (t.isStringLiteral(key)) {
602
+ return key.value;
603
+ }
604
+ if (t.isBooleanLiteral(key)) {
605
+ return String(key.value);
606
+ }
607
+ if (t.isNullLiteral(key)) {
608
+ return 'null';
609
+ }
610
+ if (t.isNumericLiteral(key) || t.isBigIntLiteral(key)) {
611
+ return String(key.value);
612
+ }
613
+ return null;
614
+ }
615
+ function debugSuffix(debug) {
616
+ if (debug === undefined || debug === false) {
617
+ return null;
618
+ }
619
+ return debug === true ? '' : debug;
620
+ }
621
+ function compileRegex(regex) {
622
+ if (regex === undefined) {
623
+ return null;
624
+ }
625
+ if (typeof regex === 'string') {
626
+ // Terser accepts legacy-mode pattern strings; forcing `u` changes their grammar.
627
+ // oxlint-disable-next-line require-unicode-regexp
628
+ return new RegExp(regex);
629
+ }
630
+ return new RegExp(regex.source, regex.flags);
631
+ }
632
+ const BUILTIN_OBJECTS = [
633
+ Object,
634
+ Array,
635
+ Function,
636
+ Number,
637
+ String,
638
+ Boolean,
639
+ BigInt,
640
+ Symbol,
641
+ Error,
642
+ AggregateError,
643
+ EvalError,
644
+ RangeError,
645
+ ReferenceError,
646
+ SyntaxError,
647
+ TypeError,
648
+ URIError,
649
+ Math,
650
+ Date,
651
+ RegExp,
652
+ JSON,
653
+ ArrayBuffer,
654
+ SharedArrayBuffer,
655
+ DataView,
656
+ Atomics,
657
+ Float32Array,
658
+ Float64Array,
659
+ Int8Array,
660
+ Int16Array,
661
+ Int32Array,
662
+ BigInt64Array,
663
+ Uint8Array,
664
+ Uint8ClampedArray,
665
+ Uint16Array,
666
+ Uint32Array,
667
+ BigUint64Array,
668
+ Map,
669
+ Set,
670
+ WeakMap,
671
+ WeakSet,
672
+ WeakRef,
673
+ FinalizationRegistry,
674
+ Promise,
675
+ Proxy,
676
+ Reflect,
677
+ Intl,
678
+ WebAssembly,
679
+ ];
680
+ let cachedBuiltinProperties;
681
+ function builtinProperties() {
682
+ cachedBuiltinProperties ??= collectBuiltinProperties();
683
+ return cachedBuiltinProperties;
684
+ }
685
+ function collectBuiltinProperties() {
686
+ const names = new Set(DOM_PROPERTIES);
687
+ for (const name of ['null', 'true', 'false', 'NaN', 'Infinity', '-Infinity', 'undefined']) {
688
+ names.add(name);
689
+ }
690
+ for (const object of BUILTIN_OBJECTS) {
691
+ for (const name of Object.getOwnPropertyNames(object)) {
692
+ names.add(name);
693
+ }
694
+ const prototype = 'prototype' in object ? object.prototype : null;
695
+ if (prototype && (typeof prototype === 'object' || typeof prototype === 'function')) {
696
+ for (const name of Object.getOwnPropertyNames(prototype)) {
697
+ names.add(name);
698
+ }
699
+ }
700
+ }
701
+ return names;
702
+ }
703
+ //# sourceMappingURL=mangle-properties.js.map