@@ -34,17 +34,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
34
34
defaultOptions : [ ] ,
35
35
36
36
create ( context ) {
37
- const importNodes : TSESTree . ImportDeclaration [ ] = [ ] ;
38
- const waitNodes : TSESTree . Identifier [ ] = [ ] ;
39
-
40
37
const reportImport = ( node : TSESTree . ImportDeclaration ) => {
41
38
context . report ( {
42
39
node : node ,
43
40
messageId : 'preferWaitForImport' ,
44
41
fix ( fixer ) {
45
42
const excludedImports = [ ...DEPRECATED_METHODS , 'waitFor' ] ;
46
43
47
- // TODO: refactor `importNodes` to TSESTree.ImportSpecifier[] ? (to not have to traverse the list twice)
48
44
// get all import names excluding all testing library `wait*` utils...
49
45
const newImports = node . specifiers
50
46
. filter (
@@ -119,36 +115,44 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
119
115
'ImportDeclaration[source.value=/testing-library/]' (
120
116
node : TSESTree . ImportDeclaration
121
117
) {
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
+ }
147
129
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
+ ) ;
150
137
} ) ;
151
138
} ,
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
+ } ,
152
156
} ;
153
157
} ,
154
158
} ) ;
0 commit comments