mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
source map WIP integration
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import { IBlockStatementNode } from "../src/interfaces/nodes/IBlockStatementNode";
|
||||
import { IExpressionStatementNode } from "../src/interfaces/nodes/IExpressionStatementNode";
|
||||
import { IFunctionDeclarationNode } from "../src/interfaces/nodes/IFunctionDeclarationNode";
|
||||
import { IIdentifierNode } from "../src/interfaces/nodes/IIdentifierNode";
|
||||
import { IIfStatementNode } from "../src/interfaces/nodes/IIfStatementNode";
|
||||
import { ILiteralNode } from "../src/interfaces/nodes/ILiteralNode";
|
||||
import { INode } from "../src/interfaces/nodes/INode";
|
||||
import { IProgramNode } from "../src/interfaces/nodes/IProgramNode";
|
||||
import { IVariableDeclarationNode } from "../src/interfaces/nodes/IVariableDeclarationNode";
|
||||
import { IVariableDeclaratorNode } from "../src/interfaces/nodes/IVariableDeclaratorNode";
|
||||
|
||||
import { NodeType } from "../src/enums/NodeType";
|
||||
|
||||
import { NodeMocks } from './mocks/NodeMocks';
|
||||
import { NodeUtils } from '../src/NodeUtils';
|
||||
@@ -65,6 +71,45 @@ describe('NodeUtils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertCodeToStructure (code: string): INode', () => {
|
||||
let code: string,
|
||||
identifierNode: IIdentifierNode,
|
||||
literalNode: ILiteralNode,
|
||||
programNode: IProgramNode,
|
||||
variableDeclarationNode: IVariableDeclarationNode,
|
||||
variableDeclaratorNode: IVariableDeclaratorNode;
|
||||
|
||||
beforeEach(() => {
|
||||
code = `
|
||||
var abc = 'cde';
|
||||
`;
|
||||
|
||||
identifierNode = NodeMocks.getIdentifierNode('abc');
|
||||
|
||||
literalNode = NodeMocks.getLiteralNode('cde');
|
||||
|
||||
variableDeclaratorNode = NodeMocks.getVariableDeclaratorNode(identifierNode, literalNode);
|
||||
|
||||
variableDeclarationNode = NodeMocks.getVariableDeclarationNode([
|
||||
variableDeclaratorNode
|
||||
]);
|
||||
|
||||
programNode = NodeMocks.getProgramNode([
|
||||
variableDeclarationNode
|
||||
]);
|
||||
|
||||
programNode['parentNode'] = programNode;
|
||||
variableDeclarationNode['parentNode'] = programNode;
|
||||
variableDeclaratorNode['parentNode'] = variableDeclarationNode;
|
||||
identifierNode['parentNode'] = variableDeclaratorNode;
|
||||
literalNode['parentNode'] = variableDeclaratorNode;
|
||||
});
|
||||
|
||||
xit('should convert code to `INode` structure', () => {
|
||||
assert.deepEqual(NodeUtils.convertCodeToStructure(code), variableDeclarationNode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBlockStatementNodeByIndex (node: INode, index: number = 0): INode', () => {
|
||||
let blockStatementNode: IBlockStatementNode,
|
||||
expressionStatementNode1: IExpressionStatementNode,
|
||||
|
||||
+38
-5
@@ -13,6 +13,8 @@ import { IIfStatementNode } from "../../src/interfaces/nodes/IIfStatementNode";
|
||||
import { ILiteralNode } from "../../src/interfaces/nodes/ILiteralNode";
|
||||
import { IProgramNode } from "../../src/interfaces/nodes/IProgramNode";
|
||||
import { ISpreadElementNode } from "../../src/interfaces/nodes/ISpreadElementNode";
|
||||
import { IVariableDeclarationNode } from "../../src/interfaces/nodes/IVariableDeclarationNode";
|
||||
import { IVariableDeclaratorNode } from "../../src/interfaces/nodes/IVariableDeclaratorNode";
|
||||
|
||||
import { NodeType } from "../../src/enums/NodeType";
|
||||
|
||||
@@ -24,7 +26,8 @@ export class NodeMocks {
|
||||
public static getProgramNode (bodyNodes: TStatement[] = []): IProgramNode {
|
||||
return {
|
||||
type: NodeType.Program,
|
||||
body: bodyNodes
|
||||
body: bodyNodes,
|
||||
sourceType: 'script'
|
||||
};
|
||||
}
|
||||
|
||||
@@ -130,17 +133,47 @@ export class NodeMocks {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value
|
||||
* @returns {ILiteralNode}
|
||||
*/
|
||||
public static getLiteralNode (): ILiteralNode {
|
||||
public static getLiteralNode (value: boolean|number|string = 'value'): ILiteralNode {
|
||||
return {
|
||||
type: NodeType.Literal,
|
||||
value: 'string',
|
||||
raw: `'string'`,
|
||||
value: value,
|
||||
raw: `'${value}'`,
|
||||
'x-verbatim-property': {
|
||||
content: `'string'`,
|
||||
content: `'${value}'`,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param declarations
|
||||
* @param kind
|
||||
* @returns {IVariableDeclarationNode}
|
||||
*/
|
||||
public static getVariableDeclarationNode (
|
||||
declarations: IVariableDeclaratorNode[] = [],
|
||||
kind: string = 'var'
|
||||
): IVariableDeclarationNode {
|
||||
return {
|
||||
type: NodeType.VariableDeclaration,
|
||||
declarations: declarations,
|
||||
kind: kind
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param init
|
||||
* @returns {IVariableDeclaratorNode}
|
||||
*/
|
||||
public static getVariableDeclaratorNode (id: IIdentifierNode, init: any): IVariableDeclaratorNode {
|
||||
return {
|
||||
type: NodeType.VariableDeclarator,
|
||||
id: id,
|
||||
init: init
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user