forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectExpressionTransformer.ts
More file actions
123 lines (105 loc) · 3.91 KB
/
Copy pathObjectExpressionTransformer.ts
File metadata and controls
123 lines (105 loc) · 3.91 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
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
import { TransformationStage } from '../../enums/node-transformers/TransformationStage';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeGuards } from '../../node/NodeGuards';
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
/**
* replaces:
* var object = { PSEUDO: 1 };
*
* on:
* var object = { 'PSEUDO': 1 };
*/
@injectable()
export class ObjectExpressionTransformer extends AbstractNodeTransformer {
/**
* @type {IEscapeSequenceEncoder}
*/
private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
/**
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(randomGenerator, options);
this.escapeSequenceEncoder = escapeSequenceEncoder;
}
/**
* @param {TransformationStage} transformationStage
* @returns {IVisitor | null}
*/
public getVisitor (transformationStage: TransformationStage): IVisitor | null {
switch (transformationStage) {
case TransformationStage.Converting:
return {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
if (parentNode && NodeGuards.isObjectExpressionNode(node)) {
return this.transformNode(node, parentNode);
}
}
};
default:
return null;
}
}
/**
* @param {ObjectExpression} objectExpressionNode
* @param {NodeGuards} parentNode
* @returns {NodeGuards}
*/
public transformNode (objectExpressionNode: ESTree.ObjectExpression, parentNode: ESTree.Node): ESTree.Node {
objectExpressionNode.properties
.forEach((property: ESTree.Property) => {
if (!property.key) {
return;
}
if (property.computed) {
this.transformComputedProperty(property);
} else {
this.transformBaseProperty(property);
}
});
return objectExpressionNode;
}
/**
* @param {Property} property
*/
private transformComputedProperty (property: ESTree.Property): void {
if (!NodeGuards.isLiteralNode(property.key) || !(typeof property.key.value === 'string')) {
return;
}
property.key = NodeFactory.literalNode(this.getPropertyKeyValue(property.key.value));
}
/**
* @param {Property} property
*/
private transformBaseProperty (property: ESTree.Property): void {
if (property.shorthand) {
property.shorthand = false;
}
if (!NodeGuards.isIdentifierNode(property.key)) {
return;
}
property.key = NodeFactory.literalNode(this.getPropertyKeyValue(property.key.name));
}
/**
* @param {string} inputValue
* @returns {string}
*/
private getPropertyKeyValue (inputValue: string): string {
return this.options.unicodeEscapeSequence
? this.escapeSequenceEncoder.encode(inputValue, true)
: inputValue;
}
}