js-confuser 1.7.3 → 2.0.0-alpha.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.
Files changed (269) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +6 -4
  2. package/CHANGELOG.md +70 -0
  3. package/Migration.md +57 -0
  4. package/README.md +23 -929
  5. package/dist/constants.js +65 -14
  6. package/dist/index.js +108 -160
  7. package/dist/obfuscator.js +316 -118
  8. package/dist/options.js +1 -119
  9. package/dist/order.js +30 -30
  10. package/dist/presets.js +47 -45
  11. package/dist/probability.js +25 -32
  12. package/dist/templates/bufferToStringTemplate.js +9 -0
  13. package/dist/templates/deadCodeTemplates.js +9 -0
  14. package/dist/templates/getGlobalTemplate.js +19 -0
  15. package/dist/templates/integrityTemplate.js +30 -0
  16. package/dist/templates/setFunctionLengthTemplate.js +9 -0
  17. package/dist/templates/stringCompressionTemplate.js +10 -0
  18. package/dist/templates/tamperProtectionTemplates.js +21 -0
  19. package/dist/templates/template.js +199 -184
  20. package/dist/transforms/astScrambler.js +100 -0
  21. package/dist/transforms/calculator.js +70 -127
  22. package/dist/transforms/controlFlowFlattening.js +1182 -0
  23. package/dist/transforms/deadCode.js +62 -587
  24. package/dist/transforms/dispatcher.js +300 -313
  25. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +88 -189
  26. package/dist/transforms/extraction/objectExtraction.js +131 -215
  27. package/dist/transforms/finalizer.js +56 -59
  28. package/dist/transforms/flatten.js +275 -276
  29. package/dist/transforms/functionOutlining.js +230 -0
  30. package/dist/transforms/identifier/globalConcealing.js +214 -135
  31. package/dist/transforms/identifier/movedDeclarations.js +167 -91
  32. package/dist/transforms/identifier/renameVariables.js +239 -193
  33. package/dist/transforms/lock/integrity.js +61 -184
  34. package/dist/transforms/lock/lock.js +261 -387
  35. package/dist/transforms/minify.js +431 -436
  36. package/dist/transforms/opaquePredicates.js +65 -118
  37. package/dist/transforms/pack.js +160 -0
  38. package/dist/transforms/plugin.js +179 -0
  39. package/dist/transforms/preparation.js +261 -173
  40. package/dist/transforms/renameLabels.js +132 -56
  41. package/dist/transforms/rgf.js +140 -267
  42. package/dist/transforms/shuffle.js +52 -145
  43. package/dist/transforms/string/encoding.js +44 -175
  44. package/dist/transforms/string/stringCompression.js +79 -155
  45. package/dist/transforms/string/stringConcealing.js +189 -225
  46. package/dist/transforms/string/stringEncoding.js +32 -40
  47. package/dist/transforms/string/stringSplitting.js +54 -55
  48. package/dist/transforms/variableMasking.js +232 -0
  49. package/dist/utils/ControlObject.js +125 -0
  50. package/dist/utils/IntGen.js +46 -0
  51. package/dist/utils/NameGen.js +106 -0
  52. package/dist/utils/ast-utils.js +560 -0
  53. package/dist/utils/function-utils.js +56 -0
  54. package/dist/utils/gen-utils.js +48 -0
  55. package/dist/utils/node.js +77 -0
  56. package/dist/utils/object-utils.js +21 -0
  57. package/dist/utils/random-utils.js +91 -0
  58. package/dist/utils/static-utils.js +64 -0
  59. package/dist/validateOptions.js +122 -0
  60. package/index.d.ts +1 -17
  61. package/package.json +27 -22
  62. package/src/constants.ts +139 -82
  63. package/src/index.ts +70 -165
  64. package/src/obfuscationResult.ts +43 -0
  65. package/src/obfuscator.ts +328 -135
  66. package/src/options.ts +149 -658
  67. package/src/order.ts +14 -14
  68. package/src/presets.ts +39 -34
  69. package/src/probability.ts +21 -36
  70. package/src/templates/bufferToStringTemplate.ts +57 -0
  71. package/src/templates/deadCodeTemplates.ts +1185 -0
  72. package/src/templates/getGlobalTemplate.ts +72 -0
  73. package/src/templates/integrityTemplate.ts +69 -0
  74. package/src/templates/setFunctionLengthTemplate.ts +11 -0
  75. package/src/templates/stringCompressionTemplate.ts +42 -0
  76. package/src/templates/tamperProtectionTemplates.ts +116 -0
  77. package/src/templates/template.ts +149 -157
  78. package/src/transforms/astScrambler.ts +99 -0
  79. package/src/transforms/calculator.ts +96 -226
  80. package/src/transforms/controlFlowFlattening.ts +1594 -0
  81. package/src/transforms/deadCode.ts +85 -676
  82. package/src/transforms/dispatcher.ts +431 -640
  83. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -295
  84. package/src/transforms/extraction/objectExtraction.ts +160 -333
  85. package/src/transforms/finalizer.ts +63 -64
  86. package/src/transforms/flatten.ts +439 -557
  87. package/src/transforms/functionOutlining.ts +225 -0
  88. package/src/transforms/identifier/globalConcealing.ts +255 -266
  89. package/src/transforms/identifier/movedDeclarations.ts +228 -142
  90. package/src/transforms/identifier/renameVariables.ts +250 -271
  91. package/src/transforms/lock/integrity.ts +85 -263
  92. package/src/transforms/lock/lock.ts +338 -579
  93. package/src/transforms/minify.ts +523 -663
  94. package/src/transforms/opaquePredicates.ts +90 -229
  95. package/src/transforms/pack.ts +195 -0
  96. package/src/transforms/plugin.ts +185 -0
  97. package/src/transforms/preparation.ts +337 -231
  98. package/src/transforms/renameLabels.ts +176 -77
  99. package/src/transforms/rgf.ts +293 -424
  100. package/src/transforms/shuffle.ts +80 -254
  101. package/src/transforms/string/encoding.ts +20 -126
  102. package/src/transforms/string/stringCompression.ts +117 -307
  103. package/src/transforms/string/stringConcealing.ts +254 -342
  104. package/src/transforms/string/stringEncoding.ts +28 -47
  105. package/src/transforms/string/stringSplitting.ts +61 -75
  106. package/src/transforms/variableMasking.ts +257 -0
  107. package/src/utils/ControlObject.ts +141 -0
  108. package/src/utils/IntGen.ts +33 -0
  109. package/src/utils/NameGen.ts +106 -0
  110. package/src/utils/ast-utils.ts +667 -0
  111. package/src/utils/function-utils.ts +50 -0
  112. package/src/utils/gen-utils.ts +48 -0
  113. package/src/utils/node.ts +78 -0
  114. package/src/utils/object-utils.ts +21 -0
  115. package/src/utils/random-utils.ts +79 -0
  116. package/src/utils/static-utils.ts +66 -0
  117. package/src/validateOptions.ts +256 -0
  118. package/tsconfig.json +13 -8
  119. package/babel.config.js +0 -12
  120. package/dev.js +0 -8
  121. package/dist/compiler.js +0 -34
  122. package/dist/parser.js +0 -59
  123. package/dist/precedence.js +0 -66
  124. package/dist/templates/bufferToString.js +0 -129
  125. package/dist/templates/core.js +0 -35
  126. package/dist/templates/crash.js +0 -28
  127. package/dist/templates/es5.js +0 -137
  128. package/dist/templates/functionLength.js +0 -34
  129. package/dist/templates/globals.js +0 -9
  130. package/dist/transforms/antiTooling.js +0 -88
  131. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +0 -1287
  132. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +0 -131
  133. package/dist/transforms/es5/antiClass.js +0 -164
  134. package/dist/transforms/es5/antiDestructuring.js +0 -193
  135. package/dist/transforms/es5/antiES6Object.js +0 -185
  136. package/dist/transforms/es5/antiSpreadOperator.js +0 -35
  137. package/dist/transforms/es5/antiTemplate.js +0 -66
  138. package/dist/transforms/es5/es5.js +0 -123
  139. package/dist/transforms/extraction/classExtraction.js +0 -83
  140. package/dist/transforms/identifier/globalAnalysis.js +0 -83
  141. package/dist/transforms/identifier/variableAnalysis.js +0 -104
  142. package/dist/transforms/lock/antiDebug.js +0 -76
  143. package/dist/transforms/stack.js +0 -349
  144. package/dist/transforms/transform.js +0 -372
  145. package/dist/traverse.js +0 -110
  146. package/dist/util/compare.js +0 -145
  147. package/dist/util/gen.js +0 -564
  148. package/dist/util/guard.js +0 -14
  149. package/dist/util/identifiers.js +0 -355
  150. package/dist/util/insert.js +0 -362
  151. package/dist/util/math.js +0 -19
  152. package/dist/util/object.js +0 -40
  153. package/dist/util/random.js +0 -156
  154. package/dist/util/scope.js +0 -20
  155. package/docs/ControlFlowFlattening.md +0 -595
  156. package/docs/Countermeasures.md +0 -70
  157. package/docs/ES5.md +0 -197
  158. package/docs/Integrity.md +0 -82
  159. package/docs/RGF.md +0 -424
  160. package/docs/RenameVariables.md +0 -116
  161. package/docs/TamperProtection.md +0 -100
  162. package/docs/Template.md +0 -117
  163. package/samples/example.js +0 -15
  164. package/samples/high.js +0 -1
  165. package/samples/input.js +0 -3
  166. package/samples/javascriptobfuscator.com.js +0 -8
  167. package/samples/jscrambler_advanced.js +0 -1894
  168. package/samples/jscrambler_light.js +0 -1134
  169. package/samples/low.js +0 -1
  170. package/samples/medium.js +0 -1
  171. package/samples/obfuscator.io.js +0 -1686
  172. package/samples/preemptive.com.js +0 -16
  173. package/src/compiler.ts +0 -35
  174. package/src/parser.ts +0 -49
  175. package/src/precedence.ts +0 -61
  176. package/src/templates/bufferToString.ts +0 -136
  177. package/src/templates/core.ts +0 -29
  178. package/src/templates/crash.ts +0 -23
  179. package/src/templates/es5.ts +0 -131
  180. package/src/templates/functionLength.ts +0 -32
  181. package/src/templates/globals.ts +0 -3
  182. package/src/transforms/antiTooling.ts +0 -102
  183. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +0 -2153
  184. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
  185. package/src/transforms/es5/antiClass.ts +0 -276
  186. package/src/transforms/es5/antiDestructuring.ts +0 -294
  187. package/src/transforms/es5/antiES6Object.ts +0 -267
  188. package/src/transforms/es5/antiSpreadOperator.ts +0 -56
  189. package/src/transforms/es5/antiTemplate.ts +0 -98
  190. package/src/transforms/es5/es5.ts +0 -149
  191. package/src/transforms/extraction/classExtraction.ts +0 -168
  192. package/src/transforms/identifier/globalAnalysis.ts +0 -102
  193. package/src/transforms/identifier/variableAnalysis.ts +0 -118
  194. package/src/transforms/lock/antiDebug.ts +0 -112
  195. package/src/transforms/stack.ts +0 -557
  196. package/src/transforms/transform.ts +0 -441
  197. package/src/traverse.ts +0 -120
  198. package/src/types.ts +0 -133
  199. package/src/util/compare.ts +0 -181
  200. package/src/util/gen.ts +0 -651
  201. package/src/util/guard.ts +0 -17
  202. package/src/util/identifiers.ts +0 -494
  203. package/src/util/insert.ts +0 -419
  204. package/src/util/math.ts +0 -15
  205. package/src/util/object.ts +0 -39
  206. package/src/util/random.ts +0 -221
  207. package/src/util/scope.ts +0 -21
  208. package/test/code/Cash.src.js +0 -1011
  209. package/test/code/Cash.test.ts +0 -132
  210. package/test/code/Dynamic.src.js +0 -118
  211. package/test/code/Dynamic.test.ts +0 -49
  212. package/test/code/ES6.src.js +0 -235
  213. package/test/code/ES6.test.ts +0 -42
  214. package/test/code/NewFeatures.test.ts +0 -19
  215. package/test/code/StrictMode.src.js +0 -65
  216. package/test/code/StrictMode.test.js +0 -37
  217. package/test/compare.test.ts +0 -104
  218. package/test/index.test.ts +0 -249
  219. package/test/options.test.ts +0 -150
  220. package/test/presets.test.ts +0 -22
  221. package/test/probability.test.ts +0 -44
  222. package/test/templates/template.test.ts +0 -224
  223. package/test/transforms/antiTooling.test.ts +0 -52
  224. package/test/transforms/calculator.test.ts +0 -78
  225. package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +0 -1274
  226. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +0 -192
  227. package/test/transforms/deadCode.test.ts +0 -85
  228. package/test/transforms/dispatcher.test.ts +0 -457
  229. package/test/transforms/es5/antiClass.test.ts +0 -427
  230. package/test/transforms/es5/antiDestructuring.test.ts +0 -157
  231. package/test/transforms/es5/antiES6Object.test.ts +0 -245
  232. package/test/transforms/es5/antiTemplate.test.ts +0 -116
  233. package/test/transforms/es5/es5.test.ts +0 -110
  234. package/test/transforms/extraction/classExtraction.test.ts +0 -86
  235. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +0 -200
  236. package/test/transforms/extraction/objectExtraction.test.ts +0 -491
  237. package/test/transforms/flatten.test.ts +0 -721
  238. package/test/transforms/hexadecimalNumbers.test.ts +0 -62
  239. package/test/transforms/identifier/globalConcealing.test.ts +0 -142
  240. package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
  241. package/test/transforms/identifier/renameVariables.test.ts +0 -695
  242. package/test/transforms/lock/antiDebug.test.ts +0 -66
  243. package/test/transforms/lock/browserLock.test.ts +0 -129
  244. package/test/transforms/lock/countermeasures.test.ts +0 -100
  245. package/test/transforms/lock/integrity.test.ts +0 -161
  246. package/test/transforms/lock/lock.test.ts +0 -204
  247. package/test/transforms/lock/osLock.test.ts +0 -312
  248. package/test/transforms/lock/selfDefending.test.ts +0 -68
  249. package/test/transforms/lock/tamperProtection.test.ts +0 -336
  250. package/test/transforms/minify.test.ts +0 -575
  251. package/test/transforms/opaquePredicates.test.ts +0 -43
  252. package/test/transforms/preparation.test.ts +0 -157
  253. package/test/transforms/renameLabels.test.ts +0 -95
  254. package/test/transforms/rgf.test.ts +0 -378
  255. package/test/transforms/shuffle.test.ts +0 -135
  256. package/test/transforms/stack.test.ts +0 -573
  257. package/test/transforms/string/stringCompression.test.ts +0 -120
  258. package/test/transforms/string/stringConcealing.test.ts +0 -299
  259. package/test/transforms/string/stringEncoding.test.ts +0 -95
  260. package/test/transforms/string/stringSplitting.test.ts +0 -135
  261. package/test/transforms/transform.test.ts +0 -66
  262. package/test/traverse.test.ts +0 -139
  263. package/test/util/compare.test.ts +0 -34
  264. package/test/util/gen.test.ts +0 -121
  265. package/test/util/identifiers.test.ts +0 -253
  266. package/test/util/insert.test.ts +0 -142
  267. package/test/util/math.test.ts +0 -5
  268. package/test/util/random.test.ts +0 -71
  269. /package/dist/{types.js → obfuscationResult.js} +0 -0
@@ -1,355 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.containsLexicallyBoundVariables = containsLexicallyBoundVariables;
7
- exports.getDefiningIdentifier = getDefiningIdentifier;
8
- exports.getFunctionParameters = getFunctionParameters;
9
- exports.getIdentifierInfo = getIdentifierInfo;
10
- exports.isFunctionParameter = isFunctionParameter;
11
- exports.validateChain = validateChain;
12
- var _assert = require("assert");
13
- var _traverse = require("../traverse");
14
- var _insert = require("./insert");
15
- /**
16
- * Ensures the chain (object and parents) are connected.
17
- * @param object
18
- * @param parents
19
- */
20
- function validateChain(object, parents) {
21
- if (!Array.isArray(parents)) {
22
- throw new Error("parents need to be an array");
23
- }
24
- if (!object) {
25
- throw new Error("object must be a node (not null)");
26
- }
27
- if (parents.length > 0) {
28
- if (object == parents[0]) {
29
- throw new Error("parent overlap");
30
- }
31
- if (!Object.values(parents[0]).includes(object)) {
32
- console.log("parents=", parents);
33
- console.log("object=", object);
34
- throw new Error("parents[0] is not connected to object");
35
- }
36
- }
37
- }
38
- function objectPatternCheck(object, parents) {
39
- var objectPatternIndex = parents.findIndex(x => x.type === "ObjectPattern");
40
- if (objectPatternIndex == -1) {
41
- return true;
42
- }
43
- var property = parents[objectPatternIndex].properties.find(property => parents[objectPatternIndex - 2] === property);
44
- if (property.key === (parents[objectPatternIndex - 3] || object)) {
45
- return false;
46
- }
47
- return true;
48
- }
49
-
50
- /**
51
- * Returns detailed information about the given Identifier node.
52
- * @param object
53
- * @param parents
54
- */
55
- function getIdentifierInfo(object, parents) {
56
- if (object.type != "Identifier") {
57
- console.log(object);
58
- throw new Error("object is not an Identifier, its a type=" + object.type);
59
- }
60
- var parent = parents[0] || {};
61
- var isAccessor = parent.type == "MemberExpression" && parent.object != object && parent.property === object && !parent.computed;
62
- var propIndex = parents.findIndex(x => x.type == "Property" || x.type == "MethodDefinition");
63
- var isPropertyKey = propIndex != -1 && parents[propIndex].key == (parents[propIndex - 1] || object) && !parents[propIndex].computed;
64
- var objectPatternIndex = parents.findIndex(x => x.type == "ObjectPattern");
65
- if (objectPatternIndex !== -1 && parents[objectPatternIndex].properties == parents[objectPatternIndex - 1]) {
66
- if (objectPatternIndex - propIndex == 2) {
67
- if (parents[propIndex].value === (parents[propIndex - 1] || object)) {
68
- isPropertyKey = false;
69
- }
70
- }
71
- }
72
- var varIndex = parents.findIndex(x => x.type == "VariableDeclarator");
73
- var isVariableDeclaration = varIndex != -1 && parents[varIndex].id == (parents[varIndex - 1] || object) && parents.find(x => x.type == "VariableDeclaration") && objectPatternCheck(object, parents);
74
- var functionIndex = parents.findIndex(x => (0, _insert.isFunction)(x));
75
-
76
- // Assignment pattern check!
77
- if (isVariableDeclaration) {
78
- var slicedParents = parents.slice(0, functionIndex != -1 ? Math.min(varIndex, functionIndex) : varIndex);
79
- var i = 0;
80
- for (var parent of slicedParents) {
81
- var childNode = slicedParents[i - 1] || object;
82
- if (parent.type === "AssignmentPattern" && parent.right === childNode) {
83
- isVariableDeclaration = false;
84
- break;
85
- }
86
- i++;
87
- }
88
- }
89
- var forIndex = parents.findIndex(x => x.type == "ForStatement");
90
- var isForInitializer = forIndex != -1 && parents[forIndex].init == (parents[forIndex - 1] || object);
91
- var isFunctionDeclaration = functionIndex != -1 && parents[functionIndex].type == "FunctionDeclaration" && parents[functionIndex].id == object;
92
- var isNamedFunctionExpression = functionIndex != -1 && parents[functionIndex].type === "FunctionExpression" && parents[functionIndex].id === object;
93
- var isAFunctionParameter = isFunctionParameter(object, parents);
94
- var isClauseParameter = false;
95
-
96
- // Special case for Catch clauses
97
- var clauseIndex = parents.findIndex(x => x.type == "CatchClause");
98
- if (clauseIndex != -1) {
99
- if (parents[clauseIndex].param == (parents[clauseIndex - 1] || object)) {
100
- isClauseParameter = true;
101
- }
102
- }
103
- var isImportSpecifier = (parent.type == "ImportDefaultSpecifier" || parent.type == "ImportSpecifier") && parent.local == object;
104
- var isFunctionCall = parent.callee == object; // NewExpression and CallExpression
105
-
106
- var assignmentIndex = parents.findIndex(p => p.type === "AssignmentExpression");
107
- var isAssignmentLeft = assignmentIndex !== -1 && parents[assignmentIndex].left === (parents[assignmentIndex - 1] || object) && objectPatternCheck(object, parents);
108
- var isAssignmentValue = assignmentIndex !== -1 && parents[assignmentIndex].right === (parents[assignmentIndex - 1] || object);
109
- var isUpdateExpression = parent.type == "UpdateExpression";
110
- var isClassDeclaration = (parent.type == "ClassDeclaration" || parent.type == "ClassExpression") && parent.id == object;
111
- var isMethodDefinition = parent.type == "MethodDefinition" && parent.key == object && !parent.computed;
112
- var isMetaProperty = parent.type == "MetaProperty";
113
- var isLabel = parent.type == "LabeledStatement" && parent.label == object;
114
-
115
- // Fix 1: Labels are properly identified
116
- if (parent.type == "BreakStatement" || parent.type == "ContinueStatement") {
117
- if (parent.label == object) {
118
- isLabel = true;
119
- }
120
- }
121
- var isDeleteExpression = false;
122
- var deleteIndex = parents.findIndex(x => x.type == "UnaryExpression" && x.operator == "delete");
123
- if (deleteIndex != -1) {
124
- isDeleteExpression = true;
125
- }
126
- var isReferenced = !isAccessor && !isPropertyKey && !isMetaProperty && !isLabel && !object.name.startsWith("0") && !object.name.startsWith("'");
127
- return {
128
- /**
129
- * MemberExpression: `parent.identifier`
130
- */
131
- isAccessor,
132
- /**
133
- * Property: `{identifier: ...}`
134
- */
135
- isPropertyKey,
136
- /**
137
- * `var identifier = ...`
138
- */
139
- isVariableDeclaration,
140
- /**
141
- * `function identifier(){...}`
142
- */
143
- isFunctionDeclaration,
144
- /**
145
- * `function a(identifier){...}`
146
- */
147
- isFunctionParameter: isAFunctionParameter,
148
- /**
149
- * ```js
150
- * try ... catch ( identifier ) {
151
- * ...
152
- * }
153
- * ```
154
- */
155
- isClauseParameter,
156
- /**
157
- * CallExpression: `identifier()`
158
- */
159
- isFunctionCall,
160
- /**
161
- * AssignmentExpression: `identifier = ...`
162
- */
163
- isAssignmentLeft,
164
- /**
165
- * AssignmentExpression (right): `x = identifier`
166
- */
167
- isAssignmentValue,
168
- /**
169
- * UpdateExpression: `identifier++`
170
- */
171
- isUpdateExpression,
172
- /**
173
- * ClassDeclaration `class identifier {...}`
174
- */
175
- isClassDeclaration,
176
- /**
177
- * Method Definition inside a class body
178
- * ```js
179
- * class Rectangle {
180
- * identifier(){...}
181
- *
182
- * get identifier(){...}
183
- * }
184
- * ```
185
- */
186
- isMethodDefinition,
187
- /**
188
- * `new.target` or `yield.input`
189
- */
190
- isMetaProperty,
191
- /**
192
- * LabelStatement: `identifier: for ( var i...)`
193
- */
194
- isLabel,
195
- /**
196
- * ```js
197
- * for (var i=0; ...) {
198
- * ...
199
- * }
200
- * ```
201
- */
202
- isForInitializer,
203
- /**
204
- * ```js
205
- * import identifier from "...";
206
- * import {key as identifier} from "...";
207
- * ```
208
- */
209
- isImportSpecifier,
210
- /**
211
- * ```js
212
- * delete identifier[identifier]
213
- * ```
214
- */
215
- isDeleteExpression: isDeleteExpression,
216
- spec: {
217
- /**
218
- * - `export function identifier()...`
219
- * - `export var identifier = ...`
220
- */
221
- isExported: isVariableDeclaration && parents[3] && parents[3].type == "ExportNamedDeclaration" || isFunctionDeclaration && parents[1] && parents[1].type == "ExportNamedDeclaration",
222
- /**
223
- * Is the Identifier defined, i.e a variable declaration, function declaration, parameter, or class definition
224
- */
225
- isDefined: isVariableDeclaration || isFunctionDeclaration || isNamedFunctionExpression || isAFunctionParameter || isClassDeclaration || isClauseParameter || isMethodDefinition || isImportSpecifier,
226
- /**
227
- * Is the Identifier modified, either by an `AssignmentExpression` or `UpdateExpression`
228
- */
229
- isModified: isAssignmentLeft || isUpdateExpression || isDeleteExpression,
230
- /**
231
- * Is the Identifier referenced as a variable.
232
- *
233
- * - true: `if ( identifier ) {...}`
234
- * - false `if ( obj.identifier ) {...}`
235
- * - false `identifier: for ( var ...)`
236
- * - false `var {identifier: ...}`
237
- * - false `break identifier;`
238
- */
239
- isReferenced: isReferenced
240
- }
241
- };
242
- }
243
- function getDefiningIdentifier(object, parents) {
244
- (0, _assert.ok)(object.type == "Identifier", "must be identifier");
245
- (0, _assert.ok)(typeof object.name === "string");
246
- (0, _assert.ok)(parents[parents.length - 1].type == "Program", "root node must be type Program. Found '" + parents[parents.length - 1].type + "'");
247
- var seen = new Set();
248
- var i = 0;
249
- for (var parent of parents) {
250
- var l;
251
- var bestScore = Infinity;
252
- (0, _traverse.walk)(parent, parents.slice(i + 1), (o, p) => {
253
- // if (p.find((x) => seen.has(x))) {
254
- // return "EXIT";
255
- // }
256
-
257
- if (o.type == "Identifier" && o.name === object.name && o !== object) {
258
- var info = getIdentifierInfo(o, p);
259
- if (info.spec.isDefined) {
260
- var contexts = p.filter(x => (0, _insert.isVarContext)(x));
261
- var definingContext = info.isFunctionDeclaration ? (0, _insert.getVarContext)(p[0], p.slice(1)) : (0, _insert.getVarContext)(o, p);
262
- if (parents.includes(definingContext)) {
263
- var index = contexts.indexOf(definingContext);
264
- if (index < bestScore) {
265
- l = [o, p];
266
- bestScore = index;
267
- }
268
- }
269
- }
270
- }
271
- });
272
- if (l) {
273
- // console.log(l[0].name, "->", l[0], bestScore);
274
-
275
- return l;
276
- }
277
- seen.add(parent);
278
- i++;
279
- }
280
- }
281
- function isFunctionParameter(o, p, c) {
282
- (0, _assert.ok)(o);
283
- (0, _assert.ok)(p);
284
- validateChain(o, p);
285
- if (o.type !== "Identifier") {
286
- return false;
287
- }
288
- var object = p.find(x => (0, _insert.isFunction)(x) && x.params);
289
- if (!object) {
290
- return false;
291
- }
292
- c = c || (0, _insert.getVarContext)(o, p);
293
- if (c === object) {
294
- var pIndex = p.indexOf(object.params);
295
- if (pIndex == -1) {
296
- return false;
297
- }
298
- var param = p[pIndex - 1] || o;
299
- var paramIndex = object.params.indexOf(param);
300
- (0, _assert.ok)(paramIndex !== -1);
301
- var sliced = p.slice(0, pIndex);
302
- var isReferenced = true;
303
- var i = 0;
304
- for (var node of sliced) {
305
- var down = sliced[i - 1] || o;
306
- (0, _assert.ok)(down);
307
- if (node.type) {
308
- if (node.type == "AssignmentPattern" && node.right === down) {
309
- isReferenced = false;
310
- break;
311
- }
312
- if (node.type == "Property" && node.key === down && sliced[i + 2] && sliced[i + 2].type == "ObjectPattern") {
313
- isReferenced = false;
314
- break;
315
- }
316
- }
317
- i++;
318
- }
319
- if (isReferenced) {
320
- return true;
321
- }
322
- }
323
- return false;
324
- }
325
- function getFunctionParameters(object, parents) {
326
- (0, _assert.ok)((0, _insert.isFunction)(object));
327
- (0, _assert.ok)(object.params);
328
- var locations = [];
329
- (0, _traverse.walk)(object.params, [object, ...parents], (o, p) => {
330
- if (o.type == "Identifier") {
331
- if (isFunctionParameter(o, p, object)) {
332
- locations.push([o, p]);
333
- }
334
- }
335
- });
336
- return locations;
337
- }
338
- function containsLexicallyBoundVariables(object, parents) {
339
- var contains = false;
340
- (0, _traverse.walk)(object, parents, (o, p) => {
341
- if (o.type == "VariableDeclaration") {
342
- if (o.kind === "let" || o.kind === "const") {
343
- // Control Flow Flattening changes the lexical block, therefore this is not possible
344
- // Maybe a transformation to remove let
345
- contains = true;
346
- return "EXIT";
347
- }
348
- }
349
- if (o.type == "ClassDeclaration") {
350
- contains = true;
351
- return "EXIT";
352
- }
353
- });
354
- return contains;
355
- }
@@ -1,362 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.append = append;
7
- exports.clone = clone;
8
- exports.computeFunctionLength = computeFunctionLength;
9
- exports.deleteDeclaration = deleteDeclaration;
10
- exports.deleteDirect = deleteDirect;
11
- exports.getAllDefiningContexts = getAllDefiningContexts;
12
- exports.getBlockBody = getBlockBody;
13
- exports.getContexts = getContexts;
14
- exports.getDefiningContext = getDefiningContext;
15
- exports.getFunction = getFunction;
16
- exports.getIndexDirect = getIndexDirect;
17
- exports.getLexContext = getLexContext;
18
- exports.getReferencingContexts = getReferencingContexts;
19
- exports.getVarContext = getVarContext;
20
- exports.isClass = isClass;
21
- exports.isContext = isContext;
22
- exports.isForInitialize = isForInitialize;
23
- exports.isFunction = isFunction;
24
- exports.isLexContext = isLexContext;
25
- exports.isStrictModeFunction = isStrictModeFunction;
26
- exports.isVarContext = isVarContext;
27
- exports.prepend = prepend;
28
- var _assert = require("assert");
29
- var _traverse = require("../traverse");
30
- var _identifiers = require("./identifiers");
31
- function isClass(object) {
32
- return object.type === "ClassDeclaration" || object.type === "ClassExpression";
33
- }
34
-
35
- /**
36
- * - `FunctionDeclaration`
37
- * - `FunctionExpression`
38
- * - `ArrowFunctionExpression`
39
- * @param object
40
- * @returns
41
- */
42
- function isFunction(object) {
43
- return ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"].includes(object && object.type);
44
- }
45
- function isStrictModeFunction(object) {
46
- (0, _assert.ok)(isFunction(object));
47
- return object.body.type === "BlockStatement" && object.body.body[0] && object.body.body[0].type === "ExpressionStatement" && object.body.body[0].directive === "use strict";
48
- }
49
-
50
- /**
51
- * The function context where the object is.
52
- *
53
- * - Determines if async context.
54
- * - Determines variable context.
55
- *
56
- * @param object
57
- * @param parents
58
- */
59
- function getFunction(object, parents) {
60
- return parents.find(x => isFunction(x));
61
- }
62
-
63
- /**
64
- * Refers to the current function or Root node
65
- * @param parents
66
- */
67
- function getVarContext(object, parents) {
68
- var fn = getFunction(object, parents);
69
- if (fn) {
70
- return fn;
71
- }
72
- var top = parents[parents.length - 1] || object;
73
- if (top) {
74
- (0, _assert.ok)(top.type == "Program", "Root node not program, its " + top.type);
75
- return top;
76
- }
77
- throw new Error("Missing root node");
78
- }
79
-
80
- /**
81
- * `Function` or root node
82
- * @param object
83
- * @returns
84
- */
85
- function isVarContext(object) {
86
- return isFunction(object) || object.type == "Program" || object.type == "DoExpression"; // Stage 1
87
- }
88
-
89
- /**
90
- * `Block` or root node
91
- * @param object
92
- * @returns
93
- */
94
- function isLexContext(object) {
95
- return (0, _traverse.isBlock)(object) || object.type == "Program";
96
- }
97
-
98
- /**
99
- * Either a `var context` or `lex context`
100
- * @param object
101
- * @returns
102
- */
103
- function isContext(object) {
104
- return isVarContext(object) || isLexContext(object);
105
- }
106
- function getContexts(object, parents) {
107
- return [object, ...parents].filter(x => isContext(x));
108
- }
109
-
110
- /**
111
- * Refers to the current lexical block or Root node.
112
- * @param parents
113
- */
114
- function getLexContext(object, parents) {
115
- var block = (0, _traverse.getBlock)(object, parents);
116
- if (block) {
117
- return block;
118
- }
119
- var top = parents[parents.length - 1];
120
- if (!top) {
121
- throw new Error("Missing root node");
122
- }
123
- }
124
- function getDefiningContext(o, p) {
125
- (0, _identifiers.validateChain)(o, p);
126
- (0, _assert.ok)(o.type == "Identifier");
127
- var info = (0, _identifiers.getIdentifierInfo)(o, p);
128
- (0, _assert.ok)(info.spec.isDefined);
129
- if (info.isVariableDeclaration) {
130
- var variableDeclaration = p.find(x => x.type == "VariableDeclaration");
131
- (0, _assert.ok)(variableDeclaration);
132
- if (variableDeclaration.kind === "let" || variableDeclaration.kind === "const") {
133
- var context = getVarContext(o, p);
134
- if (context && context.type === "Program") {
135
- return getLexContext(o, p);
136
- }
137
- }
138
- }
139
- if (info.isFunctionDeclaration) {
140
- return getVarContext(p[0], p.slice(1));
141
- }
142
- return getVarContext(o, p);
143
- }
144
-
145
- /**
146
- * A more accurate context finding function.
147
- * @param o Object
148
- * @param p Parents
149
- * @returns Contexts
150
- */
151
- function getAllDefiningContexts(o, p) {
152
- var contexts = [getDefiningContext(o, p)];
153
- var info = (0, _identifiers.getIdentifierInfo)(o, p);
154
- if (info.isFunctionParameter) {
155
- // Get Function
156
- var fn = getFunction(o, p);
157
-
158
- // contexts.push(fn.body);
159
- }
160
- if (info.isClauseParameter) {
161
- var catchClause = p.find(x => x.type === "CatchClause");
162
- if (catchClause) {
163
- return [catchClause];
164
- }
165
- }
166
- return contexts;
167
- }
168
- function getReferencingContexts(o, p, info) {
169
- (0, _identifiers.validateChain)(o, p);
170
- (0, _assert.ok)(o.type == "Identifier");
171
- if (!info) {
172
- info = (0, _identifiers.getIdentifierInfo)(o, p);
173
- }
174
- (0, _assert.ok)(info.spec.isReferenced);
175
- return [getVarContext(o, p), getLexContext(o, p)];
176
- }
177
- function getBlockBody(block) {
178
- if (!block) {
179
- throw new Error("no block body");
180
- }
181
- if (Array.isArray(block)) {
182
- return block;
183
- }
184
- return getBlockBody(block.body);
185
- }
186
- function getIndexDirect(object, parent) {
187
- return Object.keys(parent).find(x => parent[x] == object);
188
- }
189
-
190
- /**
191
- * Attempts to a delete a variable/functions declaration.
192
- * @param object
193
- * @param parents
194
- */
195
- function deleteDeclaration(object, parents) {
196
- (0, _identifiers.validateChain)(object, parents);
197
-
198
- // variables
199
- var list = [object, ...parents];
200
- var declaratorIndex = list.findIndex(x => x.type == "VariableDeclarator");
201
- if (declaratorIndex != -1) {
202
- var declarator = list[declaratorIndex]; // {type: VariableDeclarator, id: Identifier, init: Literal|Expression...}
203
- var declarations = list[declaratorIndex + 1]; // declarator[]
204
- var VariableDeclaration = list[declaratorIndex + 2];
205
- var body = list[declaratorIndex + 3];
206
- deleteDirect(declarator, declarations);
207
- if (VariableDeclaration.declarations.length == 0) {
208
- deleteDirect(VariableDeclaration, body);
209
- }
210
- } else {
211
- if (object.type != "FunctionDeclaration") {
212
- throw new Error("No method to delete: " + object.type);
213
- }
214
- deleteDirect(object, parents[0]);
215
- }
216
- }
217
-
218
- /**
219
- * Object must be directly nested in parent
220
- */
221
- function deleteDirect(object, parent) {
222
- if (!object) {
223
- throw new Error("object undefined");
224
- }
225
- if (!parent) {
226
- throw new Error("parent undefined");
227
- }
228
- (0, _identifiers.validateChain)(object, [parent]);
229
- if (typeof parent === "object") {
230
- if (Array.isArray(parent)) {
231
- var index = parent.indexOf(object);
232
- if (index != -1) {
233
- // delete
234
- parent.splice(index, 1);
235
- } else {
236
- console.log("parent=", parent);
237
- console.log("object=", object);
238
- throw new Error("index -1");
239
- }
240
- } else {
241
- var keyName = Object.keys(parent).find(x => parent[x] == object);
242
- if (keyName) {
243
- delete parent[keyName];
244
- } else {
245
- throw new Error("keyName undefined");
246
- }
247
- }
248
- }
249
- }
250
- function prepend(block) {
251
- (0, _assert.ok)(!Array.isArray(block), "block should not be array");
252
- for (var _len = arguments.length, nodes = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
253
- nodes[_key - 1] = arguments[_key];
254
- }
255
- if (block.type == "Program") {
256
- var moveBy = 0;
257
- block.body.forEach((stmt, i) => {
258
- if (stmt.type == "ImportDeclaration") {
259
- if (moveBy == i) {
260
- moveBy++;
261
- }
262
- }
263
- if (stmt.type === "ExpressionStatement" && typeof stmt.directive === "string") {
264
- if (moveBy == i) {
265
- moveBy++;
266
- }
267
- }
268
- });
269
- block.body.splice(moveBy, 0, ...nodes);
270
- } else if (block.type === "SwitchCase") {
271
- block.consequent.unshift(...nodes);
272
- } else {
273
- var bodyArray = getBlockBody(block);
274
-
275
- // Check for 'use strict'
276
- if (bodyArray[0] && bodyArray[0].directive) {
277
- // Insert under 'use strict' directive
278
- bodyArray.splice(1, 0, ...nodes);
279
- } else {
280
- // Prepend at the top of the block
281
- bodyArray.unshift(...nodes);
282
- }
283
- }
284
- }
285
- function append(block) {
286
- (0, _assert.ok)(!Array.isArray(block), "block should not be array");
287
- for (var _len2 = arguments.length, nodes = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
288
- nodes[_key2 - 1] = arguments[_key2];
289
- }
290
- getBlockBody(block).push(...nodes);
291
- }
292
- function clone(object) {
293
- if (typeof object === "object" && object) {
294
- if (Array.isArray(object)) {
295
- var newArray = [];
296
- object.forEach(element => {
297
- newArray.push(clone(element));
298
- });
299
- return newArray;
300
- } else {
301
- var newObject = {};
302
- Object.keys(object).forEach(key => {
303
- if (!(key + "").startsWith("$")) {
304
- newObject[key] = clone(object[key]);
305
- }
306
- });
307
- return newObject;
308
- }
309
- }
310
- return object;
311
- }
312
-
313
- /**
314
- * | Return Value | Description |
315
- * | --- | --- |
316
- * | `"initializer"` | For-statement initializer (`.init`) |
317
- * | `"left-hand"` | For-In/Of-statement left-hand (`.left`) |
318
- * | `false` | None of the above |
319
- *
320
- * Determines if given node is a for-loop initializer.
321
- *
322
- * @param o
323
- * @param p
324
- * @returns
325
- */
326
- function isForInitialize(o, p) {
327
- (0, _identifiers.validateChain)(o, p);
328
- var forIndex = p.findIndex(x => x.type == "ForStatement" || x.type == "ForInStatement" || x.type == "ForOfStatement");
329
- if (p.slice(0, forIndex).find(x => ["ArrowFunctionExpression", "BlockStatement"].includes(x.type))) {
330
- return false;
331
- }
332
- if (forIndex !== -1) {
333
- if (p[forIndex].type == "ForStatement") {
334
- if (p[forIndex].init == (p[forIndex - 1] || o)) {
335
- return "initializer";
336
- }
337
- } else {
338
- if (p[forIndex].left == (p[forIndex - 1] || o)) {
339
- return "left-hand";
340
- }
341
- }
342
- }
343
- return false;
344
- }
345
-
346
- /**
347
- * Computes the `function.length` property given the parameter nodes.
348
- *
349
- * @param params
350
- * @returns
351
- */
352
- function computeFunctionLength(params) {
353
- var count = 0;
354
- for (var parameterNode of params) {
355
- if (parameterNode.type === "Identifier" || parameterNode.type === "ObjectPattern" || parameterNode.type === "ArrayPattern") {
356
- count++;
357
- } else {
358
- break;
359
- }
360
- }
361
- return count;
362
- }