|
@@ -1,25 +1,19 @@
|
|
import * as estraverse from 'estraverse';
|
|
import * as estraverse from 'estraverse';
|
|
|
|
|
|
-import { IBlockStatementNode } from "./interfaces/nodes/IBlockStatementNode";
|
|
|
|
-import { IIdentifierNode } from "./interfaces/nodes/IIdentifierNode";
|
|
|
|
-import { ILiteralNode } from "./interfaces/nodes/ILiteralNode";
|
|
|
|
-import { IMemberExpressionNode } from "./interfaces/nodes/IMemberExpressionNode";
|
|
|
|
-import { IProgramNode } from "./interfaces/nodes/IProgramNode";
|
|
|
|
-import { IPropertyNode } from "./interfaces/nodes/IPropertyNode";
|
|
|
|
import { INode } from './interfaces/nodes/INode';
|
|
import { INode } from './interfaces/nodes/INode';
|
|
-import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode";
|
|
|
|
|
|
|
|
-import { TBlockScopeNode } from "./types/TBlockScopeNode";
|
|
|
|
|
|
+import { TNodeWithBlockStatement } from "./types/TNodeWithBlockStatement";
|
|
|
|
|
|
import { NodeType } from "./enums/NodeType";
|
|
import { NodeType } from "./enums/NodeType";
|
|
|
|
|
|
|
|
+import { Nodes } from "./Nodes";
|
|
import { Utils } from "./Utils";
|
|
import { Utils } from "./Utils";
|
|
|
|
|
|
export class NodeUtils {
|
|
export class NodeUtils {
|
|
/**
|
|
/**
|
|
* @type {string[]}
|
|
* @type {string[]}
|
|
*/
|
|
*/
|
|
- private static scopeNodes: string[] = [
|
|
|
|
|
|
+ private static nodesWithBlockScope: string[] = [
|
|
NodeType.ArrowFunctionExpression,
|
|
NodeType.ArrowFunctionExpression,
|
|
NodeType.FunctionDeclaration,
|
|
NodeType.FunctionDeclaration,
|
|
NodeType.FunctionExpression,
|
|
NodeType.FunctionExpression,
|
|
@@ -32,7 +26,7 @@ export class NodeUtils {
|
|
public static addXVerbatimPropertyToLiterals (node: INode): void {
|
|
public static addXVerbatimPropertyToLiterals (node: INode): void {
|
|
estraverse.replace(node, {
|
|
estraverse.replace(node, {
|
|
enter: (node: INode, parentNode: INode): any => {
|
|
enter: (node: INode, parentNode: INode): any => {
|
|
- if (NodeUtils.isLiteralNode(node)) {
|
|
|
|
|
|
+ if (Nodes.isLiteralNode(node)) {
|
|
node['x-verbatim-property'] = node.raw;
|
|
node['x-verbatim-property'] = node.raw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -56,8 +50,8 @@ export class NodeUtils {
|
|
* @param index
|
|
* @param index
|
|
* @returns {INode}
|
|
* @returns {INode}
|
|
*/
|
|
*/
|
|
- public static getBlockScopeNodeByIndex (node: INode, index: number = 0): INode {
|
|
|
|
- if (NodeUtils.isNodeHasBlockScope(node) && node.body[index]) {
|
|
|
|
|
|
+ public static getBlockStatementNodeByIndex (node: INode, index: number = 0): INode {
|
|
|
|
+ if (Nodes.isNodeHasBlockStatement(node) && node.body[index]) {
|
|
return node.body[index];
|
|
return node.body[index];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -69,16 +63,16 @@ export class NodeUtils {
|
|
* @param depth
|
|
* @param depth
|
|
* @returns {INode}
|
|
* @returns {INode}
|
|
*/
|
|
*/
|
|
- public static getBlockScopeOfNode (node: INode, depth: number = 0): TBlockScopeNode {
|
|
|
|
|
|
+ public static getBlockScopeOfNode (node: INode, depth: number = 0): TNodeWithBlockStatement {
|
|
if (!node.parentNode) {
|
|
if (!node.parentNode) {
|
|
throw new ReferenceError('`parentNode` property of given node is `undefined`');
|
|
throw new ReferenceError('`parentNode` property of given node is `undefined`');
|
|
}
|
|
}
|
|
|
|
|
|
if (node.parentNode.type === NodeType.Program) {
|
|
if (node.parentNode.type === NodeType.Program) {
|
|
- return <TBlockScopeNode> node.parentNode;
|
|
|
|
|
|
+ return <TNodeWithBlockStatement> node.parentNode;
|
|
}
|
|
}
|
|
|
|
|
|
- if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
|
|
|
|
|
|
+ if (!Utils.arrayContains(NodeUtils.nodesWithBlockScope, node.parentNode.type)) {
|
|
return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
|
|
return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -90,18 +84,7 @@ export class NodeUtils {
|
|
return NodeUtils.getBlockScopeOfNode(node.parentNode);
|
|
return NodeUtils.getBlockScopeOfNode(node.parentNode);
|
|
}
|
|
}
|
|
|
|
|
|
- return <TBlockScopeNode> node; // blocks statement of scopeNodes
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param bodyNode
|
|
|
|
- * @returns IProgramNode
|
|
|
|
- */
|
|
|
|
- public static getProgramNode (bodyNode: INode[]): IProgramNode {
|
|
|
|
- return {
|
|
|
|
- 'type': NodeType.Program,
|
|
|
|
- 'body': bodyNode
|
|
|
|
- };
|
|
|
|
|
|
+ return <TNodeWithBlockStatement> node; // blocks statement of scopeNodes
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -117,73 +100,6 @@ export class NodeUtils {
|
|
blockScopeBody.splice(index, 0, node);
|
|
blockScopeBody.splice(index, 0, node);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isBlockStatementNode (node: INode): node is IBlockStatementNode {
|
|
|
|
- return node.type === NodeType.BlockStatement;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isIdentifierNode (node: INode): node is IIdentifierNode {
|
|
|
|
- return node.type === NodeType.Identifier;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isLiteralNode (node: INode): node is ILiteralNode {
|
|
|
|
- return node.type === NodeType.Literal;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isMemberExpressionNode (node: INode): node is IMemberExpressionNode {
|
|
|
|
- return node.type === NodeType.MemberExpression;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isNodeHasBlockScope (node: INode): node is TBlockScopeNode {
|
|
|
|
- return node.hasOwnProperty('body');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- *
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isProgramNode (node: INode): node is IProgramNode {
|
|
|
|
- return node.type === NodeType.Program;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- *
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isPropertyNode (node: INode): node is IPropertyNode {
|
|
|
|
- return node.type === NodeType.Property;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- *
|
|
|
|
- * @param node
|
|
|
|
- * @returns {boolean}
|
|
|
|
- */
|
|
|
|
- public static isVariableDeclaratorNode (node: INode): node is IVariableDeclaratorNode {
|
|
|
|
- return node.type === NodeType.VariableDeclarator;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* @param node
|
|
* @param node
|
|
*/
|
|
*/
|
|
@@ -195,7 +111,7 @@ export class NodeUtils {
|
|
Object.defineProperty(node, 'parentNode', {
|
|
Object.defineProperty(node, 'parentNode', {
|
|
configurable: true,
|
|
configurable: true,
|
|
enumerable: true,
|
|
enumerable: true,
|
|
- value: isRootNode ? NodeUtils.getProgramNode([node]) : parentNode || node,
|
|
|
|
|
|
+ value: isRootNode ? Nodes.getProgramNode([node]) : parentNode || node,
|
|
writable: true
|
|
writable: true
|
|
});
|
|
});
|
|
|
|
|