diff --git a/dist/index.js b/dist/index.js index 08f728de..56ab9c84 100644 --- a/dist/index.js +++ b/dist/index.js @@ -88,7 +88,7 @@ module.exports = /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports -/******/ return __webpack_require__(__webpack_require__.s = 81); +/******/ return __webpack_require__(__webpack_require__.s = 80); /******/ }) /************************************************************************/ /******/ ([ @@ -3685,12 +3685,15 @@ var FunctionExpressionCalleeDataExtractor = function () { if (Nodes_1.Nodes.isIdentifierNode(this.callee)) { calleeBlockStatement = this.getCalleeBlockStatement(NodeUtils_1.NodeUtils.getBlockScopeOfNode(this.blockScopeBody[0]), this.callee.name); } + if (Nodes_1.Nodes.isFunctionExpressionNode(this.callee)) { + calleeBlockStatement = this.callee.body; + } if (!calleeBlockStatement) { return null; } return { callee: calleeBlockStatement, - name: this.callee.name + name: this.callee.name || null }; } }, { @@ -3912,8 +3915,7 @@ module.exports = require("fs"); module.exports = require("mkdirp"); /***/ }, -/* 80 */, -/* 81 */ +/* 80 */ /***/ function(module, exports, __webpack_require__) { "use strict"; diff --git a/src/interfaces/stack-trace-analyzer/ICalleeData.d.ts b/src/interfaces/stack-trace-analyzer/ICalleeData.d.ts index cf42d2df..3cfed620 100644 --- a/src/interfaces/stack-trace-analyzer/ICalleeData.d.ts +++ b/src/interfaces/stack-trace-analyzer/ICalleeData.d.ts @@ -2,5 +2,5 @@ import { TNodeWithBlockStatement } from '../../types/TNodeWithBlockStatement'; export interface ICalleeData { callee: TNodeWithBlockStatement; - name: string; + name: string | null; } diff --git a/src/stack-trace-analyzer/callee-data-extractors/FunctionExpressionCalleeDataExtractor.ts b/src/stack-trace-analyzer/callee-data-extractors/FunctionExpressionCalleeDataExtractor.ts index 1cf55116..49a48815 100644 --- a/src/stack-trace-analyzer/callee-data-extractors/FunctionExpressionCalleeDataExtractor.ts +++ b/src/stack-trace-analyzer/callee-data-extractors/FunctionExpressionCalleeDataExtractor.ts @@ -42,13 +42,17 @@ export class FunctionExpressionCalleeDataExtractor implements ICalleeDataExtract ); } + if (Nodes.isFunctionExpressionNode(this.callee)) { + calleeBlockStatement = this.callee.body; + } + if (!calleeBlockStatement) { return null; } return { callee: calleeBlockStatement, - name: this.callee.name + name: this.callee.name || null }; } diff --git a/test/fixtures/stack-trace-analyzer/self-invoking-functions.js b/test/fixtures/stack-trace-analyzer/self-invoking-functions.js new file mode 100644 index 00000000..9858dabc --- /dev/null +++ b/test/fixtures/stack-trace-analyzer/self-invoking-functions.js @@ -0,0 +1,9 @@ +(function foo (){ + (function bar (){ + baz(); + + function baz () { + + } + }()); +})(); \ No newline at end of file diff --git a/test/functional-tests/stack-trace-analyzer/StackTraceAnalyzer.spec.ts b/test/functional-tests/stack-trace-analyzer/StackTraceAnalyzer.spec.ts index 1a220104..28e6c8d7 100644 --- a/test/functional-tests/stack-trace-analyzer/StackTraceAnalyzer.spec.ts +++ b/test/functional-tests/stack-trace-analyzer/StackTraceAnalyzer.spec.ts @@ -66,6 +66,32 @@ function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTre return functionExpressionNode; } +/** + * @param astTree + * @param id + * @returns {ESTree.FunctionExpression|null} + */ +function getFunctionExpressionById (astTree: ESTree.Node, id: string): ESTree.FunctionExpression|null { + let functionExpressionNode: ESTree.FunctionExpression|null = null; + + estraverse.traverse(astTree, { + enter: (node: ESTree.Node): any => { + if ( + Nodes.isFunctionExpressionNode(node) && + node.id && + Nodes.isIdentifierNode(node.id) && + node.id.name === id + ) { + functionExpressionNode = node; + + return estraverse.VisitorOption.Break; + } + } + }); + + return functionExpressionNode; +} + describe('StackTraceAnalyzer', () => { describe('extract (): IStackTraceData[]', () => { let astTree: TNodeWithBlockStatement, @@ -245,5 +271,32 @@ describe('StackTraceAnalyzer', () => { assert.deepEqual(stackTraceData, expectedStackTraceData); }); + + it('should returns correct BlockScopeTraceData - variant #8: self-invoking functions', () => { + astTree = NodeUtils.convertCodeToStructure( + readFileAsString('./test/fixtures/stack-trace-analyzer/self-invoking-functions.js'), + false + ); + + expectedStackTraceData = [ + { + name: null, + callee: (getFunctionExpressionById(astTree, 'foo')).body, + stackTrace: [{ + name: null, + callee: (getFunctionExpressionById(astTree, 'bar')).body, + stackTrace: [{ + name: 'baz', + callee: (getFunctionDeclarationByName(astTree, 'baz')).body, + stackTrace: [] + }] + }] + } + ]; + + stackTraceData = new StackTraceAnalyzer(astTree.body).analyze(); + + assert.deepEqual(stackTraceData, expectedStackTraceData); + }); }); });