/
util.js
232 lines (192 loc) · 5.97 KB
/
util.js
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
'use strict';
const path = require('path');
const { version } = require('../../package.json');
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
const expectCase = node =>
node.callee.name === 'expect' &&
node.arguments.length === 1 &&
node.parent &&
node.parent.type === 'MemberExpression' &&
node.parent.parent;
const expectNotCase = node =>
expectCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node) === 'not';
const expectResolveCase = node =>
expectCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node) === 'resolve';
const expectRejectCase = node =>
expectCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node) === 'reject';
const expectToBeCase = (node, arg) =>
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
expectCase(node) &&
methodName(node) === 'toBe' &&
argument(node) &&
((argument(node).type === 'Literal' &&
argument(node).value === null &&
arg === null) ||
(argument(node).name === 'undefined' && arg === undefined));
const expectNotToBeCase = (node, arg) =>
expectNotCase(node) &&
methodName2(node) === 'toBe' &&
argument2(node) &&
((argument2(node).type === 'Literal' &&
argument2(node).value === null &&
arg === null) ||
(argument2(node).name === 'undefined' && arg === undefined));
const expectToEqualCase = (node, arg) =>
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
expectCase(node) &&
methodName(node) === 'toEqual' &&
argument(node) &&
((argument(node).type === 'Literal' &&
argument(node).value === null &&
arg === null) ||
(argument(node).name === 'undefined' && arg === undefined));
const expectNotToEqualCase = (node, arg) =>
expectNotCase(node) &&
methodName2(node) === 'toEqual' &&
argument2(node) &&
((argument2(node).type === 'Literal' &&
argument2(node).value === null &&
arg === null) ||
(argument2(node).name === 'undefined' && arg === undefined));
const method = node => node.parent.property;
const method2 = node => node.parent.parent.property;
const methodName = node => method(node).name;
const methodName2 = node => method2(node).name;
const argument = node =>
node.parent.parent.arguments && node.parent.parent.arguments[0];
const argument2 = node =>
node.parent.parent.parent.arguments && node.parent.parent.parent.arguments[0];
const describeAliases = Object.assign(Object.create(null), {
describe: true,
'describe.only': true,
'describe.skip': true,
fdescribe: true,
xdescribe: true,
});
const testCaseNames = Object.assign(Object.create(null), {
fit: true,
it: true,
'it.only': true,
'it.skip': true,
test: true,
'test.only': true,
'test.skip': true,
xit: true,
xtest: true,
});
const getNodeName = node => {
function joinNames(a, b) {
return a && b ? `${a}.${b}` : null;
}
switch (node && node.type) {
case 'Identifier':
return node.name;
case 'Literal':
return node.value;
case 'TemplateLiteral':
if (node.expressions.length === 0) return node.quasis[0].value.cooked;
break;
case 'MemberExpression':
return joinNames(getNodeName(node.object), getNodeName(node.property));
}
return null;
};
const isTestCase = node =>
node &&
node.type === 'CallExpression' &&
testCaseNames[getNodeName(node.callee)];
const isDescribe = node =>
node &&
node.type === 'CallExpression' &&
describeAliases[getNodeName(node.callee)];
const isFunction = node =>
(node && node.type === 'FunctionExpression') ||
node.type === 'ArrowFunctionExpression';
const isString = node =>
(node && (node.type === 'Literal' && typeof node.value === 'string')) ||
isTemplateLiteral(node);
const isTemplateLiteral = node => node && node.type === 'TemplateLiteral';
const hasExpressions = node =>
node && node.expressions && node.expressions.length > 0;
const getStringValue = arg =>
isTemplateLiteral(arg) ? arg.quasis[0].value.raw : arg.value;
/**
* Generates the URL to documentation for the given rule name. It uses the
* package version to build the link to a tagged version of the
* documentation file.
*
* @param {string} filename - Name of the eslint rule
* @returns {string} URL to the documentation for the given rule
*/
const getDocsUrl = filename => {
const ruleName = path.basename(filename, '.js');
return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`;
};
const collectReferences = scope => {
const locals = new Set();
const unresolved = new Set();
let currentScope = 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 };
};
const scopeHasLocalReference = (scope, referenceName) => {
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)
);
};
function composeFixers(node) {
return (...fixers) => {
return fixerApi => {
return fixers.reduce((all, fixer) => [...all, fixer(node, fixerApi)], []);
};
};
}
module.exports = {
method,
method2,
argument,
argument2,
expectCase,
expectNotCase,
expectResolveCase,
expectRejectCase,
expectToBeCase,
expectNotToBeCase,
expectToEqualCase,
expectNotToEqualCase,
getNodeName,
getStringValue,
isDescribe,
isFunction,
isTemplateLiteral,
isTestCase,
isString,
hasExpressions,
getDocsUrl,
scopeHasLocalReference,
composeFixers,
};