diff --git a/docs/rules/no-node-access.md b/docs/rules/no-node-access.md index 95f1d952..d5688657 100644 --- a/docs/rules/no-node-access.md +++ b/docs/rules/no-node-access.md @@ -45,6 +45,17 @@ const signinModal = getByLabelText('Sign In'); within(signinModal).getByPlaceholderText('Username'); ``` +```js +import { screen } from '@testing-library/react'; + +function ComponentA(props) { + // props.children is not reported + return
{props.children}
; +} + +render(); +``` + ```js // If is not importing a testing-library package diff --git a/lib/rules/no-node-access.ts b/lib/rules/no-node-access.ts index 8d0b5302..1beb3f2c 100644 --- a/lib/rules/no-node-access.ts +++ b/lib/rules/no-node-access.ts @@ -59,6 +59,13 @@ export default createTestingLibraryRule({ return; } + if ( + ASTUtils.isIdentifier(node.object) && + node.object.name === 'props' + ) { + return; + } + context.report({ node, loc: node.property.loc.start, diff --git a/tests/lib/rules/no-node-access.test.ts b/tests/lib/rules/no-node-access.test.ts index e4d81a6b..717fdcac 100644 --- a/tests/lib/rules/no-node-access.test.ts +++ b/tests/lib/rules/no-node-access.test.ts @@ -76,6 +76,41 @@ ruleTester.run(RULE_NAME, rule, { ); `, }, + { + code: `// issue #386 examples, props.children should not be reported + import { screen } from '${testingFramework}'; + jest.mock('@/some/path', () => ({ + someProperty: jest.fn((props) => props.children), + })); + `, + }, + { + code: `// issue #386 examples + import { screen } from '${testingFramework}'; + function ComponentA(props) { + if (props.children) { + // ... + } + + return
{props.children}
+ } + `, + }, + { + code: `/* related to issue #386 fix + * now all node accessing properties (listed in lib/utils/index.ts, in PROPERTIES_RETURNING_NODES) + * will not be reported by this rule because anything props.something won't be reported. + */ + import { screen } from '${testingFramework}'; + function ComponentA(props) { + if (props.firstChild) { + // ... + } + + return
{props.nextSibling}
+ } + `, + }, { settings: { 'testing-library/utils-module': 'test-utils',