forked from typescript-eslint/typescript-eslint
/
predicates.ts
171 lines (149 loc) · 4.32 KB
/
predicates.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from '../ts-estree';
import {
isNodeOfType,
isNodeOfTypes,
isNodeOfTypeWithConditions,
isNotTokenOfTypeWithConditions,
isTokenOfTypeWithConditions,
} from './helpers';
const isOptionalChainPunctuator = isTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '?.' },
);
const isNotOptionalChainPunctuator = isNotTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '?.' },
);
const isNonNullAssertionPunctuator = isTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '!' },
);
const isNotNonNullAssertionPunctuator = isNotTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '!' },
);
/**
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
*/
const isOptionalCallExpression = isNodeOfTypeWithConditions(
AST_NODE_TYPES.CallExpression,
// this flag means the call expression itself is option
// i.e. it is foo.bar?.() and not foo?.bar()
{ optional: true },
);
/**
* Returns true if and only if the node represents logical OR
*/
const isLogicalOrOperator = isNodeOfTypeWithConditions(
AST_NODE_TYPES.LogicalExpression,
{ operator: '||' },
);
/**
* Checks if a node is a type assertion:
* ```
* x as foo
* <foo>x
* ```
*/
const isTypeAssertion = isNodeOfTypes([
AST_NODE_TYPES.TSAsExpression,
AST_NODE_TYPES.TSTypeAssertion,
] as const);
const isVariableDeclarator = isNodeOfType(AST_NODE_TYPES.VariableDeclarator);
const functionTypes = [
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
] as const;
const isFunction = isNodeOfTypes(functionTypes);
const functionTypeTypes = [
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSConstructorType,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
AST_NODE_TYPES.TSFunctionType,
AST_NODE_TYPES.TSMethodSignature,
] as const;
const isFunctionType = isNodeOfTypes(functionTypeTypes);
const isFunctionOrFunctionType = isNodeOfTypes([
...functionTypes,
...functionTypeTypes,
] as const);
const isTSFunctionType = isNodeOfType(AST_NODE_TYPES.TSFunctionType);
const isTSConstructorType = isNodeOfType(AST_NODE_TYPES.TSConstructorType);
const isClassOrTypeElement = isNodeOfTypes([
// ClassElement
AST_NODE_TYPES.PropertyDefinition,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.MethodDefinition,
AST_NODE_TYPES.TSAbstractPropertyDefinition,
AST_NODE_TYPES.TSAbstractMethodDefinition,
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
AST_NODE_TYPES.TSIndexSignature,
// TypeElement
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
// AST_NODE_TYPES.TSIndexSignature,
AST_NODE_TYPES.TSMethodSignature,
AST_NODE_TYPES.TSPropertySignature,
] as const);
/**
* Checks if a node is a constructor method.
*/
const isConstructor = isNodeOfTypeWithConditions(
AST_NODE_TYPES.MethodDefinition,
{ kind: 'constructor' },
);
/**
* Checks if a node is a setter method.
*/
function isSetter(
node: TSESTree.Node | undefined,
): node is (TSESTree.MethodDefinition | TSESTree.Property) & { kind: 'set' } {
return (
!!node &&
(node.type === AST_NODE_TYPES.MethodDefinition ||
node.type === AST_NODE_TYPES.Property) &&
node.kind === 'set'
);
}
const isIdentifier = isNodeOfType(AST_NODE_TYPES.Identifier);
/**
* Checks if a node represents an `await …` expression.
*/
const isAwaitExpression = isNodeOfType(AST_NODE_TYPES.AwaitExpression);
/**
* Checks if a possible token is the `await` keyword.
*/
const isAwaitKeyword = isTokenOfTypeWithConditions(AST_TOKEN_TYPES.Identifier, {
value: 'await',
});
const isLoop = isNodeOfTypes([
AST_NODE_TYPES.DoWhileStatement,
AST_NODE_TYPES.ForStatement,
AST_NODE_TYPES.ForInStatement,
AST_NODE_TYPES.ForOfStatement,
AST_NODE_TYPES.WhileStatement,
] as const);
export {
isAwaitExpression,
isAwaitKeyword,
isConstructor,
isClassOrTypeElement,
isFunction,
isFunctionOrFunctionType,
isFunctionType,
isIdentifier,
isLoop,
isLogicalOrOperator,
isNonNullAssertionPunctuator,
isNotNonNullAssertionPunctuator,
isNotOptionalChainPunctuator,
isOptionalChainPunctuator,
isOptionalCallExpression,
isSetter,
isTSConstructorType,
isTSFunctionType,
isTypeAssertion,
isVariableDeclarator,
};