Ver Fonte

tests and refactoring

sanex3339 há 9 anos atrás
pai
commit
320719360c
3 ficheiros alterados com 20 adições e 9 exclusões
  1. 5 2
      dist/index.js
  2. 6 2
      src/NodeUtils.ts
  3. 9 5
      test/NodeUtils.spec.ts

+ 5 - 2
dist/index.js

@@ -247,10 +247,13 @@ module.exports =
 	        value: function getBlockStatementNodeByIndex(node) {
 	        value: function getBlockStatementNodeByIndex(node) {
 	            var index = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
 	            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.body[index];
 	            }
 	            }
-	            return node;
+	            throw new TypeError('The specified node has not block statement');
 	        }
 	        }
 	    }, {
 	    }, {
 	        key: "getBlockScopeOfNode",
 	        key: "getBlockScopeOfNode",

+ 6 - 2
src/NodeUtils.ts

@@ -51,11 +51,15 @@ export class NodeUtils {
      * @returns {INode}
      * @returns {INode}
      */
      */
     public static getBlockStatementNodeByIndex (node: INode, index: number = 0): 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.body[index];
         }
         }
 
 
-        return node;
+        throw new TypeError('The specified node has not block statement');
     }
     }
 
 
     /**
     /**

+ 9 - 5
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), identifierNode);
             assert.deepEqual(NodeUtils.getBlockStatementNodeByIndex(blockStatementNode, 1), literalNode);
             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);
         });
         });
     });
     });