forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringArrayScopeCallsWrapperFunctionNode.ts
More file actions
137 lines (117 loc) · 5.34 KB
/
Copy pathStringArrayScopeCallsWrapperFunctionNode.ts
File metadata and controls
137 lines (117 loc) · 5.34 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
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
import { TStatement } from '../../types/node/TStatement';
import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { initializable } from '../../decorators/Initializable';
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class StringArrayScopeCallsWrapperFunctionNode extends AbstractStringArrayCallNode {
/**
* @type {number}
*/
@initializable()
private shiftedIndex!: number;
/**
* @type {string}
*/
@initializable()
private stringArrayCallsWrapperName!: string;
/**
* @type {string}
*/
@initializable()
private stringArrayScopeCallsWrapperName!: string;
/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
public constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(
identifierNamesGeneratorFactory,
customCodeHelperFormatter,
randomGenerator,
options
);
}
/**
* @param {string} stringArrayScopeCallsWrapperName
* @param {string} stringArrayCallsWrapperName
* @param {number} shiftedIndex
*/
public initialize (
stringArrayScopeCallsWrapperName: string,
stringArrayCallsWrapperName: string,
shiftedIndex: number
): void {
this.stringArrayScopeCallsWrapperName = stringArrayScopeCallsWrapperName;
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
this.shiftedIndex = shiftedIndex;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
// identifiers of function expression parameters
// as a temporary names use random strings
const firstParameterIdentifierNode: ESTree.Identifier = NodeFactory.identifierNode(this.randomGenerator.getRandomString(6));
const secondParameterIdentifierNode: ESTree.Identifier = NodeFactory.identifierNode(this.randomGenerator.getRandomString(6));
// identifiers of call to the parent string array scope wrapper
// as a temporary names use random strings
const firstCallArgumentIdentifierNode: ESTree.Identifier = NodeFactory.identifierNode(this.randomGenerator.getRandomString(6));
const secondCallArgumentIdentifierNode: ESTree.Identifier = NodeFactory.identifierNode(this.randomGenerator.getRandomString(6));
// function expression node
const functionExpressionNode: ESTree.FunctionExpression = NodeFactory.functionExpressionNode(
[
firstParameterIdentifierNode,
secondParameterIdentifierNode,
],
NodeFactory.blockStatementNode([
NodeFactory.returnStatementNode(
NodeFactory.callExpressionNode(
NodeFactory.identifierNode(this.stringArrayCallsWrapperName),
[
NodeFactory.binaryExpressionNode(
'-',
firstCallArgumentIdentifierNode,
this.getHexadecimalNode(this.shiftedIndex)
),
secondCallArgumentIdentifierNode
]
)
)
])
);
const structure: TStatement = NodeFactory.variableDeclarationNode(
[
NodeFactory.variableDeclaratorNode(
NodeFactory.identifierNode(this.stringArrayScopeCallsWrapperName),
functionExpressionNode
)
],
'const',
);
NodeUtils.parentizeAst(structure);
// have to generate names for both parameter and call identifiers
const firstParameterName: string = this.identifierNamesGenerator.generateForLexicalScope(functionExpressionNode);
const secondParameterName: string = this.identifierNamesGenerator.generateForLexicalScope(functionExpressionNode);
firstParameterIdentifierNode.name = firstParameterName;
secondParameterIdentifierNode.name = secondParameterName;
firstCallArgumentIdentifierNode.name = firstParameterName;
secondCallArgumentIdentifierNode.name = secondParameterName;
return [structure];
}
}