@@ -34,17 +34,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
3434 defaultOptions : [ ] ,
3535
3636 create ( context ) {
37- const importNodes : TSESTree . ImportDeclaration [ ] = [ ] ;
38- const waitNodes : TSESTree . Identifier [ ] = [ ] ;
39-
4037 const reportImport = ( node : TSESTree . ImportDeclaration ) => {
4138 context . report ( {
4239 node : node ,
4340 messageId : 'preferWaitForImport' ,
4441 fix ( fixer ) {
4542 const excludedImports = [ ...DEPRECATED_METHODS , 'waitFor' ] ;
4643
47- // TODO: refactor `importNodes` to TSESTree.ImportSpecifier[] ? (to not have to traverse the list twice)
4844 // get all import names excluding all testing library `wait*` utils...
4945 const newImports = node . specifiers
5046 . filter (
@@ -119,36 +115,44 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
119115 'ImportDeclaration[source.value=/testing-library/]' (
120116 node : TSESTree . ImportDeclaration
121117 ) {
122- const importedNames = node . specifiers
123- . filter (
124- specifier => isImportSpecifier ( specifier ) && specifier . imported
125- )
126- . map (
127- ( specifier : TSESTree . ImportSpecifier ) => specifier . imported . name
128- ) ;
129-
130- if (
131- importedNames . some ( importedName =>
132- DEPRECATED_METHODS . includes ( importedName )
133- )
134- ) {
135- importNodes . push ( node ) ;
136- }
137- } ,
138- 'CallExpression Identifier[name=/^(wait|waitForElement|waitForDomChange)$/]' (
139- node : TSESTree . Identifier
140- ) {
141- waitNodes . push ( node ) ;
142- } ,
143- 'Program:exit' ( ) {
144- waitNodes . forEach ( waitNode => {
145- reportWait ( waitNode ) ;
146- } ) ;
118+ const deprecatedImportSpecifiers = node . specifiers . filter (
119+ specifier =>
120+ isImportSpecifier ( specifier ) &&
121+ specifier . imported &&
122+ DEPRECATED_METHODS . includes ( specifier . imported . name )
123+ ) ;
124+
125+ deprecatedImportSpecifiers . forEach ( ( importSpecifier , i ) => {
126+ if ( i === 0 ) {
127+ reportImport ( node ) ;
128+ }
147129
148- importNodes . forEach ( importNode => {
149- reportImport ( importNode ) ;
130+ context
131+ . getDeclaredVariables ( importSpecifier )
132+ . forEach ( variable =>
133+ variable . references . forEach ( reference =>
134+ reportWait ( reference . identifier )
135+ )
136+ ) ;
150137 } ) ;
151138 } ,
139+ 'ImportDeclaration[source.value=/testing-library/] > ImportNamespaceSpecifier' (
140+ node : TSESTree . ImportNamespaceSpecifier
141+ ) {
142+ context . getDeclaredVariables ( node ) . forEach ( variable =>
143+ variable . references . forEach ( reference => {
144+ if (
145+ isMemberExpression ( reference . identifier . parent ) &&
146+ isIdentifier ( reference . identifier . parent . property ) &&
147+ DEPRECATED_METHODS . includes (
148+ reference . identifier . parent . property . name
149+ )
150+ ) {
151+ reportWait ( reference . identifier . parent . property ) ;
152+ }
153+ } )
154+ ) ;
155+ } ,
152156 } ;
153157 } ,
154158} ) ;
0 commit comments