Skip to content

Commit 33441eb

Browse files
committed
Shifted indexes of string array wrappers #3: added ability to generate a negative shifted index
1 parent 3036e8b commit 33441eb

File tree

8 files changed

+98
-49
lines changed

8 files changed

+98
-49
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.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { inject, injectable, } from 'inversify';
2+
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
3+
4+
import * as ESTree from 'estree';
5+
6+
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
7+
8+
import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
9+
import { IOptions } from '../../interfaces/options/IOptions';
10+
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
11+
12+
import { AbstractCustomNode } from '../AbstractCustomNode';
13+
import { NodeFactory } from '../../node/NodeFactory';
14+
import { NodeMetadata } from '../../node/NodeMetadata';
15+
import { NodeUtils } from '../../node/NodeUtils';
16+
import { NumberUtils } from '../../utils/NumberUtils';
17+
18+
@injectable()
19+
export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
20+
/**
21+
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
22+
* @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
23+
* @param {IRandomGenerator} randomGenerator
24+
* @param {IOptions} options
25+
*/
26+
protected constructor (
27+
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
28+
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
29+
@inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
30+
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
31+
@inject(ServiceIdentifiers.IOptions) options: IOptions
32+
) {
33+
super(
34+
identifierNamesGeneratorFactory,
35+
customCodeHelperFormatter,
36+
randomGenerator,
37+
options
38+
);
39+
}
40+
41+
/**
42+
* @param {number} index
43+
* @returns {Expression}
44+
*/
45+
protected getHexadecimalNode (index: number): ESTree.Expression {
46+
const isPositive: boolean = index >= 0;
47+
const normalizedIndex: number = Math.abs(index);
48+
49+
const hexadecimalIndex: string = NumberUtils.toHex(normalizedIndex);
50+
const hexadecimalLiteralNode: ESTree.Literal = NodeFactory.literalNode(hexadecimalIndex);
51+
52+
NodeMetadata.set(hexadecimalLiteralNode, { replacedLiteral: true });
53+
54+
const hexadecimalNode: ESTree.Expression = isPositive
55+
? hexadecimalLiteralNode
56+
: NodeFactory.unaryExpressionNode(
57+
'-',
58+
hexadecimalLiteralNode
59+
);
60+
61+
NodeUtils.parentizeAst(hexadecimalNode);
62+
63+
return hexadecimalNode;
64+
}
65+
66+
/**
67+
* @param {string} decodeKey
68+
* @returns {Literal}
69+
*/
70+
protected getRc4KeyLiteralNode (decodeKey: string): ESTree.Literal {
71+
const rc4KeyLiteralNode: ESTree.Literal = NodeFactory.literalNode(decodeKey);
72+
73+
NodeMetadata.set(rc4KeyLiteralNode, { replacedLiteral: true });
74+
75+
return rc4KeyLiteralNode;
76+
}
77+
}

src/custom-nodes/string-array-nodes/StringArrayCallNode.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
1212

1313
import { initializable } from '../../decorators/Initializable';
1414

15-
import { AbstractCustomNode } from '../AbstractCustomNode';
15+
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
1616
import { NodeFactory } from '../../node/NodeFactory';
17-
import { NodeMetadata } from '../../node/NodeMetadata';
1817
import { NodeUtils } from '../../node/NodeUtils';
19-
import { NumberUtils } from '../../utils/NumberUtils';
2018

2119
@injectable()
22-
export class StringArrayCallNode extends AbstractCustomNode {
20+
export class StringArrayCallNode extends AbstractStringArrayCallNode {
2321
/**
2422
* @type {string | null}
2523
*/
@@ -60,31 +58,6 @@ export class StringArrayCallNode extends AbstractCustomNode {
6058
);
6159
}
6260

63-
/**
64-
* @param {string} index
65-
* @returns {Literal}
66-
*/
67-
public static getHexadecimalLiteralNode (index: number): ESTree.Literal {
68-
const hexadecimalIndex: string = NumberUtils.toHex(index);
69-
const hexadecimalLiteralNode: ESTree.Literal = NodeFactory.literalNode(hexadecimalIndex);
70-
71-
NodeMetadata.set(hexadecimalLiteralNode, { replacedLiteral: true });
72-
73-
return hexadecimalLiteralNode;
74-
}
75-
76-
/**
77-
* @param {string} decodeKey
78-
* @returns {Literal}
79-
*/
80-
private static getRc4KeyLiteralNode (decodeKey: string): ESTree.Literal {
81-
const rc4KeyLiteralNode: ESTree.Literal = NodeFactory.literalNode(decodeKey);
82-
83-
NodeMetadata.set(rc4KeyLiteralNode, { replacedLiteral: true });
84-
85-
return rc4KeyLiteralNode;
86-
}
87-
8861
/**
8962
* @param {string} stringArrayCallsWrapperName
9063
* @param {number} index
@@ -104,12 +77,12 @@ export class StringArrayCallNode extends AbstractCustomNode {
10477
* @returns {TStatement[]}
10578
*/
10679
protected getNodeStructure (): TStatement[] {
107-
const callExpressionArgs: ESTree.Literal[] = [
108-
StringArrayCallNode.getHexadecimalLiteralNode(this.index)
80+
const callExpressionArgs: ESTree.Expression[] = [
81+
this.getHexadecimalNode(this.index)
10982
];
11083

11184
if (this.decodeKey) {
112-
callExpressionArgs.push(StringArrayCallNode.getRc4KeyLiteralNode(this.decodeKey));
85+
callExpressionArgs.push(this.getRc4KeyLiteralNode(this.decodeKey));
11386
}
11487

11588
const stringArrayIdentifierNode: ESTree.Identifier =

src/custom-nodes/string-array-nodes/StringArrayScopeCallsWrapperFunctionNode.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
1212

1313
import { initializable } from '../../decorators/Initializable';
1414

15-
import { AbstractCustomNode } from '../AbstractCustomNode';
15+
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
1616
import { NodeFactory } from '../../node/NodeFactory';
1717
import { NodeUtils } from '../../node/NodeUtils';
18-
import { StringArrayCallNode } from './StringArrayCallNode';
1918

2019
@injectable()
21-
export class StringArrayScopeCallsWrapperFunctionNode extends AbstractCustomNode {
20+
export class StringArrayScopeCallsWrapperFunctionNode extends AbstractStringArrayCallNode {
2221
/**
2322
* @type {number}
2423
*/
@@ -102,7 +101,7 @@ export class StringArrayScopeCallsWrapperFunctionNode extends AbstractCustomNode
102101
NodeFactory.binaryExpressionNode(
103102
'-',
104103
firstCallArgumentIdentifierNode,
105-
StringArrayCallNode.getHexadecimalLiteralNode(this.shiftedIndex)
104+
this.getHexadecimalNode(this.shiftedIndex)
106105
),
107106
secondCallArgumentIdentifierNode
108107
]

src/custom-nodes/string-array-nodes/StringArrayScopeCallsWrapperVariableNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
1010

1111
import { initializable } from '../../decorators/Initializable';
1212

13-
import { AbstractCustomNode } from '../AbstractCustomNode';
13+
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
1414
import { NodeFactory } from '../../node/NodeFactory';
1515
import { NodeUtils } from '../../node/NodeUtils';
1616

1717
@injectable()
18-
export class StringArrayScopeCallsWrapperVariableNode extends AbstractCustomNode {
18+
export class StringArrayScopeCallsWrapperVariableNode extends AbstractStringArrayCallNode {
1919
/**
2020
* @type {string}
2121
*/

src/node-transformers/string-array-transformers/StringArrayTransformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
4242
/**
4343
* @type {number}
4444
*/
45-
private static readonly minShiftedIndexValue: number = 10;
45+
private static readonly minShiftedIndexValue: number = -1000;
4646

4747
/**
4848
* @type {number}
4949
*/
50-
private static readonly maxShiftedIndexValue: number = 100;
50+
private static readonly maxShiftedIndexValue: number = 1000;
5151

5252
/**
5353
* @type {IEscapeSequenceEncoder}

0 commit comments

Comments
 (0)