js-confuser 1.7.2 → 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.
- package/.github/ISSUE_TEMPLATE/bug_report.md +6 -4
- package/.github/workflows/node.js.yml +1 -1
- package/CHANGELOG.md +105 -0
- package/Migration.md +57 -0
- package/README.md +23 -913
- package/dist/constants.js +69 -13
- package/dist/index.js +108 -152
- package/dist/obfuscator.js +316 -118
- package/dist/options.js +1 -109
- package/dist/order.js +30 -30
- package/dist/presets.js +47 -45
- package/dist/probability.js +25 -32
- package/dist/templates/bufferToStringTemplate.js +9 -0
- package/dist/templates/deadCodeTemplates.js +9 -0
- package/dist/templates/getGlobalTemplate.js +19 -0
- package/dist/templates/integrityTemplate.js +30 -0
- package/dist/templates/setFunctionLengthTemplate.js +9 -0
- package/dist/templates/stringCompressionTemplate.js +10 -0
- package/dist/templates/tamperProtectionTemplates.js +21 -0
- package/dist/templates/template.js +213 -93
- package/dist/transforms/astScrambler.js +100 -0
- package/dist/transforms/calculator.js +70 -127
- package/dist/transforms/controlFlowFlattening.js +1182 -0
- package/dist/transforms/deadCode.js +62 -577
- package/dist/transforms/dispatcher.js +300 -309
- package/dist/transforms/extraction/duplicateLiteralsRemoval.js +88 -189
- package/dist/transforms/extraction/objectExtraction.js +131 -215
- package/dist/transforms/finalizer.js +56 -59
- package/dist/transforms/flatten.js +275 -276
- package/dist/transforms/functionOutlining.js +230 -0
- package/dist/transforms/identifier/globalConcealing.js +217 -103
- package/dist/transforms/identifier/movedDeclarations.js +167 -91
- package/dist/transforms/identifier/renameVariables.js +240 -187
- package/dist/transforms/lock/integrity.js +61 -184
- package/dist/transforms/lock/lock.js +263 -303
- package/dist/transforms/minify.js +431 -436
- package/dist/transforms/opaquePredicates.js +65 -118
- package/dist/transforms/pack.js +160 -0
- package/dist/transforms/plugin.js +179 -0
- package/dist/transforms/preparation.js +263 -163
- package/dist/transforms/renameLabels.js +132 -56
- package/dist/transforms/rgf.js +142 -240
- package/dist/transforms/shuffle.js +52 -145
- package/dist/transforms/string/encoding.js +45 -173
- package/dist/transforms/string/stringCompression.js +81 -126
- package/dist/transforms/string/stringConcealing.js +189 -224
- package/dist/transforms/string/stringEncoding.js +32 -40
- package/dist/transforms/string/stringSplitting.js +54 -55
- package/dist/transforms/variableMasking.js +232 -0
- package/dist/utils/ControlObject.js +125 -0
- package/dist/utils/IntGen.js +46 -0
- package/dist/utils/NameGen.js +106 -0
- package/dist/utils/ast-utils.js +560 -0
- package/dist/utils/function-utils.js +56 -0
- package/dist/utils/gen-utils.js +48 -0
- package/dist/utils/node.js +77 -0
- package/dist/utils/object-utils.js +21 -0
- package/dist/utils/random-utils.js +91 -0
- package/dist/utils/static-utils.js +64 -0
- package/dist/validateOptions.js +122 -0
- package/index.d.ts +1 -17
- package/package.json +27 -22
- package/src/constants.ts +139 -77
- package/src/index.ts +70 -163
- package/src/obfuscationResult.ts +43 -0
- package/src/obfuscator.ts +328 -135
- package/src/options.ts +154 -623
- package/src/order.ts +14 -14
- package/src/presets.ts +39 -34
- package/src/probability.ts +21 -36
- package/src/templates/{bufferToString.ts → bufferToStringTemplate.ts} +5 -54
- package/src/templates/deadCodeTemplates.ts +1185 -0
- package/src/templates/getGlobalTemplate.ts +72 -0
- package/src/templates/integrityTemplate.ts +69 -0
- package/src/templates/setFunctionLengthTemplate.ts +11 -0
- package/src/templates/stringCompressionTemplate.ts +42 -0
- package/src/templates/tamperProtectionTemplates.ts +116 -0
- package/src/templates/template.ts +183 -92
- package/src/transforms/astScrambler.ts +99 -0
- package/src/transforms/calculator.ts +96 -224
- package/src/transforms/controlFlowFlattening.ts +1594 -0
- package/src/transforms/deadCode.ts +85 -628
- package/src/transforms/dispatcher.ts +431 -636
- package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -299
- package/src/transforms/extraction/objectExtraction.ts +160 -333
- package/src/transforms/finalizer.ts +63 -64
- package/src/transforms/flatten.ts +439 -557
- package/src/transforms/functionOutlining.ts +225 -0
- package/src/transforms/identifier/globalConcealing.ts +261 -189
- package/src/transforms/identifier/movedDeclarations.ts +228 -142
- package/src/transforms/identifier/renameVariables.ts +252 -258
- package/src/transforms/lock/integrity.ts +84 -260
- package/src/transforms/lock/lock.ts +342 -491
- package/src/transforms/minify.ts +523 -663
- package/src/transforms/opaquePredicates.ts +90 -229
- package/src/transforms/pack.ts +195 -0
- package/src/transforms/plugin.ts +185 -0
- package/src/transforms/preparation.ts +337 -215
- package/src/transforms/renameLabels.ts +176 -77
- package/src/transforms/rgf.ts +293 -386
- package/src/transforms/shuffle.ts +80 -254
- package/src/transforms/string/encoding.ts +26 -129
- package/src/transforms/string/stringCompression.ts +118 -236
- package/src/transforms/string/stringConcealing.ts +255 -339
- package/src/transforms/string/stringEncoding.ts +28 -47
- package/src/transforms/string/stringSplitting.ts +61 -75
- package/src/transforms/variableMasking.ts +257 -0
- package/src/utils/ControlObject.ts +141 -0
- package/src/utils/IntGen.ts +33 -0
- package/src/utils/NameGen.ts +106 -0
- package/src/utils/ast-utils.ts +667 -0
- package/src/utils/function-utils.ts +50 -0
- package/src/utils/gen-utils.ts +48 -0
- package/src/utils/node.ts +78 -0
- package/src/utils/object-utils.ts +21 -0
- package/src/utils/random-utils.ts +79 -0
- package/src/utils/static-utils.ts +66 -0
- package/src/validateOptions.ts +256 -0
- package/tsconfig.json +13 -8
- package/babel.config.js +0 -12
- package/dev.js +0 -8
- package/dist/compiler.js +0 -34
- package/dist/parser.js +0 -59
- package/dist/precedence.js +0 -66
- package/dist/templates/bufferToString.js +0 -108
- package/dist/templates/crash.js +0 -59
- package/dist/templates/es5.js +0 -137
- package/dist/templates/functionLength.js +0 -34
- package/dist/templates/globals.js +0 -9
- package/dist/transforms/antiTooling.js +0 -88
- package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +0 -1281
- package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +0 -131
- package/dist/transforms/es5/antiClass.js +0 -164
- package/dist/transforms/es5/antiDestructuring.js +0 -193
- package/dist/transforms/es5/antiES6Object.js +0 -185
- package/dist/transforms/es5/antiSpreadOperator.js +0 -35
- package/dist/transforms/es5/antiTemplate.js +0 -66
- package/dist/transforms/es5/es5.js +0 -123
- package/dist/transforms/extraction/classExtraction.js +0 -83
- package/dist/transforms/identifier/globalAnalysis.js +0 -70
- package/dist/transforms/identifier/variableAnalysis.js +0 -104
- package/dist/transforms/lock/antiDebug.js +0 -76
- package/dist/transforms/stack.js +0 -343
- package/dist/transforms/transform.js +0 -350
- package/dist/traverse.js +0 -110
- package/dist/util/compare.js +0 -145
- package/dist/util/gen.js +0 -564
- package/dist/util/guard.js +0 -9
- package/dist/util/identifiers.js +0 -355
- package/dist/util/insert.js +0 -362
- package/dist/util/math.js +0 -19
- package/dist/util/object.js +0 -40
- package/dist/util/random.js +0 -130
- package/dist/util/scope.js +0 -20
- package/docs/ControlFlowFlattening.md +0 -595
- package/docs/Countermeasures.md +0 -63
- package/docs/ES5.md +0 -197
- package/docs/Integrity.md +0 -75
- package/docs/RGF.md +0 -419
- package/samples/example.js +0 -15
- package/samples/high.js +0 -1
- package/samples/input.js +0 -3
- package/samples/javascriptobfuscator.com.js +0 -8
- package/samples/jscrambler_advanced.js +0 -1894
- package/samples/jscrambler_light.js +0 -1134
- package/samples/low.js +0 -1
- package/samples/medium.js +0 -1
- package/samples/obfuscator.io.js +0 -1686
- package/samples/preemptive.com.js +0 -16
- package/src/compiler.ts +0 -35
- package/src/parser.ts +0 -49
- package/src/precedence.ts +0 -61
- package/src/templates/crash.ts +0 -55
- package/src/templates/es5.ts +0 -131
- package/src/templates/functionLength.ts +0 -32
- package/src/templates/globals.ts +0 -3
- package/src/transforms/antiTooling.ts +0 -102
- package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +0 -2146
- package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
- package/src/transforms/es5/antiClass.ts +0 -272
- package/src/transforms/es5/antiDestructuring.ts +0 -294
- package/src/transforms/es5/antiES6Object.ts +0 -267
- package/src/transforms/es5/antiSpreadOperator.ts +0 -56
- package/src/transforms/es5/antiTemplate.ts +0 -98
- package/src/transforms/es5/es5.ts +0 -149
- package/src/transforms/extraction/classExtraction.ts +0 -168
- package/src/transforms/identifier/globalAnalysis.ts +0 -85
- package/src/transforms/identifier/variableAnalysis.ts +0 -118
- package/src/transforms/lock/antiDebug.ts +0 -112
- package/src/transforms/stack.ts +0 -551
- package/src/transforms/transform.ts +0 -453
- package/src/traverse.ts +0 -120
- package/src/types.ts +0 -131
- package/src/util/compare.ts +0 -181
- package/src/util/gen.ts +0 -651
- package/src/util/guard.ts +0 -7
- package/src/util/identifiers.ts +0 -494
- package/src/util/insert.ts +0 -419
- package/src/util/math.ts +0 -15
- package/src/util/object.ts +0 -39
- package/src/util/random.ts +0 -141
- package/src/util/scope.ts +0 -21
- package/test/code/Cash.src.js +0 -1011
- package/test/code/Cash.test.ts +0 -49
- package/test/code/Dynamic.src.js +0 -118
- package/test/code/Dynamic.test.ts +0 -49
- package/test/code/ES6.src.js +0 -235
- package/test/code/ES6.test.ts +0 -42
- package/test/code/NewFeatures.test.ts +0 -19
- package/test/code/StrictMode.src.js +0 -65
- package/test/code/StrictMode.test.js +0 -37
- package/test/compare.test.ts +0 -104
- package/test/index.test.ts +0 -249
- package/test/options.test.ts +0 -132
- package/test/presets.test.ts +0 -22
- package/test/probability.test.ts +0 -44
- package/test/templates/template.test.ts +0 -14
- package/test/transforms/antiTooling.test.ts +0 -52
- package/test/transforms/calculator.test.ts +0 -78
- package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +0 -1274
- package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +0 -192
- package/test/transforms/deadCode.test.ts +0 -85
- package/test/transforms/dispatcher.test.ts +0 -457
- package/test/transforms/es5/antiClass.test.ts +0 -427
- package/test/transforms/es5/antiDestructuring.test.ts +0 -157
- package/test/transforms/es5/antiES6Object.test.ts +0 -245
- package/test/transforms/es5/antiTemplate.test.ts +0 -116
- package/test/transforms/es5/es5.test.ts +0 -110
- package/test/transforms/extraction/classExtraction.test.ts +0 -86
- package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +0 -200
- package/test/transforms/extraction/objectExtraction.test.ts +0 -491
- package/test/transforms/flatten.test.ts +0 -721
- package/test/transforms/hexadecimalNumbers.test.ts +0 -62
- package/test/transforms/identifier/globalConcealing.test.ts +0 -72
- package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
- package/test/transforms/identifier/renameVariables.test.ts +0 -621
- package/test/transforms/lock/antiDebug.test.ts +0 -66
- package/test/transforms/lock/browserLock.test.ts +0 -129
- package/test/transforms/lock/countermeasures.test.ts +0 -100
- package/test/transforms/lock/integrity.test.ts +0 -161
- package/test/transforms/lock/lock.test.ts +0 -204
- package/test/transforms/lock/osLock.test.ts +0 -312
- package/test/transforms/lock/selfDefending.test.ts +0 -68
- package/test/transforms/minify.test.ts +0 -575
- package/test/transforms/opaquePredicates.test.ts +0 -43
- package/test/transforms/preparation.test.ts +0 -157
- package/test/transforms/renameLabels.test.ts +0 -95
- package/test/transforms/rgf.test.ts +0 -378
- package/test/transforms/shuffle.test.ts +0 -135
- package/test/transforms/stack.test.ts +0 -573
- package/test/transforms/string/stringCompression.test.ts +0 -120
- package/test/transforms/string/stringConcealing.test.ts +0 -299
- package/test/transforms/string/stringEncoding.test.ts +0 -95
- package/test/transforms/string/stringSplitting.test.ts +0 -135
- package/test/transforms/transform.test.ts +0 -66
- package/test/traverse.test.ts +0 -139
- package/test/util/compare.test.ts +0 -34
- package/test/util/gen.test.ts +0 -121
- package/test/util/identifiers.test.ts +0 -253
- package/test/util/insert.test.ts +0 -142
- package/test/util/math.test.ts +0 -5
- package/test/util/random.test.ts +0 -71
- /package/dist/{types.js → obfuscationResult.js} +0 -0
package/src/util/identifiers.ts
DELETED
|
@@ -1,494 +0,0 @@
|
|
|
1
|
-
import { ok } from "assert";
|
|
2
|
-
import traverse, { walk } from "../traverse";
|
|
3
|
-
import { Location, Node } from "./gen";
|
|
4
|
-
import { getVarContext, isVarContext, isFunction } from "./insert";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Ensures the chain (object and parents) are connected.
|
|
8
|
-
* @param object
|
|
9
|
-
* @param parents
|
|
10
|
-
*/
|
|
11
|
-
export function validateChain(object: Node, parents: Node[]) {
|
|
12
|
-
if (!Array.isArray(parents)) {
|
|
13
|
-
throw new Error("parents need to be an array");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (!object) {
|
|
17
|
-
throw new Error("object must be a node (not null)");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (parents.length > 0) {
|
|
21
|
-
if (object == parents[0]) {
|
|
22
|
-
throw new Error("parent overlap");
|
|
23
|
-
}
|
|
24
|
-
if (!Object.values(parents[0]).includes(object)) {
|
|
25
|
-
console.log("parents=", parents);
|
|
26
|
-
console.log("object=", object);
|
|
27
|
-
|
|
28
|
-
throw new Error("parents[0] is not connected to object");
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function objectPatternCheck(object: Node, parents: Node[]) {
|
|
34
|
-
var objectPatternIndex = parents.findIndex((x) => x.type === "ObjectPattern");
|
|
35
|
-
if (objectPatternIndex == -1) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
var property = parents[objectPatternIndex].properties.find(
|
|
40
|
-
(property) => parents[objectPatternIndex - 2] === property
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
if (property.key === (parents[objectPatternIndex - 3] || object)) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Returns detailed information about the given Identifier node.
|
|
52
|
-
* @param object
|
|
53
|
-
* @param parents
|
|
54
|
-
*/
|
|
55
|
-
export function getIdentifierInfo(object: Node, parents: Node[]) {
|
|
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
|
-
|
|
61
|
-
var parent = parents[0] || ({} as Node);
|
|
62
|
-
|
|
63
|
-
var isAccessor =
|
|
64
|
-
parent.type == "MemberExpression" &&
|
|
65
|
-
parent.object != object &&
|
|
66
|
-
parent.property === object &&
|
|
67
|
-
!parent.computed;
|
|
68
|
-
|
|
69
|
-
var propIndex = parents.findIndex(
|
|
70
|
-
(x) => x.type == "Property" || x.type == "MethodDefinition"
|
|
71
|
-
);
|
|
72
|
-
var isPropertyKey =
|
|
73
|
-
propIndex != -1 &&
|
|
74
|
-
parents[propIndex].key == (parents[propIndex - 1] || object) &&
|
|
75
|
-
!parents[propIndex].computed;
|
|
76
|
-
|
|
77
|
-
var objectPatternIndex = parents.findIndex((x) => x.type == "ObjectPattern");
|
|
78
|
-
if (
|
|
79
|
-
objectPatternIndex !== -1 &&
|
|
80
|
-
parents[objectPatternIndex].properties == parents[objectPatternIndex - 1]
|
|
81
|
-
) {
|
|
82
|
-
if (objectPatternIndex - propIndex == 2) {
|
|
83
|
-
if (parents[propIndex].value === (parents[propIndex - 1] || object)) {
|
|
84
|
-
isPropertyKey = false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
var varIndex = parents.findIndex((x) => x.type == "VariableDeclarator");
|
|
90
|
-
|
|
91
|
-
var isVariableDeclaration =
|
|
92
|
-
varIndex != -1 &&
|
|
93
|
-
parents[varIndex].id == (parents[varIndex - 1] || object) &&
|
|
94
|
-
parents.find((x) => x.type == "VariableDeclaration") &&
|
|
95
|
-
objectPatternCheck(object, parents);
|
|
96
|
-
|
|
97
|
-
var functionIndex = parents.findIndex((x) => isFunction(x));
|
|
98
|
-
|
|
99
|
-
// Assignment pattern check!
|
|
100
|
-
if (isVariableDeclaration) {
|
|
101
|
-
var slicedParents = parents.slice(
|
|
102
|
-
0,
|
|
103
|
-
functionIndex != -1 ? Math.min(varIndex, functionIndex) : varIndex
|
|
104
|
-
);
|
|
105
|
-
var i = 0;
|
|
106
|
-
for (var parent of slicedParents) {
|
|
107
|
-
var childNode = slicedParents[i - 1] || object;
|
|
108
|
-
if (parent.type === "AssignmentPattern" && parent.right === childNode) {
|
|
109
|
-
isVariableDeclaration = false;
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
i++;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
var forIndex = parents.findIndex((x) => x.type == "ForStatement");
|
|
117
|
-
var isForInitializer =
|
|
118
|
-
forIndex != -1 &&
|
|
119
|
-
parents[forIndex].init == (parents[forIndex - 1] || object);
|
|
120
|
-
|
|
121
|
-
var isFunctionDeclaration =
|
|
122
|
-
functionIndex != -1 &&
|
|
123
|
-
parents[functionIndex].type == "FunctionDeclaration" &&
|
|
124
|
-
parents[functionIndex].id == object;
|
|
125
|
-
|
|
126
|
-
var isNamedFunctionExpression =
|
|
127
|
-
functionIndex != -1 &&
|
|
128
|
-
parents[functionIndex].type === "FunctionExpression" &&
|
|
129
|
-
parents[functionIndex].id === object;
|
|
130
|
-
|
|
131
|
-
var isAFunctionParameter = isFunctionParameter(object, parents);
|
|
132
|
-
|
|
133
|
-
var isClauseParameter = false;
|
|
134
|
-
|
|
135
|
-
// Special case for Catch clauses
|
|
136
|
-
var clauseIndex = parents.findIndex((x) => x.type == "CatchClause");
|
|
137
|
-
if (clauseIndex != -1) {
|
|
138
|
-
if (parents[clauseIndex].param == (parents[clauseIndex - 1] || object)) {
|
|
139
|
-
isClauseParameter = true;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
var isImportSpecifier =
|
|
144
|
-
(parent.type == "ImportDefaultSpecifier" ||
|
|
145
|
-
parent.type == "ImportSpecifier") &&
|
|
146
|
-
parent.local == object;
|
|
147
|
-
|
|
148
|
-
var isFunctionCall = parent.callee == object; // NewExpression and CallExpression
|
|
149
|
-
|
|
150
|
-
var assignmentIndex = parents.findIndex(
|
|
151
|
-
(p) => p.type === "AssignmentExpression"
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
var isAssignmentLeft =
|
|
155
|
-
assignmentIndex !== -1 &&
|
|
156
|
-
parents[assignmentIndex].left ===
|
|
157
|
-
(parents[assignmentIndex - 1] || object) &&
|
|
158
|
-
objectPatternCheck(object, parents);
|
|
159
|
-
var isAssignmentValue =
|
|
160
|
-
assignmentIndex !== -1 &&
|
|
161
|
-
parents[assignmentIndex].right === (parents[assignmentIndex - 1] || object);
|
|
162
|
-
|
|
163
|
-
var isUpdateExpression = parent.type == "UpdateExpression";
|
|
164
|
-
|
|
165
|
-
var isClassDeclaration =
|
|
166
|
-
(parent.type == "ClassDeclaration" || parent.type == "ClassExpression") &&
|
|
167
|
-
parent.id == object;
|
|
168
|
-
var isMethodDefinition =
|
|
169
|
-
parent.type == "MethodDefinition" &&
|
|
170
|
-
parent.key == object &&
|
|
171
|
-
!parent.computed;
|
|
172
|
-
|
|
173
|
-
var isMetaProperty = parent.type == "MetaProperty";
|
|
174
|
-
|
|
175
|
-
var isLabel = parent.type == "LabeledStatement" && parent.label == object;
|
|
176
|
-
|
|
177
|
-
// Fix 1: Labels are properly identified
|
|
178
|
-
if (parent.type == "BreakStatement" || parent.type == "ContinueStatement") {
|
|
179
|
-
if (parent.label == object) {
|
|
180
|
-
isLabel = true;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
var isDeleteExpression = false;
|
|
185
|
-
var deleteIndex = parents.findIndex(
|
|
186
|
-
(x) => x.type == "UnaryExpression" && x.operator == "delete"
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
if (deleteIndex != -1) {
|
|
190
|
-
isDeleteExpression = true;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
var isReferenced =
|
|
194
|
-
!isAccessor &&
|
|
195
|
-
!isPropertyKey &&
|
|
196
|
-
!isMetaProperty &&
|
|
197
|
-
!isLabel &&
|
|
198
|
-
!object.name.startsWith("0") &&
|
|
199
|
-
!object.name.startsWith("'");
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
/**
|
|
203
|
-
* MemberExpression: `parent.identifier`
|
|
204
|
-
*/
|
|
205
|
-
isAccessor,
|
|
206
|
-
/**
|
|
207
|
-
* Property: `{identifier: ...}`
|
|
208
|
-
*/
|
|
209
|
-
isPropertyKey,
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* `var identifier = ...`
|
|
213
|
-
*/
|
|
214
|
-
isVariableDeclaration,
|
|
215
|
-
/**
|
|
216
|
-
* `function identifier(){...}`
|
|
217
|
-
*/
|
|
218
|
-
isFunctionDeclaration,
|
|
219
|
-
/**
|
|
220
|
-
* `function a(identifier){...}`
|
|
221
|
-
*/
|
|
222
|
-
isFunctionParameter: isAFunctionParameter,
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* ```js
|
|
226
|
-
* try ... catch ( identifier ) {
|
|
227
|
-
* ...
|
|
228
|
-
* }
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
isClauseParameter,
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* CallExpression: `identifier()`
|
|
235
|
-
*/
|
|
236
|
-
isFunctionCall,
|
|
237
|
-
/**
|
|
238
|
-
* AssignmentExpression: `identifier = ...`
|
|
239
|
-
*/
|
|
240
|
-
isAssignmentLeft,
|
|
241
|
-
/**
|
|
242
|
-
* AssignmentExpression (right): `x = identifier`
|
|
243
|
-
*/
|
|
244
|
-
isAssignmentValue,
|
|
245
|
-
/**
|
|
246
|
-
* UpdateExpression: `identifier++`
|
|
247
|
-
*/
|
|
248
|
-
isUpdateExpression,
|
|
249
|
-
/**
|
|
250
|
-
* ClassDeclaration `class identifier {...}`
|
|
251
|
-
*/
|
|
252
|
-
isClassDeclaration,
|
|
253
|
-
/**
|
|
254
|
-
* Method Definition inside a class body
|
|
255
|
-
* ```js
|
|
256
|
-
* class Rectangle {
|
|
257
|
-
* identifier(){...}
|
|
258
|
-
*
|
|
259
|
-
* get identifier(){...}
|
|
260
|
-
* }
|
|
261
|
-
* ```
|
|
262
|
-
*/
|
|
263
|
-
isMethodDefinition,
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* `new.target` or `yield.input`
|
|
267
|
-
*/
|
|
268
|
-
isMetaProperty,
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* LabelStatement: `identifier: for ( var i...)`
|
|
272
|
-
*/
|
|
273
|
-
isLabel,
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* ```js
|
|
277
|
-
* for (var i=0; ...) {
|
|
278
|
-
* ...
|
|
279
|
-
* }
|
|
280
|
-
* ```
|
|
281
|
-
*/
|
|
282
|
-
isForInitializer,
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* ```js
|
|
286
|
-
* import identifier from "...";
|
|
287
|
-
* import {key as identifier} from "...";
|
|
288
|
-
* ```
|
|
289
|
-
*/
|
|
290
|
-
isImportSpecifier,
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* ```js
|
|
294
|
-
* delete identifier[identifier]
|
|
295
|
-
* ```
|
|
296
|
-
*/
|
|
297
|
-
isDeleteExpression: isDeleteExpression,
|
|
298
|
-
|
|
299
|
-
spec: {
|
|
300
|
-
/**
|
|
301
|
-
* - `export function identifier()...`
|
|
302
|
-
* - `export var identifier = ...`
|
|
303
|
-
*/
|
|
304
|
-
isExported:
|
|
305
|
-
(isVariableDeclaration &&
|
|
306
|
-
parents[3] &&
|
|
307
|
-
parents[3].type == "ExportNamedDeclaration") ||
|
|
308
|
-
(isFunctionDeclaration &&
|
|
309
|
-
parents[1] &&
|
|
310
|
-
parents[1].type == "ExportNamedDeclaration"),
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Is the Identifier defined, i.e a variable declaration, function declaration, parameter, or class definition
|
|
314
|
-
*/
|
|
315
|
-
isDefined:
|
|
316
|
-
isVariableDeclaration ||
|
|
317
|
-
isFunctionDeclaration ||
|
|
318
|
-
isNamedFunctionExpression ||
|
|
319
|
-
isAFunctionParameter ||
|
|
320
|
-
isClassDeclaration ||
|
|
321
|
-
isClauseParameter ||
|
|
322
|
-
isMethodDefinition ||
|
|
323
|
-
isImportSpecifier,
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Is the Identifier modified, either by an `AssignmentExpression` or `UpdateExpression`
|
|
327
|
-
*/
|
|
328
|
-
isModified: isAssignmentLeft || isUpdateExpression || isDeleteExpression,
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Is the Identifier referenced as a variable.
|
|
332
|
-
*
|
|
333
|
-
* - true: `if ( identifier ) {...}`
|
|
334
|
-
* - false `if ( obj.identifier ) {...}`
|
|
335
|
-
* - false `identifier: for ( var ...)`
|
|
336
|
-
* - false `var {identifier: ...}`
|
|
337
|
-
* - false `break identifier;`
|
|
338
|
-
*/
|
|
339
|
-
isReferenced: isReferenced,
|
|
340
|
-
},
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
export function getDefiningIdentifier(object: Node, parents: Node[]): Location {
|
|
345
|
-
ok(object.type == "Identifier", "must be identifier");
|
|
346
|
-
ok(typeof object.name === "string");
|
|
347
|
-
ok(
|
|
348
|
-
parents[parents.length - 1].type == "Program",
|
|
349
|
-
"root node must be type Program. Found '" +
|
|
350
|
-
parents[parents.length - 1].type +
|
|
351
|
-
"'"
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
var seen = new Set<Node>();
|
|
355
|
-
var i = 0;
|
|
356
|
-
for (var parent of parents) {
|
|
357
|
-
var l;
|
|
358
|
-
var bestScore = Infinity;
|
|
359
|
-
walk(parent, parents.slice(i + 1), (o, p) => {
|
|
360
|
-
// if (p.find((x) => seen.has(x))) {
|
|
361
|
-
// return "EXIT";
|
|
362
|
-
// }
|
|
363
|
-
|
|
364
|
-
if (o.type == "Identifier" && o.name === object.name && o !== object) {
|
|
365
|
-
var info = getIdentifierInfo(o, p);
|
|
366
|
-
if (info.spec.isDefined) {
|
|
367
|
-
var contexts = p.filter((x) => isVarContext(x));
|
|
368
|
-
var definingContext = info.isFunctionDeclaration
|
|
369
|
-
? getVarContext(p[0], p.slice(1))
|
|
370
|
-
: getVarContext(o, p);
|
|
371
|
-
|
|
372
|
-
if (parents.includes(definingContext)) {
|
|
373
|
-
var index = contexts.indexOf(definingContext);
|
|
374
|
-
|
|
375
|
-
if (index < bestScore) {
|
|
376
|
-
l = [o, p];
|
|
377
|
-
bestScore = index;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
if (l) {
|
|
385
|
-
// console.log(l[0].name, "->", l[0], bestScore);
|
|
386
|
-
|
|
387
|
-
return l;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
seen.add(parent);
|
|
391
|
-
i++;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
export function isFunctionParameter(o: Node, p: Node[], c?: Node) {
|
|
396
|
-
ok(o);
|
|
397
|
-
ok(p);
|
|
398
|
-
validateChain(o, p);
|
|
399
|
-
|
|
400
|
-
if (o.type !== "Identifier") {
|
|
401
|
-
return false;
|
|
402
|
-
}
|
|
403
|
-
var object = p.find((x) => isFunction(x) && x.params);
|
|
404
|
-
if (!object) {
|
|
405
|
-
return false;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
c = c || getVarContext(o, p);
|
|
409
|
-
if (c === object) {
|
|
410
|
-
var pIndex = p.indexOf(object.params);
|
|
411
|
-
if (pIndex == -1) {
|
|
412
|
-
return false;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
var param = p[pIndex - 1] || o;
|
|
416
|
-
var paramIndex = object.params.indexOf(param);
|
|
417
|
-
ok(paramIndex !== -1);
|
|
418
|
-
|
|
419
|
-
var sliced = p.slice(0, pIndex);
|
|
420
|
-
|
|
421
|
-
var isReferenced = true;
|
|
422
|
-
var i = 0;
|
|
423
|
-
for (var node of sliced) {
|
|
424
|
-
var down = sliced[i - 1] || o;
|
|
425
|
-
ok(down);
|
|
426
|
-
|
|
427
|
-
if (node.type) {
|
|
428
|
-
if (node.type == "AssignmentPattern" && node.right === down) {
|
|
429
|
-
isReferenced = false;
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
if (
|
|
434
|
-
node.type == "Property" &&
|
|
435
|
-
node.key === down &&
|
|
436
|
-
sliced[i + 2] &&
|
|
437
|
-
sliced[i + 2].type == "ObjectPattern"
|
|
438
|
-
) {
|
|
439
|
-
isReferenced = false;
|
|
440
|
-
break;
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
i++;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
if (isReferenced) {
|
|
448
|
-
return true;
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
return false;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
export function getFunctionParameters(
|
|
456
|
-
object: Node,
|
|
457
|
-
parents: Node[]
|
|
458
|
-
): [{ type: "Identifier"; name: string }, Node[]][] {
|
|
459
|
-
ok(isFunction(object));
|
|
460
|
-
ok(object.params);
|
|
461
|
-
|
|
462
|
-
var locations = [];
|
|
463
|
-
|
|
464
|
-
walk(object.params, [object, ...parents], (o, p) => {
|
|
465
|
-
if (o.type == "Identifier") {
|
|
466
|
-
if (isFunctionParameter(o, p, object)) {
|
|
467
|
-
locations.push([o, p]);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
return locations;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
export function containsLexicallyBoundVariables(object: Node, parents: Node[]) {
|
|
476
|
-
var contains = false;
|
|
477
|
-
walk(object, parents, (o, p) => {
|
|
478
|
-
if (o.type == "VariableDeclaration") {
|
|
479
|
-
if (o.kind === "let" || o.kind === "const") {
|
|
480
|
-
// Control Flow Flattening changes the lexical block, therefore this is not possible
|
|
481
|
-
// Maybe a transformation to remove let
|
|
482
|
-
contains = true;
|
|
483
|
-
return "EXIT";
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
if (o.type == "ClassDeclaration") {
|
|
488
|
-
contains = true;
|
|
489
|
-
return "EXIT";
|
|
490
|
-
}
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
return contains;
|
|
494
|
-
}
|