@@ -7,6 +7,28 @@ const isNodeOfType =
7
7
) : node is TSESTree . Node & { type : NodeType } =>
8
8
node ?. type === nodeType ;
9
9
10
+ type ObjectEntry < BaseType > = [ keyof BaseType , BaseType [ keyof BaseType ] ] ;
11
+ type ObjectEntries < BaseType > = Array < ObjectEntry < BaseType > > ;
12
+ const isNodeOfTypeWithConditions = <
13
+ NodeType extends AST_NODE_TYPES ,
14
+ Conditions extends Partial < TSESTree . Node & { type : NodeType } > ,
15
+ > (
16
+ nodeType : NodeType ,
17
+ conditions : Conditions ,
18
+ ) : ( (
19
+ node : TSESTree . Node | null | undefined ,
20
+ ) => node is TSESTree . Node & { type : NodeType } & Conditions ) => {
21
+ const entries = Object . entries ( conditions ) as ObjectEntries <
22
+ TSESTree . Node & { type : NodeType }
23
+ > ;
24
+
25
+ return (
26
+ node : TSESTree . Node | null | undefined ,
27
+ ) : node is TSESTree . Node & { type : NodeType } & Conditions =>
28
+ node ?. type === nodeType &&
29
+ entries . every ( ( [ key , value ] ) => node [ key ] === value ) ;
30
+ } ;
31
+
10
32
function isOptionalChainPunctuator (
11
33
token : TSESTree . Token ,
12
34
) : token is TSESTree . PunctuatorToken & { value : '?.' } {
@@ -35,27 +57,20 @@ function isNotNonNullAssertionPunctuator(
35
57
/**
36
58
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
37
59
*/
38
- function isOptionalCallExpression (
39
- node : TSESTree . Node ,
40
- ) : node is TSESTree . CallExpression & { optional : true } {
41
- return (
42
- node . type === AST_NODE_TYPES . CallExpression &&
43
- // this flag means the call expression itself is option
44
- // i.e. it is foo.bar?.() and not foo?.bar()
45
- node . optional
46
- ) ;
47
- }
60
+ const isOptionalCallExpression = isNodeOfTypeWithConditions (
61
+ AST_NODE_TYPES . CallExpression ,
62
+ // this flag means the call expression itself is option
63
+ // i.e. it is foo.bar?.() and not foo?.bar()
64
+ { optional : true } ,
65
+ ) ;
48
66
49
67
/**
50
68
* Returns true if and only if the node represents logical OR
51
69
*/
52
- function isLogicalOrOperator (
53
- node : TSESTree . Node ,
54
- ) : node is TSESTree . LogicalExpression & { operator : '||' } {
55
- return (
56
- node . type === AST_NODE_TYPES . LogicalExpression && node . operator === '||'
57
- ) ;
58
- }
70
+ const isLogicalOrOperator = isNodeOfTypeWithConditions (
71
+ AST_NODE_TYPES . LogicalExpression ,
72
+ { operator : '||' } ,
73
+ ) ;
59
74
60
75
/**
61
76
* Checks if a node is a type assertion:
@@ -165,14 +180,10 @@ function isClassOrTypeElement(
165
180
/**
166
181
* Checks if a node is a constructor method.
167
182
*/
168
- function isConstructor (
169
- node : TSESTree . Node | undefined ,
170
- ) : node is TSESTree . MethodDefinition {
171
- return (
172
- node ?. type === AST_NODE_TYPES . MethodDefinition &&
173
- node . kind === 'constructor'
174
- ) ;
175
- }
183
+ const isConstructor = isNodeOfTypeWithConditions (
184
+ AST_NODE_TYPES . MethodDefinition ,
185
+ { kind : 'constructor' } ,
186
+ ) ;
176
187
177
188
/**
178
189
* Checks if a node is a setter method.
0 commit comments