Skip to content

fix(no-node-access): narrow detection to Testing Library queries #1064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
101 changes: 22 additions & 79 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import {
DefinitionType,
type ScopeVariable,
} from '@typescript-eslint/scope-manager';
import { TSESTree, ASTUtils } from '@typescript-eslint/utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
getDeepestIdentifierNode,
getPropertyIdentifierNode,
isCallExpression,
isLiteral,
isMemberExpression,
} from '../node-utils';
import {
ALL_QUERIES_COMBINATIONS,
ALL_RETURNING_NODES,
EVENT_HANDLER_METHODS,
getScope,
resolveToTestingLibraryFn,
} from '../utils';

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

create(context, [{ allowContainerFirstChild = false }], helpers) {
const userEventInstanceNames = new Set<string>();

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 @@ -99,94 +92,44 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function detectTestingLibraryFn(
node: TSESTree.CallExpression,
variable: ScopeVariable | null
function getProperty(
node: TSESTree.PrivateIdentifier | TSESTree.Expression
) {
if (variable && variable.defs.length > 0) {
const def = variable.defs[0];
if (
def.type === DefinitionType.Variable &&
isCallExpression(def.node.init)
) {
return resolveToTestingLibraryFn(def.node.init, context);
}
if (isLiteral(node)) {
return node;
}

return resolveToTestingLibraryFn(node, context);
return getDeepestIdentifierNode(node);
}

return {
CallExpression(node: TSESTree.CallExpression) {
const property = getDeepestIdentifierNode(node);
const identifier = getPropertyIdentifierNode(node);

const isEventHandlerMethod = EVENT_HANDLER_METHODS.some(
(method) => method === property?.name
);
const hasUserEventInstanceName = userEventInstanceNames.has(
identifier?.name ?? ''
);

const variable = identifier
? ASTUtils.findVariable(getScope(context, node), identifier)
: null;
const testingLibraryFn = detectTestingLibraryFn(node, variable);
if (!isMemberExpression(node.callee)) return;

const { callee } = node;
if (
!testingLibraryFn &&
isEventHandlerMethod &&
!hasUserEventInstanceName
!EVENT_HANDLER_METHODS.some(
(method) => method === ASTUtils.getPropertyName(callee)
)
) {
context.report({
node,
loc: property?.loc.start,
messageId: 'noNodeAccess',
});
}
},
VariableDeclarator(node: TSESTree.VariableDeclarator) {
const { init, id } = node;

if (!isCallExpression(init)) {
return;
}
const identifier = getDeepestIdentifierNode(callee.object);

if (
!isMemberExpression(init.callee) ||
!ASTUtils.isIdentifier(init.callee.object)
!identifier ||
!ALL_QUERIES_COMBINATIONS.includes(identifier.name)
) {
return;
}

const testingLibraryFn = resolveToTestingLibraryFn(init, context);
if (
init.callee.object.name === testingLibraryFn?.local &&
ASTUtils.isIdentifier(init.callee.property) &&
init.callee.property.name === 'setup' &&
ASTUtils.isIdentifier(id)
) {
userEventInstanceNames.add(id.name);
}
},
AssignmentExpression(node: TSESTree.AssignmentExpression) {
if (
ASTUtils.isIdentifier(node.left) &&
isCallExpression(node.right) &&
isMemberExpression(node.right.callee) &&
ASTUtils.isIdentifier(node.right.callee.object)
) {
const testingLibraryFn = resolveToTestingLibraryFn(
node.right,
context
);
if (
node.right.callee.object.name === testingLibraryFn?.local &&
ASTUtils.isIdentifier(node.right.callee.property) &&
node.right.callee.property.name === 'setup'
) {
userEventInstanceNames.add(node.left.name);
}
if (resolveToTestingLibraryFn(node, context)) {
const property = getProperty(callee.property);
context.report({
node,
loc: property?.loc.start,
messageId: 'noNodeAccess',
});
}
},
'ExpressionStatement MemberExpression': showErrorForNodeAccess,
Expand Down
Loading