@@ -13,12 +13,23 @@ export const RULE_NAME = 'prefer-screen-queries';
13
13
export type MessageIds = 'preferScreenQueries' ;
14
14
type Options = [ ] ;
15
15
16
- const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = [ 'container' , 'baseElement' ]
16
+ const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = [
17
+ 'container' ,
18
+ 'baseElement' ,
19
+ ] ;
17
20
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS . join ( '|' ) ;
18
21
19
22
function usesContainerOrBaseElement ( node : TSESTree . CallExpression ) {
20
- const secondArgument = node . arguments [ 1 ]
21
- return isObjectExpression ( secondArgument ) && secondArgument . properties . some ( ( property ) => isProperty ( property ) && isIdentifier ( property . key ) && ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING . includes ( property . key . name ) )
23
+ const secondArgument = node . arguments [ 1 ] ;
24
+ return (
25
+ isObjectExpression ( secondArgument ) &&
26
+ secondArgument . properties . some (
27
+ property =>
28
+ isProperty ( property ) &&
29
+ isIdentifier ( property . key ) &&
30
+ ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING . includes ( property . key . name )
31
+ )
32
+ ) ;
22
33
}
23
34
24
35
export default ESLintUtils . RuleCreator ( getDocsUrl ) < Options , MessageIds > ( {
@@ -53,33 +64,43 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
53
64
const queriesRegex = new RegExp ( ALL_QUERIES_COMBINATIONS_REGEXP ) ;
54
65
const queriesDestructuredInWithinDeclaration : string [ ] = [ ] ;
55
66
// use an array as within might be used more than once in a test
56
- const withinDeclaredVariables : string [ ] = [ ]
67
+ const withinDeclaredVariables : string [ ] = [ ] ;
57
68
58
69
return {
59
70
VariableDeclarator ( node ) {
60
71
if ( ! isCallExpression ( node . init ) || ! isIdentifier ( node . init . callee ) ) {
61
- return
72
+ return ;
62
73
}
63
- const isWithinFunction = node . init . callee . name === 'within' ;
74
+ const isWithinFunction = node . init . callee . name === 'within' ;
64
75
// TODO add the custom render option #198
65
- const usesRenderOptions = node . init . callee . name === 'render' && usesContainerOrBaseElement ( node . init ) ;
76
+ const usesRenderOptions =
77
+ node . init . callee . name === 'render' &&
78
+ usesContainerOrBaseElement ( node . init ) ;
66
79
67
80
if ( ! isWithinFunction && ! usesRenderOptions ) {
68
- return
81
+ return ;
69
82
}
70
83
71
84
if ( isObjectPattern ( node . id ) ) {
72
85
// save the destructured query methods
73
86
const identifiers = node . id . properties
74
- . filter ( property => isProperty ( property ) && isIdentifier ( property . key ) && queriesRegex . test ( property . key . name ) )
75
- . map ( ( property : TSESTree . Property ) => ( property . key as TSESTree . Identifier ) . name ) ;
87
+ . filter (
88
+ property =>
89
+ isProperty ( property ) &&
90
+ isIdentifier ( property . key ) &&
91
+ queriesRegex . test ( property . key . name )
92
+ )
93
+ . map (
94
+ ( property : TSESTree . Property ) =>
95
+ ( property . key as TSESTree . Identifier ) . name
96
+ ) ;
76
97
77
98
queriesDestructuredInWithinDeclaration . push ( ...identifiers ) ;
78
- return
99
+ return ;
79
100
}
80
101
81
102
if ( isIdentifier ( node . id ) ) {
82
- withinDeclaredVariables . push ( node . id . name )
103
+ withinDeclaredVariables . push ( node . id . name ) ;
83
104
}
84
105
} ,
85
106
[ `CallExpression > Identifier[name=/^${ ALL_QUERIES_COMBINATIONS_REGEXP } $/]` ] (
@@ -96,18 +117,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
96
117
[ `MemberExpression > Identifier[name=/^${ ALL_QUERIES_COMBINATIONS_REGEXP } $/]` ] (
97
118
node : TSESTree . Identifier
98
119
) {
99
-
100
120
function isIdentifierAllowed ( name : string ) {
101
- return [ 'screen' , ...withinDeclaredVariables ] . includes ( name )
121
+ return [ 'screen' , ...withinDeclaredVariables ] . includes ( name ) ;
102
122
}
103
123
104
124
if (
105
125
isIdentifier ( node ) &&
106
126
isMemberExpression ( node . parent ) &&
107
127
isCallExpression ( node . parent . object ) &&
108
- isIdentifier ( node . parent . object . callee ) &&
109
- node . parent . object . callee . name !== 'within' &&
110
- node . parent . object . callee . name === 'render' && ! usesContainerOrBaseElement ( node . parent . object )
128
+ isIdentifier ( node . parent . object . callee ) &&
129
+ node . parent . object . callee . name !== 'within' &&
130
+ node . parent . object . callee . name === 'render' &&
131
+ ! usesContainerOrBaseElement ( node . parent . object )
111
132
) {
112
133
reportInvalidUsage ( node ) ;
113
134
return ;
@@ -123,4 +144,4 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
123
144
} ,
124
145
} ;
125
146
} ,
126
- } ) ;
147
+ } ) ;
0 commit comments