6
6
7
7
import {
8
8
findClosestVariableDeclaratorNode ,
9
+ findImportSpecifier ,
9
10
getAssertNodeInfo ,
10
11
getDeepestIdentifierNode ,
11
12
getImportModuleName ,
@@ -196,7 +197,7 @@ export function detectTestingLibraryUtils<
196
197
return false ;
197
198
}
198
199
199
- const importedUtilSpecifier = getImportedUtilSpecifier (
200
+ const importedUtilSpecifier = getTestingLibraryImportedUtilSpecifier (
200
201
referenceNodeIdentifier
201
202
) ;
202
203
@@ -470,7 +471,9 @@ export function detectTestingLibraryUtils<
470
471
* Determines whether a given node is fireEvent method or not
471
472
*/
472
473
const isFireEventMethod : IsFireEventMethodFn = ( node ) => {
473
- const fireEventUtil = findImportedUtilSpecifier ( FIRE_EVENT_NAME ) ;
474
+ const fireEventUtil = findImportedTestingLibraryUtilSpecifier (
475
+ FIRE_EVENT_NAME
476
+ ) ;
474
477
let fireEventUtilName : string | undefined ;
475
478
476
479
if ( fireEventUtil ) {
@@ -687,60 +690,18 @@ export function detectTestingLibraryUtils<
687
690
} ;
688
691
689
692
/**
690
- * Gets a string and verifies if it was imported/required by Testing Library
691
- * related module.
693
+ * Finds the import util specifier related to Testing Library for a given name.
692
694
*/
693
- const findImportedUtilSpecifier : FindImportedUtilSpecifierFn = (
695
+ const findImportedTestingLibraryUtilSpecifier : FindImportedUtilSpecifierFn = (
694
696
specifierName
695
- ) => {
697
+ ) : TSESTree . ImportClause | TSESTree . Identifier | undefined => {
696
698
const node = getCustomModuleImportNode ( ) ?? getTestingLibraryImportNode ( ) ;
697
699
698
700
if ( ! node ) {
699
701
return undefined ;
700
702
}
701
703
702
- if ( isImportDeclaration ( node ) ) {
703
- const namedExport = node . specifiers . find ( ( n ) => {
704
- return (
705
- isImportSpecifier ( n ) &&
706
- [ n . imported . name , n . local . name ] . includes ( specifierName )
707
- ) ;
708
- } ) ;
709
-
710
- // it is "import { foo [as alias] } from 'baz'""
711
- if ( namedExport ) {
712
- return namedExport ;
713
- }
714
-
715
- // it could be "import * as rtl from 'baz'"
716
- return node . specifiers . find ( ( n ) => isImportNamespaceSpecifier ( n ) ) ;
717
- } else {
718
- if ( ! ASTUtils . isVariableDeclarator ( node . parent ) ) {
719
- return undefined ;
720
- }
721
- const requireNode = node . parent ;
722
-
723
- if ( ASTUtils . isIdentifier ( requireNode . id ) ) {
724
- // this is const rtl = require('foo')
725
- return requireNode . id ;
726
- }
727
-
728
- // this should be const { something } = require('foo')
729
- if ( ! isObjectPattern ( requireNode . id ) ) {
730
- return undefined ;
731
- }
732
-
733
- const property = requireNode . id . properties . find (
734
- ( n ) =>
735
- isProperty ( n ) &&
736
- ASTUtils . isIdentifier ( n . key ) &&
737
- n . key . name === specifierName
738
- ) ;
739
- if ( ! property ) {
740
- return undefined ;
741
- }
742
- return ( property as TSESTree . Property ) . key as TSESTree . Identifier ;
743
- }
704
+ return findImportSpecifier ( specifierName , node ) ;
744
705
} ;
745
706
746
707
const findImportedUserEventSpecifier : ( ) => TSESTree . Identifier | null = ( ) => {
@@ -774,7 +735,7 @@ export function detectTestingLibraryUtils<
774
735
return null ;
775
736
} ;
776
737
777
- const getImportedUtilSpecifier = (
738
+ const getTestingLibraryImportedUtilSpecifier = (
778
739
node : TSESTree . MemberExpression | TSESTree . Identifier
779
740
) : TSESTree . ImportClause | TSESTree . Identifier | undefined => {
780
741
const identifierName : string | undefined = getPropertyIdentifierNode ( node )
@@ -784,7 +745,7 @@ export function detectTestingLibraryUtils<
784
745
return undefined ;
785
746
}
786
747
787
- return findImportedUtilSpecifier ( identifierName ) ;
748
+ return findImportedTestingLibraryUtilSpecifier ( identifierName ) ;
788
749
} ;
789
750
790
751
/**
@@ -803,7 +764,7 @@ export function detectTestingLibraryUtils<
803
764
const isNodeComingFromTestingLibrary : IsNodeComingFromTestingLibraryFn = (
804
765
node
805
766
) => {
806
- const importNode = getImportedUtilSpecifier ( node ) ;
767
+ const importNode = getTestingLibraryImportedUtilSpecifier ( node ) ;
807
768
808
769
if ( ! importNode ) {
809
770
return false ;
@@ -877,7 +838,7 @@ export function detectTestingLibraryUtils<
877
838
isPresenceAssert,
878
839
isAbsenceAssert,
879
840
canReportErrors,
880
- findImportedUtilSpecifier,
841
+ findImportedUtilSpecifier : findImportedTestingLibraryUtilSpecifier ,
881
842
isNodeComingFromTestingLibrary,
882
843
} ;
883
844
0 commit comments