From 72acb6d2437e58b2616cb6662ad9ba3a729c9905 Mon Sep 17 00:00:00 2001 From: Senja Jarva Date: Sat, 1 Oct 2022 19:07:52 +0300 Subject: [PATCH 1/2] fix: props.children is not reported by no-node-access If node's object name is "props" any of the node accessing properties is not reported. Ref: #386 --- docs/rules/no-node-access.md | 11 ++++++++ lib/rules/no-node-access.ts | 7 ++++++ tests/lib/rules/no-node-access.test.ts | 35 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/docs/rules/no-node-access.md b/docs/rules/no-node-access.md index 95f1d952..aaa22dba 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 '${testingFramework}'; + +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', From cc10e0bf6031d6cea97c3c0dfcfcdce259dacad3 Mon Sep 17 00:00:00 2001 From: Senja Jarva Date: Sun, 2 Oct 2022 19:52:48 +0300 Subject: [PATCH 2/2] fix: review feedback by replacing a placeholder --- docs/rules/no-node-access.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-node-access.md b/docs/rules/no-node-access.md index aaa22dba..d5688657 100644 --- a/docs/rules/no-node-access.md +++ b/docs/rules/no-node-access.md @@ -46,7 +46,7 @@ within(signinModal).getByPlaceholderText('Username'); ``` ```js -import { screen } from '${testingFramework}'; +import { screen } from '@testing-library/react'; function ComponentA(props) { // props.children is not reported