-
Notifications
You must be signed in to change notification settings - Fork 227
/
misc.ts
211 lines (179 loc) · 5.49 KB
/
misc.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
import { parse as parsePath } from 'path';
import {
AST_NODE_TYPES,
ESLintUtils,
TSESLint,
TSESTree,
} from '@typescript-eslint/utils';
import { version } from '../../../package.json';
import {
AccessorNode,
getAccessorValue,
isSupportedAccessor,
} from './accessors';
import { followTypeAssertionChain } from './followTypeAssertionChain';
import { ParsedExpectFnCall, isTypeOfJestFnCall } from './parseJestFnCall';
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
export const createRule = ESLintUtils.RuleCreator(name => {
const ruleName = parsePath(name).name;
return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`;
});
/**
* Represents a `MemberExpression` with a "known" `property`.
*/
export interface KnownMemberExpression<Name extends string = string>
extends TSESTree.MemberExpressionComputedName {
property: AccessorNode<Name>;
}
/**
* Represents a `CallExpression` with a "known" `property` accessor.
*
* i.e `KnownCallExpression<'includes'>` represents `.includes()`.
*/
export interface KnownCallExpression<Name extends string = string>
extends TSESTree.CallExpression {
callee: CalledKnownMemberExpression<Name>;
}
/**
* Represents a `MemberExpression` with a "known" `property`, that is called.
*
* This is `KnownCallExpression` from the perspective of the `MemberExpression` node.
*/
interface CalledKnownMemberExpression<Name extends string = string>
extends KnownMemberExpression<Name> {
parent: KnownCallExpression<Name>;
}
/**
* Represents a `CallExpression` with a single argument.
*/
export interface CallExpressionWithSingleArgument<
Argument extends TSESTree.CallExpression['arguments'][number] = TSESTree.CallExpression['arguments'][number],
> extends TSESTree.CallExpression {
arguments: [Argument];
}
/**
* Guards that the given `call` has only one `argument`.
*
* @param {CallExpression} call
*
* @return {call is CallExpressionWithSingleArgument}
*/
export const hasOnlyOneArgument = (
call: TSESTree.CallExpression,
): call is CallExpressionWithSingleArgument => call.arguments.length === 1;
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 ModifierName {
not = 'not',
rejects = 'rejects',
resolves = 'resolves',
}
export enum EqualityMatcher {
toBe = 'toBe',
toEqual = 'toEqual',
toStrictEqual = 'toStrictEqual',
}
const joinNames = (a: string | null, b: string | null): string | null =>
a && b ? `${a}.${b}` : null;
export function getNodeName(node: TSESTree.Node): string | null {
if (isSupportedAccessor(node)) {
return getAccessorValue(node);
}
switch (node.type) {
case AST_NODE_TYPES.TaggedTemplateExpression:
return getNodeName(node.tag);
case AST_NODE_TYPES.MemberExpression:
return joinNames(getNodeName(node.object), getNodeName(node.property));
case AST_NODE_TYPES.NewExpression:
case AST_NODE_TYPES.CallExpression:
return getNodeName(node.callee);
}
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 getTestCallExpressionsFromDeclaredVariables = (
declaredVariables: readonly TSESLint.Scope.Variable[],
context: TSESLint.RuleContext<string, unknown[]>,
): TSESTree.CallExpression[] => {
return declaredVariables.reduce<TSESTree.CallExpression[]>(
(acc, { references }) =>
acc.concat(
references
.map(({ identifier }) => identifier.parent)
.filter(
(node): node is TSESTree.CallExpression =>
node?.type === AST_NODE_TYPES.CallExpression &&
isTypeOfJestFnCall(node, context, ['test']),
),
),
[],
);
};
/**
* Replaces an accessor node with the given `text`, surrounding it in quotes if required.
*
* This ensures that fixes produce valid code when replacing both dot-based and
* bracket-based property accessors.
*/
export const replaceAccessorFixer = (
fixer: TSESLint.RuleFixer,
node: AccessorNode,
text: string,
) => {
return fixer.replaceText(
node,
node.type === AST_NODE_TYPES.Identifier ? text : `'${text}'`,
);
};
export const findTopMostCallExpression = (
node: TSESTree.CallExpression,
): TSESTree.CallExpression => {
let topMostCallExpression = node;
let { parent } = node;
while (parent) {
if (parent.type === AST_NODE_TYPES.CallExpression) {
topMostCallExpression = parent;
parent = parent.parent;
continue;
}
if (parent.type !== AST_NODE_TYPES.MemberExpression) {
break;
}
parent = parent.parent;
}
return topMostCallExpression;
};
export const isBooleanLiteral = (
node: TSESTree.Node,
): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
export const getFirstMatcherArg = (
expectFnCall: ParsedExpectFnCall,
): TSESTree.SpreadElement | TSESTree.Expression => {
const [firstArg] = expectFnCall.args;
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
return firstArg;
}
return followTypeAssertionChain(firstArg);
};