forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCodeHelperFormatter.ts
More file actions
65 lines (54 loc) · 2.11 KB
/
Copy pathCustomCodeHelperFormatter.ts
File metadata and controls
65 lines (54 loc) · 2.11 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
import { inject, injectable } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import * as estraverse from '@javascript-obfuscator/estraverse';
import * as ESTree from 'estree';
import format from 'string-template';
import { TDictionary } from '../types/TDictionary';
import { TStatement } from '../types/node/TStatement';
import { ICustomCodeHelperFormatter } from '../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
import { IPrevailingKindOfVariablesAnalyzer } from '../interfaces/analyzers/calls-graph-analyzer/IPrevailingKindOfVariablesAnalyzer';
import { NodeGuards } from '../node/NodeGuards';
@injectable()
export class CustomCodeHelperFormatter implements ICustomCodeHelperFormatter {
/**
* @type {ESTree.VariableDeclaration['kind']}
*/
private readonly prevailingKindOfVariables: ESTree.VariableDeclaration['kind'];
public constructor (
@inject(ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer)
prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer
) {
this.prevailingKindOfVariables = prevailingKindOfVariablesAnalyzer.getPrevailingKind();
}
/**
* @param {string} template
* @param {TMapping} mapping
* @returns {string}
*/
public formatTemplate <TMapping extends TDictionary> (
template: string,
mapping: TMapping
): string {
return format(template, mapping);
}
/**
* @param {TStatement[]} statements
* @returns {TStatement[]}
*/
public formatStructure (statements: TStatement[]): TStatement[] {
for (const statement of statements) {
estraverse.replace(statement, {
enter: (node: ESTree.Node): ESTree.Node | void => {
if (!NodeGuards.isVariableDeclarationNode(node)) {
return;
}
if (this.prevailingKindOfVariables === 'var') {
node.kind = 'var';
}
return node;
}
});
}
return statements;
}
}