9
9
isObjectPattern ,
10
10
isProperty ,
11
11
} from '../node-utils' ;
12
+ import { getScope , getSourceCode } from '../utils' ;
12
13
13
14
export const RULE_NAME = 'prefer-find-by' ;
14
15
export type MessageIds = 'preferFindBy' ;
@@ -69,7 +70,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
69
70
defaultOptions : [ ] ,
70
71
71
72
create ( context , _ , helpers ) {
72
- const sourceCode = context . getSourceCode ( ) ;
73
+ const sourceCode = getSourceCode ( context ) ;
73
74
74
75
/**
75
76
* Reports the invalid usage of wait* plus getBy/QueryBy methods and automatically fixes the scenario
@@ -118,7 +119,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
118
119
isCallExpression ( node . body . callee . object . arguments [ 0 ] ) &&
119
120
ASTUtils . isIdentifier ( node . body . callee . object . arguments [ 0 ] . callee )
120
121
) {
121
- return node . body . callee . object . arguments [ 0 ] . callee . name ;
122
+ return node . body . callee . object . arguments [ 0 ] . callee ;
122
123
}
123
124
124
125
if ( ! ASTUtils . isIdentifier ( node . body . callee . property ) ) {
@@ -134,7 +135,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
134
135
node . body . callee . object . arguments [ 0 ] . callee . property
135
136
)
136
137
) {
137
- return node . body . callee . object . arguments [ 0 ] . callee . property . name ;
138
+ return node . body . callee . object . arguments [ 0 ] . callee . property ;
138
139
}
139
140
140
141
// expect(screen.getByText).not shape
@@ -149,7 +150,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
149
150
node . body . callee . object . object . arguments [ 0 ] . callee . property
150
151
)
151
152
) {
152
- return node . body . callee . object . object . arguments [ 0 ] . callee . property . name ;
153
+ return node . body . callee . object . object . arguments [ 0 ] . callee . property ;
153
154
}
154
155
155
156
// expect(getByText).not shape
@@ -161,10 +162,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
161
162
node . body . callee . object . object . arguments [ 0 ] . callee
162
163
)
163
164
) {
164
- return node . body . callee . object . object . arguments [ 0 ] . callee . name ;
165
+ return node . body . callee . object . object . arguments [ 0 ] . callee ;
165
166
}
166
167
167
- return node . body . callee . property . name ;
168
+ return node . body . callee . property ;
168
169
}
169
170
170
171
function getWrongQueryName ( node : TSESTree . ArrowFunctionExpression ) {
@@ -177,7 +178,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
177
178
ASTUtils . isIdentifier ( node . body . callee ) &&
178
179
helpers . isSyncQuery ( node . body . callee )
179
180
) {
180
- return node . body . callee . name ;
181
+ return node . body . callee ;
181
182
}
182
183
183
184
return getWrongQueryNameInAssertion ( node ) ;
@@ -353,12 +354,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
353
354
}
354
355
355
356
// shape of () => screen.getByText
356
- const fullQueryMethod = getWrongQueryName ( argument ) ;
357
+ const fullQueryMethodNode = getWrongQueryName ( argument ) ;
357
358
358
- if ( ! fullQueryMethod ) {
359
+ if ( ! fullQueryMethodNode ) {
359
360
return ;
360
361
}
361
362
363
+ const fullQueryMethod = fullQueryMethodNode . name ;
364
+
362
365
// if there is a second argument to AwaitExpression, it is the options
363
366
const waitOptions = node . arguments [ 1 ] ;
364
367
let waitOptionsSourceCode = '' ;
@@ -400,12 +403,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
400
403
}
401
404
402
405
// shape of () => getByText
403
- const fullQueryMethod = getWrongQueryName ( argument ) ;
406
+ const fullQueryMethodNode = getWrongQueryName ( argument ) ;
404
407
405
- if ( ! fullQueryMethod ) {
408
+ if ( ! fullQueryMethodNode ) {
406
409
return ;
407
410
}
408
411
412
+ const fullQueryMethod = fullQueryMethodNode . name ;
413
+
409
414
const queryMethod = fullQueryMethod . split ( 'By' ) [ 1 ] ;
410
415
const queryVariant = getFindByQueryVariant ( fullQueryMethod ) ;
411
416
const callArguments = getQueryArguments ( argument . body ) ;
@@ -434,7 +439,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
434
439
435
440
// this adds the findBy* declaration - adding it to the list of destructured variables { findBy* } = render()
436
441
const definition = findRenderDefinitionDeclaration (
437
- context . getScope ( ) ,
442
+ getScope ( context , fullQueryMethodNode ) ,
438
443
fullQueryMethod
439
444
) ;
440
445
// I think it should always find it, otherwise code should not be valid (it'd be using undeclared variables)
0 commit comments