/
astUtils.ts
150 lines (137 loc) · 3.69 KB
/
astUtils.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
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
const LINEBREAK_MATCHER = /\r\n|[\r\n\u2028\u2029]/;
function isOptionalChainPunctuator(
token: TSESTree.Token | TSESTree.Comment,
): token is TSESTree.PunctuatorToken & { value: '?.' } {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '?.';
}
function isNotOptionalChainPunctuator(
token: TSESTree.Token | TSESTree.Comment,
): boolean {
return !isOptionalChainPunctuator(token);
}
function isNonNullAssertionPunctuator(
token: TSESTree.Token | TSESTree.Comment,
): token is TSESTree.PunctuatorToken & { value: '!' } {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '!';
}
function isNotNonNullAssertionPunctuator(
token: TSESTree.Token | TSESTree.Comment,
): boolean {
return !isNonNullAssertionPunctuator(token);
}
/**
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
*/
function isOptionalOptionalChain(
node: TSESTree.Node,
): node is TSESTree.OptionalCallExpression & { optional: true } {
return (
node.type === AST_NODE_TYPES.OptionalCallExpression &&
// this flag means the call expression itself is option
// i.e. it is foo.bar?.() and not foo?.bar()
node.optional
);
}
/**
* Returns true if and only if the node represents logical OR
*/
function isLogicalOrOperator(
node: TSESTree.Node,
): node is TSESTree.LogicalExpression & { operator: '||' } {
return (
node.type === AST_NODE_TYPES.LogicalExpression && node.operator === '||'
);
}
/**
* Determines whether two adjacent tokens are on the same line
*/
function isTokenOnSameLine(
left: TSESTree.Token | TSESTree.Comment,
right: TSESTree.Token | TSESTree.Comment,
): boolean {
return left.loc.end.line === right.loc.start.line;
}
/**
* Checks if a node is a type assertion:
* ```
* x as foo
* <foo>x
* ```
*/
function isTypeAssertion(
node: TSESTree.Node | undefined | null,
): node is TSESTree.TSAsExpression | TSESTree.TSTypeAssertion {
if (!node) {
return false;
}
return (
node.type === AST_NODE_TYPES.TSAsExpression ||
node.type === AST_NODE_TYPES.TSTypeAssertion
);
}
/**
* Checks if a node is a constructor method.
*/
function isConstructor(
node: TSESTree.Node | undefined,
): node is TSESTree.MethodDefinition {
return (
node?.type === AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor'
);
}
/**
* Checks if a node is a setter method.
*/
function isSetter(
node: TSESTree.Node | undefined,
): node is TSESTree.MethodDefinition | TSESTree.Property {
return (
!!node &&
(node.type === AST_NODE_TYPES.MethodDefinition ||
node.type === AST_NODE_TYPES.Property) &&
node.kind === 'set'
);
}
function isIdentifier(
node: TSESTree.Node | undefined,
): node is TSESTree.Identifier {
return node?.type === 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;
}
/**
* Checks if a possible token is the `await` keyword.
*/
function isAwaitKeyword(
node: TSESTree.Token | TSESTree.Comment | undefined | null,
): node is TSESTree.KeywordToken & { value: 'await' } {
return node?.type === AST_TOKEN_TYPES.Identifier && node.value === 'await';
}
export {
isAwaitExpression,
isAwaitKeyword,
isConstructor,
isIdentifier,
isLogicalOrOperator,
isNonNullAssertionPunctuator,
isNotNonNullAssertionPunctuator,
isNotOptionalChainPunctuator,
isOptionalChainPunctuator,
isOptionalOptionalChain,
isSetter,
isTokenOnSameLine,
isTypeAssertion,
LINEBREAK_MATCHER,
};