/
tsUtils.ts
278 lines (234 loc) · 8.21 KB
/
tsUtils.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// TODO: rename to utils.ts when TS migration is complete
import { basename } from 'path';
import {
AST_NODE_TYPES,
ESLintUtils,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { version } from '../../package.json';
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
export const createRule = ESLintUtils.RuleCreator(name => {
const ruleName = basename(name, '.ts');
return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`;
});
interface JestExpectIdentifier extends TSESTree.Identifier {
name: 'expect';
}
/**
* Checks if the given `node` is considered a {@link JestExpectIdentifier}.
*
* A `node` is considered to be as such if it is of type `Identifier`,
* and `name`d `"expect"`.
*
* @param {Node} node
*
* @return {node is JestExpectIdentifier}
*/
const isExpectIdentifier = (
node: TSESTree.Node,
): node is JestExpectIdentifier =>
node.type === AST_NODE_TYPES.Identifier && node.name === 'expect';
// represents "expect()" specifically
interface JestExpectCallExpression extends TSESTree.CallExpression {
callee: JestExpectIdentifier;
}
// represents expect usage like "expect().toBe" & "expect().not.toBe"
interface JestExpectCallMemberExpression extends TSESTree.MemberExpression {
object: JestExpectCallMemberExpression | JestExpectCallExpression;
property: TSESTree.Identifier;
}
// represents expect usage like "expect.anything" & "expect.hasAssertions"
interface JestExpectNamespaceMemberExpression
extends TSESTree.MemberExpression {
object: JestExpectIdentifier;
property: TSESTree.Identifier;
}
/**
* Checks if the given `node` is a {@link JestExpectCallExpression}.
*
* @param {Node} node
*
* @return {node is JestExpectCallExpression}
*/
const isExpectCall = (node: TSESTree.Node): node is JestExpectCallExpression =>
node.type === AST_NODE_TYPES.CallExpression &&
isExpectIdentifier(node.callee);
interface JestExpectCallWithParent extends JestExpectCallExpression {
parent: JestExpectCallMemberExpression;
}
export const isExpectCallWithParent = (
node: TSESTree.Node,
): node is JestExpectCallWithParent =>
isExpectCall(node) &&
node.parent !== undefined &&
node.parent.type === AST_NODE_TYPES.MemberExpression &&
node.parent.property.type === AST_NODE_TYPES.Identifier;
export enum DescribeAlias {
'describe' = 'describe',
'fdescribe' = 'fdescribe',
'xdescribe' = 'xdescribe',
}
export enum TestCaseName {
'fit' = 'fit',
'it' = 'it',
'test' = 'test',
'xit' = 'xit',
'xtest' = 'xtest',
}
export enum HookName {
'beforeAll' = 'beforeAll',
'beforeEach' = 'beforeEach',
'afterAll' = 'afterAll',
'afterEach' = 'afterEach',
}
export enum DescribeProperty {
'each' = 'each',
'only' = 'only',
'skip' = 'skip',
}
export enum TestCaseProperty {
'each' = 'each',
'only' = 'only',
'skip' = 'skip',
'todo' = 'todo',
}
export type JestFunctionName = DescribeAlias | TestCaseName | HookName;
export interface JestFunctionIdentifier<FunctionName extends JestFunctionName>
extends TSESTree.Identifier {
name: FunctionName;
}
export interface JestFunctionMemberExpression<
FunctionName extends JestFunctionName
> extends TSESTree.MemberExpression {
object: JestFunctionIdentifier<FunctionName>;
}
export interface JestFunctionCallExpressionWithMemberExpressionCallee<
FunctionName extends JestFunctionName
> extends TSESTree.CallExpression {
callee: JestFunctionMemberExpression<FunctionName>;
}
export interface JestFunctionCallExpressionWithIdentifierCallee<
FunctionName extends JestFunctionName
> extends TSESTree.CallExpression {
callee: JestFunctionIdentifier<FunctionName>;
}
export type JestFunctionCallExpression<
FunctionName extends JestFunctionName = JestFunctionName
> =
| JestFunctionCallExpressionWithMemberExpressionCallee<FunctionName>
| JestFunctionCallExpressionWithIdentifierCallee<FunctionName>;
export function getNodeName(
node:
| JestFunctionMemberExpression<JestFunctionName>
| JestFunctionIdentifier<JestFunctionName>,
): string;
export function getNodeName(node: TSESTree.Node): string | null;
export function getNodeName(node: TSESTree.Node): string | null {
function joinNames(a?: string | null, b?: string | null): string | null {
return a && b ? `${a}.${b}` : null;
}
switch (node.type) {
case AST_NODE_TYPES.Identifier:
return node.name;
case AST_NODE_TYPES.Literal:
return `${node.value}`;
case AST_NODE_TYPES.TemplateLiteral:
if (node.expressions.length === 0) return node.quasis[0].value.cooked;
break;
case AST_NODE_TYPES.MemberExpression:
return joinNames(getNodeName(node.object), getNodeName(node.property));
}
return null;
}
export type FunctionExpression =
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression;
export const isFunction = (node: TSESTree.Node): node is FunctionExpression =>
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression;
export const isHook = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpressionWithIdentifierCallee<HookName> => {
return (
node.callee.type === AST_NODE_TYPES.Identifier &&
HookName.hasOwnProperty(node.callee.name)
);
};
export const isTestCase = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<TestCaseName> => {
return (
(node.callee.type === AST_NODE_TYPES.Identifier &&
TestCaseName.hasOwnProperty(node.callee.name)) ||
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.Identifier &&
TestCaseName.hasOwnProperty(node.callee.object.name) &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
TestCaseProperty.hasOwnProperty(node.callee.property.name))
);
};
export const isDescribe = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<DescribeAlias> => {
return (
(node.callee.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.name)) ||
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.object.name) &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
DescribeProperty.hasOwnProperty(node.callee.property.name))
);
};
export const isLiteralNode = (node: {
type: AST_NODE_TYPES;
}): node is TSESTree.Literal => node.type === AST_NODE_TYPES.Literal;
export interface StringLiteral extends TSESTree.Literal {
value: string;
}
export type StringNode = StringLiteral | TSESTree.TemplateLiteral;
export const isStringLiteral = (node: TSESTree.Node): node is StringLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string';
export const isTemplateLiteral = (
node: TSESTree.Node,
): node is TSESTree.TemplateLiteral =>
node && node.type === AST_NODE_TYPES.TemplateLiteral;
export const isStringNode = (node: TSESTree.Node): node is StringNode =>
isStringLiteral(node) || isTemplateLiteral(node);
/* istanbul ignore next we'll need this later */
export const getStringValue = (arg: StringNode): string =>
isTemplateLiteral(arg) ? arg.quasis[0].value.raw : arg.value;
const collectReferences = (scope: TSESLint.Scope.Scope) => {
const locals = new Set();
const unresolved = new Set();
let currentScope: TSESLint.Scope.Scope | null = scope;
while (currentScope !== null) {
for (const ref of currentScope.variables) {
const isReferenceDefined = ref.defs.some(def => {
return def.type !== 'ImplicitGlobalVariable';
});
if (isReferenceDefined) {
locals.add(ref.name);
}
}
for (const ref of currentScope.through) {
unresolved.add(ref.identifier.name);
}
currentScope = currentScope.upper;
}
return { locals, unresolved };
};
export const scopeHasLocalReference = (
scope: TSESLint.Scope.Scope,
referenceName: string,
) => {
const references = collectReferences(scope);
return (
// referenceName was found as a local variable or function declaration.
references.locals.has(referenceName) ||
// referenceName was not found as an unresolved reference,
// meaning it is likely not an implicit global reference.
!references.unresolved.has(referenceName)
);
};