mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Some refactoring
This commit is contained in:
+85
-84
@@ -9,15 +9,15 @@ import { NodeGuards } from './NodeGuards';
|
||||
|
||||
export class NodeAppender {
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @param {TStatement[]} scopeStatements
|
||||
* @param {TNodeWithScope} scope
|
||||
* @param {TStatement[]} statements
|
||||
*/
|
||||
public static appendNode (scopeNode: TNodeWithScope, scopeStatements: TStatement[]): void {
|
||||
scopeStatements = NodeAppender.parentizeScopeStatementsBeforeAppend(scopeNode, scopeStatements);
|
||||
public static append (scope: TNodeWithScope, statements: TStatement[]): void {
|
||||
statements = NodeAppender.parentizeScopeStatementsBeforeAppend(scope, statements);
|
||||
|
||||
NodeAppender.setScopeNodeStatements(scopeNode, [
|
||||
...NodeAppender.getScopeNodeStatements(scopeNode),
|
||||
...scopeStatements
|
||||
NodeAppender.setScopeStatements(scope, [
|
||||
...NodeAppender.getScopeStatements(scope),
|
||||
...statements
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -38,45 +38,38 @@ export class NodeAppender {
|
||||
*
|
||||
* Appends node into block statement of `baz` function expression
|
||||
*
|
||||
* @param {IStackTraceData[]} blockScopeStackTraceData
|
||||
* @param {IStackTraceData[]} stackTraceData
|
||||
* @param {TNodeWithBlockScope} blockScopeNode
|
||||
* @param {TStatement[]} nodeBodyStatements
|
||||
* @param {TStatement[]} bodyStatements
|
||||
* @param {number} index
|
||||
*/
|
||||
public static appendNodeToOptimalBlockScope (
|
||||
blockScopeStackTraceData: IStackTraceData[],
|
||||
public static appendToOptimalBlockScope (
|
||||
stackTraceData: IStackTraceData[],
|
||||
blockScopeNode: TNodeWithBlockScope,
|
||||
nodeBodyStatements: TStatement[],
|
||||
bodyStatements: TStatement[],
|
||||
index: number = 0
|
||||
): void {
|
||||
let targetBlockScope: TNodeWithBlockScope;
|
||||
const targetBlockScope: TNodeWithBlockScope = stackTraceData.length
|
||||
? NodeAppender.getOptimalBlockScope(stackTraceData, index)
|
||||
: blockScopeNode;
|
||||
|
||||
if (!blockScopeStackTraceData.length) {
|
||||
targetBlockScope = blockScopeNode;
|
||||
} else {
|
||||
targetBlockScope = NodeAppender.getOptimalBlockScope(
|
||||
blockScopeStackTraceData,
|
||||
index
|
||||
);
|
||||
}
|
||||
|
||||
NodeAppender.prependNode(targetBlockScope, nodeBodyStatements);
|
||||
NodeAppender.prepend(targetBlockScope, bodyStatements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns deepest block scope node at given deep.
|
||||
*
|
||||
* @param {IStackTraceData[]} blockScopeTraceData
|
||||
* @param {IStackTraceData[]} stackTraceData
|
||||
* @param {number} index
|
||||
* @param {number} deep
|
||||
* @returns {BlockStatement}
|
||||
*/
|
||||
public static getOptimalBlockScope (
|
||||
blockScopeTraceData: IStackTraceData[],
|
||||
stackTraceData: IStackTraceData[],
|
||||
index: number,
|
||||
deep: number = Infinity
|
||||
): ESTree.BlockStatement {
|
||||
const firstCall: IStackTraceData = blockScopeTraceData[index];
|
||||
const firstCall: IStackTraceData = stackTraceData[index];
|
||||
|
||||
if (deep <= 0) {
|
||||
throw new Error('Invalid `deep` argument value. Value should be bigger then 0.');
|
||||
@@ -90,82 +83,90 @@ export class NodeAppender {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @param {TStatement[]} scopeStatements
|
||||
* @param {Node} targetStatement
|
||||
* @param {TNodeWithScope} scope
|
||||
* @param {TStatement[]} statements
|
||||
* @param {Node} target
|
||||
*/
|
||||
public static insertNodeAfter (scopeNode: TNodeWithScope, scopeStatements: TStatement[], targetStatement: ESTree.Statement): void {
|
||||
public static insertAfter (
|
||||
scope: TNodeWithScope,
|
||||
statements: TStatement[],
|
||||
target: ESTree.Statement
|
||||
): void {
|
||||
const indexInScopeStatement: number = NodeAppender
|
||||
.getScopeNodeStatements(scopeNode)
|
||||
.indexOf(targetStatement);
|
||||
.getScopeStatements(scope)
|
||||
.indexOf(target);
|
||||
|
||||
NodeAppender.insertNodeAtIndex(scopeNode, scopeStatements, indexInScopeStatement + 1);
|
||||
NodeAppender.insertAtIndex(scope, statements, indexInScopeStatement + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @param {TStatement[]} scopeStatements
|
||||
* @param {TNodeWithScope} scope
|
||||
* @param {TStatement[]} statements
|
||||
* @param {number} index
|
||||
*/
|
||||
public static insertNodeAtIndex (scopeNode: TNodeWithScope, scopeStatements: TStatement[], index: number): void {
|
||||
scopeStatements = NodeAppender.parentizeScopeStatementsBeforeAppend(scopeNode, scopeStatements);
|
||||
public static insertAtIndex (
|
||||
scope: TNodeWithScope,
|
||||
statements: TStatement[],
|
||||
index: number
|
||||
): void {
|
||||
statements = NodeAppender.parentizeScopeStatementsBeforeAppend(scope, statements);
|
||||
|
||||
NodeAppender.setScopeNodeStatements(scopeNode, [
|
||||
...NodeAppender.getScopeNodeStatements(scopeNode).slice(0, index),
|
||||
...scopeStatements,
|
||||
...NodeAppender.getScopeNodeStatements(scopeNode).slice(index)
|
||||
NodeAppender.setScopeStatements(scope, [
|
||||
...NodeAppender.getScopeStatements(scope).slice(0, index),
|
||||
...statements,
|
||||
...NodeAppender.getScopeStatements(scope).slice(index)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @param {TStatement[]} scopeStatements
|
||||
*/
|
||||
public static prependNode (scopeNode: TNodeWithScope, scopeStatements: TStatement[]): void {
|
||||
scopeStatements = NodeAppender.parentizeScopeStatementsBeforeAppend(scopeNode, scopeStatements);
|
||||
|
||||
NodeAppender.setScopeNodeStatements(scopeNode, [
|
||||
...scopeStatements,
|
||||
...NodeAppender.getScopeNodeStatements(scopeNode),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @returns {TStatement[]}
|
||||
*/
|
||||
private static getScopeNodeStatements (scopeNode: TNodeWithScope): TStatement[] {
|
||||
if (NodeGuards.isSwitchCaseNode(scopeNode)) {
|
||||
return scopeNode.consequent;
|
||||
}
|
||||
|
||||
return scopeNode.body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @param {TStatement[]} scopeStatements
|
||||
* @returns {TStatement[]}
|
||||
*/
|
||||
private static parentizeScopeStatementsBeforeAppend (scopeNode: TNodeWithScope, scopeStatements: TStatement[]): TStatement[] {
|
||||
scopeStatements.forEach((statement: TStatement) => {
|
||||
statement.parentNode = scopeNode;
|
||||
});
|
||||
|
||||
return scopeStatements;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scopeNode
|
||||
* @param {TNodeWithScope} scope
|
||||
* @param {TStatement[]} statements
|
||||
*/
|
||||
private static setScopeNodeStatements (scopeNode: TNodeWithScope, statements: TStatement[]): void {
|
||||
if (NodeGuards.isSwitchCaseNode(scopeNode)) {
|
||||
scopeNode.consequent = <ESTree.Statement[]>statements;
|
||||
public static prepend (scope: TNodeWithScope, statements: TStatement[]): void {
|
||||
statements = NodeAppender.parentizeScopeStatementsBeforeAppend(scope, statements);
|
||||
|
||||
NodeAppender.setScopeStatements(scope, [
|
||||
...statements,
|
||||
...NodeAppender.getScopeStatements(scope),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scope
|
||||
* @returns {TStatement[]}
|
||||
*/
|
||||
private static getScopeStatements (scope: TNodeWithScope): TStatement[] {
|
||||
if (NodeGuards.isSwitchCaseNode(scope)) {
|
||||
return scope.consequent;
|
||||
}
|
||||
|
||||
return scope.body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scope
|
||||
* @param {TStatement[]} statements
|
||||
* @returns {TStatement[]}
|
||||
*/
|
||||
private static parentizeScopeStatementsBeforeAppend (scope: TNodeWithScope, statements: TStatement[]): TStatement[] {
|
||||
statements.forEach((statement: TStatement) => {
|
||||
statement.parentNode = scope;
|
||||
});
|
||||
|
||||
return statements;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TNodeWithScope} scope
|
||||
* @param {TStatement[]} statements
|
||||
*/
|
||||
private static setScopeStatements (scope: TNodeWithScope, statements: TStatement[]): void {
|
||||
if (NodeGuards.isSwitchCaseNode(scope)) {
|
||||
scope.consequent = <ESTree.Statement[]>statements;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
scopeNode.body = statements;
|
||||
scope.body = statements;
|
||||
}
|
||||
}
|
||||
|
||||
+27
-25
@@ -13,18 +13,16 @@ import { NodeMetadata } from './NodeMetadata';
|
||||
|
||||
export class NodeUtils {
|
||||
/**
|
||||
* @param {T} node
|
||||
* @param {T} literalNode
|
||||
* @returns {T}
|
||||
*/
|
||||
public static addXVerbatimPropertyToLiteralNode <T extends ESTree.Node = ESTree.Node> (node: T): T {
|
||||
if (NodeGuards.isLiteralNode(node)) {
|
||||
node['x-verbatim-property'] = {
|
||||
content: node.raw,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
}
|
||||
public static addXVerbatimPropertyTo (literalNode: ESTree.Literal): ESTree.Literal {
|
||||
literalNode['x-verbatim-property'] = {
|
||||
content: literalNode.raw,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
|
||||
return node;
|
||||
return literalNode;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +30,7 @@ export class NodeUtils {
|
||||
* @returns {T}
|
||||
*/
|
||||
public static clone <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
|
||||
return NodeUtils.parentize(NodeUtils.cloneRecursive(astTree));
|
||||
return NodeUtils.parentizeAst(NodeUtils.cloneRecursive(astTree));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +43,11 @@ export class NodeUtils {
|
||||
estraverse.replace(structure, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node => {
|
||||
NodeUtils.parentizeNode(node, parentNode);
|
||||
NodeUtils.addXVerbatimPropertyToLiteralNode(node);
|
||||
|
||||
if (NodeGuards.isLiteralNode(node)) {
|
||||
NodeUtils.addXVerbatimPropertyTo(node);
|
||||
}
|
||||
|
||||
NodeMetadata.set(node, { ignoredNode: false });
|
||||
|
||||
return node;
|
||||
@@ -68,27 +70,27 @@ export class NodeUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} targetNode
|
||||
* @param {Node} node
|
||||
* @returns {TNodeWithBlockScope[]}
|
||||
*/
|
||||
public static getBlockScopesOfNode (targetNode: ESTree.Node): TNodeWithBlockScope[] {
|
||||
return NodeUtils.getBlockScopesOfNodeRecursive(targetNode);
|
||||
public static getBlockScopesOfNode (node: ESTree.Node): TNodeWithBlockScope[] {
|
||||
return NodeUtils.getBlockScopesOfNodeRecursive(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Statement} node
|
||||
* @param {Statement} statement
|
||||
* @returns {TStatement | null}
|
||||
*/
|
||||
public static getNextSiblingStatementNode (node: ESTree.Statement): TStatement | null {
|
||||
return NodeUtils.getSiblingStatementNodeByOffset(node, 1);
|
||||
public static getNextSiblingStatement (statement: ESTree.Statement): TStatement | null {
|
||||
return NodeUtils.getSiblingStatementByOffset(statement, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Statement} node
|
||||
* @param {Statement} statement
|
||||
* @returns {TStatement | null}
|
||||
*/
|
||||
public static getPreviousSiblingStatementNode (node: ESTree.Statement): TStatement | null {
|
||||
return NodeUtils.getSiblingStatementNodeByOffset(node, -1);
|
||||
public static getPreviousSiblingStatement (statement: ESTree.Statement): TStatement | null {
|
||||
return NodeUtils.getSiblingStatementByOffset(statement, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +127,7 @@ export class NodeUtils {
|
||||
* @param {T} astTree
|
||||
* @returns {T}
|
||||
*/
|
||||
public static parentize <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
|
||||
public static parentizeAst <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
|
||||
estraverse.replace(astTree, {
|
||||
enter: NodeUtils.parentizeNode
|
||||
});
|
||||
@@ -231,16 +233,16 @@ export class NodeUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Statement} node
|
||||
* @param {Statement} statement
|
||||
* @param {number} offset
|
||||
* @returns {TStatement | null}
|
||||
*/
|
||||
private static getSiblingStatementNodeByOffset (node: ESTree.Statement, offset: number): TStatement | null {
|
||||
const scopeNode: TNodeWithScope = NodeUtils.getScopeOfNode(node);
|
||||
private static getSiblingStatementByOffset (statement: ESTree.Statement, offset: number): TStatement | null {
|
||||
const scopeNode: TNodeWithScope = NodeUtils.getScopeOfNode(statement);
|
||||
const scopeBody: TStatement[] = !NodeGuards.isSwitchCaseNode(scopeNode)
|
||||
? scopeNode.body
|
||||
: scopeNode.consequent;
|
||||
const indexInScope: number = scopeBody.indexOf(node);
|
||||
const indexInScope: number = scopeBody.indexOf(statement);
|
||||
|
||||
return scopeBody[indexInScope + offset] || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user