8000 feat(experimental-utils): extract `isNodeOfType` out of `ast-utils`' … · mvximvs/typescript-eslint@4bfa437 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bfa437

Browse files
feat(experimental-utils): extract isNodeOfType out of ast-utils' predicates (typescript-eslint#3677)
1 parent ea40ab6 commit 4bfa437

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

packages/experimental-utils/src/ast-utils/predicates.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from '../ts-estree';
22

3+
const isNodeOfType =
4+
<NodeType extends AST_NODE_TYPES>(nodeType: NodeType) =>
5+
(
6+
node: TSESTree.Node | null | undefined,
7+
): node is TSESTree.Node & { type: NodeType } =>
8+
node?.type === nodeType;
9+
310
function isOptionalChainPunctuator(
411
token: TSESTree.Token,
512
): token is TSESTree.PunctuatorToken & { value: '?.' } {
@@ -69,11 +76,7 @@ function isTypeAssertion(
6976
);
7077
}
7178

72-
function isVariableDeclarator(
73-
node: TSESTree.Node | undefined,
74-
): node is TSESTree.VariableDeclarator {
75-
return node?.type === AST_NODE_TYPES.VariableDeclarator;
76-
}
79+
const isVariableDeclarator = isNodeOfType(AST_NODE_TYPES.VariableDeclarator);
7780

7881
function isFunction(
7982
node: TSESTree.Node | undefined,
@@ -130,17 +133,9 @@ function isFunctionOrFunctionType(
130133
return isFunction(node) || isFunctionType(node);
131134
}
132135

133-
function isTSFunctionType(
134-
node: TSESTree.Node | undefined,
135-
): node is TSESTree.TSFunctionType {
136-
return node?.type === AST_NODE_TYPES.TSFunctionType;
137-
}
136+
const isTSFunctionType = isNodeOfType(AST_NODE_TYPES.TSFunctionType);
138137

139-
function isTSConstructorType(
140-
node: TSESTree.Node | undefined,
141-
): node is TSESTree.TSConstructorType {
142-
return node?.type === AST_NODE_TYPES.TSConstructorType;
143-
}
138+
const isTSConstructorType = isNodeOfType(AST_NODE_TYPES.TSConstructorType);
144139

145140
function isClassOrTypeElement(
146141
node: TSESTree.Node | undefined,
@@ -193,20 +188,12 @@ function isSetter(
193188
);
194189
}
195190

196-
function isIdentifier(
197-
node: TSESTree.Node | undefined,
198-
): node is TSESTree.Identifier {
199-
return node?.type === AST_NODE_TYPES.Identifier;
200-
}
191+
const isIdentifier = isNodeOfType(AST_NODE_TYPES.Identifier);
201192

202193
/**
203194
* Checks if a node represents an `await …` expression.
204195
*/
205-
function isAwaitExpression(
206-
node: TSESTree.Node | undefined | null,
207-
): node is TSESTree.AwaitExpression {
208-
return node?.type === AST_NODE_TYPES.AwaitExpression;
209-
}
196+
const isAwaitExpression = isNodeOfType(AST_NODE_TYPES.AwaitExpression);
210197

211198
/**
212199
* Checks if a possible token is the `await` keyword.

0 commit comments

Comments
 (0)
0