@@ -6,11 +6,11 @@ import {
66 isIdentifier ,
77 findClosestCallExpressionNode ,
88 isCallExpression ,
9- isImportDeclaration ,
109 isImportNamespaceSpecifier ,
11- isVariableDeclarator ,
1210 isObjectPattern ,
1311 isProperty ,
12+ isMemberFromMethodCallFromTestingLibrary ,
13+ isIdentifierInCallExpressionFromTestingLibrary ,
1414} from '../node-utils' ;
1515
1616export const RULE_NAME = 'prefer-wait-for' ;
@@ -158,62 +158,19 @@ export default createTestingLibraryRule<Options, MessageIds>({
158158 // the method does not match a deprecated method
159159 return ;
160160 }
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 ) ) {
181162 // the method does not match from the imported elements from TL (even from custom)
182163 return ;
183164 }
184165 addWaitFor = true ;
185166 reportWait ( node . property as TSESTree . Identifier ) ; // compiler is not picking up correctly, it should have inferred it is an identifier
186167 } ,
187168 '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 ) ) {
217174 return ;
218175 }
219176 addWaitFor = true ;
0 commit comments