|
| 1 | +import { |
| 2 | + DefinitionType, |
| 3 | + type ScopeVariable, |
| 4 | +} from '@typescript-eslint/scope-manager'; |
1 | 5 | import { TSESTree, ASTUtils } from '@typescript-eslint/utils';
|
2 | 6 |
|
3 | 7 | import { createTestingLibraryRule } from '../create-testing-library-rule';
|
4 |
| -import { isCallExpression, isMemberExpression } from '../node-utils'; |
| 8 | +import { |
| 9 | + getDeepestIdentifierNode, |
| 10 | + getPropertyIdentifierNode, |
| 11 | + isCallExpression, |
| 12 | + isMemberExpression, |
| 13 | +} from '../node-utils'; |
5 | 14 | import {
|
6 | 15 | ALL_RETURNING_NODES,
|
7 | 16 | EVENT_HANDLER_METHODS,
|
| 17 | + getScope, |
8 | 18 | resolveToTestingLibraryFn,
|
9 | 19 | } from '../utils';
|
10 | 20 |
|
@@ -88,24 +98,39 @@ export default createTestingLibraryRule<Options, MessageIds>({
|
88 | 98 | }
|
89 | 99 | }
|
90 | 100 |
|
| 101 | + function detectTestingLibraryFn( |
| 102 | + node: TSESTree.CallExpression, |
| 103 | + variable: ScopeVariable | null |
| 104 | + ) { |
| 105 | + if (variable && variable.defs.length > 0) { |
| 106 | + const def = variable.defs[0]; |
| 107 | + if ( |
| 108 | + def.type === DefinitionType.Variable && |
| 109 | + isCallExpression(def.node.init) |
| 110 | + ) { |
| 111 | + return resolveToTestingLibraryFn(def.node.init, context); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return resolveToTestingLibraryFn(node, context); |
| 116 | + } |
| 117 | + |
91 | 118 | return {
|
92 | 119 | CallExpression(node: TSESTree.CallExpression) {
|
93 |
| - const { callee } = node; |
94 |
| - const property = isMemberExpression(callee) ? callee.property : null; |
95 |
| - const object = isMemberExpression(callee) ? callee.object : null; |
96 |
| - |
97 |
| - const propertyName = ASTUtils.isIdentifier(property) |
98 |
| - ? property.name |
99 |
| - : null; |
100 |
| - const objectName = ASTUtils.isIdentifier(object) ? object.name : null; |
| 120 | + const property = getDeepestIdentifierNode(node); |
| 121 | + const identifier = getPropertyIdentifierNode(node); |
101 | 122 |
|
102 | 123 | const isEventHandlerMethod = EVENT_HANDLER_METHODS.some(
|
103 |
| - (method) => method === propertyName |
| 124 | + (method) => method === property?.name |
104 | 125 | );
|
105 | 126 | const hasUserEventInstanceName = userEventInstanceNames.has(
|
106 |
| - objectName ?? '' |
| 127 | + identifier?.name ?? '' |
107 | 128 | );
|
108 |
| - const testingLibraryFn = resolveToTestingLibraryFn(node, context); |
| 129 | + |
| 130 | + const variable = identifier |
| 131 | + ? ASTUtils.findVariable(getScope(context, node), identifier) |
| 132 | + : null; |
| 133 | + const testingLibraryFn = detectTestingLibraryFn(node, variable); |
109 | 134 |
|
110 | 135 | if (
|
111 | 136 | !testingLibraryFn &&
|
|
0 commit comments