-
Notifications
You must be signed in to change notification settings - Fork 237
/
prefer-importing-jest-globals.ts
184 lines (166 loc) · 5.4 KB
/
prefer-importing-jest-globals.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
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type JestFnType,
createRule,
getAccessorValue,
getSourceCode,
isIdentifier,
isStringNode,
isSupportedAccessor,
parseJestFnCall,
} from './utils';
const createFixerImports = (
isModule: boolean,
functionsToImport: Set<string>,
) => {
const allImportsFormatted = Array.from(functionsToImport).sort().join(', ');
return isModule
? `import { ${allImportsFormatted} } from '@jest/globals';`
: `const { ${allImportsFormatted} } = require('@jest/globals');`;
};
const allJestFnTypes: JestFnType[] = [
'hook',
'describe',
'test',
'expect',
'jest',
'unknown',
];
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Prefer importing Jest globals',
},
messages: {
preferImportingJestGlobal: `Import the following Jest functions from '@jest/globals': {{ jestFunctions }}`,
},
fixable: 'code',
type: 'problem',
schema: [
{
type: 'object',
properties: {
types: {
type: 'array',
items: {
type: 'string',
enum: allJestFnTypes,
},
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{ types: allJestFnTypes }],
create(context) {
const { types = allJestFnTypes } = context.options[0] || {};
const functionsToImport = new Set<string>();
let reportingNode: TSESTree.Node;
return {
CallExpression(node: TSESTree.CallExpression) {
const jestFnCall = parseJestFnCall(node, context);
if (!jestFnCall) {
return;
}
if (
jestFnCall.head.type !== 'import' &&
types.includes(jestFnCall.type)
) {
functionsToImport.add(jestFnCall.name);
reportingNode ||= jestFnCall.head.node;
}
},
'Program:exit'() {
// this means we found at least one function to import
if (!reportingNode) {
return;
}
const isModule =
context.parserOptions.sourceType === 'module' ||
context.languageOptions?.sourceType === 'module';
context.report({
node: reportingNode,
messageId: 'preferImportingJestGlobal',
data: { jestFunctions: Array.from(functionsToImport).join(', ') },
fix(fixer) {
const sourceCode = getSourceCode(context);
const [firstNode] = sourceCode.ast.body;
// check if "use strict" directive exists
if (
firstNode.type === AST_NODE_TYPES.ExpressionStatement &&
isStringNode(firstNode.expression, 'use strict')
) {
return fixer.insertTextAfter(
firstNode,
`\n${createFixerImports(isModule, functionsToImport)}`,
);
}
const importNode = sourceCode.ast.body.find(
node =>
node.type === AST_NODE_TYPES.ImportDeclaration &&
node.source.value === '@jest/globals',
);
if (importNode?.type === AST_NODE_TYPES.ImportDeclaration) {
for (const specifier of importNode.specifiers) {
if (
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
specifier.imported?.name
) {
functionsToImport.add(specifier.imported.name);
}
if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier) {
functionsToImport.add(specifier.local.name);
}
}
return fixer.replaceText(
importNode,
createFixerImports(isModule, functionsToImport),
);
}
const requireNode = sourceCode.ast.body.find(
node =>
node.type === AST_NODE_TYPES.VariableDeclaration &&
node.declarations.some(
declaration =>
declaration.init?.type === AST_NODE_TYPES.CallExpression &&
isIdentifier(declaration.init.callee, 'require') &&
isStringNode(
declaration.init.arguments[0],
'@jest/globals',
) &&
(declaration.id.type === AST_NODE_TYPES.Identifier ||
declaration.id.type === AST_NODE_TYPES.ObjectPattern),
),
);
if (requireNode?.type !== AST_NODE_TYPES.VariableDeclaration) {
return fixer.insertTextBefore(
firstNode,
`${createFixerImports(isModule, functionsToImport)}\n`,
);
}
if (
requireNode.declarations[0]?.id.type ===
AST_NODE_TYPES.ObjectPattern
) {
for (const property of requireNode.declarations[0].id
.properties) {
if (
property.type === AST_NODE_TYPES.Property &&
isSupportedAccessor(property.key)
) {
functionsToImport.add(getAccessorValue(property.key));
}
}
}
return fixer.replaceText(
requireNode,
`${createFixerImports(isModule, functionsToImport)}`,
);
},
});
},
};
},
});