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:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+16
-83
@@ -184,7 +184,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
return astTree;
|
||||
}
|
||||
|
||||
astTree = this.runPreparingStage(astTree);
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.Preparing);
|
||||
|
||||
this.logger.info(LoggingMessage.AnalyzingASTTreeStage);
|
||||
const stackTraceData: IStackTraceData[] = this.stackTraceAnalyzer.analyze(astTree);
|
||||
@@ -202,11 +202,17 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
|
||||
this.obfuscationEventEmitter.emit(ObfuscationEvent.BeforeObfuscation, astTree, stackTraceData);
|
||||
|
||||
astTree = this.runDeadCodeInjectionStage(astTree);
|
||||
astTree = this.runControlFlowFlatteningStage(astTree);
|
||||
astTree = this.runConvertingStage(astTree);
|
||||
astTree = this.runObfuscatingStage(astTree);
|
||||
astTree = this.runFinalizingStage(astTree);
|
||||
if (this.options.deadCodeInjection) {
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.DeadCodeInjection);
|
||||
}
|
||||
|
||||
if (this.options.controlFlowFlattening) {
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.ControlFlowFlattening);
|
||||
}
|
||||
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.Converting);
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.Obfuscating);
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.Finalizing);
|
||||
|
||||
this.obfuscationEventEmitter.emit(ObfuscationEvent.AfterObfuscation, astTree, stackTraceData);
|
||||
|
||||
@@ -253,89 +259,16 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
|
||||
/**
|
||||
* @param {Program} astTree
|
||||
* @param {TransformationStage} transformationStage
|
||||
* @returns {Program}
|
||||
*/
|
||||
private runPreparingStage (astTree: ESTree.Program): ESTree.Program {
|
||||
this.logger.info(LoggingMessage.TransformationStage, TransformationStage.Preparing);
|
||||
private runTransformationStage (astTree: ESTree.Program, transformationStage: TransformationStage): ESTree.Program {
|
||||
this.logger.info(LoggingMessage.TransformationStage, transformationStage);
|
||||
|
||||
return this.transformersRunner.transform(
|
||||
astTree,
|
||||
JavaScriptObfuscator.transformersList,
|
||||
TransformationStage.Preparing
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Program} astTree
|
||||
* @returns {Program}
|
||||
*/
|
||||
private runDeadCodeInjectionStage (astTree: ESTree.Program): ESTree.Program {
|
||||
if (this.options.deadCodeInjection) {
|
||||
this.logger.info(LoggingMessage.TransformationStage, TransformationStage.DeadCodeInjection);
|
||||
}
|
||||
|
||||
return this.transformersRunner.transform(
|
||||
astTree,
|
||||
JavaScriptObfuscator.transformersList,
|
||||
TransformationStage.DeadCodeInjection
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Program} astTree
|
||||
* @returns {Program}
|
||||
*/
|
||||
private runControlFlowFlatteningStage (astTree: ESTree.Program): ESTree.Program {
|
||||
if (this.options.controlFlowFlattening) {
|
||||
this.logger.info(LoggingMessage.TransformationStage, TransformationStage.ControlFlowFlattening);
|
||||
}
|
||||
|
||||
return this.transformersRunner.transform(
|
||||
astTree,
|
||||
JavaScriptObfuscator.transformersList,
|
||||
TransformationStage.ControlFlowFlattening
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Program} astTree
|
||||
* @returns {Program}
|
||||
*/
|
||||
private runConvertingStage (astTree: ESTree.Program): ESTree.Program {
|
||||
this.logger.info(LoggingMessage.TransformationStage, TransformationStage.Converting);
|
||||
|
||||
return this.transformersRunner.transform(
|
||||
astTree,
|
||||
JavaScriptObfuscator.transformersList,
|
||||
TransformationStage.Converting
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Program} astTree
|
||||
* @returns {Program}
|
||||
*/
|
||||
private runObfuscatingStage (astTree: ESTree.Program): ESTree.Program {
|
||||
this.logger.info(LoggingMessage.TransformationStage, TransformationStage.Obfuscating);
|
||||
|
||||
return this.transformersRunner.transform(
|
||||
astTree,
|
||||
JavaScriptObfuscator.transformersList,
|
||||
TransformationStage.Obfuscating
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Program} astTree
|
||||
* @returns {Program}
|
||||
*/
|
||||
private runFinalizingStage (astTree: ESTree.Program): ESTree.Program {
|
||||
this.logger.info(LoggingMessage.TransformationStage, TransformationStage.Finalizing);
|
||||
|
||||
return this.transformersRunner.transform(
|
||||
astTree,
|
||||
JavaScriptObfuscator.transformersList,
|
||||
TransformationStage.Finalizing
|
||||
transformationStage
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
-4
@@ -104,10 +104,6 @@ export class BlockStatementControlFlowTransformer extends AbstractNodeTransforme
|
||||
* @returns {IVisitor | null}
|
||||
*/
|
||||
public getVisitor (transformationStage: TransformationStage): IVisitor | null {
|
||||
if (!this.options.controlFlowFlattening) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (transformationStage) {
|
||||
case TransformationStage.ControlFlowFlattening:
|
||||
return {
|
||||
|
||||
@@ -106,10 +106,6 @@ export class FunctionControlFlowTransformer extends AbstractNodeTransformer {
|
||||
* @returns {IVisitor | null}
|
||||
*/
|
||||
public getVisitor (transformationStage: TransformationStage): IVisitor | null {
|
||||
if (!this.options.controlFlowFlattening) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (transformationStage) {
|
||||
case TransformationStage.ControlFlowFlattening:
|
||||
return {
|
||||
|
||||
+3
-7
@@ -51,7 +51,7 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
private collectedBlockStatementsLength: number;
|
||||
private collectedBlockStatementsTotalLength: number;
|
||||
|
||||
/**
|
||||
* @type {TDeadNodeInjectionCustomNodeFactory}
|
||||
@@ -113,10 +113,6 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
* @returns {IVisitor | null}
|
||||
*/
|
||||
public getVisitor (transformationStage: TransformationStage): IVisitor | null {
|
||||
if (!this.options.deadCodeInjection) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (transformationStage) {
|
||||
case TransformationStage.DeadCodeInjection:
|
||||
return {
|
||||
@@ -173,7 +169,7 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
}
|
||||
});
|
||||
|
||||
this.collectedBlockStatementsLength = this.collectedBlockStatements.length;
|
||||
this.collectedBlockStatementsTotalLength = this.collectedBlockStatements.length;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,7 +178,7 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
* @returns {NodeGuards | VisitorOption}
|
||||
*/
|
||||
public transformNode (blockStatementNode: ESTree.BlockStatement, parentNode: ESTree.Node): ESTree.Node | estraverse.VisitorOption {
|
||||
if (this.collectedBlockStatementsLength < DeadCodeInjectionTransformer.minCollectedBlockStatementsCount) {
|
||||
if (this.collectedBlockStatementsTotalLength < DeadCodeInjectionTransformer.minCollectedBlockStatementsCount) {
|
||||
return estraverse.VisitorOption.Break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user