mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
refactoring
This commit is contained in:
@@ -4,7 +4,7 @@ import * as chai from 'chai';
|
||||
import * as estraverse from 'estraverse';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TNodeWithBlockStatement } from '../../../src/types/TNodeWithBlockStatement';
|
||||
import { TNodeWithBlockStatement } from '../../../src/types/node/TNodeWithBlockStatement';
|
||||
|
||||
import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IStackTraceAnalyzer } from '../../../src/interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as escodegen from 'escodegen';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TStatement } from '../../src/types/TStatement';
|
||||
import { TStatement } from '../../src/types/node/TStatement';
|
||||
|
||||
import { NodeType } from '../../src/enums/NodeType';
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { IInputOptions } from '../../src/interfaces/IInputOptions';
|
||||
import { IOptions } from '../../src/interfaces/IOptions';
|
||||
import { IInputOptions } from '../../src/interfaces/options/IInputOptions';
|
||||
import { IOptions } from '../../src/interfaces/options/IOptions';
|
||||
|
||||
import { DEFAULT_PRESET } from '../../src/preset-options/DefaultPreset';
|
||||
|
||||
|
||||
@@ -1,35 +1,41 @@
|
||||
import { IObfuscationResult } from '../../src/interfaces/IObfuscationResult';
|
||||
import { ISourceMapCorrector } from '../../src/interfaces/ISourceMapCorrector';
|
||||
import { ServiceIdentifiers } from '../../src/container/ServiceIdentifiers';
|
||||
|
||||
import { TSourceMapMode } from '../../src/types/TSourceMapMode';
|
||||
|
||||
import { IInversifyContainerFacade } from '../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IObfuscationResult } from '../../src/interfaces/IObfuscationResult';
|
||||
import { ISourceMapCorrector } from '../../src/interfaces/ISourceMapCorrector';
|
||||
|
||||
import { SourceMapMode } from '../../src/enums/SourceMapMode';
|
||||
|
||||
import { ObfuscationResult } from '../../src/ObfuscationResult';
|
||||
import { SourceMapCorrector } from '../../src/SourceMapCorrector';
|
||||
import { InversifyContainerFacade } from '../../src/container/InversifyContainerFacade';
|
||||
|
||||
const assert: Chai.AssertStatic = require('chai').assert;
|
||||
|
||||
/**
|
||||
* @param obfuscatedCode
|
||||
* @param sourceMap
|
||||
* @param sourceMapUrl
|
||||
* @param sourceMapBaseUrl
|
||||
* @param sourceMapFileName
|
||||
* @param sourceMapMode
|
||||
*/
|
||||
function getCorrectedObfuscationResult (
|
||||
obfuscatedCode: string,
|
||||
sourceMap: string,
|
||||
sourceMapUrl: string,
|
||||
sourceMapBaseUrl: string,
|
||||
sourceMapFileName: string,
|
||||
sourceMapMode: TSourceMapMode
|
||||
): IObfuscationResult {
|
||||
let obfuscationResult: IObfuscationResult = new ObfuscationResult(obfuscatedCode, sourceMap),
|
||||
sourceMapCorrector: ISourceMapCorrector = new SourceMapCorrector(
|
||||
obfuscationResult,
|
||||
sourceMapUrl,
|
||||
sourceMapMode
|
||||
);
|
||||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade({
|
||||
sourceMap: true,
|
||||
sourceMapBaseUrl: sourceMapBaseUrl,
|
||||
sourceMapFileName: sourceMapFileName,
|
||||
sourceMapMode: sourceMapMode
|
||||
});
|
||||
const sourceMapCorrector: ISourceMapCorrector = inversifyContainerFacade
|
||||
.get<ISourceMapCorrector>(ServiceIdentifiers.ISourceMapCorrector);
|
||||
|
||||
return sourceMapCorrector.correct();
|
||||
return sourceMapCorrector.correct(obfuscatedCode, sourceMap);
|
||||
}
|
||||
|
||||
describe('SourceMapCorrector', () => {
|
||||
@@ -39,7 +45,13 @@ describe('SourceMapCorrector', () => {
|
||||
sourceMap: string = 'test';
|
||||
|
||||
it('should return untouched obfuscated code if source map does not exist', () => {
|
||||
expectedObfuscationResult = getCorrectedObfuscationResult(obfuscatedCode, '', '', SourceMapMode.Separate);
|
||||
expectedObfuscationResult = getCorrectedObfuscationResult(
|
||||
obfuscatedCode,
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
SourceMapMode.Separate)
|
||||
;
|
||||
|
||||
assert.equal(expectedObfuscationResult.getObfuscatedCode(), obfuscatedCode);
|
||||
});
|
||||
@@ -50,6 +62,7 @@ describe('SourceMapCorrector', () => {
|
||||
obfuscatedCode,
|
||||
sourceMap,
|
||||
'',
|
||||
'',
|
||||
SourceMapMode.Inline
|
||||
);
|
||||
});
|
||||
@@ -63,11 +76,12 @@ describe('SourceMapCorrector', () => {
|
||||
expectedObfuscationResult = getCorrectedObfuscationResult(
|
||||
obfuscatedCode,
|
||||
sourceMap,
|
||||
'http://example.com',
|
||||
'output.js.map',
|
||||
SourceMapMode.Separate
|
||||
);
|
||||
|
||||
assert.match(expectedObfuscationResult.getObfuscatedCode(), /sourceMappingURL=output\.js\.map/);
|
||||
assert.match(expectedObfuscationResult.getObfuscatedCode(), /sourceMappingURL=http:\/\/example\.com\/output\.js\.map/);
|
||||
});
|
||||
|
||||
it('should not touch obfuscated code if source map mode is `separate` and `sourceMapUrl` is not set', () => {
|
||||
@@ -75,6 +89,7 @@ describe('SourceMapCorrector', () => {
|
||||
obfuscatedCode,
|
||||
sourceMap,
|
||||
'',
|
||||
'',
|
||||
SourceMapMode.Separate
|
||||
);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
|
||||
import * as chai from 'chai';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TStatement } from '../../../src/types/TStatement';
|
||||
import { TStatement } from '../../../src/types/node/TStatement';
|
||||
|
||||
import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IStackTraceAnalyzer } from '../../../src/interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
|
||||
|
||||
@@ -159,6 +159,88 @@ describe('NodeUtils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('getNodeBlockScopeDepth (node: ESTree.Node, depth: number = 0): number', () => {
|
||||
let functionDeclarationBlockStatementNode1: ESTree.BlockStatement,
|
||||
functionDeclarationBlockStatementNode2: ESTree.BlockStatement,
|
||||
ifStatementBlockStatementNode1: ESTree.BlockStatement,
|
||||
ifStatementBlockStatementNode2: ESTree.BlockStatement,
|
||||
ifStatementNode1: ESTree.IfStatement,
|
||||
ifStatementNode2: ESTree.IfStatement,
|
||||
expressionStatementNode1: ESTree.ExpressionStatement,
|
||||
expressionStatementNode2: ESTree.ExpressionStatement,
|
||||
expressionStatementNode3: ESTree.ExpressionStatement,
|
||||
functionDeclarationNode1: ESTree.FunctionDeclaration,
|
||||
functionDeclarationNode2: ESTree.FunctionDeclaration,
|
||||
programNode: ESTree.Program;
|
||||
|
||||
beforeEach(() => {
|
||||
expressionStatementNode1 = NodeMocks.getExpressionStatementNode();
|
||||
expressionStatementNode2 = NodeMocks.getExpressionStatementNode();
|
||||
expressionStatementNode3 = NodeMocks.getExpressionStatementNode();
|
||||
|
||||
ifStatementBlockStatementNode2 = NodeMocks.getBlockStatementNode([
|
||||
expressionStatementNode3
|
||||
]);
|
||||
|
||||
ifStatementNode2 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode2);
|
||||
|
||||
functionDeclarationBlockStatementNode2 = NodeMocks.getBlockStatementNode([
|
||||
ifStatementNode2,
|
||||
expressionStatementNode2
|
||||
]);
|
||||
|
||||
functionDeclarationNode2 = NodeMocks.getFunctionDeclarationNode('test', functionDeclarationBlockStatementNode2);
|
||||
|
||||
ifStatementBlockStatementNode1 = NodeMocks.getBlockStatementNode([
|
||||
functionDeclarationNode2
|
||||
]);
|
||||
|
||||
ifStatementNode1 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode1);
|
||||
|
||||
functionDeclarationBlockStatementNode1 = NodeMocks.getBlockStatementNode([
|
||||
expressionStatementNode1,
|
||||
ifStatementNode1
|
||||
]);
|
||||
|
||||
functionDeclarationNode1 = NodeMocks.getFunctionDeclarationNode('test', functionDeclarationBlockStatementNode1);
|
||||
|
||||
programNode = NodeMocks.getProgramNode([
|
||||
functionDeclarationNode1
|
||||
]);
|
||||
|
||||
programNode['parentNode'] = programNode;
|
||||
functionDeclarationNode1['parentNode'] = programNode;
|
||||
functionDeclarationBlockStatementNode1['parentNode'] = functionDeclarationNode1;
|
||||
expressionStatementNode1['parentNode'] = functionDeclarationBlockStatementNode1;
|
||||
ifStatementNode1['parentNode'] = functionDeclarationBlockStatementNode1;
|
||||
ifStatementBlockStatementNode1['parentNode'] = ifStatementNode1;
|
||||
functionDeclarationNode2['parentNode'] = ifStatementBlockStatementNode1;
|
||||
functionDeclarationBlockStatementNode2['parentNode'] = functionDeclarationNode2;
|
||||
expressionStatementNode2['parentNode'] = functionDeclarationBlockStatementNode2;
|
||||
ifStatementNode2['parentNode'] = functionDeclarationBlockStatementNode2;
|
||||
ifStatementBlockStatementNode2['parentNode'] = ifStatementNode2;
|
||||
});
|
||||
|
||||
it('should return block-scope depth for given node', () => {
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(programNode), 0);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(functionDeclarationNode1), 0);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(functionDeclarationBlockStatementNode1), 1);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(expressionStatementNode1), 1);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(ifStatementNode1), 1);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(ifStatementBlockStatementNode1), 1);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(functionDeclarationNode2), 1);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(functionDeclarationBlockStatementNode2), 2);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(expressionStatementNode2), 2);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(ifStatementNode2), 2);
|
||||
assert.deepEqual(NodeUtils.getNodeBlockScopeDepth(ifStatementBlockStatementNode2), 2);
|
||||
});
|
||||
|
||||
it('should throw a `ReferenceError` if node has no `parentNode` property', () => {
|
||||
assert.throws(() => NodeUtils.getNodeBlockScopeDepth(expressionStatementNode3), ReferenceError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parentize (node: ESTree.Node): void', () => {
|
||||
let ifStatementNode: ESTree.IfStatement,
|
||||
ifStatementBlockStatementNode: ESTree.BlockStatement,
|
||||
|
||||
Reference in New Issue
Block a user