forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeAppender.ts
More file actions
175 lines (152 loc) · 5.48 KB
/
NodeAppender.ts
File metadata and controls
175 lines (152 loc) · 5.48 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
import * as ESTree from 'estree';
import { TNodeWithStatements } from '../types/node/TNodeWithStatements';
import { TStatement } from '../types/node/TStatement';
import { IStackTraceData } from '../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
import { NodeGuards } from './NodeGuards';
export class NodeAppender {
/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
*/
public static append (nodeWithStatements: TNodeWithStatements, statements: TStatement[]): void {
statements = NodeAppender.parentizeScopeStatementsBeforeAppend(nodeWithStatements, statements);
NodeAppender.setScopeStatements(nodeWithStatements, [
...NodeAppender.getScopeStatements(nodeWithStatements),
...statements
]);
}
/**
* Appends node into a first deepest BlockStatement in order of function calls
*
* For example:
*
* function Foo () {
* var baz = function () {
*
* }
*
* baz();
* }
*
* foo();
*
* Appends node into block statement of `baz` function expression
*
* @param {IStackTraceData[]} stackTraceData
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} bodyStatements
* @param {number} index
*/
public static appendToOptimalBlockScope (
stackTraceData: IStackTraceData[],
nodeWithStatements: TNodeWithStatements,
bodyStatements: TStatement[],
index: number = 0
): void {
const targetBlockScope: TNodeWithStatements = stackTraceData.length
? NodeAppender.getOptimalBlockScope(stackTraceData, index)
: nodeWithStatements;
NodeAppender.prepend(targetBlockScope, bodyStatements);
}
/**
* Returns deepest block scope node at given deep.
*
* @param {IStackTraceData[]} stackTraceData
* @param {number} index
* @param {number} deep
* @returns {BlockStatement}
*/
public static getOptimalBlockScope (
stackTraceData: IStackTraceData[],
index: number,
deep: number = Infinity
): ESTree.BlockStatement {
const firstCall: IStackTraceData = stackTraceData[index];
if (deep <= 0) {
throw new Error('Invalid `deep` argument value. Value should be bigger then 0.');
}
if (deep > 1 && firstCall.stackTrace.length) {
return NodeAppender.getOptimalBlockScope(firstCall.stackTrace, 0, --deep);
} else {
return firstCall.callee;
}
}
/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
* @param {Node} target
*/
public static insertAfter (
nodeWithStatements: TNodeWithStatements,
statements: TStatement[],
target: ESTree.Statement
): void {
const indexInScopeStatement: number = NodeAppender
.getScopeStatements(nodeWithStatements)
.indexOf(target);
NodeAppender.insertAtIndex(nodeWithStatements, statements, indexInScopeStatement + 1);
}
/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
* @param {number} index
*/
public static insertAtIndex (
nodeWithStatements: TNodeWithStatements,
statements: TStatement[],
index: number
): void {
statements = NodeAppender.parentizeScopeStatementsBeforeAppend(nodeWithStatements, statements);
NodeAppender.setScopeStatements(nodeWithStatements, [
...NodeAppender.getScopeStatements(nodeWithStatements).slice(0, index),
...statements,
...NodeAppender.getScopeStatements(nodeWithStatements).slice(index)
]);
}
/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
*/
public static prepend (nodeWithStatements: TNodeWithStatements, statements: TStatement[]): void {
statements = NodeAppender.parentizeScopeStatementsBeforeAppend(nodeWithStatements, statements);
NodeAppender.setScopeStatements(nodeWithStatements, [
...statements,
...NodeAppender.getScopeStatements(nodeWithStatements),
]);
}
/**
* @param {TNodeWithStatements} nodeWithStatements
* @returns {TStatement[]}
*/
private static getScopeStatements (nodeWithStatements: TNodeWithStatements): TStatement[] {
if (NodeGuards.isSwitchCaseNode(nodeWithStatements)) {
return nodeWithStatements.consequent;
}
return nodeWithStatements.body;
}
/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
* @returns {TStatement[]}
*/
private static parentizeScopeStatementsBeforeAppend (
nodeWithStatements: TNodeWithStatements,
statements: TStatement[]
): TStatement[] {
statements.forEach((statement: TStatement) => {
statement.parentNode = nodeWithStatements;
});
return statements;
}
/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
*/
private static setScopeStatements (nodeWithStatements: TNodeWithStatements, statements: TStatement[]): void {
if (NodeGuards.isSwitchCaseNode(nodeWithStatements)) {
nodeWithStatements.consequent = <ESTree.Statement[]>statements;
return;
}
nodeWithStatements.body = statements;
}
}