1+ import { Rule } from 'eslint'
12import ESTree from 'estree'
23import { getStringValue , isPageMethod } from '../utils/ast'
34import { createRule } from '../utils/createRule'
5+ import { parseFnCall } from '../utils/parseFnCall'
46
57const locatorMethods = new Set ( [
68 'and' ,
@@ -38,6 +40,32 @@ const pageMethods = new Set([
3840 'workers' ,
3941] )
4042
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+
4169function isSupportedMethod ( node : ESTree . CallExpression ) {
4270 if ( node . callee . type !== 'MemberExpression' ) return false
4371
@@ -50,32 +78,40 @@ function isSupportedMethod(node: ESTree.CallExpression) {
5078
5179export default createRule ( {
5280 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 !
6184
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+ }
6497
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+ }
67109
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+ }
79115 } ,
80116 }
81117 } ,
0 commit comments