Skip to content

Commit 395bef5

Browse files
committed
fix: add support for AssignmentExpression
1 parent 2e7e1b6 commit 395bef5

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/rules/no-node-access.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export const RULE_NAME = 'no-node-access';
1212
export type MessageIds = 'noNodeAccess';
1313
export type Options = [{ allowContainerFirstChild: boolean }];
1414

15-
const userEventInstanceNames = new Set<string>();
16-
1715
export default createTestingLibraryRule<Options, MessageIds>({
1816
name: RULE_NAME,
1917
meta: {
@@ -52,6 +50,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
5250
],
5351

5452
create(context, [{ allowContainerFirstChild = false }], helpers) {
53+
const userEventInstanceNames = new Set<string>();
54+
5555
function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
5656
// This rule is so aggressive that can cause tons of false positives outside test files when Aggressive Reporting
5757
// is enabled. Because of that, this rule will skip this mechanism and report only if some Testing Library package
@@ -144,6 +144,26 @@ export default createTestingLibraryRule<Options, MessageIds>({
144144
userEventInstanceNames.add(id.name);
145145
}
146146
},
147+
AssignmentExpression(node: TSESTree.AssignmentExpression) {
148+
if (
149+
ASTUtils.isIdentifier(node.left) &&
150+
isCallExpression(node.right) &&
151+
isMemberExpression(node.right.callee) &&
152+
ASTUtils.isIdentifier(node.right.callee.object)
153+
) {
154+
const testingLibraryFn = resolveToTestingLibraryFn(
155+
node.right,
156+
context
157+
);
158+
if (
159+
node.right.callee.object.name === testingLibraryFn?.local &&
160+
ASTUtils.isIdentifier(node.right.callee.property) &&
161+
node.right.callee.property.name === 'setup'
162+
) {
163+
userEventInstanceNames.add(node.left.name);
164+
}
165+
}
166+
},
147167
'ExpressionStatement MemberExpression': showErrorForNodeAccess,
148168
'VariableDeclarator MemberExpression': showErrorForNodeAccess,
149169
};

0 commit comments

Comments
 (0)