1
+ import { Rule } from 'eslint'
1
2
import ESTree from 'estree'
2
3
import { getStringValue , isPageMethod } from '../utils/ast'
3
4
import { createRule } from '../utils/createRule'
5
+ import { parseFnCall } from '../utils/parseFnCall'
4
6
5
7
const locatorMethods = new Set ( [
6
8
'and' ,
@@ -38,6 +40,32 @@ const pageMethods = new Set([
38
40
'workers' ,
39
41
] )
40
42
43
+ const expectMatchers = new Set ( [
44
+ 'toBe' ,
45
+ 'toBeCloseTo' ,
46
+ 'toBeDefined' ,
47
+ 'toBeFalsy' ,
48
+ 'toBeGreaterThan' ,
49
+ 'toBeGreaterThanOrEqual' ,
50
+ 'toBeInstanceOf' ,
51
+ 'toBeLessThan' ,
52
+ 'toBeLessThanOrEqual' ,
53
+ 'toBeNaN' ,
54
+ 'toBeNull' ,
55
+ 'toBeTruthy' ,
56
+ 'toBeUndefined' ,
57
+ 'toContain' ,
58
+ 'toContainEqual' ,
59
+ 'toEqual' ,
60
+ 'toHaveLength' ,
61
+ 'toHaveProperty' ,
62
+ 'toMatch' ,
63
+ 'toMatchObject' ,
64
+ 'toStrictEqual' ,
65
+ 'toThrow' ,
66
+ 'toThrowError' ,
67
+ ] )
68
+
41
69
function isSupportedMethod ( node : ESTree . CallExpression ) {
42
70
if ( node . callee . type !== 'MemberExpression' ) return false
43
71
@@ -50,32 +78,40 @@ function isSupportedMethod(node: ESTree.CallExpression) {
50
78
51
79
export default createRule ( {
52
80
create ( context ) {
53
- return {
54
- AwaitExpression ( node ) {
55
- // Must be a call expression
56
- if ( node . argument . type !== 'CallExpression' ) return
57
-
58
- // Must be a foo.bar() call, bare calls are ignored
59
- const { callee } = node . argument
60
- if ( callee . type !== 'MemberExpression' ) return
81
+ function fix ( node : ESTree . Node ) {
82
+ const start = node . loc ! . start
83
+ const range = node . range !
61
84
62
- // Must be a method we care about
63
- if ( ! isSupportedMethod ( node . argument ) ) return
85
+ context . report ( {
86
+ fix : ( fixer ) => fixer . removeRange ( [ range [ 0 ] , range [ 0 ] + 6 ] ) ,
87
+ loc : {
88
+ end : {
89
+ column : start . column + 5 ,
90
+ line : start . line ,
91
+ } ,
92
+ start,
93
+ } ,
94
+ messageId : 'noUselessAwait' ,
95
+ } )
96
+ }
64
97
65
- const start = node . loc ! . start
66
- const range = node . range !
98
+ return {
99
+ 'AwaitExpression > CallExpression' (
100
+ node : ESTree . CallExpression & Rule . NodeParentExtension ,
101
+ ) {
102
+ // await page.locator('.foo')
103
+ if (
104
+ node . callee . type === 'MemberExpression' &&
105
+ isSupportedMethod ( node )
106
+ ) {
107
+ return fix ( node . parent )
108
+ }
67
109
68
- context . report ( {
69
- fix : ( fixer ) => fixer . removeRange ( [ range [ 0 ] , range [ 0 ] + 6 ] ) ,
70
- loc : {
71
- end : {
72
- column : start . column + 5 ,
73
- line : start . line ,
74
- } ,
75
- start,
76
- } ,
77
- messageId : 'noUselessAwait' ,
78
- } )
110
+ // await expect(true).toBe(true)
111
+ const call = parseFnCall ( context , node )
112
+ if ( call ?. type === 'expect' && expectMatchers . has ( call . matcherName ) ) {
113
+ return fix ( node . parent )
114
+ }
79
115
} ,
80
116
}
81
117
} ,
0 commit comments