mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
tests and refactoring
This commit is contained in:
Vendored
+406
-378
File diff suppressed because it is too large
Load Diff
+11
-95
@@ -1,25 +1,19 @@
|
||||
import * as estraverse from 'estraverse';
|
||||
|
||||
import { IBlockStatementNode } from "./interfaces/nodes/IBlockStatementNode";
|
||||
import { IIdentifierNode } from "./interfaces/nodes/IIdentifierNode";
|
||||
import { ILiteralNode } from "./interfaces/nodes/ILiteralNode";
|
||||
import { IMemberExpressionNode } from "./interfaces/nodes/IMemberExpressionNode";
|
||||
import { IProgramNode } from "./interfaces/nodes/IProgramNode";
|
||||
import { IPropertyNode } from "./interfaces/nodes/IPropertyNode";
|
||||
import { INode } from './interfaces/nodes/INode';
|
||||
import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode";
|
||||
|
||||
import { TBlockScopeNode } from "./types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "./types/TNodeWithBlockStatement";
|
||||
|
||||
import { NodeType } from "./enums/NodeType";
|
||||
|
||||
import { Nodes } from "./Nodes";
|
||||
import { Utils } from "./Utils";
|
||||
|
||||
export class NodeUtils {
|
||||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
private static scopeNodes: string[] = [
|
||||
private static nodesWithBlockScope: string[] = [
|
||||
NodeType.ArrowFunctionExpression,
|
||||
NodeType.FunctionDeclaration,
|
||||
NodeType.FunctionExpression,
|
||||
@@ -32,7 +26,7 @@ export class NodeUtils {
|
||||
public static addXVerbatimPropertyToLiterals (node: INode): void {
|
||||
estraverse.replace(node, {
|
||||
enter: (node: INode, parentNode: INode): any => {
|
||||
if (NodeUtils.isLiteralNode(node)) {
|
||||
if (Nodes.isLiteralNode(node)) {
|
||||
node['x-verbatim-property'] = node.raw;
|
||||
}
|
||||
}
|
||||
@@ -56,8 +50,8 @@ export class NodeUtils {
|
||||
* @param index
|
||||
* @returns {INode}
|
||||
*/
|
||||
public static getBlockScopeNodeByIndex (node: INode, index: number = 0): INode {
|
||||
if (NodeUtils.isNodeHasBlockScope(node) && node.body[index]) {
|
||||
public static getBlockStatementNodeByIndex (node: INode, index: number = 0): INode {
|
||||
if (Nodes.isNodeHasBlockStatement(node) && node.body[index]) {
|
||||
return node.body[index];
|
||||
}
|
||||
|
||||
@@ -69,16 +63,16 @@ export class NodeUtils {
|
||||
* @param depth
|
||||
* @returns {INode}
|
||||
*/
|
||||
public static getBlockScopeOfNode (node: INode, depth: number = 0): TBlockScopeNode {
|
||||
public static getBlockScopeOfNode (node: INode, depth: number = 0): TNodeWithBlockStatement {
|
||||
if (!node.parentNode) {
|
||||
throw new ReferenceError('`parentNode` property of given node is `undefined`');
|
||||
}
|
||||
|
||||
if (node.parentNode.type === NodeType.Program) {
|
||||
return <TBlockScopeNode> node.parentNode;
|
||||
return <TNodeWithBlockStatement> node.parentNode;
|
||||
}
|
||||
|
||||
if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
|
||||
if (!Utils.arrayContains(NodeUtils.nodesWithBlockScope, node.parentNode.type)) {
|
||||
return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
|
||||
}
|
||||
|
||||
@@ -90,18 +84,7 @@ export class NodeUtils {
|
||||
return NodeUtils.getBlockScopeOfNode(node.parentNode);
|
||||
}
|
||||
|
||||
return <TBlockScopeNode> node; // blocks statement of scopeNodes
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bodyNode
|
||||
* @returns IProgramNode
|
||||
*/
|
||||
public static getProgramNode (bodyNode: INode[]): IProgramNode {
|
||||
return {
|
||||
'type': NodeType.Program,
|
||||
'body': bodyNode
|
||||
};
|
||||
return <TNodeWithBlockStatement> node; // blocks statement of scopeNodes
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,73 +100,6 @@ export class NodeUtils {
|
||||
blockScopeBody.splice(index, 0, node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isBlockStatementNode (node: INode): node is IBlockStatementNode {
|
||||
return node.type === NodeType.BlockStatement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isIdentifierNode (node: INode): node is IIdentifierNode {
|
||||
return node.type === NodeType.Identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isLiteralNode (node: INode): node is ILiteralNode {
|
||||
return node.type === NodeType.Literal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isMemberExpressionNode (node: INode): node is IMemberExpressionNode {
|
||||
return node.type === NodeType.MemberExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isNodeHasBlockScope (node: INode): node is TBlockScopeNode {
|
||||
return node.hasOwnProperty('body');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isProgramNode (node: INode): node is IProgramNode {
|
||||
return node.type === NodeType.Program;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isPropertyNode (node: INode): node is IPropertyNode {
|
||||
return node.type === NodeType.Property;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isVariableDeclaratorNode (node: INode): node is IVariableDeclaratorNode {
|
||||
return node.type === NodeType.VariableDeclarator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
*/
|
||||
@@ -195,7 +111,7 @@ export class NodeUtils {
|
||||
Object.defineProperty(node, 'parentNode', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: isRootNode ? NodeUtils.getProgramNode([node]) : parentNode || node,
|
||||
value: isRootNode ? Nodes.getProgramNode([node]) : parentNode || node,
|
||||
writable: true
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { IBlockStatementNode } from "./interfaces/nodes/IBlockStatementNode";
|
||||
import { IIdentifierNode } from "./interfaces/nodes/IIdentifierNode";
|
||||
import { ILiteralNode } from "./interfaces/nodes/ILiteralNode";
|
||||
import { IMemberExpressionNode } from "./interfaces/nodes/IMemberExpressionNode";
|
||||
import { INode } from "./interfaces/nodes/INode";
|
||||
import { IProgramNode } from "./interfaces/nodes/IProgramNode";
|
||||
import { IPropertyNode } from "./interfaces/nodes/IPropertyNode";
|
||||
import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode";
|
||||
|
||||
import { TNodeWithBlockStatement } from "./types/TNodeWithBlockStatement";
|
||||
|
||||
import { NodeType } from "./enums/NodeType";
|
||||
|
||||
export class Nodes {
|
||||
/**
|
||||
* @param bodyNode
|
||||
* @returns IProgramNode
|
||||
*/
|
||||
public static getProgramNode (bodyNode: INode[]): IProgramNode {
|
||||
return {
|
||||
'type': NodeType.Program,
|
||||
'body': bodyNode
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isBlockStatementNode (node: INode): node is IBlockStatementNode {
|
||||
return node.type === NodeType.BlockStatement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isIdentifierNode (node: INode): node is IIdentifierNode {
|
||||
return node.type === NodeType.Identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isLiteralNode (node: INode): node is ILiteralNode {
|
||||
return node.type === NodeType.Literal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isMemberExpressionNode (node: INode): node is IMemberExpressionNode {
|
||||
return node.type === NodeType.MemberExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isProgramNode (node: INode): node is IProgramNode {
|
||||
return node.type === NodeType.Program;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isPropertyNode (node: INode): node is IPropertyNode {
|
||||
return node.type === NodeType.Property;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isVariableDeclaratorNode (node: INode): node is IVariableDeclaratorNode {
|
||||
return node.type === NodeType.VariableDeclarator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isNodeHasBlockStatement (node: INode): node is TNodeWithBlockStatement {
|
||||
return node.hasOwnProperty('body') && Array.isArray((<TNodeWithBlockStatement>node).body);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import * as esprima from 'esprima';
|
||||
import { INode } from "../../interfaces/nodes/INode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { Node } from '../Node';
|
||||
import { NodeUtils } from "../../NodeUtils";
|
||||
@@ -21,7 +21,7 @@ export class ConsoleOutputDisableExpressionNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.prependNode(blockScopeNode.body, this.getNode());
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export class ConsoleOutputDisableExpressionNode extends Node {
|
||||
* @returns {INode}
|
||||
*/
|
||||
protected getNodeStructure (): INode {
|
||||
return NodeUtils.getBlockScopeNodeByIndex(
|
||||
return NodeUtils.getBlockStatementNodeByIndex(
|
||||
esprima.parse(`
|
||||
(function () {
|
||||
var _ = '(\u0004\u0006\u0003\u0005[]' + '["filter"]["\u0007tructor"]' + '("return this")()' + '.' + '\u0003;\u0006\u0002\u0005\u0004};' + '_\u0003.log\u0001.in' + 'fo\u0001.' + 'war' + 'n\u0001.er' + 'r' + 'or\u0001})();' + '\u0001\u0005_\u0002;' + '_\u0003\u0002function' + '\u0003\u0007ole\u0004\u0002 ()' + '{\u0005 = \u0006var ' + '_\u0007cons',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { IExpressionStatementNode } from "../../interfaces/nodes/IExpressionStatementNode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { NodeType } from "../../enums/NodeType";
|
||||
|
||||
@@ -29,7 +29,7 @@ export class DebugProtectionFunctionCallNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.appendNode(blockScopeNode.body, this.getNode());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { IExpressionStatementNode } from "../../interfaces/nodes/IExpressionStatementNode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { NodeType } from '../../enums/NodeType';
|
||||
|
||||
@@ -29,7 +29,7 @@ export class DebugProtectionFunctionIntervalNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.appendNode(blockScopeNode.body, this.getNode());
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as esprima from 'esprima';
|
||||
import { INode } from "../../interfaces/nodes/INode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { Node } from '../Node';
|
||||
import { NodeUtils } from '../../NodeUtils';
|
||||
@@ -30,7 +30,7 @@ export class DebugProtectionFunctionNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
let programBodyLength: number = blockScopeNode.body.length,
|
||||
randomIndex: number = Utils.getRandomGenerator().integer({
|
||||
min: 0,
|
||||
@@ -53,7 +53,7 @@ export class DebugProtectionFunctionNode extends Node {
|
||||
* @returns {INode}
|
||||
*/
|
||||
protected getNodeStructure (): INode {
|
||||
return NodeUtils.getBlockScopeNodeByIndex(
|
||||
return NodeUtils.getBlockStatementNodeByIndex(
|
||||
esprima.parse(`
|
||||
var ${this.debugProtectionFunctionName} = function () {
|
||||
function debuggerProtection (counter) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as esprima from 'esprima';
|
||||
import { INode } from "../../interfaces/nodes/INode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { AppendState } from "../../enums/AppendState";
|
||||
import { JSFuck } from "../../enums/JSFuck";
|
||||
@@ -33,7 +33,7 @@ export class SelfDefendingUnicodeNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
let programBodyLength: number = blockScopeNode.body.length,
|
||||
randomIndex: number = 0;
|
||||
|
||||
@@ -66,6 +66,6 @@ export class SelfDefendingUnicodeNode extends Node {
|
||||
|
||||
NodeUtils.addXVerbatimPropertyToLiterals(node);
|
||||
|
||||
return NodeUtils.getBlockScopeNodeByIndex(node);
|
||||
return NodeUtils.getBlockStatementNodeByIndex(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as esprima from 'esprima';
|
||||
import { INode } from "../../interfaces/nodes/INode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { AppendState } from "../../enums/AppendState";
|
||||
|
||||
@@ -56,7 +56,7 @@ export class UnicodeArrayCallsWrapper extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
|
||||
}
|
||||
|
||||
@@ -95,6 +95,6 @@ export class UnicodeArrayCallsWrapper extends Node {
|
||||
|
||||
NodeUtils.addXVerbatimPropertyToLiterals(node);
|
||||
|
||||
return NodeUtils.getBlockScopeNodeByIndex(node);
|
||||
return NodeUtils.getBlockStatementNodeByIndex(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
|
||||
import { INode } from "../../interfaces/nodes/INode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { AppendState } from "../../enums/AppendState";
|
||||
import { JSFuck } from "../../enums/JSFuck";
|
||||
@@ -52,7 +52,7 @@ export class UnicodeArrayDecodeNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
|
||||
}
|
||||
|
||||
@@ -130,6 +130,6 @@ export class UnicodeArrayDecodeNode extends Node {
|
||||
|
||||
NodeUtils.addXVerbatimPropertyToLiterals(node);
|
||||
|
||||
return NodeUtils.getBlockScopeNodeByIndex(node);
|
||||
return NodeUtils.getBlockStatementNodeByIndex(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { INode } from '../../interfaces/nodes/INode';
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
import { IVariableDeclarationNode } from "../../interfaces/nodes/IVariableDeclarationNode";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { AppendState } from '../../enums/AppendState';
|
||||
import { NodeType } from "../../enums/NodeType";
|
||||
@@ -60,7 +60,7 @@ export class UnicodeArrayNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.prependNode(blockScopeNode.body, this.getNode());
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as esprima from 'esprima';
|
||||
import { INode } from "../../interfaces/nodes/INode";
|
||||
import { IOptions } from "../../interfaces/IOptions";
|
||||
|
||||
import { TBlockScopeNode } from "../../types/TBlockScopeNode";
|
||||
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
|
||||
|
||||
import { AppendState } from "../../enums/AppendState";
|
||||
import { JSFuck } from "../../enums/JSFuck";
|
||||
@@ -60,7 +60,7 @@ export class UnicodeArrayRotateFunctionNode extends Node {
|
||||
/**
|
||||
* @param blockScopeNode
|
||||
*/
|
||||
public appendNode (blockScopeNode: TBlockScopeNode): void {
|
||||
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
|
||||
NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
|
||||
}
|
||||
|
||||
@@ -112,6 +112,6 @@ export class UnicodeArrayRotateFunctionNode extends Node {
|
||||
|
||||
NodeUtils.addXVerbatimPropertyToLiterals(node);
|
||||
|
||||
return NodeUtils.getBlockScopeNodeByIndex(node);
|
||||
return NodeUtils.getBlockStatementNodeByIndex(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ICatchClauseNode } from "../interfaces/nodes/ICatchClauseNode";
|
||||
import { INode } from '../interfaces/nodes/INode';
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
import { Utils } from '../Utils';
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ export class CatchClauseObfuscator extends NodeObfuscator {
|
||||
private replaceCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
|
||||
estraverse.replace(catchClauseNode.param, {
|
||||
leave: (node: INode, parentNode: INode): any => {
|
||||
if (NodeUtils.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
if (Nodes.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
this.catchClauseParam.set(node.name, Utils.getRandomVariableName());
|
||||
node.name = this.catchClauseParam.get(node.name);
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { INode } from "../interfaces/nodes/INode";
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { Nodes } from "../Nodes";
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Utils } from '../Utils';
|
||||
|
||||
@@ -43,7 +44,7 @@ export class FunctionDeclarationObfuscator extends NodeObfuscator {
|
||||
private replaceFunctionName (functionDeclarationNode: IFunctionDeclarationNode): void {
|
||||
estraverse.replace(functionDeclarationNode.id, {
|
||||
leave: (node: INode): any => {
|
||||
if (NodeUtils.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
if (Nodes.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
this.functionName.set(node.name, Utils.getRandomVariableName());
|
||||
node.name = this.functionName.get(node.name);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { IFunctionNode } from "../interfaces/nodes/IFunctionNode";
|
||||
import { INode } from "../interfaces/nodes/INode";
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
import { Utils } from '../Utils';
|
||||
|
||||
/**
|
||||
@@ -36,7 +36,7 @@ export class FunctionObfuscator extends NodeObfuscator {
|
||||
functionNode.params.forEach((paramsNode: INode) => {
|
||||
estraverse.replace(paramsNode, {
|
||||
leave: (node: INode): any => {
|
||||
if (NodeUtils.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
if (Nodes.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
this.functionParams.set(node.name, Utils.getRandomVariableName());
|
||||
node.name = this.functionParams.get(node.name);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ILiteralNode } from "../interfaces/nodes/ILiteralNode";
|
||||
|
||||
import { INode } from "../interfaces/nodes/INode";
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
|
||||
export class LiteralObfuscator extends NodeObfuscator {
|
||||
/**
|
||||
@@ -12,7 +12,7 @@ export class LiteralObfuscator extends NodeObfuscator {
|
||||
* @param parentNode
|
||||
*/
|
||||
public obfuscateNode (literalNode: ILiteralNode, parentNode: INode): void {
|
||||
if (NodeUtils.isPropertyNode(parentNode) && parentNode.key === literalNode) {
|
||||
if (Nodes.isPropertyNode(parentNode) && parentNode.key === literalNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import { INode } from "../interfaces/nodes/INode";
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
|
||||
export class MemberExpressionObfuscator extends NodeObfuscator {
|
||||
/**
|
||||
@@ -18,13 +18,13 @@ export class MemberExpressionObfuscator extends NodeObfuscator {
|
||||
public obfuscateNode (memberExpressionNode: IMemberExpressionNode): void {
|
||||
estraverse.replace(memberExpressionNode.property, {
|
||||
leave: (node: INode, parentNode: INode): any => {
|
||||
if (NodeUtils.isLiteralNode(node)) {
|
||||
if (Nodes.isLiteralNode(node)) {
|
||||
this.literalNodeController(node);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (NodeUtils.isIdentifierNode(node)) {
|
||||
if (Nodes.isIdentifierNode(node)) {
|
||||
if (memberExpressionNode.computed) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { IMethodDefinitionNode } from "../interfaces/nodes/IMethodDefinitionNode
|
||||
import { INode } from "../interfaces/nodes/INode";
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
import { Utils } from "../Utils";
|
||||
|
||||
/**
|
||||
@@ -37,7 +37,7 @@ export class MethodDefinitionObfuscator extends NodeObfuscator {
|
||||
estraverse.replace(methodDefinitionNode.key, {
|
||||
leave: (node: INode): any => {
|
||||
if (
|
||||
NodeUtils.isIdentifierNode(node) &&
|
||||
Nodes.isIdentifierNode(node) &&
|
||||
!Utils.arrayContains(this.ignoredNames, node.name) &&
|
||||
methodDefinitionNode.computed === false
|
||||
) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { IOptions } from "../interfaces/IOptions";
|
||||
|
||||
import { JSFuck } from "../enums/JSFuck";
|
||||
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
import { UnicodeArrayNode } from "../custom-nodes/unicode-array-nodes/UnicodeArrayNode";
|
||||
import { Utils } from '../Utils';
|
||||
|
||||
@@ -52,13 +52,13 @@ export abstract class NodeObfuscator implements INodeObfuscator {
|
||||
* @param namesMap
|
||||
*/
|
||||
protected replaceNodeIdentifierByNewValue (node: INode, parentNode: INode, namesMap: Map <string, string>): void {
|
||||
if (NodeUtils.isIdentifierNode(node) && namesMap.has(node.name)) {
|
||||
if (Nodes.isIdentifierNode(node) && namesMap.has(node.name)) {
|
||||
const parentNodeIsPropertyNode: boolean = (
|
||||
NodeUtils.isPropertyNode(parentNode) &&
|
||||
Nodes.isPropertyNode(parentNode) &&
|
||||
parentNode.key === node
|
||||
),
|
||||
parentNodeIsMemberExpressionNode: boolean = (
|
||||
NodeUtils.isMemberExpressionNode(parentNode) &&
|
||||
Nodes.isMemberExpressionNode(parentNode) &&
|
||||
parentNode.computed === false &&
|
||||
parentNode.property === node
|
||||
);
|
||||
|
||||
@@ -10,7 +10,7 @@ import { INode } from "../interfaces/nodes/INode";
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Nodes } from "../Nodes";
|
||||
import { Utils } from '../Utils';
|
||||
|
||||
/**
|
||||
@@ -31,13 +31,13 @@ export class ObjectExpressionObfuscator extends NodeObfuscator {
|
||||
objectExpressionNode.properties.forEach((property: IPropertyNode) => {
|
||||
estraverse.replace(property.key, {
|
||||
leave: (node: INode, parentNode: INode): any => {
|
||||
if (NodeUtils.isLiteralNode(node)) {
|
||||
if (Nodes.isLiteralNode(node)) {
|
||||
this.literalNodeController(node);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (NodeUtils.isIdentifierNode(node)) {
|
||||
if (Nodes.isIdentifierNode(node)) {
|
||||
this.identifierNodeController(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { IVariableDeclaratorNode } from "../interfaces/nodes/IVariableDeclarator
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
import { NodeObfuscator } from './NodeObfuscator';
|
||||
import { Nodes } from "../Nodes";
|
||||
import { NodeUtils } from "../NodeUtils";
|
||||
import { Utils } from '../Utils';
|
||||
|
||||
@@ -46,7 +47,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
|
||||
variableDeclarationNode.declarations.forEach((declarationNode: IVariableDeclaratorNode) => {
|
||||
estraverse.replace(declarationNode.id, {
|
||||
enter: (node: INode): any => {
|
||||
if (NodeUtils.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
if (Nodes.isIdentifierNode(node) && !this.isReservedName(node.name)) {
|
||||
this.variableNames.set(node.name, Utils.getRandomVariableName());
|
||||
node.name = this.variableNames.get(node.name);
|
||||
|
||||
|
||||
@@ -2,4 +2,4 @@ import { IBlockStatementNode } from "../interfaces/nodes/IBlockStatementNode";
|
||||
import { ICatchClauseNode } from "../interfaces/nodes/ICatchClauseNode";
|
||||
import { IProgramNode } from "../interfaces/nodes/IProgramNode";
|
||||
|
||||
export type TBlockScopeNode = IBlockStatementNode|ICatchClauseNode|IProgramNode;
|
||||
export type TNodeWithBlockStatement = IBlockStatementNode|ICatchClauseNode|IProgramNode;
|
||||
@@ -60,7 +60,7 @@ describe('NodeUtils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBlockScopeNodeByIndex (node: INode, index: number = 0): INode', () => {
|
||||
describe('getBlockStatementNodeByIndex (node: INode, index: number = 0): INode', () => {
|
||||
let blockStatementNode: IBlockStatementNode,
|
||||
identifierNode: IIdentifierNode,
|
||||
literalNode: ILiteralNode;
|
||||
@@ -88,20 +88,20 @@ describe('NodeUtils', () => {
|
||||
});
|
||||
|
||||
it('should return block-scope node of given node by given index if node has block-scope', () => {
|
||||
assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(blockStatementNode), identifierNode);
|
||||
assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(blockStatementNode, 1), literalNode);
|
||||
assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode), identifierNode);
|
||||
assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode, 1), literalNode);
|
||||
});
|
||||
|
||||
it('should return root node if index is out of boundaries', () => {
|
||||
assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(blockStatementNode, 2), blockStatementNode);
|
||||
assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode, 2), blockStatementNode);
|
||||
});
|
||||
|
||||
it('should return root node if node has not block-scope', () => {
|
||||
assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(identifierNode, 1), identifierNode);
|
||||
assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(identifierNode, 1), identifierNode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBlockScopeOfNode (node: INode, depth: number = 0): TBlockScopeNode', () => {
|
||||
describe('getBlockScopeOfNode (node: INode, depth: number = 0): TNodeWithBlockStatement', () => {
|
||||
let blockStatementNode: IBlockStatementNode,
|
||||
identifierNode: IIdentifierNode,
|
||||
functionDeclarationNode: IFunctionDeclarationNode,
|
||||
|
||||
Reference in New Issue
Block a user