@@ -13,12 +13,23 @@ export const RULE_NAME = 'prefer-screen-queries';
1313export type MessageIds = 'preferScreenQueries' ;
1414type Options = [ ] ;
1515
16- const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = [ 'container' , 'baseElement' ]
16+ const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = [
17+ 'container' ,
18+ 'baseElement' ,
19+ ] ;
1720const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS . join ( '|' ) ;
1821
1922function 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+ ) ;
2233}
2334
2435export default ESLintUtils . RuleCreator ( getDocsUrl ) < Options , MessageIds > ( {
@@ -53,33 +64,43 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5364 const queriesRegex = new RegExp ( ALL_QUERIES_COMBINATIONS_REGEXP ) ;
5465 const queriesDestructuredInWithinDeclaration : string [ ] = [ ] ;
5566 // use an array as within might be used more than once in a test
56- const withinDeclaredVariables : string [ ] = [ ]
67+ const withinDeclaredVariables : string [ ] = [ ] ;
5768
5869 return {
5970 VariableDeclarator ( node ) {
6071 if ( ! isCallExpression ( node . init ) || ! isIdentifier ( node . init . callee ) ) {
61- return
72+ return ;
6273 }
63- const isWithinFunction = node . init . callee . name === 'within' ;
74+ const isWithinFunction = node . init . callee . name === 'within' ;
6475 // 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 ) ;
6679
6780 if ( ! isWithinFunction && ! usesRenderOptions ) {
68- return
81+ return ;
6982 }
7083
7184 if ( isObjectPattern ( node . id ) ) {
7285 // save the destructured query methods
7386 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+ ) ;
7697
7798 queriesDestructuredInWithinDeclaration . push ( ...identifiers ) ;
78- return
99+ return ;
79100 }
80101
81102 if ( isIdentifier ( node . id ) ) {
82- withinDeclaredVariables . push ( node . id . name )
103+ withinDeclaredVariables . push ( node . id . name ) ;
83104 }
84105 } ,
85106 [ `CallExpression > Identifier[name=/^${ ALL_QUERIES_COMBINATIONS_REGEXP } $/]` ] (
@@ -96,18 +117,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
96117 [ `MemberExpression > Identifier[name=/^${ ALL_QUERIES_COMBINATIONS_REGEXP } $/]` ] (
97118 node : TSESTree . Identifier
98119 ) {
99-
100120 function isIdentifierAllowed ( name : string ) {
101- return [ 'screen' , ...withinDeclaredVariables ] . includes ( name )
121+ return [ 'screen' , ...withinDeclaredVariables ] . includes ( name ) ;
102122 }
103123
104124 if (
105125 isIdentifier ( node ) &&
106126 isMemberExpression ( node . parent ) &&
107127 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 )
111132 ) {
112133 reportInvalidUsage ( node ) ;
113134 return ;
@@ -123,4 +144,4 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
123144 } ,
124145 } ;
125146 } ,
126- } ) ;
147+ } ) ;
0 commit comments