forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeUtils.ts
More file actions
225 lines (184 loc) · 6.74 KB
/
NodeUtils.ts
File metadata and controls
225 lines (184 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import * as escodegen from 'escodegen-wallaby';
import * as esprima from 'esprima';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { TNodeWithBlockStatement } from '../types/node/TNodeWithBlockStatement';
import { TObject } from '../types/TObject';
import { TStatement } from '../types/node/TStatement';
import { NodeType } from '../enums/node/NodeType';
import { NodeGuards } from './NodeGuards';
export class NodeUtils {
/**
* @type {string[]}
*/
private static readonly nodesWithBlockScope: string[] = [
NodeType.ArrowFunctionExpression,
NodeType.FunctionDeclaration,
NodeType.FunctionExpression,
NodeType.MethodDefinition,
NodeType.Program
];
/**
* @param {T} astTree
* @returns {T}
*/
public static addXVerbatimPropertyToLiterals <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
estraverse.replace(astTree, {
leave: (node: ESTree.Node) => {
if (NodeGuards.isLiteralNode(node)) {
node['x-verbatim-property'] = {
content: node.raw,
precedence: escodegen.Precedence.Primary
};
}
}
});
return astTree;
}
/**
* @param {T} astTree
* @returns {T}
*/
public static clone <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
/**
* @param {T} node
* @returns {T}
*/
const cloneRecursive: (node: T) => T = (node: T) => {
if (node === null) {
return node;
}
const copy: TObject = {};
Object
.keys(node)
.filter((property: string) => property !== 'parentNode')
.forEach((property: string): void => {
const value: any = (<TObject>node)[property];
let clonedValue: any | null;
if (value === null || value instanceof RegExp) {
clonedValue = value;
} else if (Array.isArray(value)) {
clonedValue = value.map(cloneRecursive);
} else if (typeof value === 'object') {
clonedValue = cloneRecursive(value);
} else {
clonedValue = value;
}
copy[property] = clonedValue;
});
return <T>copy;
};
return NodeUtils.parentize(cloneRecursive(astTree));
}
/**
* @param {string} code
* @returns {TStatement[]}
*/
public static convertCodeToStructure (code: string): TStatement[] {
let structure: ESTree.Program = esprima.parseScript(code);
structure = NodeUtils.addXVerbatimPropertyToLiterals(structure);
structure = NodeUtils.parentize(structure);
return structure.body;
}
/**
* @param {NodeGuards[]} structure
* @returns {string}
*/
public static convertStructureToCode (structure: ESTree.Node[]): string {
let code: string = '';
structure.forEach((node: ESTree.Node) => {
code += escodegen.generate(node, {
sourceMapWithCode: true
}).code;
});
return code;
}
/**
* @param {NodeGuards} node
* @param {number} index
* @returns {NodeGuards}
*/
public static getBlockStatementNodeByIndex (node: ESTree.Node, index: number = 0): ESTree.Node {
if (NodeGuards.isNodeHasBlockStatement(node)) {
if (node.body[index] === undefined) {
throw new ReferenceError(`Wrong index \`${index}\`. Block-statement body length is \`${node.body.length}\``);
}
return node.body[index];
}
throw new TypeError('The specified node have no a block-statement');
}
/**
* @param {NodeGuards} node
* @param {TNodeWithBlockStatement[]} blockScopes
* @returns {TNodeWithBlockStatement[]}
*/
public static getBlockScopesOfNode (node: ESTree.Node, blockScopes: TNodeWithBlockStatement[] = []): TNodeWithBlockStatement[] {
const parentNode: ESTree.Node | undefined = node.parentNode;
if (!parentNode) {
throw new ReferenceError('`parentNode` property of given node is `undefined`');
}
if (NodeGuards.isBlockStatementNode(parentNode)) {
if (!parentNode.parentNode) {
throw new ReferenceError('`parentNode` property of `parentNode` of given node is `undefined`');
}
if (NodeUtils.nodesWithBlockScope.includes(parentNode.parentNode.type)) {
blockScopes.push(parentNode);
}
}
if (node !== parentNode) {
return NodeUtils.getBlockScopesOfNode(parentNode, blockScopes);
}
if (NodeGuards.isNodeHasBlockStatement(parentNode)) {
blockScopes.push(parentNode);
}
return blockScopes;
}
/**
* @param {NodeGuards} node
* @param {number} depth
* @returns {number}
*/
public static getNodeBlockScopeDepth (node: ESTree.Node, depth: number = 0): number {
const parentNode: ESTree.Node | undefined = node.parentNode;
if (!parentNode) {
throw new ReferenceError('`parentNode` property of given node is `undefined`');
}
if (NodeGuards.isProgramNode(parentNode)) {
return depth;
}
if (NodeGuards.isBlockStatementNode(node) && NodeUtils.nodesWithBlockScope.includes(parentNode.type)) {
return NodeUtils.getNodeBlockScopeDepth(parentNode, ++depth);
}
return NodeUtils.getNodeBlockScopeDepth(parentNode, depth);
}
/**
* @param {UnaryExpression} unaryExpressionNode
* @returns {NodeGuards}
*/
public static getUnaryExpressionArgumentNode (unaryExpressionNode: ESTree.UnaryExpression): ESTree.Node {
if (NodeGuards.isUnaryExpressionNode(unaryExpressionNode.argument)) {
return NodeUtils.getUnaryExpressionArgumentNode(unaryExpressionNode.argument);
}
return unaryExpressionNode.argument;
}
/**
* @param {T} astTree
* @returns {T}
*/
public static parentize <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
estraverse.traverse(astTree, {
enter: NodeUtils.parentizeNode
});
return astTree;
}
/**
* @param {T} node
* @param {Node} parentNode
* @returns {T}
*/
public static parentizeNode <T extends ESTree.Node = ESTree.Node> (node: T, parentNode: ESTree.Node | null): T {
node.parentNode = parentNode || node;
node.obfuscatedNode = false;
return node;
}
}