@@ -6,11 +6,11 @@ import {
6
6
isIdentifier ,
7
7
findClosestCallExpressionNode ,
8
8
isCallExpression ,
9
- isImportDeclaration ,
10
9
isImportNamespaceSpecifier ,
11
- isVariableDeclarator ,
12
10
isObjectPattern ,
13
11
isProperty ,
12
+ isMemberFromMethodCallFromTestingLibrary ,
13
+ isIdentifierInCallExpressionFromTestingLibrary ,
14
14
} from '../node-utils' ;
15
15
16
16
export const RULE_NAME = 'prefer-wait-for' ;
@@ -158,62 +158,19 @@ export default createTestingLibraryRule<Options, MessageIds>({
158
158
// the method does not match a deprecated method
159
159
return ;
160
160
}
161
- const testingLibraryNode =
162
- helpers . getCustomModuleImportNode ( ) ??
163
- helpers . getTestingLibraryImportNode ( ) ;
164
- // this verifies the owner of the MemberExpression is the same as the node if it was imported with "import * as TL from 'foo'"
165
- const callerIsTestingLibraryFromImport =
166
- isIdentifier ( node . object ) &&
167
- isImportDeclaration ( testingLibraryNode ) &&
168
- isImportNamespaceSpecifier ( testingLibraryNode . specifiers [ 0 ] ) &&
169
- node . object . name === testingLibraryNode . specifiers [ 0 ] . local . name ;
170
- // this verifies the owner of the MemberExpression is the same as the node if it was imported with "const tl = require('foo')"
171
- const callerIsTestingLibraryFromRequire =
172
- isIdentifier ( node . object ) &&
173
- isCallExpression ( testingLibraryNode ) &&
174
- isVariableDeclarator ( testingLibraryNode . parent ) &&
175
- isIdentifier ( testingLibraryNode . parent . id ) &&
176
- node . object . name === testingLibraryNode . parent . id . name ;
177
- if (
178
- ! callerIsTestingLibraryFromImport &&
179
- ! callerIsTestingLibraryFromRequire
180
- ) {
161
+ if ( ! isMemberFromMethodCallFromTestingLibrary ( node , helpers ) ) {
181
162
// the method does not match from the imported elements from TL (even from custom)
182
163
return ;
183
164
}
184
165
addWaitFor = true ;
185
166
reportWait ( node . property as TSESTree . Identifier ) ; // compiler is not picking up correctly, it should have inferred it is an identifier
186
167
} ,
187
168
'CallExpression > Identifier' ( node : TSESTree . Identifier ) {
188
- const testingLibraryNode =
189
- helpers . getCustomModuleImportNode ( ) ??
190
- helpers . getTestingLibraryImportNode ( ) ;
191
- // this verifies the owner of the MemberExpression is the same as the node if it was imported with "import { deprecated as aliased } from 'foo'"
192
- const callerIsTestingLibraryFromImport =
193
- isImportDeclaration ( testingLibraryNode ) &&
194
- testingLibraryNode . specifiers . some (
195
- ( s ) =>
196
- isImportSpecifier ( s ) &&
197
- DEPRECATED_METHODS . includes ( s . imported . name ) &&
198
- s . local . name === node . name
199
- ) ;
200
- // this verifies the owner of the MemberExpression is the same as the node if it was imported with "const { deprecatedMethod } = require('foo')"
201
- const callerIsTestingLibraryFromRequire =
202
- isCallExpression ( testingLibraryNode ) &&
203
- isVariableDeclarator ( testingLibraryNode . parent ) &&
204
- isObjectPattern ( testingLibraryNode . parent . id ) &&
205
- testingLibraryNode . parent . id . properties . some (
206
- ( p ) =>
207
- isProperty ( p ) &&
208
- isIdentifier ( p . key ) &&
209
- isIdentifier ( p . value ) &&
210
- p . value . name === node . name &&
211
- DEPRECATED_METHODS . includes ( p . key . name )
212
- ) ;
213
- if (
214
- ! callerIsTestingLibraryFromRequire &&
215
- ! callerIsTestingLibraryFromImport
216
- ) {
169
+ if ( ! DEPRECATED_METHODS . includes ( node . name ) ) {
170
+ return ;
171
+ }
172
+
173
+ if ( ! isIdentifierInCallExpressionFromTestingLibrary ( node , helpers ) ) {
217
174
return ;
218
175
}
219
176
addWaitFor = true ;
0 commit comments