Refactoring of unicodeEscapeSequence logic

This commit is contained in:
sanex
2020-09-25 17:37:18 +03:00
parent 2dc37c389f
commit 34b1619ee6
14 changed files with 101 additions and 48 deletions
@@ -9,6 +9,7 @@ import { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helper
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IStringArrayStorage } from '../../interfaces/storages/string-array-transformers/IStringArrayStorage';
import { IStringArrayStorageItemData } from '../../interfaces/storages/string-array-transformers/IStringArrayStorageItem';
import { initializable } from '../../decorators/Initializable';
@@ -16,6 +17,7 @@ import { StringArrayTemplate } from './templates/string-array/StringArrayTemplat
import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
import { NodeUtils } from '../../node/NodeUtils';
import { StringUtils } from '../../utils/StringUtils';
@injectable()
export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
@@ -81,7 +83,21 @@ export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
protected getCodeHelperTemplate (): string {
return this.customCodeHelperFormatter.formatTemplate(StringArrayTemplate(), {
stringArrayName: this.stringArrayName,
stringArray: this.stringArrayStorage.toString()
stringArrayStorageItems: this.getEncodedStringArrayStorageItems()
});
}
/**
* @returns {string}
*/
private getEncodedStringArrayStorageItems (): string {
return Array
.from(this.stringArrayStorage.getStorage().values())
.map((stringArrayStorageItemData: IStringArrayStorageItemData): string => {
const escapedEncodedValue: string = StringUtils.escapeJsString(stringArrayStorageItemData.encodedValue);
return `'${escapedEncodedValue}'`;
})
.toString();
}
}
@@ -3,6 +3,6 @@
*/
export function StringArrayTemplate (): string {
return `
const {stringArrayName} = [{stringArray}];
const {stringArrayName} = [{stringArrayStorageItems}];
`;
}
@@ -9,6 +9,7 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeGuards } from '../../node/NodeGuards';
@@ -18,6 +19,13 @@ import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class EscapeSequenceTransformer extends AbstractNodeTransformer {
/**
* @type {NodeTransformer[]}
*/
public readonly runAfter: NodeTransformer[] = [
NodeTransformer.CustomCodeHelpersTransformer
];
/**
* @type {IEscapeSequenceEncoder}
*/
@@ -68,12 +76,11 @@ export class EscapeSequenceTransformer extends AbstractNodeTransformer {
return literalNode;
}
const newLiteralNode: ESTree.Literal = NodeFactory.literalNode(
this.escapeSequenceEncoder.encode(
literalNode.value,
this.options.unicodeEscapeSequence
)
const encodedValue: string = this.escapeSequenceEncoder.encode(
literalNode.value,
this.options.unicodeEscapeSequence
);
const newLiteralNode: ESTree.Literal = NodeFactory.literalNode(encodedValue);
NodeUtils.parentizeNode(newLiteralNode, parentNode);
@@ -22,6 +22,7 @@ export class EvalCallExpressionTransformer extends AbstractNodeTransformer {
* @type {NodeTransformer.ParentificationTransformer[]}
*/
public readonly runAfter: NodeTransformer[] = [
NodeTransformer.EscapeSequenceTransformer,
NodeTransformer.ParentificationTransformer,
NodeTransformer.VariablePreserveTransformer
];
@@ -7,7 +7,6 @@ import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
import { ICryptUtilsSwappedAlphabet } from '../../interfaces/utils/ICryptUtilsSwappedAlphabet';
import { IEncodedValue } from '../../interfaces/IEncodedValue';
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
@@ -55,11 +54,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
*/
private readonly cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet;
/**
* @type {IEscapeSequenceEncoder}
*/
private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
/**
* @type {IIdentifierNamesGenerator}
*/
@@ -96,7 +90,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
* @param {ICryptUtilsSwappedAlphabet} cryptUtilsSwappedAlphabet
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
*/
public constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
@@ -104,15 +97,13 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
@inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions,
@inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet,
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
@inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet
) {
super(randomGenerator, options);
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
this.arrayUtils = arrayUtils;
this.cryptUtilsSwappedAlphabet = cryptUtilsSwappedAlphabet;
this.escapeSequenceEncoder = escapeSequenceEncoder;
this.rc4Keys = this.randomGenerator.getRandomGenerator()
.n(
@@ -225,21 +216,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
);
}
/**
* @returns {string}
*/
public toString (): string {
return Array
.from(this.storage.values())
.map((stringArrayStorageItemData: IStringArrayStorageItemData) => {
// we have to encode here, because of possible errors during `parse` of StringArrayCustomNode
return `'${this.escapeSequenceEncoder.encode(
stringArrayStorageItemData.encodedValue,
this.options.unicodeEscapeSequence
)}'`;
}).toString();
}
/**
* @param {string} value
* @returns {IStringArrayStorageItemData}
+11
View File
@@ -0,0 +1,11 @@
import jsStringEscape from 'js-string-escape';
export class StringUtils {
/**
* @param {string} string
* @returns {string}
*/
public static escapeJsString (string: string): string {
return jsStringEscape(string);
}
}