Ver Fonte

NodeUtils refactoring

sanex3339 há 9 anos atrás
pai
commit
8145210d22
2 ficheiros alterados com 11 adições e 20 exclusões
  1. 5 9
      dist/index.js
  2. 6 11
      src/NodeUtils.ts

+ 5 - 9
dist/index.js

@@ -263,19 +263,15 @@ module.exports =
 	            if (!node.parentNode) {
 	                throw new ReferenceError('`parentNode` property of given node is `undefined`');
 	            }
-	            if (node.parentNode.type === NodeType_1.NodeType.Program) {
-	                return node.parentNode;
-	            }
 	            if (!Utils_1.Utils.arrayContains(NodeUtils.nodesWithBlockScope, node.parentNode.type)) {
 	                return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
-	            }
-	            if (depth > 0) {
+	            } else if (depth > 0) {
 	                return NodeUtils.getBlockScopeOfNode(node.parentNode, --depth);
 	            }
-	            if (node.type !== NodeType_1.NodeType.BlockStatement) {
-	                return NodeUtils.getBlockScopeOfNode(node.parentNode);
+	            if (Nodes_1.Nodes.isProgramNode(node) || Nodes_1.Nodes.isBlockStatementNode(node)) {
+	                return node;
 	            }
-	            return node;
+	            return NodeUtils.getBlockScopeOfNode(node.parentNode);
 	        }
 	    }, {
 	        key: "insertNodeAtIndex",
@@ -319,7 +315,7 @@ module.exports =
 	    return NodeUtils;
 	}();
 	
-	NodeUtils.nodesWithBlockScope = [NodeType_1.NodeType.ArrowFunctionExpression, NodeType_1.NodeType.FunctionDeclaration, NodeType_1.NodeType.FunctionExpression, NodeType_1.NodeType.MethodDefinition];
+	NodeUtils.nodesWithBlockScope = [NodeType_1.NodeType.ArrowFunctionExpression, NodeType_1.NodeType.FunctionDeclaration, NodeType_1.NodeType.FunctionExpression, NodeType_1.NodeType.MethodDefinition, NodeType_1.NodeType.Program];
 	exports.NodeUtils = NodeUtils;
 
 /***/ },

+ 6 - 11
src/NodeUtils.ts

@@ -17,7 +17,8 @@ export class NodeUtils {
         NodeType.ArrowFunctionExpression,
         NodeType.FunctionDeclaration,
         NodeType.FunctionExpression,
-        NodeType.MethodDefinition
+        NodeType.MethodDefinition,
+        NodeType.Program
     ];
 
     /**
@@ -72,23 +73,17 @@ export class NodeUtils {
             throw new ReferenceError('`parentNode` property of given node is `undefined`');
         }
 
-        if (node.parentNode.type === NodeType.Program) {
-            return <TNodeWithBlockStatement> node.parentNode;
-        }
-
         if (!Utils.arrayContains(NodeUtils.nodesWithBlockScope, node.parentNode.type)) {
             return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
-        }
-
-        if (depth > 0) {
+        } else if (depth > 0) {
             return NodeUtils.getBlockScopeOfNode(node.parentNode, --depth);
         }
 
-        if (node.type !== NodeType.BlockStatement) {
-            return NodeUtils.getBlockScopeOfNode(node.parentNode);
+        if (Nodes.isProgramNode(node) || Nodes.isBlockStatementNode(node)) {
+            return node;
         }
 
-        return <TNodeWithBlockStatement> node; // blocks statement of scopeNodes
+        return NodeUtils.getBlockScopeOfNode(node.parentNode);
     }
 
     /**