/
misc.ts
156 lines (137 loc) · 4.2 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
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 { 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.Expression = TSESTree.Expression,
> 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',
}
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[],
scope: TSESLint.Scope.Scope,
): 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, scope, ['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}'`,
);
};