Skip to content

Commit

Permalink
refactor(node-utils): extract isNodeOfType functions (#329)
Browse files Browse the repository at this point in the history
* refactor(node-utils): extract isNodeOfType functions

* refactor(node-utils): make isNodeOfType more type-safe
  • Loading branch information
MichaelDeBoey authored Apr 14, 2021
1 parent 9931a51 commit 445adc8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 102 deletions.
116 changes: 14 additions & 102 deletions lib/node-utils.ts → lib/node-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ import {
} from '@typescript-eslint/experimental-utils';
import { RuleContext } from '@typescript-eslint/experimental-utils/dist/ts-eslint';

import {
isArrayExpression,
isArrowFunctionExpression,
isBlockStatement,
isCallExpression,
isExpressionStatement,
isImportDeclaration,
isLiteral,
isMemberExpression,
isReturnStatement,
} from './is-node-of-type';

export * from './is-node-of-type';

const ValidLeftHandSideExpressions = [
AST_NODE_TYPES.CallExpression,
AST_NODE_TYPES.ClassExpression,
Expand Down Expand Up @@ -35,78 +49,6 @@ const ValidLeftHandSideExpressions = [
AST_NODE_TYPES.ArrowFunctionExpression,
];

export function isCallExpression(
node: TSESTree.Node | null | undefined
): node is TSESTree.CallExpression {
return node?.type === AST_NODE_TYPES.CallExpression;
}

export function isNewExpression(
node: TSESTree.Node | null | undefined
): node is TSESTree.NewExpression {
return node?.type === 'NewExpression';
}

export function isMemberExpression(
node: TSESTree.Node | null | undefined
): node is TSESTree.MemberExpression {
return node?.type === AST_NODE_TYPES.MemberExpression;
}

export function isLiteral(
node: TSESTree.Node | null | undefined
): node is TSESTree.Literal {
return node?.type === AST_NODE_TYPES.Literal;
}

export function isImportSpecifier(
node: TSESTree.Node | null | undefined
): node is TSESTree.ImportSpecifier {
return node?.type === AST_NODE_TYPES.ImportSpecifier;
}

export function isImportNamespaceSpecifier(
node: TSESTree.Node | null | undefined
): node is TSESTree.ImportNamespaceSpecifier {
return node?.type === AST_NODE_TYPES.ImportNamespaceSpecifier;
}

export function isImportDefaultSpecifier(
node: TSESTree.Node | null | undefined
): node is TSESTree.ImportDefaultSpecifier {
return node?.type === AST_NODE_TYPES.ImportDefaultSpecifier;
}

export function isBlockStatement(
node: TSESTree.Node | null | undefined
): node is TSESTree.BlockStatement {
return node?.type === AST_NODE_TYPES.BlockStatement;
}

export function isObjectPattern(
node: TSESTree.Node | null | undefined
): node is TSESTree.ObjectPattern {
return node?.type === AST_NODE_TYPES.ObjectPattern;
}

export function isProperty(
node: TSESTree.Node | null | undefined
): node is TSESTree.Property {
return node?.type === AST_NODE_TYPES.Property;
}

export function isJSXAttribute(
node: TSESTree.Node | null | undefined
): node is TSESTree.JSXAttribute {
return node?.type === AST_NODE_TYPES.JSXAttribute;
}

export function isExpressionStatement(
node: TSESTree.Node | null | undefined
): node is TSESTree.ExpressionStatement {
return node?.type === AST_NODE_TYPES.ExpressionStatement;
}

/**
* Finds the closest CallExpression node for a given node.
* @param node
Expand Down Expand Up @@ -153,12 +95,6 @@ export function findClosestCallNode(
}
}

export function isObjectExpression(
node: TSESTree.Expression
): node is TSESTree.ObjectExpression {
return node?.type === AST_NODE_TYPES.ObjectExpression;
}

export function hasThenProperty(node: TSESTree.Node): boolean {
return (
isMemberExpression(node) &&
Expand All @@ -167,30 +103,6 @@ export function hasThenProperty(node: TSESTree.Node): boolean {
);
}

export function isArrowFunctionExpression(
node: TSESTree.Node
): node is TSESTree.ArrowFunctionExpression {
return node?.type === AST_NODE_TYPES.ArrowFunctionExpression;
}

export function isReturnStatement(
node: TSESTree.Node
): node is TSESTree.ReturnStatement {
return node?.type === AST_NODE_TYPES.ReturnStatement;
}

export function isArrayExpression(
node: TSESTree.Node
): node is TSESTree.ArrayExpression {
return node?.type === AST_NODE_TYPES.ArrayExpression;
}

export function isImportDeclaration(
node: TSESTree.Node | null | undefined
): node is TSESTree.ImportDeclaration {
return node?.type === AST_NODE_TYPES.ImportDeclaration;
}

export function hasChainedThen(node: TSESTree.Node): boolean {
const parent = node.parent;

Expand Down
36 changes: 36 additions & 0 deletions lib/node-utils/is-node-of-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';

const isNodeOfType = <NodeType extends AST_NODE_TYPES>(nodeType: NodeType) => (
node: TSESTree.Node | null | undefined
): node is TSESTree.Node & { type: NodeType } => node?.type === nodeType;

export const isArrayExpression = isNodeOfType(AST_NODE_TYPES.ArrayExpression);
export const isArrowFunctionExpression = isNodeOfType(
AST_NODE_TYPES.ArrowFunctionExpression
);
export const isBlockStatement = isNodeOfType(AST_NODE_TYPES.BlockStatement);
export const isCallExpression = isNodeOfType(AST_NODE_TYPES.CallExpression);
export const isExpressionStatement = isNodeOfType(
AST_NODE_TYPES.ExpressionStatement
);
export const isImportDeclaration = isNodeOfType(
AST_NODE_TYPES.ImportDeclaration
);
export const isImportDefaultSpecifier = isNodeOfType(
AST_NODE_TYPES.ImportDefaultSpecifier
);
export const isImportNamespaceSpecifier = isNodeOfType(
AST_NODE_TYPES.ImportNamespaceSpecifier
);
export const isImportSpecifier = isNodeOfType(AST_NODE_TYPES.ImportSpecifier);
export const isJSXAttribute = isNodeOfType(AST_NODE_TYPES.JSXAttribute);
export const isLiteral = isNodeOfType(AST_NODE_TYPES.Literal);
export const isMemberExpression = isNodeOfType(AST_NODE_TYPES.MemberExpression);
export const isNewExpression = isNodeOfType(AST_NODE_TYPES.NewExpression);
export const isObjectExpression = isNodeOfType(AST_NODE_TYPES.ObjectExpression);
export const isObjectPattern = isNodeOfType(AST_NODE_TYPES.ObjectPattern);
export const isProperty = isNodeOfType(AST_NODE_TYPES.Property);
export const isReturnStatement = isNodeOfType(AST_NODE_TYPES.ReturnStatement);

0 comments on commit 445adc8

Please sign in to comment.