mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Merge branch 'master' into string-array-wrappers-type-option
# Conflicts: # CHANGELOG.md # dist/index.browser.js # dist/index.cli.js # dist/index.js # package.json # src/node-transformers/string-array-transformers/StringArrayScopeCallsWrapperTransformer.ts
This commit is contained in:
+5
-1
@@ -2,7 +2,11 @@ Change Log
|
||||
|
||||
v2.3.0
|
||||
---
|
||||
* **New option:** `stringArrayWrappersType` allows to select a type of the wrappers that are appending by the `stringArrayWrappersCount` option
|
||||
* **New option:** `stringArrayWrappersType` allows to select a type of the wrappers that are appending by the `stringArrayWrappersCount` option
|
||||
|
||||
v2.2.1
|
||||
---
|
||||
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/745
|
||||
|
||||
v2.2.0
|
||||
---
|
||||
|
||||
Vendored
+2
-2
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+2
-2
@@ -1,10 +1,10 @@
|
||||
import { TNodeWithLexicalScope } from '../../../types/node/TNodeWithLexicalScope';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../../types/node/TNodeWithLexicalScopeStatements';
|
||||
import { TStringArrayScopeCallsWrapperDataByEncoding } from '../../../types/node-transformers/string-array-transformers/TStringArrayScopeCallsWrapperDataByEncoding';
|
||||
|
||||
import { IMapStorage } from '../IMapStorage';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IStringArrayScopeCallsWrapperDataStorage extends IMapStorage<
|
||||
TNodeWithLexicalScope,
|
||||
TNodeWithLexicalScopeStatements,
|
||||
TStringArrayScopeCallsWrapperDataByEncoding
|
||||
> {}
|
||||
|
||||
+8
-9
@@ -1,21 +1,20 @@
|
||||
import { TNodeWithLexicalScopeAndStatements } from '../../../types/node/TNodeWithLexicalScopeAndStatements';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../../types/node/TNodeWithLexicalScopeStatements';
|
||||
|
||||
import { IArrayStorage } from '../IArrayStorage';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IVisitedLexicalScopeNodesStackStorage extends IArrayStorage<TNodeWithLexicalScopeAndStatements> {
|
||||
export interface IVisitedLexicalScopeNodesStackStorage extends IArrayStorage<TNodeWithLexicalScopeStatements> {
|
||||
/**
|
||||
* @returns {TNodeWithLexicalScopeAndStatements | undefined}
|
||||
* @returns {TNodeWithLexicalScopeStatements | undefined}
|
||||
*/
|
||||
getLastElement (): TNodeWithLexicalScopeAndStatements | undefined;
|
||||
getLastElement (): TNodeWithLexicalScopeStatements | undefined;
|
||||
|
||||
/**
|
||||
* @returns {TNodeWithLexicalScopeAndStatements | undefined}
|
||||
* @returns {TNodeWithLexicalScopeStatements | undefined}
|
||||
*/
|
||||
pop (): TNodeWithLexicalScopeAndStatements | undefined;
|
||||
pop (): TNodeWithLexicalScopeStatements | undefined;
|
||||
|
||||
/**
|
||||
* @param {TNodeWithLexicalScopeAndStatements} lexicalScopeNode
|
||||
* @param {TNodeWithLexicalScopeStatements} lexicalScopeBodyNode
|
||||
*/
|
||||
push (lexicalScopeNode: TNodeWithLexicalScopeAndStatements): void;
|
||||
push (lexicalScopeBodyNode: TNodeWithLexicalScopeStatements): void;
|
||||
}
|
||||
|
||||
+20
-23
@@ -4,8 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TInitialData } from '../../types/TInitialData';
|
||||
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
|
||||
import { TNodeWithLexicalScopeAndStatements } from '../../types/node/TNodeWithLexicalScopeAndStatements';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
|
||||
import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
|
||||
import { TStringArrayScopeCallsWrapperDataByEncoding } from '../../types/node-transformers/string-array-transformers/TStringArrayScopeCallsWrapperDataByEncoding';
|
||||
import { TStringArrayTransformerCustomNodeFactory } from '../../types/container/custom-nodes/TStringArrayTransformerCustomNodeFactory';
|
||||
@@ -89,13 +88,13 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
|
||||
switch (nodeTransformationStage) {
|
||||
case NodeTransformationStage.StringArray:
|
||||
return {
|
||||
enter: (node: ESTree.Node): void => {
|
||||
if (NodeGuards.isNodeWithLexicalScopeAndStatements(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
|
||||
if (parentNode && NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode)) {
|
||||
this.onLexicalScopeNodeEnter(node);
|
||||
}
|
||||
},
|
||||
leave: (node: ESTree.Node): ESTree.Node | undefined => {
|
||||
if (NodeGuards.isNodeWithLexicalScopeAndStatements(node)) {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
|
||||
if (parentNode && NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode)) {
|
||||
this.onLexicalScopeNodeLeave();
|
||||
|
||||
return this.transformNode(node);
|
||||
@@ -109,20 +108,17 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithLexicalScopeAndStatements} lexicalScopeNode
|
||||
* @returns {TNodeWithLexicalScopeAndStatements}
|
||||
* @param {TNodeWithLexicalScopeStatements} lexicalScopeBodyNode
|
||||
* @returns {TNodeWithLexicalScopeStatements}
|
||||
*/
|
||||
public transformNode (lexicalScopeNode: TNodeWithLexicalScopeAndStatements): TNodeWithLexicalScopeAndStatements {
|
||||
const lexicalScopeBodyNode: ESTree.Program | ESTree.BlockStatement =
|
||||
NodeGuards.isProgramNode(lexicalScopeNode)
|
||||
? lexicalScopeNode
|
||||
: lexicalScopeNode.body;
|
||||
|
||||
public transformNode (
|
||||
lexicalScopeBodyNode: TNodeWithLexicalScopeStatements
|
||||
): TNodeWithLexicalScopeStatements {
|
||||
const stringArrayScopeCallsWrapperDataByEncoding: TStringArrayScopeCallsWrapperDataByEncoding | null =
|
||||
this.stringArrayScopeCallsWrapperDataStorage.get(lexicalScopeNode) ?? null;
|
||||
this.stringArrayScopeCallsWrapperDataStorage.get(lexicalScopeBodyNode) ?? null;
|
||||
|
||||
if (!stringArrayScopeCallsWrapperDataByEncoding) {
|
||||
return lexicalScopeNode;
|
||||
return lexicalScopeBodyNode;
|
||||
}
|
||||
|
||||
const stringArrayScopeCallsWrapperDataList: (IStringArrayScopeCallsWrapperData | undefined)[] =
|
||||
@@ -157,7 +153,7 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
|
||||
}
|
||||
}
|
||||
|
||||
return lexicalScopeNode;
|
||||
return lexicalScopeBodyNode;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,14 +175,15 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
|
||||
return rootStringArrayCallsWrapperName;
|
||||
}
|
||||
|
||||
const parentLexicalScope: TNodeWithLexicalScope | undefined = this.visitedLexicalScopeNodesStackStorage.getLastElement();
|
||||
const parentLexicalScopeBodyNode: TNodeWithLexicalScopeStatements | undefined =
|
||||
this.visitedLexicalScopeNodesStackStorage.getLastElement();
|
||||
|
||||
if (!parentLexicalScope) {
|
||||
if (!parentLexicalScopeBodyNode) {
|
||||
return rootStringArrayCallsWrapperName;
|
||||
}
|
||||
|
||||
const parentLexicalScopeDataByEncoding = this.stringArrayScopeCallsWrapperDataStorage
|
||||
.get(parentLexicalScope) ?? null;
|
||||
.get(parentLexicalScopeBodyNode) ?? null;
|
||||
const parentLexicalScopeNames: string[] | null = parentLexicalScopeDataByEncoding?.[encoding]?.names ?? null;
|
||||
|
||||
return parentLexicalScopeNames?.length
|
||||
@@ -266,10 +263,10 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithLexicalScopeAndStatements} lexicalScopeNode
|
||||
* @param {TNodeWithLexicalScopeStatements} lexicalScopeBodyNode
|
||||
*/
|
||||
private onLexicalScopeNodeEnter (lexicalScopeNode: TNodeWithLexicalScopeAndStatements): void {
|
||||
this.visitedLexicalScopeNodesStackStorage.push(lexicalScopeNode);
|
||||
private onLexicalScopeNodeEnter (lexicalScopeBodyNode: TNodeWithLexicalScopeStatements): void {
|
||||
this.visitedLexicalScopeNodesStackStorage.push(lexicalScopeBodyNode);
|
||||
}
|
||||
|
||||
private onLexicalScopeNodeLeave (): void {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TInitialData } from '../../types/TInitialData';
|
||||
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
|
||||
import { TStatement } from '../../types/node/TStatement';
|
||||
import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
|
||||
import { TStringArrayScopeCallsWrapperDataByEncoding } from '../../types/node-transformers/string-array-transformers/TStringArrayScopeCallsWrapperDataByEncoding';
|
||||
@@ -251,14 +251,15 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
|
||||
* @returns {string}
|
||||
*/
|
||||
private getUpperStringArrayCallsWrapperName (encoding: TStringArrayEncoding): string {
|
||||
const currentLexicalScopeNode: TNodeWithLexicalScope | undefined = this.visitedLexicalScopeNodesStackStorage.getLastElement();
|
||||
const currentLexicalScopeBodyNode: TNodeWithLexicalScopeStatements | undefined =
|
||||
this.visitedLexicalScopeNodesStackStorage.getLastElement();
|
||||
|
||||
if (!currentLexicalScopeNode) {
|
||||
throw new Error('Cannot find current lexical scope node');
|
||||
if (!currentLexicalScopeBodyNode) {
|
||||
throw new Error('Cannot find current lexical scope body node');
|
||||
}
|
||||
|
||||
const stringArrayScopeCallsWrapperDataByEncoding: TStringArrayScopeCallsWrapperDataByEncoding =
|
||||
this.stringArrayScopeCallsWrapperDataStorage.get(currentLexicalScopeNode) ?? {};
|
||||
this.stringArrayScopeCallsWrapperDataStorage.get(currentLexicalScopeBodyNode) ?? {};
|
||||
const stringArrayScopeCallsWrapperNames: string[] = stringArrayScopeCallsWrapperDataByEncoding[encoding]?.names ?? [];
|
||||
const isFilledScopeCallsWrapperNamesList: boolean = stringArrayScopeCallsWrapperNames.length === this.options.stringArrayWrappersCount;
|
||||
|
||||
@@ -272,7 +273,7 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
|
||||
};
|
||||
|
||||
this.stringArrayScopeCallsWrapperDataStorage.set(
|
||||
currentLexicalScopeNode,
|
||||
currentLexicalScopeBodyNode,
|
||||
stringArrayScopeCallsWrapperDataByEncoding
|
||||
);
|
||||
}
|
||||
|
||||
+2
-27
@@ -1,7 +1,7 @@
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TNodeWithLexicalScope } from '../types/node/TNodeWithLexicalScope';
|
||||
import { TNodeWithLexicalScopeAndStatements } from '../types/node/TNodeWithLexicalScopeAndStatements';
|
||||
import { TNodeWithLexicalScopeStatements } from '../types/node/TNodeWithLexicalScopeStatements';
|
||||
import { TNodeWithStatements } from '../types/node/TNodeWithStatements';
|
||||
|
||||
import { NodeType } from '../enums/node/NodeType';
|
||||
@@ -271,31 +271,6 @@ export class NodeGuards {
|
||||
return NodeGuards.isNodeWithLexicalScope(node) || NodeGuards.isBlockStatementNode(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {node is TNodeWithLexicalScopeAndStatements}
|
||||
*/
|
||||
public static isNodeWithLexicalScopeAndStatements (node: ESTree.Node): node is TNodeWithLexicalScopeAndStatements {
|
||||
if (!NodeGuards.isNodeWithLexicalScope(node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const lexicalScopeBodyNode: ESTree.Program | ESTree.BlockStatement | ESTree.Expression =
|
||||
NodeGuards.isProgramNode(node)
|
||||
? node
|
||||
: node.body;
|
||||
|
||||
// invalid lexical scope node
|
||||
if (
|
||||
!lexicalScopeBodyNode.parentNode
|
||||
|| !NodeGuards.isNodeWithLexicalScopeStatements(lexicalScopeBodyNode, lexicalScopeBodyNode.parentNode)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @param {Node} parentNode
|
||||
@@ -304,7 +279,7 @@ export class NodeGuards {
|
||||
public static isNodeWithLexicalScopeStatements (
|
||||
node: ESTree.Node,
|
||||
parentNode: ESTree.Node
|
||||
): node is TNodeWithStatements {
|
||||
): node is TNodeWithLexicalScopeStatements {
|
||||
return NodeGuards.isProgramNode(node)
|
||||
|| (NodeGuards.isBlockStatementNode(node) && NodeGuards.nodesWithLexicalStatements.includes(parentNode.type));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { inject, injectable } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
|
||||
import { TStringArrayScopeCallsWrapperDataByEncoding } from '../../types/node-transformers/string-array-transformers/TStringArrayScopeCallsWrapperDataByEncoding';
|
||||
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
@@ -12,7 +12,7 @@ import { MapStorage } from '../MapStorage';
|
||||
|
||||
@injectable()
|
||||
export class StringArrayScopeCallsWrapperDataStorage extends MapStorage <
|
||||
TNodeWithLexicalScope,
|
||||
TNodeWithLexicalScopeStatements,
|
||||
TStringArrayScopeCallsWrapperDataByEncoding
|
||||
> implements IStringArrayScopeCallsWrapperDataStorage {
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { inject, injectable } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import { TNodeWithLexicalScopeAndStatements } from '../../types/node/TNodeWithLexicalScopeAndStatements';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
|
||||
|
||||
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
@@ -11,7 +11,7 @@ import { IVisitedLexicalScopeNodesStackStorage } from '../../interfaces/storages
|
||||
import { ArrayStorage } from '../ArrayStorage';
|
||||
|
||||
@injectable()
|
||||
export class VisitedLexicalScopeNodesStackStorage extends ArrayStorage <TNodeWithLexicalScopeAndStatements> implements IVisitedLexicalScopeNodesStackStorage {
|
||||
export class VisitedLexicalScopeNodesStackStorage extends ArrayStorage <TNodeWithLexicalScopeStatements> implements IVisitedLexicalScopeNodesStackStorage {
|
||||
/**
|
||||
* @type {IArrayUtils}
|
||||
*/
|
||||
@@ -33,23 +33,23 @@ export class VisitedLexicalScopeNodesStackStorage extends ArrayStorage <TNodeWit
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {TNodeWithLexicalScopeAndStatements | undefined}
|
||||
* @returns {TNodeWithLexicalScopeStatements | undefined}
|
||||
*/
|
||||
public getLastElement (): TNodeWithLexicalScopeAndStatements | undefined {
|
||||
public getLastElement (): TNodeWithLexicalScopeStatements | undefined {
|
||||
return this.arrayUtils.getLastElement(this.getStorage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithLexicalScopeAndStatements} lexicalScopeNode
|
||||
* @param {TNodeWithLexicalScopeStatements} nodeWithLexicalScopeStatements
|
||||
*/
|
||||
public push (lexicalScopeNode: TNodeWithLexicalScopeAndStatements): void {
|
||||
this.storage.push(lexicalScopeNode);
|
||||
public push (nodeWithLexicalScopeStatements: TNodeWithLexicalScopeStatements): void {
|
||||
this.storage.push(nodeWithLexicalScopeStatements);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {TNodeWithLexicalScopeAndStatements | undefined}
|
||||
* @returns {TNodeWithLexicalScopeStatements| undefined}
|
||||
*/
|
||||
public pop (): TNodeWithLexicalScopeAndStatements | undefined {
|
||||
public pop (): TNodeWithLexicalScopeStatements | undefined {
|
||||
return this.storage.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TNodeWithLexicalScope } from './TNodeWithLexicalScope';
|
||||
|
||||
export type TNodeWithLexicalScopeAndStatements = TNodeWithLexicalScope
|
||||
& {
|
||||
body: ESTree.Program | ESTree.BlockStatement;
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
export type TNodeWithLexicalScopeStatements = ESTree.Program | ESTree.BlockStatement;
|
||||
+4
-3
@@ -13,13 +13,14 @@ import { StringArrayWrappersType } from '../../src/enums/node-transformers/strin
|
||||
const bar = 'bar';
|
||||
const baz = 'baz';
|
||||
|
||||
function test () {
|
||||
const bark = 'bark'
|
||||
function test (arg = 'bark') {
|
||||
const hawk = 'hawk';
|
||||
const eagle = 'eagle';
|
||||
|
||||
console.log(arg, hawk, eagle);
|
||||
}
|
||||
|
||||
console.log(foo, bar, baz);
|
||||
test();
|
||||
`,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
|
||||
+32
@@ -209,6 +209,38 @@ describe('StringArrayScopeCallsWrapperTransformer', function () {
|
||||
assert.match(obfuscatedCode, stringArrayCallRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: correct wrapper for the function default parameter', () => {
|
||||
const stringArrayCallRegExp: RegExp = new RegExp(
|
||||
'const e *= *b;' +
|
||||
'const foo *= *e\\(\'0x0\'\\);' +
|
||||
'function test *\\(c *= *e\\(\'0x1\'\\)\\) *{' +
|
||||
'const f *= *b;' +
|
||||
'const d *= *f\\(\'0x2\'\\);' +
|
||||
'}'
|
||||
);
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/wrapper-for-the-function-default-parameter.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1,
|
||||
stringArrayWrappersCount: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should add scope calls wrappers', () => {
|
||||
assert.match(obfuscatedCode, stringArrayCallRegExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: prohibited scopes', () => {
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const foo = 'foo'
|
||||
|
||||
function test (bar = 'bar') {
|
||||
const baz = 'baz'
|
||||
}
|
||||
@@ -257,99 +257,4 @@ describe('NodeGuards', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('isNodeWithLexicalScopeAndStatements', () => {
|
||||
describe('truthful checks', () => {
|
||||
const expectedResult: boolean = true;
|
||||
|
||||
describe('Variant #1: program node', () => {
|
||||
const node: ESTree.Program = NodeFactory.programNode();
|
||||
|
||||
let result: boolean;
|
||||
|
||||
before(() => {
|
||||
NodeUtils.parentizeAst(node);
|
||||
result = NodeGuards.isNodeWithLexicalScopeAndStatements(node);
|
||||
});
|
||||
|
||||
it('should check if node with a lexical scope and statements', () => {
|
||||
assert.equal(result, expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: function declaration node', () => {
|
||||
const functionDeclarationNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
|
||||
'foo',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const programNode: ESTree.Program = NodeFactory.programNode([
|
||||
functionDeclarationNode
|
||||
]);
|
||||
|
||||
let result: boolean;
|
||||
|
||||
before(() => {
|
||||
NodeUtils.parentizeAst(programNode);
|
||||
result = NodeGuards.isNodeWithLexicalScopeAndStatements(functionDeclarationNode);
|
||||
});
|
||||
|
||||
it('should check if node with a lexical scope and statements', () => {
|
||||
assert.equal(result, expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: arrow function expression node', () => {
|
||||
const arrowFunctionExpressionNode: ESTree.ArrowFunctionExpression = NodeFactory.arrowFunctionExpressionNode(
|
||||
[],
|
||||
false,
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const programNode: ESTree.Program = NodeFactory.programNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
arrowFunctionExpressionNode
|
||||
)
|
||||
]);
|
||||
|
||||
let result: boolean;
|
||||
|
||||
before(() => {
|
||||
NodeUtils.parentizeAst(programNode);
|
||||
result = NodeGuards.isNodeWithLexicalScopeAndStatements(arrowFunctionExpressionNode);
|
||||
});
|
||||
|
||||
it('should check if node with a lexical scope and statements', () => {
|
||||
assert.equal(result, expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('false checks', () => {
|
||||
const expectedResult: boolean = false;
|
||||
|
||||
describe('Variant #1: arrow function expression node without statements', () => {
|
||||
const arrowFunctionExpressionNode: ESTree.ArrowFunctionExpression = NodeFactory.arrowFunctionExpressionNode(
|
||||
[],
|
||||
true,
|
||||
NodeFactory.literalNode('foo')
|
||||
);
|
||||
const programNode: ESTree.Program = NodeFactory.programNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
arrowFunctionExpressionNode
|
||||
)
|
||||
]);
|
||||
|
||||
let result: boolean;
|
||||
|
||||
before(() => {
|
||||
NodeUtils.parentizeAst(programNode);
|
||||
result = NodeGuards.isNodeWithLexicalScopeAndStatements(arrowFunctionExpressionNode);
|
||||
});
|
||||
|
||||
it('should check if node with a lexical scope and statements', () => {
|
||||
assert.equal(result, expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+43
-43
@@ -5,7 +5,7 @@ import { assert } from 'chai';
|
||||
import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
|
||||
|
||||
import { TInputOptions } from '../../../../../src/types/options/TInputOptions';
|
||||
import { TNodeWithLexicalScopeAndStatements } from '../../../../../src/types/node/TNodeWithLexicalScopeAndStatements';
|
||||
import { TNodeWithLexicalScopeStatements } from '../../../../../src/types/node/TNodeWithLexicalScopeStatements';
|
||||
|
||||
import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IVisitedLexicalScopeNodesStackStorage } from '../../../../../src/interfaces/storages/string-array-transformers/IVisitedLexicalScopeNodesStackStorage';
|
||||
@@ -35,23 +35,23 @@ const getStorageInstance = (options: TInputOptions = {}): IVisitedLexicalScopeNo
|
||||
|
||||
describe('VisitedLexicalScopeNodesStackStorage', () => {
|
||||
describe('getLastElement', () => {
|
||||
const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'first',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'second',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const expectedLastElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'last',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('first')
|
||||
)
|
||||
]);
|
||||
const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('second')
|
||||
)
|
||||
]);
|
||||
const expectedLastElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('last')
|
||||
)
|
||||
]);
|
||||
|
||||
let lastElement: TNodeWithLexicalScopeAndStatements | undefined;
|
||||
let lastElement: TNodeWithLexicalScopeStatements | undefined;
|
||||
|
||||
before(() => {
|
||||
const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
|
||||
@@ -68,22 +68,22 @@ describe('VisitedLexicalScopeNodesStackStorage', () => {
|
||||
});
|
||||
|
||||
describe('push', () => {
|
||||
const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'first',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'second',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const expectedStorage: TNodeWithLexicalScopeAndStatements[] = [
|
||||
const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('first')
|
||||
)
|
||||
]);
|
||||
const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('second')
|
||||
)
|
||||
]);
|
||||
const expectedStorage: TNodeWithLexicalScopeStatements[] = [
|
||||
firstElement,
|
||||
secondElement
|
||||
];
|
||||
|
||||
let storage: TNodeWithLexicalScopeAndStatements[];
|
||||
let storage: TNodeWithLexicalScopeStatements[];
|
||||
|
||||
before(() => {
|
||||
const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
|
||||
@@ -99,23 +99,23 @@ describe('VisitedLexicalScopeNodesStackStorage', () => {
|
||||
});
|
||||
|
||||
describe('pop', () => {
|
||||
const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'first',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
|
||||
'second',
|
||||
[],
|
||||
NodeFactory.blockStatementNode([])
|
||||
);
|
||||
const expectedStorage: TNodeWithLexicalScopeAndStatements[] = [
|
||||
const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('first')
|
||||
)
|
||||
]);
|
||||
const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
|
||||
NodeFactory.expressionStatementNode(
|
||||
NodeFactory.literalNode('second')
|
||||
)
|
||||
]);
|
||||
const expectedStorage: TNodeWithLexicalScopeStatements[] = [
|
||||
firstElement
|
||||
];
|
||||
const expectedPoppedElement: TNodeWithLexicalScopeAndStatements = secondElement;
|
||||
const expectedPoppedElement: TNodeWithLexicalScopeStatements = secondElement;
|
||||
|
||||
let storage: TNodeWithLexicalScopeAndStatements[];
|
||||
let poppedElement: TNodeWithLexicalScopeAndStatements | undefined;
|
||||
let storage: TNodeWithLexicalScopeStatements[];
|
||||
let poppedElement: TNodeWithLexicalScopeStatements | undefined;
|
||||
|
||||
before(() => {
|
||||
const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
|
||||
|
||||
Reference in New Issue
Block a user