-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathutils.ts
751 lines (658 loc) · 21.3 KB
/
utils.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import { parse as parsePath } from 'path';
import {
AST_NODE_TYPES,
ESLintUtils,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { version } from '../../package.json';
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`;
});
export type MaybeTypeCast<Expression extends TSESTree.Expression> =
| TSTypeCastExpression<Expression>
| Expression;
type TSTypeCastExpression<
Expression extends TSESTree.Expression = TSESTree.Expression
> = AsExpressionChain<Expression> | TypeAssertionChain<Expression>;
interface AsExpressionChain<
Expression extends TSESTree.Expression = TSESTree.Expression
> extends TSESTree.TSAsExpression {
expression: AsExpressionChain<Expression> | Expression;
}
interface TypeAssertionChain<
Expression extends TSESTree.Expression = TSESTree.Expression
> extends TSESTree.TSTypeAssertion {
expression: TypeAssertionChain<Expression> | Expression;
}
const isTypeCastExpression = <Expression extends TSESTree.Expression>(
node: MaybeTypeCast<Expression>,
): node is TSTypeCastExpression<Expression> =>
node.type === AST_NODE_TYPES.TSAsExpression ||
node.type === AST_NODE_TYPES.TSTypeAssertion;
export const followTypeAssertionChain = <
Expression extends TSESTree.Expression
>(
expression: MaybeTypeCast<Expression>,
): Expression =>
isTypeCastExpression(expression)
? followTypeAssertionChain(expression.expression)
: expression;
/**
* A `Literal` with a `value` of type `string`.
*/
interface StringLiteral<Value extends string = string>
extends TSESTree.StringLiteral {
value: Value;
}
/**
* 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 = <V extends string>(
node: TSESTree.Node,
value?: V,
): node is StringLiteral<V> =>
node.type === AST_NODE_TYPES.Literal &&
typeof node.value === 'string' &&
(value === undefined || node.value === value);
interface TemplateLiteral<Value extends string = string>
extends TSESTree.TemplateLiteral {
quasis: [TSESTree.TemplateElement & { value: { raw: Value; cooked: 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 = <V extends string>(
node: TSESTree.Node,
value?: V,
): node is TemplateLiteral<V> =>
node.type === AST_NODE_TYPES.TemplateLiteral &&
node.quasis.length === 1 && // bail out if not simple
(value === undefined || node.quasis[0].value.raw === value);
export type StringNode<S extends string = string> =
| StringLiteral<S>
| TemplateLiteral<S>;
/**
* Checks if the given `node` is a {@link StringNode}.
*
* @param {Node} node
* @param {V} [specifics]
*
* @return {node is StringNode}
*
* @template V
*/
export const isStringNode = <V extends string>(
node: TSESTree.Node,
specifics?: V,
): node is StringNode<V> =>
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
*/
export const getStringValue = <S extends string>(node: StringNode<S>): S =>
isTemplateLiteral(node) ? node.quasis[0].value.raw : node.value;
/**
* Represents a `MemberExpression` with a "known" `property`.
*/
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.
*/
export 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;
/**
* An `Identifier` with a known `name` value - i.e `expect`.
*/
interface KnownIdentifier<Name extends string> extends TSESTree.Identifier {
name: Name;
}
/**
* 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 = <V extends string>(
node: TSESTree.Node,
name?: V,
): node is KnownIdentifier<V> =>
node.type === 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
*/
export const isSupportedAccessor = <V extends string>(
node: TSESTree.Node,
value?: V,
): node is AccessorNode<V> =>
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
*/
export const getAccessorValue = <S extends string = string>(
accessor: AccessorNode<S>,
): S =>
accessor.type === AST_NODE_TYPES.Identifier
? accessor.name
: getStringValue(accessor);
type AccessorNode<Specifics extends string = string> =
| StringNode<Specifics>
| KnownIdentifier<Specifics>;
interface ExpectCall extends TSESTree.CallExpression {
callee: AccessorNode<'expect'>;
parent: TSESTree.Node;
}
/**
* 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}
*/
export const isExpectCall = (node: TSESTree.Node): node is ExpectCall =>
node.type === AST_NODE_TYPES.CallExpression &&
isSupportedAccessor(node.callee, 'expect') &&
node.parent !== undefined;
interface ParsedExpectMember<
Name extends ExpectPropertyName = ExpectPropertyName,
Node extends ExpectMember<Name> = ExpectMember<Name>
> {
name: Name;
node: Node;
}
/**
* Represents a `MemberExpression` that comes after an `ExpectCall`.
*/
interface ExpectMember<
PropertyName extends ExpectPropertyName = ExpectPropertyName
> extends KnownMemberExpression<PropertyName> {
object: ExpectCall | ExpectMember;
parent: TSESTree.Node;
}
export const isExpectMember = <
Name extends ExpectPropertyName = ExpectPropertyName
>(
node: TSESTree.Node,
name?: Name,
): node is ExpectMember<Name> =>
node.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.property, name);
/**
* Represents all the jest matchers.
*/
type MatcherName = string /* & not ModifierName */;
type ExpectPropertyName = ModifierName | MatcherName;
export type ParsedEqualityMatcherCall<
Argument extends TSESTree.Expression = TSESTree.Expression,
Matcher extends EqualityMatcher = EqualityMatcher
> = Omit<ParsedExpectMatcher<Matcher>, 'arguments'> & {
parent: TSESTree.CallExpression;
arguments: [Argument];
};
export enum ModifierName {
not = 'not',
rejects = 'rejects',
resolves = 'resolves',
}
export enum EqualityMatcher {
toBe = 'toBe',
toEqual = 'toEqual',
toStrictEqual = 'toStrictEqual',
}
export const isParsedEqualityMatcherCall = <
MatcherName extends EqualityMatcher = EqualityMatcher
>(
matcher: ParsedExpectMatcher,
name?: MatcherName,
): matcher is ParsedEqualityMatcherCall<TSESTree.Expression, MatcherName> =>
(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.
*/
export interface ParsedExpectMatcher<
Matcher extends MatcherName = MatcherName,
Node extends ExpectMember<Matcher> = ExpectMember<Matcher>
> extends ParsedExpectMember<Matcher, Node> {
/**
* The arguments being passed to the matcher.
* A value of `null` means the matcher isn't being called.
*/
arguments: TSESTree.CallExpression['arguments'] | null;
}
type BaseParsedModifier<
Modifier extends ModifierName = ModifierName
> = ParsedExpectMember<Modifier>;
type NegatableModifierName = ModifierName.rejects | ModifierName.resolves;
type NotNegatableModifierName = ModifierName.not;
/**
* Represents a parsed modifier that can be followed by a `not` negation modifier.
*/
interface NegatableParsedModifier<
Modifier extends NegatableModifierName = NegatableModifierName
> extends BaseParsedModifier<Modifier> {
negation?: ExpectMember<ModifierName.not>;
}
/**
* Represents a parsed modifier that cannot be followed by a `not` negation modifier.
*/
export interface NotNegatableParsedModifier<
Modifier extends NotNegatableModifierName = NotNegatableModifierName
> extends BaseParsedModifier<Modifier> {
negation?: never;
}
type ParsedExpectModifier =
| NotNegatableParsedModifier
| NegatableParsedModifier;
interface Expectation<ExpectNode extends ExpectCall = ExpectCall> {
expect: ExpectNode;
modifier?: ParsedExpectModifier;
matcher?: ParsedExpectMatcher;
}
const parseExpectMember = <S extends ExpectPropertyName>(
expectMember: ExpectMember<S>,
): ParsedExpectMember<S> => ({
name: getAccessorValue<S>(expectMember.property),
node: expectMember,
});
const reparseAsMatcher = (
parsedMember: ParsedExpectMember,
): ParsedExpectMatcher => ({
...parsedMember,
/**
* The arguments being passed to this `Matcher`, if any.
*
* If this matcher isn't called, this will be `null`.
*/
arguments:
parsedMember.node.parent &&
parsedMember.node.parent.type === 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: ParsedExpectMember<ModifierName>,
): ParsedExpectModifier => {
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 =
parsedMember.node.parent &&
isExpectMember(parsedMember.node.parent, ModifierName.not)
? parsedMember.node.parent
: undefined;
return {
...parsedMember,
negation,
};
};
const isSpecificMember = <Name extends ExpectPropertyName>(
member: ParsedExpectMember,
specific: Name,
): member is ParsedExpectMember<Name> => 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: ParsedExpectMember,
): member is ParsedExpectMember<ModifierName> =>
ModifierName.hasOwnProperty(member.name);
export const parseExpectCall = <ExpectNode extends ExpectCall>(
expect: ExpectNode,
): Expectation<ExpectNode> => {
const expectation: Expectation<ExpectNode> = {
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 (!memberNode.parent || !isExpectMember(memberNode.parent)) {
return expectation;
}
expectation.matcher = reparseAsMatcher(parseExpectMember(memberNode.parent));
return expectation;
};
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 DescribeProperty {
'each' = 'each',
'only' = 'only',
'skip' = 'skip',
}
export enum TestCaseProperty {
'each' = 'each',
'concurrent' = 'concurrent',
'only' = 'only',
'skip' = 'skip',
'todo' = 'todo',
}
type JestFunctionName = DescribeAlias | TestCaseName | HookName;
type JestPropertyName = DescribeProperty | TestCaseProperty;
interface JestFunctionIdentifier<FunctionName extends JestFunctionName>
extends TSESTree.Identifier {
name: FunctionName;
}
interface JestFunctionMemberExpression<
FunctionName extends JestFunctionName,
PropertyName extends JestPropertyName = JestPropertyName
> extends KnownMemberExpression<PropertyName> {
object: JestFunctionIdentifier<FunctionName>;
}
interface JestFunctionCallExpressionWithMemberExpressionCallee<
FunctionName extends JestFunctionName,
PropertyName extends JestPropertyName = JestPropertyName
> extends TSESTree.CallExpression {
callee: JestFunctionMemberExpression<FunctionName, PropertyName>;
}
export interface JestFunctionCallExpressionWithIdentifierCallee<
FunctionName extends JestFunctionName
> extends TSESTree.CallExpression {
callee: JestFunctionIdentifier<FunctionName>;
}
export type JestFunctionCallExpression<
FunctionName extends JestFunctionName = JestFunctionName
> =
| JestFunctionCallExpressionWithMemberExpressionCallee<FunctionName>
| JestFunctionCallExpressionWithIdentifierCallee<FunctionName>;
const joinNames = (a: string | null, b: string | null): string | null =>
a && b ? `${a}.${b}` : null;
export function getNodeName(
node:
| JestFunctionMemberExpression<JestFunctionName>
| JestFunctionIdentifier<JestFunctionName>,
): string;
export function getNodeName(node: TSESTree.Node): string | null;
export function getNodeName(node: TSESTree.Node): string | null {
if (isSupportedAccessor(node)) {
return getAccessorValue(node);
}
switch (node.type) {
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 isHook = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpressionWithIdentifierCallee<HookName> =>
node.callee.type === AST_NODE_TYPES.Identifier &&
HookName.hasOwnProperty(node.callee.name);
export const getTestCallExpressionsFromDeclaredVariables = (
declaredVariables: TSESLint.Scope.Variable[],
): Array<JestFunctionCallExpression<TestCaseName>> => {
return declaredVariables.reduce<
Array<JestFunctionCallExpression<TestCaseName>>
>(
(acc, { references }) =>
acc.concat(
references
.map(({ identifier }) => identifier.parent)
.filter(
(node): node is JestFunctionCallExpression<TestCaseName> =>
!!node &&
node.type === AST_NODE_TYPES.CallExpression &&
isTestCase(node),
),
),
[],
);
};
export const isTestCase = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<TestCaseName> =>
(node.callee.type === AST_NODE_TYPES.Identifier &&
TestCaseName.hasOwnProperty(node.callee.name)) ||
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
TestCaseProperty.hasOwnProperty(node.callee.property.name) &&
((node.callee.object.type === AST_NODE_TYPES.Identifier &&
TestCaseName.hasOwnProperty(node.callee.object.name)) ||
(node.callee.object.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.object.type === AST_NODE_TYPES.Identifier &&
TestCaseName.hasOwnProperty(node.callee.object.object.name))));
export const isDescribe = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<DescribeAlias> =>
(node.callee.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.name)) ||
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.object.name) &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
DescribeProperty.hasOwnProperty(node.callee.property.name));
/**
* Checks if the given node` is a call to `<describe|test|it>.each(...)`.
* If `true`, the code must look like `<method>.each(...)`.
*
* @param {JestFunctionCallExpression<DescribeAlias | TestCaseName>} node
*
* @return {node is JestFunctionCallExpressionWithMemberExpressionCallee<DescribeAlias | TestCaseName, DescribeProperty.each | TestCaseProperty.each>}
*/
export const isEachCall = (
node: JestFunctionCallExpression<DescribeAlias | TestCaseName>,
): node is JestFunctionCallExpressionWithMemberExpressionCallee<
DescribeAlias | TestCaseName,
DescribeProperty.each | TestCaseProperty.each
> =>
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property, DescribeProperty.each);
/**
* Gets the arguments of the given `JestFunctionCallExpression`.
*
* If the `node` is an `each` call, then the arguments of the actual suite
* are returned, rather then the `each` array argument.
*
* @param {JestFunctionCallExpression<DescribeAlias | TestCaseName>} node
*
* @return {Expression[]}
*/
export const getJestFunctionArguments = (
node: JestFunctionCallExpression<DescribeAlias | TestCaseName>,
) =>
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property, DescribeProperty.each) &&
node.parent &&
node.parent.type === AST_NODE_TYPES.CallExpression
? node.parent.arguments
: node.arguments;
const collectReferences = (scope: TSESLint.Scope.Scope) => {
const locals = new Set();
const unresolved = new Set();
let currentScope: TSESLint.Scope.Scope | null = 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 };
};
export const scopeHasLocalReference = (
scope: TSESLint.Scope.Scope,
referenceName: string,
) => {
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)
);
};