Skip to content

Commit 34b1619

Browse files
committed
Refactoring of unicodeEscapeSequence logic
1 parent 2dc37c3 commit 34b1619

File tree

14 files changed

+101
-48
lines changed

14 files changed

+101
-48
lines changed

dist/index.browser.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"eventemitter3": "4.0.7",
3535
"fast-deep-equal": "3.1.3",
3636
"inversify": "5.0.1",
37-
"js-string-escape": "1.0.1",
37+
"js-string-escape": "^1.0.1",
3838
"md5": "2.3.0",
3939
"mkdirp": "1.0.4",
4040
"multimatch": "4.0.0",
@@ -50,6 +50,7 @@
5050
"@types/eslint-scope": "3.7.0",
5151
"@types/estraverse": "5.1.0",
5252
"@types/estree": "0.0.45",
53+
"@types/js-string-escape": "^1.0.0",
5354
"@types/md5": "2.2.0",
5455
"@types/mkdirp": "1.0.1",
5556
"@types/mocha": "8.0.3",

src/custom-code-helpers/string-array/StringArrayCodeHelper.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helper
99
import { IOptions } from '../../interfaces/options/IOptions';
1010
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
1111
import { IStringArrayStorage } from '../../interfaces/storages/string-array-transformers/IStringArrayStorage';
12+
import { IStringArrayStorageItemData } from '../../interfaces/storages/string-array-transformers/IStringArrayStorageItem';
1213

1314
import { initializable } from '../../decorators/Initializable';
1415

1516
import { StringArrayTemplate } from './templates/string-array/StringArrayTemplate';
1617

1718
import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
1819
import { NodeUtils } from '../../node/NodeUtils';
20+
import { StringUtils } from '../../utils/StringUtils';
1921

2022
@injectable()
2123
export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
@@ -81,7 +83,21 @@ export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
8183
protected getCodeHelperTemplate (): string {
8284
return this.customCodeHelperFormatter.formatTemplate(StringArrayTemplate(), {
8385
stringArrayName: this.stringArrayName,
84-
stringArray: this.stringArrayStorage.toString()
86+
stringArrayStorageItems: this.getEncodedStringArrayStorageItems()
8587
});
8688
}
89+
90+
/**
91+
* @returns {string}
92+
*/
93+
private getEncodedStringArrayStorageItems (): string {
94+
return Array
95+
.from(this.stringArrayStorage.getStorage().values())
96+
.map((stringArrayStorageItemData: IStringArrayStorageItemData): string => {
97+
const escapedEncodedValue: string = StringUtils.escapeJsString(stringArrayStorageItemData.encodedValue);
98+
99+
return `'${escapedEncodedValue}'`;
100+
})
101+
.toString();
102+
}
87103
}

src/custom-code-helpers/string-array/templates/string-array/StringArrayTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
*/
44
export function StringArrayTemplate (): string {
55
return `
6-
const {stringArrayName} = [{stringArray}];
6+
const {stringArrayName} = [{stringArrayStorageItems}];
77
`;
88
}

src/node-transformers/finalizing-transformers/EscapeSequenceTransformer.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
99
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
1010

1111
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
12+
import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
1213

1314
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
1415
import { NodeGuards } from '../../node/NodeGuards';
@@ -18,6 +19,13 @@ import { NodeUtils } from '../../node/NodeUtils';
1819

1920
@injectable()
2021
export class EscapeSequenceTransformer extends AbstractNodeTransformer {
22+
/**
23+
* @type {NodeTransformer[]}
24+
*/
25+
public readonly runAfter: NodeTransformer[] = [
26+
NodeTransformer.CustomCodeHelpersTransformer
27+
];
28+
2129
/**
2230
* @type {IEscapeSequenceEncoder}
2331
*/
@@ -68,12 +76,11 @@ export class EscapeSequenceTransformer extends AbstractNodeTransformer {
6876
return literalNode;
6977
}
7078

71-
const newLiteralNode: ESTree.Literal = NodeFactory.literalNode(
72-
this.escapeSequenceEncoder.encode(
73-
literalNode.value,
74-
this.options.unicodeEscapeSequence
75-
)
79+
const encodedValue: string = this.escapeSequenceEncoder.encode(
80+
literalNode.value,
81+
this.options.unicodeEscapeSequence
7682
);
83+
const newLiteralNode: ESTree.Literal = NodeFactory.literalNode(encodedValue);
7784

7885
NodeUtils.parentizeNode(newLiteralNode, parentNode);
7986

src/node-transformers/preparing-transformers/EvalCallExpressionTransformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class EvalCallExpressionTransformer extends AbstractNodeTransformer {
2222
* @type {NodeTransformer.ParentificationTransformer[]}
2323
*/
2424
public readonly runAfter: NodeTransformer[] = [
25+
NodeTransformer.EscapeSequenceTransformer,
2526
NodeTransformer.ParentificationTransformer,
2627
NodeTransformer.VariablePreserveTransformer
2728
];

src/storages/string-array-transformers/StringArrayStorage.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
77
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
88
import { ICryptUtilsSwappedAlphabet } from '../../interfaces/utils/ICryptUtilsSwappedAlphabet';
99
import { IEncodedValue } from '../../interfaces/IEncodedValue';
10-
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
1110
import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
1211
import { IOptions } from '../../interfaces/options/IOptions';
1312
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
@@ -55,11 +54,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
5554
*/
5655
private readonly cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet;
5756

58-
/**
59-
* @type {IEscapeSequenceEncoder}
60-
*/
61-
private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
62-
6357
/**
6458
* @type {IIdentifierNamesGenerator}
6559
*/
@@ -96,23 +90,20 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
9690
* @param {IRandomGenerator} randomGenerator
9791
* @param {IOptions} options
9892
* @param {ICryptUtilsSwappedAlphabet} cryptUtilsSwappedAlphabet
99-
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
10093
*/
10194
public constructor (
10295
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
10396
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
10497
@inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
10598
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
10699
@inject(ServiceIdentifiers.IOptions) options: IOptions,
107-
@inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet,
108-
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
100+
@inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet
109101
) {
110102
super(randomGenerator, options);
111103

112104
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
113105
this.arrayUtils = arrayUtils;
114106
this.cryptUtilsSwappedAlphabet = cryptUtilsSwappedAlphabet;
115-
this.escapeSequenceEncoder = escapeSequenceEncoder;
116107

117108
this.rc4Keys = this.randomGenerator.getRandomGenerator()
118109
.n(
@@ -225,21 +216,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
225216
);
226217
}
227218

228-
/**
229-
* @returns {string}
230-
*/
231-
public toString (): string {
232-
return Array
233-
.from(this.storage.values())
234-
.map((stringArrayStorageItemData: IStringArrayStorageItemData) => {
235-
// we have to encode here, because of possible errors during `parse` of StringArrayCustomNode
236-
return `'${this.escapeSequenceEncoder.encode(
237-
stringArrayStorageItemData.encodedValue,
238-
this.options.unicodeEscapeSequence
239-
)}'`;
240-
}).toString();
241-
}
242-
243219
/**
244220
* @param {string} value
245221
* @returns {IStringArrayStorageItemData}

src/utils/StringUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import jsStringEscape from 'js-string-escape';
2+
3+
export class StringUtils {
4+
/**
5+
* @param {string} string
6+
* @returns {string}
7+
*/
8+
public static escapeJsString (string: string): string {
9+
return jsStringEscape(string);
10+
}
11+
}

0 commit comments

Comments
 (0)