-
Notifications
You must be signed in to change notification settings - Fork 39
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
Check if page methods are missing await (missing-playwright-await
)
#310
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,6 +247,21 @@ runRuleTester('missing-playwright-await', rule, { | |
}, | ||
}, | ||
}, | ||
// Page methods | ||
{ | ||
code: `page.goto('https://example.com')`, | ||
errors: [ | ||
{ column: 1, endColumn: 10, endLine: 1, line: 1, messageId: 'page' }, | ||
], | ||
output: `await page.goto('https://example.com')`, | ||
}, | ||
{ | ||
code: test(`page.goto('https://example.com')`), | ||
errors: [ | ||
{ column: 28, endColumn: 37, endLine: 1, line: 1, messageId: 'page' }, | ||
], | ||
output: test(`await page.goto('https://example.com')`), | ||
}, | ||
], | ||
valid: [ | ||
// Basic | ||
|
@@ -368,5 +383,14 @@ runRuleTester('missing-playwright-await', rule, { | |
}, | ||
}, | ||
}, | ||
// Page methods | ||
{ code: `await page.goto('https://example.com')` }, | ||
{ code: `await page.title()` }, | ||
// Other page methods are ignored | ||
{ code: `page.frames()` }, | ||
// Other methods with the same name are ignored | ||
{ code: `randomObject.title()` }, | ||
// Does not need to be awaited when returned | ||
{ code: `() => { return page.content() }` }, | ||
Comment on lines
+387
to
+394
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a few tests for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this probably doesn't support stuff like this: |
||
], | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Rule } from 'eslint' | ||
import ESTree from 'estree' | ||
import { getParent, getStringValue, isIdentifier } from '../utils/ast' | ||
import { getParent, getStringValue, isIdentifier, isPage } from '../utils/ast' | ||
import { createRule } from '../utils/createRule' | ||
import { ParsedFnCall, parseFnCall } from '../utils/parseFnCall' | ||
|
||
|
@@ -57,6 +57,73 @@ const playwrightTestMatchers = [ | |
'toBeInViewport', | ||
] | ||
|
||
const pageMethods = new Set([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please double check these 🙏 I tried to alphabetically sort them and make sure I included all methods that return |
||
'$', // deprecated | ||
'$$', // deprecated | ||
'$eval', // deprecated | ||
'$$eval', // deprecated | ||
'addInitScript', | ||
'addLocatorHandler', | ||
'addScriptTag', | ||
'addStyleTag', | ||
'bringToFront', | ||
'check', // deprecated | ||
'click', // deprecated | ||
'close', | ||
'content', | ||
'dblclick', // deprecated | ||
'dispatchEvent', // deprecated | ||
'dragAndDrop', | ||
'emulateMedia', | ||
'evaluate', | ||
'evaluateHandle', | ||
'exposeBinding', | ||
'exposeFunction', | ||
'fill', // deprecated | ||
'focus', // deprecated | ||
'getAttribute', // deprecated | ||
'goBack', | ||
'goForward', | ||
'goto', | ||
'hover', // deprecated | ||
'innerHTML', // deprecated | ||
'innerText', // deprecated | ||
'inputValue', // deprecated | ||
'isChecked', // deprecated | ||
'isDisabled', // deprecated | ||
'isEditable', // deprecated | ||
'isEnabled', // deprecated | ||
'isHidden', // deprecated | ||
'isVisible', // deprecated | ||
'opener', | ||
'pause', | ||
'pdf', | ||
'press', // deprecated | ||
'reload', | ||
'removeLocatorHandler', | ||
'route', | ||
'routeFromHAR', | ||
'screenshot', | ||
'selectOption', // deprecated | ||
'setChecked', // deprecated | ||
'setContent', | ||
'setExtraHTTPHeaders', | ||
'setInputFiles', // deprecated | ||
'setViewportSize', | ||
'tap', // deprecated | ||
'textContent', // deprecated | ||
'title', | ||
'type', // deprecated | ||
'unroute', | ||
'unrouteAll', | ||
'waitForFunction', | ||
'waitForLoadState', | ||
'waitForNavigation', // deprecated | ||
'waitForSelector', // deprecated | ||
'waitForTimeout', // deprecated | ||
'waitForURL', | ||
]) | ||
|
||
function getReportNode(node: ESTree.Node) { | ||
const parent = getParent(node) | ||
return parent?.type === 'MemberExpression' ? parent : node | ||
|
@@ -139,6 +206,21 @@ export default createRule({ | |
|
||
return { | ||
CallExpression(node) { | ||
// Checking validity of calls to methods on the page object | ||
if (isPage(node) && node.callee.type === 'MemberExpression') { | ||
const method = getStringValue(node.callee.property) | ||
const isValid = checkValidity(node) | ||
|
||
if (!isValid && pageMethods.has(method)) { | ||
context.report({ | ||
data: { method }, | ||
fix: (fixer) => fixer.insertTextBefore(node, 'await '), | ||
messageId: 'page', | ||
node: node.callee, | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go ahead and return at the end of this if statement, if it is a page method, no need to run |
||
} | ||
|
||
const call = parseFnCall(context, node) | ||
if (call?.type !== 'step' && call?.type !== 'expect') return | ||
|
||
|
@@ -167,6 +249,7 @@ export default createRule({ | |
messages: { | ||
expect: "'{{matcherName}}' must be awaited or returned.", | ||
expectPoll: "'expect.poll' matchers must be awaited or returned.", | ||
page: "'{{method}}' must be awaited or returned.", | ||
testStep: "'test.step' must be awaited or returned.", | ||
}, | ||
schema: [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,10 +116,17 @@ export function dig(node: ESTree.Node, identifier: string | RegExp): boolean { | |
: false | ||
} | ||
|
||
export function isPage(node: ESTree.CallExpression) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm kind of dubious on whether this function is a good idea or not, since you have to check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about instead accepting a |
||
return ( | ||
node.callee.type === 'MemberExpression' && | ||
dig(node.callee.object, /(^(page|frame)|(Page|Frame)$)/) | ||
) | ||
} | ||
|
||
export function isPageMethod(node: ESTree.CallExpression, name: string) { | ||
return ( | ||
node.callee.type === 'MemberExpression' && | ||
dig(node.callee.object, /(^(page|frame)|(Page|Frame)$)/) && | ||
isPage(node) && | ||
isPropertyAccessor(node.callee, name) | ||
) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to hear more ideas on test cases here. It's a bit simpler to check page methods than the test/except cases, since it's not a chained call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add test cases to assert
myPage.goto
,frame.goto
,myFrame.goto
, etc.