forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeStatementUtils.ts
More file actions
135 lines (112 loc) · 4.27 KB
/
NodeStatementUtils.ts
File metadata and controls
135 lines (112 loc) · 4.27 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
import * as ESTree from 'estree';
import { TNodeWithStatements } from '../types/node/TNodeWithStatements';
import { TStatement } from '../types/node/TStatement';
import { NodeGuards } from './NodeGuards';
export class NodeStatementUtils {
/**
* @param {Node} node
* @returns {TNodeWithStatements}
*/
public static getParentNodeWithStatements (node: ESTree.Node): TNodeWithStatements {
return NodeStatementUtils.getParentNodesWithStatementsRecursive(node, 1)[0];
}
/**
* @param {Node} node
* @returns {TNodeWithStatements[]}
*/
public static getParentNodesWithStatements (node: ESTree.Node): TNodeWithStatements[] {
return NodeStatementUtils.getParentNodesWithStatementsRecursive(node);
}
/**
* @param {Statement} statement
* @returns {TStatement | null}
*/
public static getNextSiblingStatement (statement: ESTree.Statement): TStatement | null {
return NodeStatementUtils.getSiblingStatementByOffset(statement, 1);
}
/**
* @param {Statement} statement
* @returns {TStatement | null}
*/
public static getPreviousSiblingStatement (statement: ESTree.Statement): TStatement | null {
return NodeStatementUtils.getSiblingStatementByOffset(statement, -1);
}
/**
* @param {Node} node
* @returns {Statement}
*/
public static getRootStatementOfNode (node: ESTree.Node): ESTree.Statement {
if (NodeGuards.isProgramNode(node)) {
throw new Error('Unable to find root statement for `Program` node');
}
const parentNode: ESTree.Node | undefined = node.parentNode;
if (!parentNode) {
throw new ReferenceError('`parentNode` property of given node is `undefined`');
}
if (!NodeGuards.isNodeWithStatements(parentNode)) {
return NodeStatementUtils.getRootStatementOfNode(parentNode);
}
return <ESTree.Statement>node;
}
/**
* @param {NodeGuards} node
* @returns {TNodeWithStatements}
*/
public static getScopeOfNode (node: ESTree.Node): TNodeWithStatements {
const parentNode: ESTree.Node | undefined = node.parentNode;
if (!parentNode) {
throw new ReferenceError('`parentNode` property of given node is `undefined`');
}
if (!NodeGuards.isNodeWithStatements(parentNode)) {
return NodeStatementUtils.getScopeOfNode(parentNode);
}
return parentNode;
}
/***
* @param {Node} node
* @param {number} maxSize
* @param {TNodeWithStatements[]} nodesWithStatements
* @param {number} depth
* @returns {TNodeWithStatements[]}
*/
private static getParentNodesWithStatementsRecursive (
node: ESTree.Node,
maxSize: number = Infinity,
nodesWithStatements: TNodeWithStatements[] = [],
depth: number = 0
): TNodeWithStatements[] {
if (nodesWithStatements.length >= maxSize) {
return nodesWithStatements;
}
const parentNode: ESTree.Node | undefined = node.parentNode;
if (!parentNode) {
throw new ReferenceError('`parentNode` property of given node is `undefined`');
}
if (
/**
* we can add program node instantly
*/
NodeGuards.isProgramNode(node) ||
(NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode) && depth > 0)
) {
nodesWithStatements.push(node);
}
if (node !== parentNode) {
return NodeStatementUtils.getParentNodesWithStatementsRecursive(parentNode, maxSize, nodesWithStatements, ++depth);
}
return nodesWithStatements;
}
/**
* @param {Statement} statement
* @param {number} offset
* @returns {TStatement | null}
*/
private static getSiblingStatementByOffset (statement: ESTree.Statement, offset: number): TStatement | null {
const scopeNode: TNodeWithStatements = NodeStatementUtils.getScopeOfNode(statement);
const scopeBody: TStatement[] = !NodeGuards.isSwitchCaseNode(scopeNode)
? scopeNode.body
: scopeNode.consequent;
const indexInScope: number = scopeBody.indexOf(statement);
return scopeBody[indexInScope + offset] || null;
}
}