8
8
getImportModuleName ,
9
9
getPropertyIdentifierNode ,
10
10
getReferenceNode ,
11
+ hasImportMatch ,
11
12
ImportModuleNode ,
12
13
isImportDeclaration ,
13
14
isImportNamespaceSpecifier ,
@@ -127,6 +128,12 @@ export function detectTestingLibraryUtils<
127
128
/**
128
129
* Small method to extract common checks to determine whether a node is
129
130
* related to Testing Library or not.
131
+ *
132
+ * To determine whether a node is a valid Testing Library util, there are
133
+ * two conditions to match:
134
+ * - it's named in a particular way (decided by given callback)
135
+ * - it's imported from valid Testing Library module (depends on aggressive
136
+ * reporting)
130
137
*/
131
138
function isTestingLibraryUtil (
132
139
node : TSESTree . Identifier ,
@@ -136,28 +143,14 @@ export function detectTestingLibraryUtils<
136
143
return false ;
137
144
}
138
145
139
- const referenceNode = getReferenceNode ( node ) ;
140
- const referenceNodeIdentifier = getPropertyIdentifierNode ( referenceNode ) ;
141
-
142
146
if ( isAggressiveModuleReportingEnabled ( ) ) {
143
147
return true ;
144
148
}
145
149
146
- // TODO: extract this into function, combined with logic from isFireEventMethod
147
- // TODO: include some tests create-testing-library-rule
148
- const importNode = findImportedUtilSpecifier (
149
- referenceNodeIdentifier . name
150
- ) ;
151
-
152
- if ( ! importNode ) {
153
- return false ;
154
- }
155
-
156
- if ( ASTUtils . isIdentifier ( importNode ) ) {
157
- return importNode . name === referenceNodeIdentifier . name ;
158
- }
150
+ const referenceNode = getReferenceNode ( node ) ;
151
+ const referenceNodeIdentifier = getPropertyIdentifierNode ( referenceNode ) ;
159
152
160
- return importNode . local . name === referenceNodeIdentifier . name ;
153
+ return isNodeComingFromTestingLibrary ( referenceNodeIdentifier ) ;
161
154
}
162
155
163
156
/**
@@ -454,17 +447,26 @@ export function detectTestingLibraryUtils<
454
447
const canReportErrors : CanReportErrorsFn = ( ) => {
455
448
return isTestingLibraryImported ( ) && isValidFilename ( ) ;
456
449
} ;
450
+
457
451
/**
458
- * Takes a MemberExpression or an Identifier and verifies if its name comes from the import in TL
459
- * @param node a MemberExpression (in "foo.property" it would be property) or an Identifier
452
+ * Determines whether a node is imported from a valid Testing Library module
453
+ *
454
+ * This method will try to find any import matching the given node name,
455
+ * and also make sure the name is a valid match in case it's been renamed.
460
456
*/
461
457
const isNodeComingFromTestingLibrary : IsNodeComingFromTestingLibraryFn = (
462
458
node
463
459
) => {
464
460
const identifierName : string | undefined = getPropertyIdentifierNode ( node )
465
461
. name ;
466
462
467
- return ! ! findImportedUtilSpecifier ( identifierName ) ;
463
+ const importNode = findImportedUtilSpecifier ( identifierName ) ;
464
+
465
+ if ( ! importNode ) {
466
+ return false ;
467
+ }
468
+
469
+ return hasImportMatch ( importNode , identifierName ) ;
468
470
} ;
469
471
470
472
const helpers : DetectionHelpers = {
0 commit comments