From 445adc8dc72402d689d437ff5c2412f28887eff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Wed, 14 Apr 2021 21:07:16 +0200 Subject: [PATCH] refactor(node-utils): extract `isNodeOfType` functions (#329) * refactor(node-utils): extract isNodeOfType functions * refactor(node-utils): make isNodeOfType more type-safe --- lib/{node-utils.ts => node-utils/index.ts} | 116 +++------------------ lib/node-utils/is-node-of-type.ts | 36 +++++++ 2 files changed, 50 insertions(+), 102 deletions(-) rename lib/{node-utils.ts => node-utils/index.ts} (81%) create mode 100644 lib/node-utils/is-node-of-type.ts diff --git a/lib/node-utils.ts b/lib/node-utils/index.ts similarity index 81% rename from lib/node-utils.ts rename to lib/node-utils/index.ts index 453f7c98..caa94108 100644 --- a/lib/node-utils.ts +++ b/lib/node-utils/index.ts @@ -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, @@ -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 @@ -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) && @@ -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; diff --git a/lib/node-utils/is-node-of-type.ts b/lib/node-utils/is-node-of-type.ts new file mode 100644 index 00000000..89d8880b --- /dev/null +++ b/lib/node-utils/is-node-of-type.ts @@ -0,0 +1,36 @@ +import { + AST_NODE_TYPES, + TSESTree, +} from '@typescript-eslint/experimental-utils'; + +const isNodeOfType = (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);