forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.ts
37 lines (33 loc) · 1.26 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { AST_NODE_TYPES, TSESTree } from '../ts-estree';
export 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 isNodeOfTypes =
<NodeTypes extends readonly AST_NODE_TYPES[]>(nodeTypes: NodeTypes) =>
(
node: TSESTree.Node | null | undefined,
): node is TSESTree.Node & { type: NodeTypes[number] } =>
!!node && nodeTypes.includes(node.type);
type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;
export const isNodeOfTypeWithConditions = <
NodeType extends AST_NODE_TYPES,
Conditions extends Partial<TSESTree.Node & { type: NodeType }>,
>(
nodeType: NodeType,
conditions: Conditions,
): ((
node: TSESTree.Node | null | undefined,
) => node is TSESTree.Node & { type: NodeType } & Conditions) => {
const entries = Object.entries(conditions) as ObjectEntries<
TSESTree.Node & { type: NodeType }
>;
return (
node: TSESTree.Node | null | undefined,
): node is TSESTree.Node & { type: NodeType } & Conditions =>
node?.type === nodeType &&
entries.every(([key, value]) => node[key] === value);
};