|
@@ -68,21 +68,21 @@ function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTre
|
|
|
|
|
|
/**
|
|
/**
|
|
* @param astTree
|
|
* @param astTree
|
|
- * @param name
|
|
|
|
|
|
+ * @param id
|
|
* @returns {ESTree.FunctionExpression|null}
|
|
* @returns {ESTree.FunctionExpression|null}
|
|
*/
|
|
*/
|
|
-function getObjectFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTree.FunctionExpression|null {
|
|
|
|
|
|
+function getFunctionExpressionById (astTree: ESTree.Node, id: string): ESTree.FunctionExpression|null {
|
|
let functionExpressionNode: ESTree.FunctionExpression|null = null;
|
|
let functionExpressionNode: ESTree.FunctionExpression|null = null;
|
|
|
|
|
|
estraverse.traverse(astTree, {
|
|
estraverse.traverse(astTree, {
|
|
enter: (node: ESTree.Node): any => {
|
|
enter: (node: ESTree.Node): any => {
|
|
if (
|
|
if (
|
|
- Nodes.isPropertyNode(node) &&
|
|
|
|
- Nodes.isFunctionExpressionNode(node.value) &&
|
|
|
|
- Nodes.isIdentifierNode(node.key) &&
|
|
|
|
- node.key.name === name
|
|
|
|
|
|
+ Nodes.isFunctionExpressionNode(node) &&
|
|
|
|
+ node.id &&
|
|
|
|
+ Nodes.isIdentifierNode(node.id) &&
|
|
|
|
+ node.id.name === id
|
|
) {
|
|
) {
|
|
- functionExpressionNode = node.value;
|
|
|
|
|
|
+ functionExpressionNode = node;
|
|
|
|
|
|
return estraverse.VisitorOption.Break;
|
|
return estraverse.VisitorOption.Break;
|
|
}
|
|
}
|
|
@@ -94,21 +94,43 @@ function getObjectFunctionExpressionByName (astTree: ESTree.Node, name: string):
|
|
|
|
|
|
/**
|
|
/**
|
|
* @param astTree
|
|
* @param astTree
|
|
- * @param id
|
|
|
|
|
|
+ * @param objectName
|
|
|
|
+ * @param name
|
|
* @returns {ESTree.FunctionExpression|null}
|
|
* @returns {ESTree.FunctionExpression|null}
|
|
*/
|
|
*/
|
|
-function getFunctionExpressionById (astTree: ESTree.Node, id: string): ESTree.FunctionExpression|null {
|
|
|
|
- let functionExpressionNode: ESTree.FunctionExpression|null = 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, {
|
|
estraverse.traverse(astTree, {
|
|
enter: (node: ESTree.Node): any => {
|
|
enter: (node: ESTree.Node): any => {
|
|
if (
|
|
if (
|
|
- Nodes.isFunctionExpressionNode(node) &&
|
|
|
|
- node.id &&
|
|
|
|
|
|
+ Nodes.isVariableDeclaratorNode(node) &&
|
|
Nodes.isIdentifierNode(node.id) &&
|
|
Nodes.isIdentifierNode(node.id) &&
|
|
- node.id.name === id
|
|
|
|
|
|
+ node.init &&
|
|
|
|
+ Nodes.isObjectExpressionNode(node.init) &&
|
|
|
|
+ node.id.name === objectName
|
|
) {
|
|
) {
|
|
- functionExpressionNode = node;
|
|
|
|
|
|
+ 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 estraverse.VisitorOption.Break;
|
|
}
|
|
}
|
|
@@ -268,17 +290,17 @@ describe('StackTraceAnalyzer', () => {
|
|
expectedStackTraceData = [
|
|
expectedStackTraceData = [
|
|
{
|
|
{
|
|
name: 'baz',
|
|
name: 'baz',
|
|
- callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'baz')).body,
|
|
|
|
|
|
+ callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
|
|
stackTrace: []
|
|
stackTrace: []
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: 'baz',
|
|
name: 'baz',
|
|
- callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'baz')).body,
|
|
|
|
|
|
+ callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
|
|
stackTrace: []
|
|
stackTrace: []
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: 'bar',
|
|
name: 'bar',
|
|
- callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'bar')).body,
|
|
|
|
|
|
+ callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'bar')).body,
|
|
stackTrace: [
|
|
stackTrace: [
|
|
{
|
|
{
|
|
name: 'inner1',
|
|
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();
|
|
stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
|
|
@@ -352,5 +387,24 @@ describe('StackTraceAnalyzer', () => {
|
|
|
|
|
|
assert.deepEqual(stackTraceData, expectedStackTraceData);
|
|
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);
|
|
|
|
+ });
|
|
});
|
|
});
|
|
});
|
|
});
|