Skip to content

Commit c4c5733

Browse files
authored
Merge pull request #148 from playwright-community/fix-await
fix: Fewer false negatives for `no-useless-await`
2 parents c484b35 + 79d8c7d commit c4c5733

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/rules/no-useless-await.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import { Rule } from 'eslint';
2-
import { getStringValue } from '../utils/ast';
2+
import * as ESTree from 'estree';
3+
import { getStringValue, isPageMethod } from '../utils/ast';
34

4-
const methods = new Set([
5+
const locatorMethods = new Set([
56
'and',
6-
'childFrames',
77
'first',
8-
'frame',
9-
'frameLocator',
10-
'frames',
118
'getByAltText',
129
'getByLabel',
1310
'getByPlaceholder',
1411
'getByRole',
1512
'getByTestId',
1613
'getByText',
1714
'getByTitle',
18-
'isClosed',
19-
'isDetached',
2015
'last',
2116
'locator',
17+
'nth',
18+
'or',
19+
]);
20+
21+
const pageMethods = new Set([
22+
'childFrames',
23+
'frame',
24+
'frameLocator',
25+
'frames',
26+
'isClosed',
27+
'isDetached',
2228
'mainFrame',
2329
'name',
24-
'nth',
2530
'on',
26-
'or',
2731
'page',
2832
'parentFrame',
2933
'setDefaultNavigationTimeout',
@@ -34,6 +38,16 @@ const methods = new Set([
3438
'workers',
3539
]);
3640

41+
function isSupportedMethod(node: ESTree.CallExpression) {
42+
if (node.callee.type !== 'MemberExpression') return false;
43+
44+
const name = getStringValue(node.callee.property);
45+
return (
46+
locatorMethods.has(name) ||
47+
(pageMethods.has(name) && isPageMethod(node, name))
48+
);
49+
}
50+
3751
export default {
3852
create(context) {
3953
return {
@@ -46,8 +60,7 @@ export default {
4660
if (callee.type !== 'MemberExpression') return;
4761

4862
// Must be a method we care about
49-
const { property } = callee;
50-
if (!methods.has(getStringValue(property))) return;
63+
if (!isSupportedMethod(node.argument)) return;
5164

5265
const start = node.loc!.start;
5366
const range = node.range!;

test/spec/no-useless-await.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ runRuleTester('no-useless-await', rule, {
187187
'await foo(".my-element")',
188188
'await foo.bar()',
189189
'await foo.bar(".my-element")',
190+
'await foo.isClosed(".my-selector")',
190191

191192
'page.getByRole(".my-element")',
192193
'page.locator(".my-element")',

0 commit comments

Comments
 (0)