From 320719360c8d82bc8e9a0add15cc1c79ee18496e Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Fri, 17 Jun 2016 16:55:28 +0300 Subject: [PATCH] tests and refactoring --- dist/index.js | 7 +++++-- src/NodeUtils.ts | 8 ++++++-- test/NodeUtils.spec.ts | 14 +++++++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dist/index.js b/dist/index.js index 413e28a3..70ed8070 100644 --- a/dist/index.js +++ b/dist/index.js @@ -247,10 +247,13 @@ module.exports = value: function getBlockStatementNodeByIndex(node) { var index = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; - if (Nodes_1.Nodes.isNodeHasBlockStatement(node) && node.body[index]) { + if (Nodes_1.Nodes.isNodeHasBlockStatement(node)) { + if (node.body[index] === undefined) { + throw new ReferenceError("Wrong index `" + index + "`. BlockStatement body length is `" + node.body.length + "`."); + } return node.body[index]; } - return node; + throw new TypeError('The specified node has not block statement'); } }, { key: "getBlockScopeOfNode", diff --git a/src/NodeUtils.ts b/src/NodeUtils.ts index e859b199..8bdba31e 100644 --- a/src/NodeUtils.ts +++ b/src/NodeUtils.ts @@ -51,11 +51,15 @@ export class NodeUtils { * @returns {INode} */ public static getBlockStatementNodeByIndex (node: INode, index: number = 0): INode { - if (Nodes.isNodeHasBlockStatement(node) && node.body[index]) { + if (Nodes.isNodeHasBlockStatement(node)) { + if (node.body[index] === undefined) { + throw new ReferenceError(`Wrong index \`${index}\`. BlockStatement body length is \`${node.body.length}\`.`); + } + return node.body[index]; } - return node; + throw new TypeError('The specified node has not block statement'); } /** diff --git a/test/NodeUtils.spec.ts b/test/NodeUtils.spec.ts index 8431eb68..128856d1 100644 --- a/test/NodeUtils.spec.ts +++ b/test/NodeUtils.spec.ts @@ -87,17 +87,21 @@ describe('NodeUtils', () => { }; }); - it('should return block-scope node of given node by given index if node has block-scope', () => { + it('should return block-statement child node of given node if that node has block-statement', () => { assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode), identifierNode); assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode, 1), literalNode); }); - it('should return root node if index is out of boundaries', () => { - assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode, 2), blockStatementNode); + it('should throw a `ReferenceError` if index is out of boundaries', () => { + assert.throws(function () { + return NodeUtils.getBlockStatementNodeByIndex(blockStatementNode, 2); + }, ReferenceError); }); - it('should return root node if node has not block-scope', () => { - assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(identifierNode, 1), identifierNode); + it('should throw a `TypeError` if node has not block-statement', () => { + assert.throws(function () { + NodeUtils.getBlockStatementNodeByIndex(identifierNode, 1) + }, TypeError); }); });