123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import * as chai from 'chai';
- import * as estraverse from 'estraverse';
- import * as ESTree from 'estree';
- import { TNodeWithBlockStatement } from '../../src/types/TNodeWithBlockStatement';
- import { IStackTraceData } from '../../src/interfaces/IStackTraceData';
- import { readFileAsString } from '../helpers/readFileAsString';
- import { Nodes } from '../../src/Nodes';
- import { NodeUtils } from '../../src/NodeUtils';
- import { StackTraceAnalyzer } from '../../src/StackTraceAnalyzer';
- const assert: any = chai.assert;
- /**
- * @param astTree
- * @param name
- * @returns {ESTree.FunctionDeclaration|null}
- */
- function getFunctionDeclarationByName (astTree: ESTree.Node, name: string): ESTree.FunctionDeclaration|null {
- let functionDeclarationNode: ESTree.FunctionDeclaration|null = null;
- estraverse.traverse(astTree, {
- enter: (node: ESTree.Node): any => {
- if (
- Nodes.isFunctionDeclarationNode(node) &&
- Nodes.isIdentifierNode(node.id) &&
- node.id.name === name
- ) {
- functionDeclarationNode = node;
- return estraverse.VisitorOption.Break;
- }
- }
- });
- return functionDeclarationNode;
- }
- /**
- * @param astTree
- * @param name
- * @returns {ESTree.FunctionExpression|null}
- */
- function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTree.FunctionExpression|null {
- let functionExpressionNode: ESTree.FunctionExpression|null = null;
- estraverse.traverse(astTree, {
- enter: (node: ESTree.Node): any => {
- if (
- Nodes.isVariableDeclaratorNode(node) &&
- node.init &&
- Nodes.isFunctionExpressionNode(node.init) &&
- Nodes.isIdentifierNode(node.id) &&
- node.id.name === name
- ) {
- functionExpressionNode = node.init;
- return estraverse.VisitorOption.Break;
- }
- }
- });
- return functionExpressionNode;
- }
- describe('StackTraceAnalyzer', () => {
- describe('analyze (): IStackTraceData[]', () => {
- let ASTTree: TNodeWithBlockStatement,
- blockScopeTraceData: IStackTraceData[],
- expectedBlockScopeTraceData: IStackTraceData[];
- it('should returns correct BlockScopeTraceData - variant #1', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
- readFileAsString(
- require.resolve('../fixtures/stack-trace-analyzer/variant-1.js')
- ),
- false
- );
- expectedBlockScopeTraceData = [
- {
- name: 'baz',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'baz')).body,
- stackTrace: []
- },
- {
- name: 'foo',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'foo')).body,
- stackTrace: []
- },
- {
- name: 'bar',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
- stackTrace: [
- {
- name: 'inner2',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner2')).body,
- stackTrace: [
- {
- name: 'inner3',
- callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(ASTTree, 'inner3')).body,
- stackTrace: []
- },
- ]
- },
- {
- name: 'inner1',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner1')).body,
- stackTrace: []
- },
- ]
- }
- ];
- blockScopeTraceData = new StackTraceAnalyzer(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #2', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
- readFileAsString(
- require.resolve('../fixtures/stack-trace-analyzer/variant-2.js')
- ),
- false
- );
- expectedBlockScopeTraceData = [
- {
- name: 'bar',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
- stackTrace: []
- },
- {
- name: 'baz',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'baz')).body,
- stackTrace: [
- {
- name: 'inner1',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner1')).body,
- stackTrace: []
- },
- ]
- },
- {
- name: 'foo',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'foo')).body,
- stackTrace: []
- }
- ];
- blockScopeTraceData = new StackTraceAnalyzer(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #3', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
- readFileAsString(
- require.resolve('../fixtures/stack-trace-analyzer/variant-3.js')
- ),
- false
- );
- expectedBlockScopeTraceData = [
- {
- name: 'bar',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
- stackTrace: []
- }
- ];
- blockScopeTraceData = new StackTraceAnalyzer(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #4', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
- readFileAsString(
- require.resolve('../fixtures/stack-trace-analyzer/variant-4.js')
- ),
- false
- );
- expectedBlockScopeTraceData = [];
- blockScopeTraceData = new StackTraceAnalyzer(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #5', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
- readFileAsString(
- require.resolve('../fixtures/stack-trace-analyzer/variant-5.js')
- ),
- false
- );
- expectedBlockScopeTraceData = [];
- blockScopeTraceData = new StackTraceAnalyzer(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- });
- });
|