mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added ObjectExpressionCalleeDataExtractor: added tests
This commit is contained in:
@@ -5,6 +5,22 @@ var object = {
|
||||
}
|
||||
},
|
||||
|
||||
bar: function () {
|
||||
function inner () {
|
||||
|
||||
}
|
||||
|
||||
inner();
|
||||
}
|
||||
};
|
||||
|
||||
var object1 = {
|
||||
foo: {
|
||||
baz: function () {
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
bar: function () {
|
||||
function inner1 () {
|
||||
|
||||
@@ -14,6 +30,8 @@ var object = {
|
||||
}
|
||||
};
|
||||
|
||||
object.foo.baz();
|
||||
object['foo'].baz();
|
||||
object1.foo.baz();
|
||||
object1['foo'].baz();
|
||||
object1.bar();
|
||||
|
||||
object.bar();
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
var bar = function () {
|
||||
};
|
||||
|
||||
bar();
|
||||
@@ -66,32 +66,6 @@ function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTre
|
||||
return functionExpressionNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param astTree
|
||||
* @param name
|
||||
* @returns {ESTree.FunctionExpression|null}
|
||||
*/
|
||||
function getObjectFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTree.FunctionExpression|null {
|
||||
let functionExpressionNode: ESTree.FunctionExpression|null = null;
|
||||
|
||||
estraverse.traverse(astTree, {
|
||||
enter: (node: ESTree.Node): any => {
|
||||
if (
|
||||
Nodes.isPropertyNode(node) &&
|
||||
Nodes.isFunctionExpressionNode(node.value) &&
|
||||
Nodes.isIdentifierNode(node.key) &&
|
||||
node.key.name === name
|
||||
) {
|
||||
functionExpressionNode = node.value;
|
||||
|
||||
return estraverse.VisitorOption.Break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return functionExpressionNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param astTree
|
||||
* @param id
|
||||
@@ -118,6 +92,54 @@ function getFunctionExpressionById (astTree: ESTree.Node, id: string): ESTree.Fu
|
||||
return functionExpressionNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param astTree
|
||||
* @param objectName
|
||||
* @param name
|
||||
* @returns {ESTree.FunctionExpression|null}
|
||||
*/
|
||||
function getObjectFunctionExpressionByName (astTree: ESTree.Node, objectName: string, name: string): ESTree.FunctionExpression|null {
|
||||
let functionExpressionNode: ESTree.FunctionExpression|null = null,
|
||||
targetObjectExpressionNode: ESTree.ObjectExpression|null = null;
|
||||
|
||||
estraverse.traverse(astTree, {
|
||||
enter: (node: ESTree.Node): any => {
|
||||
if (
|
||||
Nodes.isVariableDeclaratorNode(node) &&
|
||||
Nodes.isIdentifierNode(node.id) &&
|
||||
node.init &&
|
||||
Nodes.isObjectExpressionNode(node.init) &&
|
||||
node.id.name === objectName
|
||||
) {
|
||||
targetObjectExpressionNode = node.init;
|
||||
|
||||
return estraverse.VisitorOption.Break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!targetObjectExpressionNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
estraverse.traverse(targetObjectExpressionNode, {
|
||||
enter: (node: ESTree.Node): any => {
|
||||
if (
|
||||
Nodes.isPropertyNode(node) &&
|
||||
Nodes.isFunctionExpressionNode(node.value) &&
|
||||
Nodes.isIdentifierNode(node.key) &&
|
||||
node.key.name === name
|
||||
) {
|
||||
functionExpressionNode = node.value;
|
||||
|
||||
return estraverse.VisitorOption.Break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return functionExpressionNode;
|
||||
}
|
||||
|
||||
describe('StackTraceAnalyzer', () => {
|
||||
describe('extract (): IStackTraceData[]', () => {
|
||||
let astTree: TNodeWithBlockStatement,
|
||||
@@ -268,17 +290,17 @@ describe('StackTraceAnalyzer', () => {
|
||||
expectedStackTraceData = [
|
||||
{
|
||||
name: 'baz',
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'baz')).body,
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
|
||||
stackTrace: []
|
||||
},
|
||||
{
|
||||
name: 'baz',
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'baz')).body,
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
|
||||
stackTrace: []
|
||||
},
|
||||
{
|
||||
name: 'bar',
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'bar')).body,
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'bar')).body,
|
||||
stackTrace: [
|
||||
{
|
||||
name: 'inner1',
|
||||
@@ -289,6 +311,19 @@ describe('StackTraceAnalyzer', () => {
|
||||
},
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'bar',
|
||||
callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object', 'bar')).body,
|
||||
stackTrace: [
|
||||
{
|
||||
name: 'inner',
|
||||
callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner')).body,
|
||||
stackTrace: [
|
||||
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
|
||||
@@ -352,5 +387,24 @@ describe('StackTraceAnalyzer', () => {
|
||||
|
||||
assert.deepEqual(stackTraceData, expectedStackTraceData);
|
||||
});
|
||||
|
||||
it('should returns correct BlockScopeTraceData - variant #9: no recursion', () => {
|
||||
astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
|
||||
readFileAsString('./test/fixtures/stack-trace-analyzer/no-recursion.js'),
|
||||
false
|
||||
);
|
||||
|
||||
expectedStackTraceData = [
|
||||
{
|
||||
name: 'bar',
|
||||
callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'bar')).body,
|
||||
stackTrace: []
|
||||
}
|
||||
];
|
||||
|
||||
stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
|
||||
|
||||
assert.deepEqual(stackTraceData, expectedStackTraceData);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user