Skip to content

Commit 5f4287f

Browse files
authored
fix(no-node-access): false positives with props.children (#658)
1 parent 0ae1f25 commit 5f4287f

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

docs/rules/no-node-access.md

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ const signinModal = getByLabelText('Sign In');
4545
within(signinModal).getByPlaceholderText('Username');
4646
```
4747

48+
```js
49+
import { screen } from '@testing-library/react';
50+
51+
function ComponentA(props) {
52+
// props.children is not reported
53+
return <div>{props.children}</div>;
54+
}
55+
56+
render(<ComponentA />);
57+
```
58+
4859
```js
4960
// If is not importing a testing-library package
5061

lib/rules/no-node-access.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
5959
return;
6060
}
6161

62+
if (
63+
ASTUtils.isIdentifier(node.object) &&
64+
node.object.name === 'props'
65+
) {
66+
return;
67+
}
68+
6269
context.report({
6370
node,
6471
loc: node.property.loc.start,

tests/lib/rules/no-node-access.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ ruleTester.run(RULE_NAME, rule, {
7676
);
7777
`,
7878
},
79+
{
80+
code: `// issue #386 examples, props.children should not be reported
81+
import { screen } from '${testingFramework}';
82+
jest.mock('@/some/path', () => ({
83+
someProperty: jest.fn((props) => props.children),
84+
}));
85+
`,
86+
},
87+
{
88+
code: `// issue #386 examples
89+
import { screen } from '${testingFramework}';
90+
function ComponentA(props) {
91+
if (props.children) {
92+
// ...
93+
}
94+
95+
return <div>{props.children}</div>
96+
}
97+
`,
98+
},
99+
{
100+
code: `/* related to issue #386 fix
101+
* now all node accessing properties (listed in lib/utils/index.ts, in PROPERTIES_RETURNING_NODES)
102+
* will not be reported by this rule because anything props.something won't be reported.
103+
*/
104+
import { screen } from '${testingFramework}';
105+
function ComponentA(props) {
106+
if (props.firstChild) {
107+
// ...
108+
}
109+
110+
return <div>{props.nextSibling}</div>
111+
}
112+
`,
113+
},
79114
{
80115
settings: {
81116
'testing-library/utils-module': 'test-utils',

0 commit comments

Comments
 (0)