Skip to content

Commit 78fdfe7

Browse files
refactor(no-container): add scenario
| destructure method from container
1 parent 8e2cdcc commit 78fdfe7

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/rules/no-container.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
4343

4444
create(context, [options]) {
4545
const { renderFunctions } = options;
46-
let containerName = '';
47-
let renderWrapperName = '';
4846
let hasPropertyContainer = false;
47+
let containerName: string = null;
48+
let renderWrapperName: string = null;
49+
const destructuredContainerPropNames: string[] = [];
4950

5051
return {
5152
VariableDeclarator(node) {
@@ -59,7 +60,17 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
5960
);
6061
const nodeValue =
6162
containerIndex !== -1 && node.id.properties[containerIndex].value;
62-
containerName = isIdentifier(nodeValue) && nodeValue.name;
63+
if (isIdentifier(nodeValue)) {
64+
containerName = nodeValue.name;
65+
} else {
66+
isObjectPattern(nodeValue) &&
67+
nodeValue.properties.forEach(
68+
property =>
69+
isProperty(property) &&
70+
isIdentifier(property.key) &&
71+
destructuredContainerPropNames.push(property.key.name)
72+
);
73+
}
6374
} else {
6475
renderWrapperName = isIdentifier(node.id) && node.id.name;
6576
}
@@ -83,7 +94,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
8394

8495
if (isContainerName || hasPropertyContainer) {
8596
context.report({
86-
node,
97+
node: innerNode,
8798
messageId: 'noContainer',
8899
});
89100
}
@@ -95,6 +106,12 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
95106
}
96107
if (isMemberExpression(node.callee)) {
97108
showErrorForChainedContainerMethod(node.callee);
109+
} else if (isIdentifier(node.callee)) {
110+
destructuredContainerPropNames.includes(node.callee.name) &&
111+
context.report({
112+
node,
113+
messageId: 'noContainer',
114+
});
98115
}
99116
},
100117
};

tests/lib/rules/no-container.test.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ruleTester.run(RULE_NAME, rule, {
3737
<button>Print Username</button>
3838
\`;
3939
const button = container.querySelector('button');
40-
40+
4141
button.addEventListener('click', () => console.log('clicked'));
4242
return container;
4343
}
@@ -46,6 +46,12 @@ ruleTester.run(RULE_NAME, rule, {
4646
screen.getByText(exampleDOM, 'Print Username').click();
4747
`,
4848
},
49+
{
50+
code: `
51+
const { container: { firstChild } } = render(<Example />);
52+
expect(firstChild).toBeDefined();
53+
`,
54+
},
4955
],
5056
invalid: [
5157
{
@@ -83,8 +89,19 @@ ruleTester.run(RULE_NAME, rule, {
8389
},
8490
{
8591
code: `
86-
const view = render(<Example />)
87-
const button = view.container.querySelector('.btn-primary')
92+
const view = render(<Example />);
93+
const button = view.container.querySelector('.btn-primary');
94+
`,
95+
errors: [
96+
{
97+
messageId: 'noContainer',
98+
},
99+
],
100+
},
101+
{
102+
code: `
103+
const { container: { querySelector } } = render(<Example />);
104+
querySelector('foo');
88105
`,
89106
errors: [
90107
{

0 commit comments

Comments
 (0)