forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeLexicalScopeUtils.ts
More file actions
57 lines (47 loc) · 1.72 KB
/
NodeLexicalScopeUtils.ts
File metadata and controls
57 lines (47 loc) · 1.72 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
import * as ESTree from 'estree';
import { TNodeWithLexicalScope } from '../types/node/TNodeWithLexicalScope';
import { NodeGuards } from './NodeGuards';
export class NodeLexicalScopeUtils {
/**
* @param {Node} node
* @returns {TNodeWithLexicalScope}
*/
public static getLexicalScope (node: ESTree.Node): TNodeWithLexicalScope | undefined {
return NodeLexicalScopeUtils.getLexicalScopesRecursive(node, 1)[0];
}
/**
* @param {Node} node
* @returns {TNodeWithLexicalScope[]}
*/
public static getLexicalScopes (node: ESTree.Node): TNodeWithLexicalScope[] {
return NodeLexicalScopeUtils.getLexicalScopesRecursive(node);
}
/***
* @param {Node} node
* @param {number} maxSize
* @param {TNodeWithLexicalScope[]} nodesWithLexicalScope
* @param {number} depth
* @returns {TNodeWithLexicalScope[]}
*/
private static getLexicalScopesRecursive (
node: ESTree.Node,
maxSize: number = Infinity,
nodesWithLexicalScope: TNodeWithLexicalScope[] = [],
depth: number = 0
): TNodeWithLexicalScope[] {
if (nodesWithLexicalScope.length >= maxSize) {
return nodesWithLexicalScope;
}
const parentNode: ESTree.Node | undefined = node.parentNode;
if (!parentNode) {
throw new ReferenceError('`parentNode` property of given node is `undefined`');
}
if (NodeGuards.isNodeWithLexicalScope(node)) {
nodesWithLexicalScope.push(node);
}
if (node !== parentNode) {
return NodeLexicalScopeUtils.getLexicalScopesRecursive(parentNode, maxSize, nodesWithLexicalScope, ++depth);
}
return nodesWithLexicalScope;
}
}