utils.js
16.6 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAccessorValue = exports.followTypeAssertionChain = exports.createRule = exports.TestCaseProperty = exports.TestCaseName = exports.ModifierName = exports.HookName = exports.EqualityMatcher = exports.DescribeProperty = exports.DescribeAlias = void 0;
exports.getNodeName = getNodeName;
exports.scopeHasLocalReference = exports.parseExpectCall = exports.isTestCaseCall = exports.isSupportedAccessor = exports.isStringNode = exports.isParsedEqualityMatcherCall = exports.isIdentifier = exports.isHook = exports.isFunction = exports.isExpectMember = exports.isExpectCall = exports.isDescribeCall = exports.hasOnlyOneArgument = exports.getTestCallExpressionsFromDeclaredVariables = exports.getStringValue = void 0;
var _path = require("path");
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
var _package = require("../../package.json");
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
const createRule = _experimentalUtils.ESLintUtils.RuleCreator(name => {
const ruleName = (0, _path.parse)(name).name;
return `${REPO_URL}/blob/v${_package.version}/docs/rules/${ruleName}.md`;
});
exports.createRule = createRule;
const isTypeCastExpression = node => node.type === _experimentalUtils.AST_NODE_TYPES.TSAsExpression || node.type === _experimentalUtils.AST_NODE_TYPES.TSTypeAssertion;
const followTypeAssertionChain = expression => isTypeCastExpression(expression) ? followTypeAssertionChain(expression.expression) : expression;
/**
* A `Literal` with a `value` of type `string`.
*/
exports.followTypeAssertionChain = followTypeAssertionChain;
/**
* Checks if the given `node` is a `StringLiteral`.
*
* If a `value` is provided & the `node` is a `StringLiteral`,
* the `value` will be compared to that of the `StringLiteral`.
*
* @param {Node} node
* @param {V} [value]
*
* @return {node is StringLiteral<V>}
*
* @template V
*/
const isStringLiteral = (node, value) => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof node.value === 'string' && (value === undefined || node.value === value);
/**
* Checks if the given `node` is a `TemplateLiteral`.
*
* Complex `TemplateLiteral`s are not considered specific, and so will return `false`.
*
* If a `value` is provided & the `node` is a `TemplateLiteral`,
* the `value` will be compared to that of the `TemplateLiteral`.
*
* @param {Node} node
* @param {V} [value]
*
* @return {node is TemplateLiteral<V>}
*
* @template V
*/
const isTemplateLiteral = (node, value) => node.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral && node.quasis.length === 1 && ( // bail out if not simple
value === undefined || node.quasis[0].value.raw === value);
/**
* Checks if the given `node` is a {@link StringNode}.
*
* @param {Node} node
* @param {V} [specifics]
*
* @return {node is StringNode}
*
* @template V
*/
const isStringNode = (node, specifics) => isStringLiteral(node, specifics) || isTemplateLiteral(node, specifics);
/**
* Gets the value of the given `StringNode`.
*
* If the `node` is a `TemplateLiteral`, the `raw` value is used;
* otherwise, `value` is returned instead.
*
* @param {StringNode<S>} node
*
* @return {S}
*
* @template S
*/
exports.isStringNode = isStringNode;
const getStringValue = node => isTemplateLiteral(node) ? node.quasis[0].value.raw : node.value;
/**
* Represents a `MemberExpression` with a "known" `property`.
*/
exports.getStringValue = getStringValue;
/**
* Guards that the given `call` has only one `argument`.
*
* @param {CallExpression} call
*
* @return {call is CallExpressionWithSingleArgument}
*/
const hasOnlyOneArgument = call => call.arguments.length === 1;
/**
* An `Identifier` with a known `name` value - i.e `expect`.
*/
exports.hasOnlyOneArgument = hasOnlyOneArgument;
/**
* Checks if the given `node` is an `Identifier`.
*
* If a `name` is provided, & the `node` is an `Identifier`,
* the `name` will be compared to that of the `identifier`.
*
* @param {Node} node
* @param {V} [name]
*
* @return {node is KnownIdentifier<Name>}
*
* @template V
*/
const isIdentifier = (node, name) => node.type === _experimentalUtils.AST_NODE_TYPES.Identifier && (name === undefined || node.name === name);
/**
* Checks if the given `node` is a "supported accessor".
*
* This means that it's a node can be used to access properties,
* and who's "value" can be statically determined.
*
* `MemberExpression` nodes most commonly contain accessors,
* but it's possible for other nodes to contain them.
*
* If a `value` is provided & the `node` is an `AccessorNode`,
* the `value` will be compared to that of the `AccessorNode`.
*
* Note that `value` here refers to the normalised value.
* The property that holds the value is not always called `name`.
*
* @param {Node} node
* @param {V} [value]
*
* @return {node is AccessorNode<V>}
*
* @template V
*/
exports.isIdentifier = isIdentifier;
const isSupportedAccessor = (node, value) => isIdentifier(node, value) || isStringNode(node, value);
/**
* Gets the value of the given `AccessorNode`,
* account for the different node types.
*
* @param {AccessorNode<S>} accessor
*
* @return {S}
*
* @template S
*/
exports.isSupportedAccessor = isSupportedAccessor;
const getAccessorValue = accessor => accessor.type === _experimentalUtils.AST_NODE_TYPES.Identifier ? accessor.name : getStringValue(accessor);
exports.getAccessorValue = getAccessorValue;
/**
* Checks if the given `node` is a valid `ExpectCall`.
*
* In order to be an `ExpectCall`, the `node` must:
* * be a `CallExpression`,
* * have an accessor named 'expect',
* * have a `parent`.
*
* @param {Node} node
*
* @return {node is ExpectCall}
*/
const isExpectCall = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && isSupportedAccessor(node.callee, 'expect') && node.parent !== undefined;
exports.isExpectCall = isExpectCall;
const isExpectMember = (node, name) => node.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && isSupportedAccessor(node.property, name);
/**
* Represents all the jest matchers.
*/
exports.isExpectMember = isExpectMember;
let ModifierName;
exports.ModifierName = ModifierName;
(function (ModifierName) {
ModifierName["not"] = "not";
ModifierName["rejects"] = "rejects";
ModifierName["resolves"] = "resolves";
})(ModifierName || (exports.ModifierName = ModifierName = {}));
let EqualityMatcher;
exports.EqualityMatcher = EqualityMatcher;
(function (EqualityMatcher) {
EqualityMatcher["toBe"] = "toBe";
EqualityMatcher["toEqual"] = "toEqual";
EqualityMatcher["toStrictEqual"] = "toStrictEqual";
})(EqualityMatcher || (exports.EqualityMatcher = EqualityMatcher = {}));
const isParsedEqualityMatcherCall = (matcher, name) => (name ? matcher.name === name : EqualityMatcher.hasOwnProperty(matcher.name)) && matcher.arguments !== null && matcher.arguments.length === 1;
/**
* Represents a parsed expect matcher, such as `toBe`, `toContain`, and so on.
*/
exports.isParsedEqualityMatcherCall = isParsedEqualityMatcherCall;
const parseExpectMember = expectMember => ({
name: getAccessorValue(expectMember.property),
node: expectMember
});
const reparseAsMatcher = parsedMember => ({ ...parsedMember,
/**
* The arguments being passed to this `Matcher`, if any.
*
* If this matcher isn't called, this will be `null`.
*/
arguments: parsedMember.node.parent.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? parsedMember.node.parent.arguments : null
});
/**
* Re-parses the given `parsedMember` as a `ParsedExpectModifier`.
*
* If the given `parsedMember` does not have a `name` of a valid `Modifier`,
* an exception will be thrown.
*
* @param {ParsedExpectMember<ModifierName>} parsedMember
*
* @return {ParsedExpectModifier}
*/
const reparseMemberAsModifier = parsedMember => {
if (isSpecificMember(parsedMember, ModifierName.not)) {
return parsedMember;
}
/* istanbul ignore if */
if (!isSpecificMember(parsedMember, ModifierName.resolves) && !isSpecificMember(parsedMember, ModifierName.rejects)) {
// ts doesn't think that the ModifierName.not check is the direct inverse as the above two checks
// todo: impossible at runtime, but can't be typed w/o negation support
throw new Error(`modifier name must be either "${ModifierName.resolves}" or "${ModifierName.rejects}" (got "${parsedMember.name}")`);
}
const negation = isExpectMember(parsedMember.node.parent, ModifierName.not) ? parsedMember.node.parent : undefined;
return { ...parsedMember,
negation
};
};
const isSpecificMember = (member, specific) => member.name === specific;
/**
* Checks if the given `ParsedExpectMember` should be re-parsed as an `ParsedExpectModifier`.
*
* @param {ParsedExpectMember} member
*
* @return {member is ParsedExpectMember<ModifierName>}
*/
const shouldBeParsedExpectModifier = member => ModifierName.hasOwnProperty(member.name);
const parseExpectCall = expect => {
const expectation = {
expect
};
if (!isExpectMember(expect.parent)) {
return expectation;
}
const parsedMember = parseExpectMember(expect.parent);
if (!shouldBeParsedExpectModifier(parsedMember)) {
expectation.matcher = reparseAsMatcher(parsedMember);
return expectation;
}
const modifier = expectation.modifier = reparseMemberAsModifier(parsedMember);
const memberNode = modifier.negation || modifier.node;
if (!isExpectMember(memberNode.parent)) {
return expectation;
}
expectation.matcher = reparseAsMatcher(parseExpectMember(memberNode.parent));
return expectation;
};
exports.parseExpectCall = parseExpectCall;
let DescribeAlias;
exports.DescribeAlias = DescribeAlias;
(function (DescribeAlias) {
DescribeAlias["describe"] = "describe";
DescribeAlias["fdescribe"] = "fdescribe";
DescribeAlias["xdescribe"] = "xdescribe";
})(DescribeAlias || (exports.DescribeAlias = DescribeAlias = {}));
let TestCaseName;
exports.TestCaseName = TestCaseName;
(function (TestCaseName) {
TestCaseName["fit"] = "fit";
TestCaseName["it"] = "it";
TestCaseName["test"] = "test";
TestCaseName["xit"] = "xit";
TestCaseName["xtest"] = "xtest";
})(TestCaseName || (exports.TestCaseName = TestCaseName = {}));
let HookName;
exports.HookName = HookName;
(function (HookName) {
HookName["beforeAll"] = "beforeAll";
HookName["beforeEach"] = "beforeEach";
HookName["afterAll"] = "afterAll";
HookName["afterEach"] = "afterEach";
})(HookName || (exports.HookName = HookName = {}));
let DescribeProperty;
exports.DescribeProperty = DescribeProperty;
(function (DescribeProperty) {
DescribeProperty["each"] = "each";
DescribeProperty["only"] = "only";
DescribeProperty["skip"] = "skip";
})(DescribeProperty || (exports.DescribeProperty = DescribeProperty = {}));
let TestCaseProperty;
exports.TestCaseProperty = TestCaseProperty;
(function (TestCaseProperty) {
TestCaseProperty["each"] = "each";
TestCaseProperty["concurrent"] = "concurrent";
TestCaseProperty["only"] = "only";
TestCaseProperty["skip"] = "skip";
TestCaseProperty["todo"] = "todo";
})(TestCaseProperty || (exports.TestCaseProperty = TestCaseProperty = {}));
const joinNames = (a, b) => a && b ? `${a}.${b}` : null;
function getNodeName(node) {
if (isSupportedAccessor(node)) {
return getAccessorValue(node);
}
switch (node.type) {
case _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression:
return getNodeName(node.tag);
case _experimentalUtils.AST_NODE_TYPES.MemberExpression:
return joinNames(getNodeName(node.object), getNodeName(node.property));
case _experimentalUtils.AST_NODE_TYPES.NewExpression:
case _experimentalUtils.AST_NODE_TYPES.CallExpression:
return getNodeName(node.callee);
}
return null;
}
const isFunction = node => node.type === _experimentalUtils.AST_NODE_TYPES.FunctionExpression || node.type === _experimentalUtils.AST_NODE_TYPES.ArrowFunctionExpression;
exports.isFunction = isFunction;
const isHook = node => node.callee.type === _experimentalUtils.AST_NODE_TYPES.Identifier && HookName.hasOwnProperty(node.callee.name);
exports.isHook = isHook;
const getTestCallExpressionsFromDeclaredVariables = declaredVariables => {
return declaredVariables.reduce((acc, {
references
}) => acc.concat(references.map(({
identifier
}) => identifier.parent).filter(node => !!node && node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && isTestCaseCall(node))), []);
};
exports.getTestCallExpressionsFromDeclaredVariables = getTestCallExpressionsFromDeclaredVariables;
const isTestCaseName = node => node.type === _experimentalUtils.AST_NODE_TYPES.Identifier && TestCaseName.hasOwnProperty(node.name);
const isTestCaseProperty = node => isSupportedAccessor(node) && TestCaseProperty.hasOwnProperty(getAccessorValue(node));
/**
* Checks if the given `node` is a *call* to a test case function that would
* result in tests being run by `jest`.
*
* Note that `.each()` does not count as a call in this context, as it will not
* result in `jest` running any tests.
*
* @param {TSESTree.CallExpression} node
*
* @return {node is JestFunctionCallExpression<TestCaseName>}
*/
const isTestCaseCall = node => {
if (isTestCaseName(node.callee)) {
return true;
}
const callee = node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
if (callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && isTestCaseProperty(callee.property)) {
// if we're an `each()`, ensure we're the outer CallExpression (i.e `.each()()`)
if (getAccessorValue(callee.property) === 'each' && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
return false;
}
return callee.object.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression ? isTestCaseName(callee.object.object) : isTestCaseName(callee.object);
}
return false;
};
exports.isTestCaseCall = isTestCaseCall;
const isDescribeAlias = node => node.type === _experimentalUtils.AST_NODE_TYPES.Identifier && DescribeAlias.hasOwnProperty(node.name);
const isDescribeProperty = node => isSupportedAccessor(node) && DescribeProperty.hasOwnProperty(getAccessorValue(node));
/**
* Checks if the given `node` is a *call* to a `describe` function that would
* result in a `describe` block being created by `jest`.
*
* Note that `.each()` does not count as a call in this context, as it will not
* result in `jest` creating any `describe` blocks.
*
* @param {TSESTree.CallExpression} node
*
* @return {node is JestFunctionCallExpression<TestCaseName>}
*/
const isDescribeCall = node => {
if (isDescribeAlias(node.callee)) {
return true;
}
const callee = node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
if (callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && isDescribeProperty(callee.property)) {
// if we're an `each()`, ensure we're the outer CallExpression (i.e `.each()()`)
if (getAccessorValue(callee.property) === 'each' && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
return false;
}
return callee.object.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression ? isDescribeAlias(callee.object.object) : isDescribeAlias(callee.object);
}
return false;
};
exports.isDescribeCall = isDescribeCall;
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)
);
};
exports.scopeHasLocalReference = scopeHasLocalReference;