From 4bfa4375aff8f65057d4aa116e435803cbc6b464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Fri, 3 Sep 2021 19:43:05 +0200 Subject: [PATCH] feat(experimental-utils): extract `isNodeOfType` out of `ast-utils`' `predicates` (#3677) --- .../src/ast-utils/predicates.ts | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/packages/experimental-utils/src/ast-utils/predicates.ts b/packages/experimental-utils/src/ast-utils/predicates.ts index 919820d7653..7a7bcf520b1 100644 --- a/packages/experimental-utils/src/ast-utils/predicates.ts +++ b/packages/experimental-utils/src/ast-utils/predicates.ts @@ -1,5 +1,12 @@ import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from '../ts-estree'; +const isNodeOfType = + (nodeType: NodeType) => + ( + node: TSESTree.Node | null | undefined, + ): node is TSESTree.Node & { type: NodeType } => + node?.type === nodeType; + function isOptionalChainPunctuator( token: TSESTree.Token, ): token is TSESTree.PunctuatorToken & { value: '?.' } { @@ -69,11 +76,7 @@ function isTypeAssertion( ); } -function isVariableDeclarator( - node: TSESTree.Node | undefined, -): node is TSESTree.VariableDeclarator { - return node?.type === AST_NODE_TYPES.VariableDeclarator; -} +const isVariableDeclarator = isNodeOfType(AST_NODE_TYPES.VariableDeclarator); function isFunction( node: TSESTree.Node | undefined, @@ -130,17 +133,9 @@ function isFunctionOrFunctionType( return isFunction(node) || isFunctionType(node); } -function isTSFunctionType( - node: TSESTree.Node | undefined, -): node is TSESTree.TSFunctionType { - return node?.type === AST_NODE_TYPES.TSFunctionType; -} +const isTSFunctionType = isNodeOfType(AST_NODE_TYPES.TSFunctionType); -function isTSConstructorType( - node: TSESTree.Node | undefined, -): node is TSESTree.TSConstructorType { - return node?.type === AST_NODE_TYPES.TSConstructorType; -} +const isTSConstructorType = isNodeOfType(AST_NODE_TYPES.TSConstructorType); function isClassOrTypeElement( node: TSESTree.Node | undefined, @@ -193,20 +188,12 @@ function isSetter( ); } -function isIdentifier( - node: TSESTree.Node | undefined, -): node is TSESTree.Identifier { - return node?.type === AST_NODE_TYPES.Identifier; -} +const isIdentifier = isNodeOfType(AST_NODE_TYPES.Identifier); /** * Checks if a node represents an `await …` expression. */ -function isAwaitExpression( - node: TSESTree.Node | undefined | null, -): node is TSESTree.AwaitExpression { - return node?.type === AST_NODE_TYPES.AwaitExpression; -} +const isAwaitExpression = isNodeOfType(AST_NODE_TYPES.AwaitExpression); /** * Checks if a possible token is the `await` keyword.