Skip to content

Commit 4eb5381

Browse files
author
Mateus Felix
committed
refactor(no-container): adjust to new scenarios
| update error message
1 parent 52f5995 commit 4eb5381

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

lib/rules/no-container.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22
import { getDocsUrl } from '../utils';
33
import {
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

Comments
 (0)