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,139 +0,0 @@
1
- import traverse, { assertNoCircular } from "../src/traverse";
2
- import { Node } from "../src/util/gen";
3
-
4
- describe("traverse", function () {
5
- test("Variant #1: Traverse tree", function () {
6
- var executionOrder = [];
7
-
8
- var tree: Node = {
9
- type: "Program",
10
- start: 0,
11
- end: 27,
12
- body: [
13
- {
14
- type: "ExpressionStatement",
15
- start: 0,
16
- end: 27,
17
- expression: {
18
- type: "CallExpression",
19
- start: 0,
20
- end: 26,
21
- callee: {
22
- type: "MemberExpression",
23
- start: 0,
24
- end: 11,
25
- object: {
26
- type: "Identifier",
27
- start: 0,
28
- end: 7,
29
- name: "console",
30
- },
31
- property: {
32
- type: "Identifier",
33
- start: 8,
34
- end: 11,
35
- name: "log",
36
- },
37
- computed: false,
38
- optional: false,
39
- },
40
- arguments: [
41
- {
42
- type: "Literal",
43
- start: 12,
44
- end: 25,
45
- value: "Hello World",
46
- raw: '"Hello World"',
47
- },
48
- ],
49
- optional: false,
50
- },
51
- },
52
- ],
53
- sourceType: "module",
54
- };
55
-
56
- var literalParents;
57
-
58
- traverse(tree, (object, parents) => {
59
- if (object.type) {
60
- if (object.type === "Literal") {
61
- literalParents = parents;
62
- }
63
-
64
- executionOrder.push("ENTER:" + object.type);
65
-
66
- return () => {
67
- executionOrder.push("EXIT:" + object.type);
68
- };
69
- }
70
- });
71
-
72
- var displayString = executionOrder.join(",");
73
-
74
- expect(displayString).toStrictEqual(
75
- "ENTER:Program,ENTER:ExpressionStatement,ENTER:CallExpression,ENTER:MemberExpression,ENTER:Identifier,EXIT:Identifier,ENTER:Identifier,EXIT:Identifier,EXIT:MemberExpression,ENTER:Literal,EXIT:Literal,EXIT:CallExpression,EXIT:ExpressionStatement,EXIT:Program"
76
- );
77
-
78
- var displayLiteralParents = literalParents
79
- .map((x) => (Array.isArray(x) ? "(array)" : x.type))
80
- .join(",");
81
- expect(displayLiteralParents).toStrictEqual(
82
- "(array),CallExpression,ExpressionStatement,(array),Program"
83
- );
84
- });
85
- });
86
-
87
- describe("assertNoCircular", function () {
88
- test("Variant #1: Valid tree", function () {
89
- var tree = {
90
- a: 1,
91
- b: 2,
92
- c: 3,
93
- d: {
94
- a: 1,
95
- b: 2,
96
- c: 3,
97
- },
98
- e: [
99
- {
100
- a: 1,
101
- b: 2,
102
- c: 3,
103
- f: {
104
- a: 1,
105
- },
106
- },
107
- ],
108
- };
109
-
110
- expect(() => assertNoCircular(tree)).not.toThrow();
111
- });
112
-
113
- test("Variant #2: Invalid tree", function () {
114
- var circularRef = {};
115
-
116
- var tree = {
117
- a: 1,
118
- b: 2,
119
- c: circularRef,
120
- d: {
121
- a: 1,
122
- b: 2,
123
- c: 3,
124
- },
125
- e: [
126
- {
127
- a: 1,
128
- b: 2,
129
- c: 3,
130
- f: {
131
- a: circularRef,
132
- },
133
- },
134
- ],
135
- };
136
-
137
- expect(() => assertNoCircular(tree)).toThrow();
138
- });
139
- });
@@ -1,34 +0,0 @@
1
- import { isEquivalent, isValidIdentifier } from "../../src/util/compare";
2
- import { Identifier } from "../../src/util/gen";
3
-
4
- it("should compare nodes correctly", () => {
5
- expect(isEquivalent(Identifier("name"), Identifier("name"))).toStrictEqual(
6
- true
7
- );
8
-
9
- expect(
10
- isEquivalent(Identifier("name"), Identifier("different_name"))
11
- ).toStrictEqual(false);
12
- });
13
-
14
- describe("isValidIdentifier", () => {
15
- test("Variant #1: Basic examples", () => {
16
- // true examples
17
- expect(isValidIdentifier("myClass")).toStrictEqual(true);
18
- expect(isValidIdentifier("MyClass")).toStrictEqual(true);
19
- expect(isValidIdentifier("$myObject")).toStrictEqual(true);
20
- expect(isValidIdentifier("_myObject")).toStrictEqual(true);
21
- expect(isValidIdentifier("myObject2")).toStrictEqual(true);
22
- expect(isValidIdentifier("_0")).toStrictEqual(true);
23
-
24
- // false examples
25
- expect(isValidIdentifier("0")).toStrictEqual(false);
26
- expect(isValidIdentifier("0myInvalidVar")).toStrictEqual(false);
27
- expect(isValidIdentifier("^")).toStrictEqual(false);
28
- expect(isValidIdentifier("%")).toStrictEqual(false);
29
- expect(isValidIdentifier("invalid*Var")).toStrictEqual(false);
30
- expect(isValidIdentifier("invalid!")).toStrictEqual(false);
31
- expect(isValidIdentifier("my invalid var")).toStrictEqual(false);
32
- expect(isValidIdentifier("my-invalid-var")).toStrictEqual(false);
33
- });
34
- });
@@ -1,121 +0,0 @@
1
- import {
2
- AssignmentExpression,
3
- AssignmentPattern,
4
- AwaitExpression,
5
- BinaryExpression,
6
- BlockStatement,
7
- BreakStatement,
8
- ClassDeclaration,
9
- DebuggerStatement,
10
- FunctionDeclaration,
11
- FunctionExpression,
12
- Identifier,
13
- IfStatement,
14
- Literal,
15
- MemberExpression,
16
- MethodDefinition,
17
- ObjectExpression,
18
- Property,
19
- RegexLiteral,
20
- RestElement,
21
- SequenceExpression,
22
- SwitchDefaultCase,
23
- ThrowStatement,
24
- WithStatement,
25
- } from "../../src/util/gen";
26
-
27
- it("should return correct types", async () => {
28
- expect(BreakStatement("label")).toHaveProperty("type", "BreakStatement");
29
- expect(AwaitExpression(Identifier("test"))).toHaveProperty(
30
- "type",
31
- "AwaitExpression"
32
- );
33
- expect(AwaitExpression(Identifier("test"))).toHaveProperty(
34
- "type",
35
- "AwaitExpression"
36
- );
37
- expect(RegexLiteral("match", "")).toHaveProperty("type", "Literal");
38
- expect(SwitchDefaultCase([])).toHaveProperty("type", "SwitchCase");
39
- expect(
40
- MethodDefinition(
41
- Identifier("name"),
42
- FunctionExpression([], []),
43
- "method",
44
- false,
45
- false
46
- )
47
- ).toHaveProperty("type", "MethodDefinition");
48
- expect(ThrowStatement(Identifier("error"))).toHaveProperty(
49
- "type",
50
- "ThrowStatement"
51
- );
52
- expect(ThrowStatement(Identifier("error"))).toHaveProperty(
53
- "type",
54
- "ThrowStatement"
55
- );
56
-
57
- expect(ClassDeclaration(Identifier("className"), null, [])).toHaveProperty(
58
- "type",
59
- "ClassDeclaration"
60
- );
61
-
62
- expect(WithStatement(Identifier("variable"), [])).toHaveProperty(
63
- "type",
64
- "WithStatement"
65
- );
66
-
67
- expect(DebuggerStatement()).toHaveProperty("type", "DebuggerStatement");
68
- expect(RestElement(Identifier("array"))).toHaveProperty(
69
- "type",
70
- "RestElement"
71
- );
72
- });
73
-
74
- it("should throw when parameters are missing", async () => {
75
- expect(Identifier).toThrow();
76
- expect(() => Identifier("this")).toThrow();
77
- expect(() => Identifier("super")).toThrow();
78
- expect(() => Literal(undefined)).toThrow();
79
-
80
- expect(BlockStatement).toThrow();
81
- expect(() =>
82
- BinaryExpression("||", Identifier("left"), Identifier("right"))
83
- ).toThrow();
84
-
85
- expect(() =>
86
- BinaryExpression("||", Identifier("left"), Identifier("right"))
87
- ).toThrow();
88
-
89
- expect(Property).toThrow();
90
- expect(() => Property(Identifier("key"), null)).toThrow();
91
-
92
- expect(ObjectExpression).toThrow();
93
- expect(IfStatement).toThrow();
94
- expect(() => IfStatement(Identifier("test"), null)).toThrow();
95
- expect(() =>
96
- IfStatement(Identifier("test"), Identifier("notArray") as any)
97
- ).toThrow();
98
- expect(() =>
99
- IfStatement(Identifier("test"), [], Identifier("notArray") as any)
100
- ).toThrow();
101
-
102
- expect(() => FunctionDeclaration("test", [], null)).toThrow();
103
- expect(() => FunctionDeclaration("test", [], [[]] as any)).toThrow();
104
- expect(() => SequenceExpression(null)).toThrow();
105
- expect(() => SequenceExpression([])).toThrow();
106
-
107
- expect(() => MemberExpression(null, Identifier("target"), false)).toThrow();
108
- expect(() => MemberExpression(Identifier("object"), null, false)).toThrow();
109
- expect(() =>
110
- MemberExpression(Identifier("object"), Literal("stringKey"), false)
111
- ).toThrow();
112
-
113
- expect(() => AssignmentPattern(Identifier("object"), null)).toThrow();
114
- expect(() => AssignmentPattern(null, Identifier("defaultValue"))).toThrow();
115
- expect(() => WithStatement(null, [])).toThrow();
116
- expect(() => WithStatement([] as any, [])).toThrow();
117
-
118
- expect(() =>
119
- MemberExpression(Identifier("new"), Identifier("target"), false)
120
- ).toThrow();
121
- });
@@ -1,253 +0,0 @@
1
- import { ok } from "assert";
2
- import parseJS, { parseSync } from "../../src/parser";
3
- import traverse from "../../src/traverse";
4
- import { Location, Node } from "../../src/util/gen";
5
- import {
6
- getFunctionParameters,
7
- getIdentifierInfo,
8
- validateChain,
9
- } from "../../src/util/identifiers";
10
-
11
- describe("getIdentifierInfo", () => {
12
- test("Variant #1: Determine function declarations", async () => {
13
- var tree = await parseJS(`function abc(){}`);
14
-
15
- var object = tree.body[0].id;
16
-
17
- expect(object.type).toStrictEqual("Identifier");
18
-
19
- var parents = [tree.body[0], tree.body, tree];
20
-
21
- var info = getIdentifierInfo(object, parents as any);
22
-
23
- expect(info.isFunctionDeclaration).toStrictEqual(true);
24
- expect(info.spec.isDefined).toStrictEqual(true);
25
- });
26
-
27
- test("Variant #2: Determine labels", async () => {
28
- var tree = await parseJS(`label: for (var i = 0; i < 0; i++ ) {}`);
29
-
30
- var object = tree.body[0].label;
31
-
32
- expect(object.type).toStrictEqual("Identifier");
33
-
34
- var parents = [tree.body[0], tree.body, tree];
35
-
36
- var info = getIdentifierInfo(object, parents as any);
37
-
38
- expect(info.isLabel).toStrictEqual(true);
39
- expect(info.spec.isReferenced).toStrictEqual(false);
40
- });
41
-
42
- test("Variant #3: Error when a non-identifier node is given", async () => {
43
- expect(() => {
44
- getIdentifierInfo({ type: "Literal", value: true }, []);
45
- }).toThrow();
46
- });
47
-
48
- function findIdentifier(tree: Node, identifierName: string) {
49
- var searchLocation: Location;
50
-
51
- traverse(tree, (o, p) => {
52
- if (o.type === "Identifier" && o.name === identifierName) {
53
- ok(!searchLocation);
54
- searchLocation = [o, p];
55
- }
56
- });
57
-
58
- ok(searchLocation);
59
- return searchLocation;
60
- }
61
-
62
- test("Variant #4: Variable declaration assignment pattern", async () => {
63
- var tree = parseSync(`
64
- var [ definedIdentifier = nonDefinedIdentifier ] = [];
65
- `);
66
-
67
- var definedIdentifier = findIdentifier(tree, "definedIdentifier");
68
- var definedInfo = getIdentifierInfo(
69
- definedIdentifier[0],
70
- definedIdentifier[1]
71
- );
72
- expect(definedInfo.spec.isDefined).toStrictEqual(true);
73
- expect(definedInfo.spec.isReferenced).toStrictEqual(true);
74
-
75
- var nonDefinedIdentifier = findIdentifier(tree, "nonDefinedIdentifier");
76
- var nonDefinedInfo = getIdentifierInfo(
77
- nonDefinedIdentifier[0],
78
- nonDefinedIdentifier[1]
79
- );
80
- expect(nonDefinedInfo.spec.isDefined).toStrictEqual(false);
81
- expect(nonDefinedInfo.spec.isReferenced).toStrictEqual(true);
82
- });
83
-
84
- test("Variant #5: Function parameter assignment pattern", async () => {
85
- var tree = parseSync(`
86
- function myFunction(definedIdentifier = nonDefinedIdentifier) {
87
-
88
- }
89
- `);
90
-
91
- var myFunction = findIdentifier(tree, "myFunction");
92
- var myFunctionInfo = getIdentifierInfo(myFunction[0], myFunction[1]);
93
-
94
- expect(myFunctionInfo.isFunctionDeclaration).toStrictEqual(true);
95
- expect(myFunctionInfo.spec.isDefined).toStrictEqual(true);
96
-
97
- var definedIdentifier = findIdentifier(tree, "definedIdentifier");
98
- var definedInfo = getIdentifierInfo(
99
- definedIdentifier[0],
100
- definedIdentifier[1]
101
- );
102
- expect(definedInfo.spec.isDefined).toStrictEqual(true);
103
- expect(definedInfo.spec.isReferenced).toStrictEqual(true);
104
-
105
- var nonDefinedIdentifier = findIdentifier(tree, "nonDefinedIdentifier");
106
- var nonDefinedInfo = getIdentifierInfo(
107
- nonDefinedIdentifier[0],
108
- nonDefinedIdentifier[1]
109
- );
110
- expect(nonDefinedInfo.spec.isDefined).toStrictEqual(false);
111
- expect(nonDefinedInfo.spec.isReferenced).toStrictEqual(true);
112
- });
113
-
114
- test("Variant #6: Object pattern", async () => {
115
- var tree = parseSync(`
116
- var { nonDefinedIdentifier: definedIdentifier } = {};
117
-
118
- ( { nonModifiedIdentifier: modifiedIdentifier } = {} );
119
- `);
120
-
121
- var definedIdentifier = findIdentifier(tree, "definedIdentifier");
122
- var definedInfo = getIdentifierInfo(
123
- definedIdentifier[0],
124
- definedIdentifier[1]
125
- );
126
- expect(definedInfo.spec.isDefined).toStrictEqual(true);
127
- expect(definedInfo.spec.isReferenced).toStrictEqual(true);
128
-
129
- var nonDefinedIdentifier = findIdentifier(tree, "nonDefinedIdentifier");
130
- var nonDefinedInfo = getIdentifierInfo(
131
- nonDefinedIdentifier[0],
132
- nonDefinedIdentifier[1]
133
- );
134
- expect(nonDefinedInfo.spec.isDefined).toStrictEqual(false);
135
- expect(nonDefinedInfo.spec.isReferenced).toStrictEqual(false);
136
-
137
- var modifiedIdentifier = findIdentifier(tree, "modifiedIdentifier");
138
- var modifiedInfo = getIdentifierInfo(
139
- modifiedIdentifier[0],
140
- modifiedIdentifier[1]
141
- );
142
- expect(modifiedInfo.spec.isDefined).toStrictEqual(false);
143
- expect(modifiedInfo.spec.isModified).toStrictEqual(true);
144
- expect(modifiedInfo.spec.isReferenced).toStrictEqual(true);
145
-
146
- var nonModifiedIdentifier = findIdentifier(tree, "nonModifiedIdentifier");
147
- var nonModifiedInfo = getIdentifierInfo(
148
- nonModifiedIdentifier[0],
149
- nonModifiedIdentifier[1]
150
- );
151
-
152
- expect(nonModifiedInfo.spec.isDefined).toStrictEqual(false);
153
- expect(nonModifiedInfo.spec.isModified).toStrictEqual(false);
154
- expect(nonModifiedInfo.spec.isReferenced).toStrictEqual(false);
155
- });
156
-
157
- test("Variant #7: Default parameter, function expression", async () => {
158
- var tree = parseSync(`
159
- function myFunction( myParameter = function() {
160
- var myNestedDeclaration = true;
161
- } ){
162
-
163
- }
164
- `);
165
-
166
- var myNestedDeclaration = findIdentifier(tree, "myNestedDeclaration");
167
- var myNestedDeclarationInfo = getIdentifierInfo(
168
- myNestedDeclaration[0],
169
- myNestedDeclaration[1]
170
- );
171
-
172
- expect(myNestedDeclarationInfo.isVariableDeclaration).toStrictEqual(true);
173
- expect(myNestedDeclarationInfo.spec.isDefined).toStrictEqual(true);
174
- expect(myNestedDeclarationInfo.spec.isReferenced).toStrictEqual(true);
175
- expect(myNestedDeclarationInfo.spec.isModified).toStrictEqual(false);
176
- });
177
- });
178
-
179
- describe("validateChain", () => {
180
- test("Variant #1: Error when parents is not an array", () => {
181
- expect(() => {
182
- validateChain({ type: "Identifier", name: "name" }, {} as any);
183
- }).toThrow();
184
- });
185
-
186
- test("Variant #2: Error when object is undefined", () => {
187
- expect(() => {
188
- validateChain(undefined, []);
189
- }).toThrow();
190
- });
191
-
192
- test("Variant #3: Error when object is not connected to direct parent", () => {
193
- expect(() => {
194
- validateChain({ type: "Identifier", name: "name" }, [
195
- { type: "Program", body: [] },
196
- ]);
197
- }).toThrow();
198
- });
199
- });
200
-
201
- describe("getFunctionParameters", () => {
202
- test("Variant #1: Work with default values and destructuring", async () => {
203
- var code = `function a(A=_b,{B,[_c]:C},[D]){}`;
204
- var tree = await parseJS(code);
205
-
206
- var object = tree.body[0];
207
- var parents: any = [tree.body, tree];
208
-
209
- var locations = getFunctionParameters(object, parents);
210
- var names = locations.map((x) => x[0].name);
211
-
212
- expect(names).toStrictEqual(["A", "B", "C", "D"]);
213
- });
214
-
215
- test("Variant #2: Work with spread element", async () => {
216
- var code = `function a(...A){}`;
217
- var tree = await parseJS(code);
218
-
219
- var object = tree.body[0];
220
- var parents: any = [tree.body, tree];
221
-
222
- var locations = getFunctionParameters(object, parents);
223
- var names = locations.map((x) => x[0].name);
224
-
225
- expect(names).toStrictEqual(["A"]);
226
- });
227
-
228
- test("Variant #3: Normal parameters", async () => {
229
- var code = `function a(A,B,C,D){}`;
230
- var tree = await parseJS(code);
231
-
232
- var object = tree.body[0];
233
- var parents: any = [tree.body, tree];
234
-
235
- var locations = getFunctionParameters(object, parents);
236
- var names = locations.map((x) => x[0].name);
237
-
238
- expect(names).toStrictEqual(["A", "B", "C", "D"]);
239
- });
240
-
241
- test("Variant #4: Default values as functions", async () => {
242
- var code = `function a(A = function(_a){ return _a; },B = function(_a, _b = function(){return this;}){return _a + _b();},C,D){}`;
243
- var tree = await parseJS(code);
244
-
245
- var object = tree.body[0];
246
- var parents: any = [tree.body, tree];
247
-
248
- var locations = getFunctionParameters(object, parents);
249
- var names = locations.map((x) => x[0].name);
250
-
251
- expect(names).toStrictEqual(["A", "B", "C", "D"]);
252
- });
253
- });
@@ -1,142 +0,0 @@
1
- import { ok } from "assert";
2
- import { compileJsSync } from "../../src/compiler";
3
- import parseJS, { parseSync } from "../../src/parser";
4
- import traverse, { isBlock } from "../../src/traverse";
5
- import { Identifier, Location } from "../../src/util/gen";
6
- import {
7
- deleteDeclaration,
8
- isVarContext,
9
- isFunction,
10
- deleteDirect,
11
- getContexts,
12
- getLexContext,
13
- getVarContext,
14
- computeFunctionLength,
15
- } from "../../src/util/insert";
16
-
17
- it("isBlock() should be true for block statements and program", async () => {
18
- expect(isBlock({ type: "Program", body: [] })).toStrictEqual(true);
19
- expect(isBlock({ type: "BlockStatement", body: [] })).toStrictEqual(true);
20
- });
21
-
22
- it("isVarContext() should return true for Function Nodes", () => {
23
- expect(isVarContext({ type: "FunctionDeclaration" })).toStrictEqual(true);
24
- expect(isVarContext({ type: "FunctionExpression" })).toStrictEqual(true);
25
- expect(isVarContext({ type: "ArrowFunctionExpression" })).toStrictEqual(true);
26
- });
27
-
28
- it("isFunction() should return true for Function Nodes", () => {
29
- expect(isFunction({ type: "FunctionDeclaration" })).toStrictEqual(true);
30
- expect(isFunction({ type: "FunctionExpression" })).toStrictEqual(true);
31
- expect(isFunction({ type: "ArrowFunctionExpression" })).toStrictEqual(true);
32
- });
33
-
34
- it("isVarContext() should return true for the Program Node (root node)", () => {
35
- expect(isVarContext({ type: "Program" })).toStrictEqual(true);
36
- });
37
-
38
- it("should delete variable declarations correctly", async () => {
39
- var tree = await parseJS("var a = 1;");
40
-
41
- deleteDeclaration(tree.body[0].declarations[0], [
42
- tree.body[0].declarations,
43
- tree.body[0],
44
- tree.body,
45
- tree,
46
- ]);
47
-
48
- expect(tree.body.length).toStrictEqual(0);
49
- });
50
-
51
- it("should delete function declarations correctly", async () => {
52
- var tree = await parseJS("function a(){}");
53
-
54
- deleteDeclaration(tree.body[0], [tree.body as any, tree]);
55
-
56
- expect(tree.body.length).toStrictEqual(0);
57
- });
58
-
59
- it("should delete variable declarations with multiple declarations without leave side-effects", async () => {
60
- var tree = await parseJS("var a = 1, b = 1, c = 1");
61
-
62
- // delete "b"
63
- deleteDeclaration(tree.body[0].declarations[1], [
64
- tree.body[0].declarations,
65
- tree.body[0],
66
- tree.body,
67
- tree,
68
- ]);
69
-
70
- expect(tree.body.length).toStrictEqual(1);
71
- expect(tree.body[0].declarations.length).toStrictEqual(2);
72
- expect(tree.body[0].declarations.find((x) => x.id.name == "b")).toBeFalsy();
73
- expect(tree.body[0].declarations.map((x) => x.id.name)).toEqual(["a", "c"]);
74
- });
75
-
76
- it("getContexts should return correct results", () => {
77
- expect(getContexts({ type: "Program", body: [] }, [])).toEqual([
78
- { type: "Program", body: [] },
79
- ]);
80
- });
81
-
82
- it("should throw when missing parameters", () => {
83
- expect(deleteDirect).toThrow();
84
- expect(() => deleteDirect(Identifier("node"), null)).toThrow();
85
-
86
- expect(getLexContext).toThrow();
87
- expect(getVarContext).toThrow();
88
- expect(() => getLexContext(Identifier("test"), [])).toThrow();
89
- expect(() => getVarContext(Identifier("test"), [])).toThrow();
90
- });
91
-
92
- test("computeFunctionLength", () => {
93
- var tree = parseSync(`
94
- function zeroParameters(){}; // 0
95
- function oneParameter(a){}; // 1
96
- function twoParameter(a,b){}; // 2
97
- function restParameter1(...a){}; // 0
98
- function restParameter2(a,b,...c){}; // 2
99
- function defaultValue(a,b,c=1,d){}; // 2
100
- function arrayPattern([a],[b = 2],[[c]]){}; // 3
101
- function objectPattern({a},{b = 2},{c, d}){}; // 3
102
- function mixed(a,{b},[c = 3],d,e=5,f,...g){}; // 4
103
- `);
104
-
105
- function getFunction(searchName: string): Location {
106
- var searchLocation: Location;
107
- traverse(tree, (o, p) => {
108
- if (o.type === "FunctionDeclaration" && o.id.name === searchName) {
109
- ok(!searchLocation);
110
- searchLocation = [o, p];
111
- }
112
- });
113
-
114
- ok(searchLocation);
115
- return searchLocation;
116
- }
117
-
118
- expect(
119
- computeFunctionLength(getFunction("zeroParameters")[0].params)
120
- ).toStrictEqual(0);
121
- expect(
122
- computeFunctionLength(getFunction("oneParameter")[0].params)
123
- ).toStrictEqual(1);
124
- expect(
125
- computeFunctionLength(getFunction("twoParameter")[0].params)
126
- ).toStrictEqual(2);
127
- expect(
128
- computeFunctionLength(getFunction("restParameter1")[0].params)
129
- ).toStrictEqual(0);
130
- expect(
131
- computeFunctionLength(getFunction("restParameter2")[0].params)
132
- ).toStrictEqual(2);
133
- expect(
134
- computeFunctionLength(getFunction("arrayPattern")[0].params)
135
- ).toStrictEqual(3);
136
- expect(
137
- computeFunctionLength(getFunction("objectPattern")[0].params)
138
- ).toStrictEqual(3);
139
- expect(computeFunctionLength(getFunction("mixed")[0].params)).toStrictEqual(
140
- 4
141
- );
142
- });
@@ -1,5 +0,0 @@
1
- import { getFactors } from "../../src/util/math";
2
-
3
- it("getFactors() should return correct factors", () => {
4
- expect(getFactors(6)).toEqual([1, 6, 2, 3]);
5
- });