Skip to content

No useless await #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ command line option.\
| ✔ | | | [no-eval](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-eval.md) | Disallow usage of `page.$eval` and `page.$$eval` |
| ✔ | | 💡 | [no-focused-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-focused-test.md) | Disallow usage of `.only` annotation |
| ✔ | | | [no-force-option](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-force-option.md) | Disallow usage of the `{ force: true }` option |
| ✔ | | | [no-networkidle](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-networkidle.md) | Disallow usage of the `networkidle` option |
| ✔ | | | [no-page-pause](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-page-pause.md) | Disallow using `page.pause` |
| ✔ | 🔧 | | [no-useless-await](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-useless-await.md) | Disallow unnecessary `await`s for Playwright methods |
| | | | [no-restricted-matchers](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers |
| ✔ | | 💡 | [no-skipped-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-skipped-test.md) | Disallow usage of the `.skip` annotation |
| ✔ | 🔧 | | [no-useless-not](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-useless-not.md) | Disallow usage of `not` matchers when a specific matcher exists |
Expand Down
23 changes: 23 additions & 0 deletions docs/rules/no-useless-await.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Disallow unnecessary `await`s for Playwright methods (`no-useless-await`)

Some Playwright methods are frequently, yet incorrectly, awaited when the await
expression has no effect.

## Rule Details

Examples of **incorrect** code for this rule:

```javascript
await page.locator('.my-element');
await page.getByRole('.my-element');
```

Examples of **correct** code for this rule:

```javascript
page.locator('.my-element');
page.getByRole('.my-element');

await page.$('.my-element');
await page.goto('.my-element');
```
14 changes: 11 additions & 3 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import noNetworkidle from './rules/no-networkidle';
import noPagePause from './rules/no-page-pause';
import noRestrictedMatchers from './rules/no-restricted-matchers';
import noSkippedTest from './rules/no-skipped-test';
import noUselessAwait from './rules/no-useless-await';
import noUselessNot from './rules/no-useless-not';
import noWaitForTimeout from './rules/no-wait-for-timeout';
import preferLowercaseTitle from './rules/prefer-lowercase-title';
Expand Down Expand Up @@ -37,6 +38,7 @@ const recommended = {
'playwright/no-networkidle': 'error',
'playwright/no-page-pause': 'warn',
'playwright/no-skipped-test': 'warn',
'playwright/no-useless-await': 'warn',
'playwright/no-useless-not': 'warn',
'playwright/no-wait-for-timeout': 'warn',
'playwright/prefer-web-first-assertions': 'error',
Expand Down Expand Up @@ -93,6 +95,7 @@ export = {
'no-page-pause': noPagePause,
'no-restricted-matchers': noRestrictedMatchers,
'no-skipped-test': noSkippedTest,
'no-useless-await': noUselessAwait,
'no-useless-not': noUselessNot,
'no-wait-for-timeout': noWaitForTimeout,
'prefer-lowercase-title': preferLowercaseTitle,
Expand Down
82 changes: 82 additions & 0 deletions src/rules/no-useless-await.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Rule } from 'eslint';
import { getStringValue } from '../utils/ast';

const methods = new Set([
'and',
'childFrames',
'first',
'frame',
'frameLocator',
'frames',
'getByAltText',
'getByLabel',
'getByPlaceholder',
'getByRole',
'getByTestId',
'getByText',
'getByTitle',
'isClosed',
'isDetached',
'last',
'locator',
'mainFrame',
'name',
'nth',
'on',
'or',
'page',
'parentFrame',
'setDefaultNavigationTimeout',
'setDefaultTimeout',
'url',
'video',
'viewportSize',
'workers',
]);

export default {
create(context) {
return {
AwaitExpression(node) {
// Must be a call expression
if (node.argument.type !== 'CallExpression') return;

// Must be a foo.bar() call, bare calls are ignored
const { callee } = node.argument;
if (callee.type !== 'MemberExpression') return;

// Must be a method we care about
const { property } = callee;
if (!methods.has(getStringValue(property))) return;

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

context.report({
fix: (fixer) => fixer.removeRange([range[0], range[0] + 6]),
loc: {
end: {
column: start.column + 5,
line: start.line,
},
start,
},
messageId: 'noUselessAwait',
});
},
};
},
meta: {
docs: {
category: 'Possible Errors',
description: 'Disallow unnecessary awaits for Playwright methods',
recommended: true,
},
fixable: 'code',
messages: {
noUselessAwait:
'Unnecessary await expression. This method does not return a Promise.',
},
type: 'problem',
},
} as Rule.RuleModule;
2 changes: 1 addition & 1 deletion src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
export function isPageMethod(node: ESTree.CallExpression, name: string) {
return (
node.callee.type === 'MemberExpression' &&
dig(node.callee.object, /(^page|Page$)/) &&
dig(node.callee.object, /(^(page|frame)|(Page|Frame)$)/) &&
isPropertyAccessor(node.callee, name)
);
}
198 changes: 198 additions & 0 deletions test/spec/no-useless-await.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import rule from '../../src/rules/no-useless-await';
import { runRuleTester } from '../utils/rule-tester';

const messageId = 'noUselessAwait';

runRuleTester('no-useless-await', rule, {
invalid: [
// Page, frames, and locators
{
code: 'await page.locator(".my-element")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.locator(".my-element")',
},
{
code: 'await frame.locator(".my-element")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.locator(".my-element")',
},
{
code: 'await foo.locator(".my-element")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'foo.locator(".my-element")',
},

// nth methods
{
code: 'await foo.first()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'foo.first()',
},
{
code: 'await foo.last()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'foo.last()',
},
{
code: 'await foo.nth(3)',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'foo.nth(3)',
},
{
code: 'await foo.and(page.locator(".my-element"))',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'foo.and(page.locator(".my-element"))',
},
{
code: 'await foo.or(page.locator(".my-element"))',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'foo.or(page.locator(".my-element"))',
},

// Testing library methods
{
code: 'await page.getByAltText("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.getByAltText("foo")',
},
{
code: 'await page["getByRole"]("button")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page["getByRole"]("button")',
},
{
code: 'await page[`getByLabel`]("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page[`getByLabel`]("foo")',
},
{
code: 'await page.getByPlaceholder("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.getByPlaceholder("foo")',
},
{
code: 'await page.getByTestId("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.getByTestId("foo")',
},
{
code: 'await page.getByText("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.getByText("foo")',
},
{
code: 'await page.getByTitle("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.getByTitle("foo")',
},

// Event handlers
{
code: 'await page.on("console", () => {})',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.on("console", () => {})',
},
{
code: 'await frame.on("console", () => {})',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.on("console", () => {})',
},

// Misc page methods
{
code: 'await page.frame("foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.frame("foo")',
},
{
code: 'await page.frameLocator("#foo")',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.frameLocator("#foo")',
},
{
code: 'await page.frames()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.frames()',
},
{
code: 'await page.mainFrame()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.mainFrame()',
},
{
code: 'await page.isClosed()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.isClosed()',
},
{
code: 'await page.setDefaultNavigationTimeout()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.setDefaultNavigationTimeout()',
},
{
code: 'await page.setDefaultTimeout()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.setDefaultTimeout()',
},
{
code: 'await page.url()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.url()',
},
{
code: 'await page.video()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.video()',
},
{
code: 'await page.viewportSize()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.viewportSize()',
},
{
code: 'await page.workers()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'page.workers()',
},

// Misc frame methods
{
code: 'await frame.childFrames()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.childFrames()',
},
{
code: 'await frame.isDetached()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.isDetached()',
},
{
code: 'await frame.name()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.name()',
},
{
code: 'await frame.page()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.page()',
},
{
code: 'await frame.parentFrame()',
errors: [{ column: 1, endColumn: 6, line: 1, messageId }],
output: 'frame.parentFrame()',
},
],
valid: [
'await foo()',
'await foo(".my-element")',
'await foo.bar()',
'await foo.bar(".my-element")',

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

'await page.waitForLoadState({ waitUntil: "load" })',
'await page.waitForUrl(url, { waitUntil: "load" })',
'await page.locator(".hello-world").waitFor()',
],
});