1
- import { AST_NODE_TYPES } from '@typescript-eslint/utils' ;
1
+ import { AST_NODE_TYPES , TSESTree } from '@typescript-eslint/utils' ;
2
2
import {
3
3
EqualityMatcher ,
4
4
createRule ,
@@ -7,6 +7,13 @@ import {
7
7
parseJestFnCall ,
8
8
} from './utils' ;
9
9
10
+ const followOptionalChaining = (
11
+ expression : TSESTree . CallExpressionArgument ,
12
+ ) : TSESTree . CallExpressionArgument =>
13
+ expression . type === AST_NODE_TYPES . ChainExpression
14
+ ? followOptionalChaining ( expression . expression )
15
+ : expression ;
16
+
10
17
export default createRule ( {
11
18
name : __filename ,
12
19
meta : {
@@ -41,10 +48,15 @@ export default createRule({
41
48
const [ argument ] = expect . arguments ;
42
49
const { matcher } = jestFnCall ;
43
50
51
+ if ( ! EqualityMatcher . hasOwnProperty ( getAccessorValue ( matcher ) ) ) {
52
+ return ;
53
+ }
54
+
55
+ const expression = followOptionalChaining ( argument ) ;
56
+
44
57
if (
45
- ! EqualityMatcher . hasOwnProperty ( getAccessorValue ( matcher ) ) ||
46
- argument ?. type !== AST_NODE_TYPES . MemberExpression ||
47
- ! isSupportedAccessor ( argument . property , 'length' )
58
+ expression ?. type !== AST_NODE_TYPES . MemberExpression ||
59
+ ! isSupportedAccessor ( expression . property , 'length' )
48
60
) {
49
61
return ;
50
62
}
@@ -54,7 +66,10 @@ export default createRule({
54
66
return [
55
67
// remove the "length" property accessor
56
68
fixer . removeRange ( [
57
- argument . property . range [ 0 ] - 1 ,
69
+ expression . property . range [ 0 ] -
70
+ ( expression . parent ?. type === AST_NODE_TYPES . ChainExpression
71
+ ? 2
72
+ : 1 ) ,
58
73
argument . range [ 1 ] ,
59
74
] ) ,
60
75
// replace the current matcher with "toHaveLength"
0 commit comments