11import { ESLintUtils , TSESTree } from '@typescript-eslint/experimental-utils' ;
22import { getDocsUrl } from '../utils' ;
33import {
4- isCallExpression ,
54 isIdentifier ,
65 isMemberExpression ,
76 isObjectPattern ,
@@ -22,7 +21,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
2221 } ,
2322 messages : {
2423 noContainer :
25- 'Unexpected use of container methods . Prefer the use of "screen.someMethod ()". ' ,
24+ 'Avoid using container to query for elements . Prefer using query methods from Testing Library, such as "getByRole ()"' ,
2625 } ,
2726 fixable : null ,
2827 schema : [
@@ -44,7 +43,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
4443
4544 create ( context , [ options ] ) {
4645 const { renderFunctions } = options ;
47- let destructuredContainerName = '' ;
46+ let containerName = '' ;
47+ let renderWrapperName = '' ;
48+ let hasPropertyContainer = false ;
4849
4950 return {
5051 VariableDeclarator ( node ) {
@@ -56,25 +57,45 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
5657 isIdentifier ( property . key ) &&
5758 property . key . name === 'container'
5859 ) ;
59- if ( containerIndex !== - 1 ) {
60- const nodeValue = node . id . properties [ containerIndex ] . value ;
61- destructuredContainerName =
62- isIdentifier ( nodeValue ) && nodeValue . name ;
63- }
60+ const nodeValue =
61+ containerIndex !== - 1 && node . id . properties [ containerIndex ] . value ;
62+ containerName = isIdentifier ( nodeValue ) && nodeValue . name ;
63+ } else {
64+ renderWrapperName = isIdentifier ( node . id ) && node . id . name ;
6465 }
6566 }
6667 } ,
6768
6869 CallExpression ( node : TSESTree . CallExpression ) {
69- if (
70- isMemberExpression ( node . callee ) &&
71- isIdentifier ( node . callee . object ) &&
72- node . callee . object . name === destructuredContainerName
70+ function showErrorForChainedContainerMethod (
71+ innerNode : TSESTree . MemberExpression
7372 ) {
74- context . report ( {
75- node,
76- messageId : 'noContainer' ,
77- } ) ;
73+ if ( isMemberExpression ( innerNode ) ) {
74+ if ( isIdentifier ( innerNode . object ) ) {
75+ const isScreen = innerNode . object . name === 'screen' ;
76+ const isContainerName = innerNode . object . name === containerName ;
77+ const isRenderWrapper =
78+ innerNode . object . name === renderWrapperName ;
79+
80+ hasPropertyContainer =
81+ isIdentifier ( innerNode . property ) &&
82+ innerNode . property . name === 'container' &&
83+ ( isScreen || isRenderWrapper ) ;
84+
85+ if ( isContainerName || hasPropertyContainer ) {
86+ context . report ( {
87+ node,
88+ messageId : 'noContainer' ,
89+ } ) ;
90+ }
91+ }
92+ showErrorForChainedContainerMethod (
93+ innerNode . object as TSESTree . MemberExpression
94+ ) ;
95+ }
96+ }
97+ if ( isMemberExpression ( node . callee ) ) {
98+ showErrorForChainedContainerMethod ( node . callee ) ;
7899 }
79100 } ,
80101 } ;
0 commit comments