Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/rules/no-node-access.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ASTUtils } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
Expand All @@ -10,6 +10,7 @@ import {
ALL_QUERIES_COMBINATIONS,
ALL_RETURNING_NODES,
EVENT_HANDLER_METHODS,
getScope,
resolveToTestingLibraryFn,
} from '../utils';

Expand Down Expand Up @@ -57,6 +58,55 @@ export default createTestingLibraryRule<Options, MessageIds>({
],

create(context, [{ allowContainerFirstChild = false }], helpers) {
function getObjectPropertyName(node: TSESTree.Property['key']) {
if (ASTUtils.isIdentifier(node)) {
return node.name;
}

if (isLiteral(node) && typeof node.value === 'string') {
return node.value;
}

return null;
}

function objectExpressionHasProperty(
node: TSESTree.ObjectExpression,
propertyName: string
) {
return node.properties.some(
(property) =>
property.type === AST_NODE_TYPES.Property &&
getObjectPropertyName(property.key) === propertyName
);
}

function isDeclaredObjectProperty(
node: TSESTree.MemberExpression,
propertyName: string
) {
if (!ASTUtils.isIdentifier(node.object)) {
return false;
}

const variable = ASTUtils.findVariable(
getScope(context, node),
node.object.name
);

return (
variable?.defs.some((definition) => {
const { node: definitionNode } = definition;

return (
ASTUtils.isVariableDeclarator(definitionNode) &&
definitionNode.init?.type === AST_NODE_TYPES.ObjectExpression &&
objectExpressionHasProperty(definitionNode.init, propertyName)
);
}) ?? false
);
}

function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
// This rule is so aggressive that can cause tons of false positives outside test files when Aggressive Reporting
// is enabled. Because of that, this rule will skip this mechanism and report only if some Testing Library package
Expand Down Expand Up @@ -87,7 +137,8 @@ export default createTestingLibraryRule<Options, MessageIds>({

if (
ASTUtils.isIdentifier(node.object) &&
node.object.name === 'props'
(node.object.name === 'props' ||
isDeclaredObjectProperty(node, propertyName))
) {
return;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ ruleTester.run(rule.name, rule, {
}
`,
},
{
code: `// issue #683 examples, plain object properties should not be reported
import { render } from '${testingFramework}';

const navItemWithChildren = {
title: 'Cars',
children: [
{ title: 'Add car' },
{ title: 'All cars' },
],
};

render(<NavComponent itemWithChildren={navItemWithChildren} />);
expect(navItemWithChildren.children).toHaveLength(2);
`,
},
{
code: `// issue #683 examples, plain object properties should not be reported
import { render } from '${testingFramework}';

const applicationState = {
activeElement: 'settings-tab',
};

render(<NavComponent state={applicationState} />);
expect(applicationState.activeElement).toBe('settings-tab');
`,
},
{
settings: {
'testing-library/utils-module': 'test-utils',
Expand Down