forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefer-screen-queries.ts
122 lines (111 loc) · 3.43 KB
/
prefer-screen-queries.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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, ALL_QUERIES_COMBINATIONS } from '../utils';
import {
isMemberExpression,
isObjectPattern,
isCallExpression,
isProperty,
isIdentifier,
} from '../node-utils';
export const RULE_NAME = 'prefer-screen-queries';
export type MessageIds = 'preferScreenQueries';
type Options = [];
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Suggest using screen while using queries',
category: 'Best Practices',
recommended: 'error',
},
messages: {
preferScreenQueries:
'Use screen to query DOM elements, `screen.{{ name }}`',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context) {
function reportInvalidUsage(node: TSESTree.Identifier) {
context.report({
node,
messageId: 'preferScreenQueries',
data: {
name: node.name,
},
});
}
const queriesRegex = new RegExp(ALL_QUERIES_COMBINATIONS_REGEXP);
const queriesDestructuredInWithinDeclaration: string[] = [];
// use an array as within might be used more than once in a test
const withinDeclaredVariables: string[] = [];
return {
VariableDeclarator(node) {
const isWithinFunction =
isCallExpression(node.init) &&
isIdentifier(node.init.callee) &&
node.init.callee.name === 'within';
if (!isWithinFunction) {
return;
}
if (isObjectPattern(node.id)) {
// save the destructured query methods
const identifiers = node.id.properties
.filter(
property =>
isProperty(property) &&
isIdentifier(property.key) &&
queriesRegex.test(property.key.name)
)
.map(
(property: TSESTree.Property) =>
(property.key as TSESTree.Identifier).name
);
queriesDestructuredInWithinDeclaration.push(...identifiers);
return;
}
if (isIdentifier(node.id)) {
withinDeclaredVariables.push(node.id.name);
}
},
[`CallExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
node: TSESTree.Identifier
) {
if (
!queriesDestructuredInWithinDeclaration.some(
queryName => queryName === node.name
)
) {
reportInvalidUsage(node);
}
},
[`MemberExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
node: TSESTree.Identifier
) {
function isIdentifierAllowed(name: string) {
return ['screen', ...withinDeclaredVariables].includes(name);
}
if (
isIdentifier(node) &&
isMemberExpression(node.parent) &&
isCallExpression(node.parent.object) &&
isIdentifier(node.parent.object.callee) &&
node.parent.object.callee.name !== 'within'
) {
reportInvalidUsage(node);
return;
}
if (
isMemberExpression(node.parent) &&
isIdentifier(node.parent.object) &&
!isIdentifierAllowed(node.parent.object.name)
) {
reportInvalidUsage(node);
}
},
};
},
});