-
Notifications
You must be signed in to change notification settings - Fork 45
Add new "no element handle" rule #40
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
mskelton
merged 13 commits into
playwright-community:master
from
elaichenkov:no-element-handle-rule
Apr 11, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
83ca91d
rule: add new noElementHandle rule
elaichenkov 09acdee
chore: move end range to const
elaichenkov bf274cc
fix: getRange method for non-awaiting statements
elaichenkov 6ba89e1
test: add additional tests for the noElementHandle rule
elaichenkov e151e3c
chore: improve noElementHandle rule
elaichenkov fc8c9b3
docs: update the wording of noElementHandle rule
elaichenkov 3e8c4e8
rule: update message to match with other rule naming conventions
elaichenkov 54725f3
rule: change noElementHandle to warn level
elaichenkov bad4c71
rule: noElementHandle change fixable to suggestion
elaichenkov 4890c75
test: add valid tests for noElementHandle rule
elaichenkov 7bb335b
rule: change type for noElementHandle to suggestion
elaichenkov 03f016e
rule: update noElementHandle rule's message for suggestions
elaichenkov c776eed
rule: update noElementHandle with getRange method
elaichenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
function isPageIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.type === 'MemberExpression' && | ||
callee.object.type === 'Identifier' && | ||
callee.object.name === 'page' | ||
); | ||
} | ||
|
||
function isElementHandleIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === '$' | ||
); | ||
} | ||
|
||
function isElementHandlesIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === '$$' | ||
); | ||
} | ||
|
||
function getRange(node) { | ||
const start = node.parent && node.parent.type === 'AwaitExpression' | ||
? node.parent.range[0] | ||
: node.callee.object.range[0]; | ||
|
||
return [start, node.callee.property.range[1]]; | ||
} | ||
|
||
module.exports = { | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isPageIdentifier(node) && (isElementHandleIdentifier(node) || isElementHandlesIdentifier(node))) { | ||
context.report({ | ||
messageId: 'noElementHandle', | ||
suggest: [ | ||
{ | ||
messageId: isElementHandleIdentifier(node) | ||
? 'replaceElementHandleWithLocator' | ||
: 'replaceElementHandlesWithLocator', | ||
fix: (fixer) => fixer.replaceTextRange(getRange(node), 'page.locator'), | ||
}, | ||
], | ||
node, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Possible Errors', | ||
description: 'The use of ElementHandle is discouraged, use Locator instead', | ||
recommended: true, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright#no-element-handle', | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
noElementHandle: 'Unexpected use of element handles.', | ||
replaceElementHandleWithLocator: 'Replace `page.$` with `page.locator`', | ||
replaceElementHandlesWithLocator: 'Replace `page.$$` with `page.locator`', | ||
}, | ||
type: 'suggestion', | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const { RuleTester } = require('eslint'); | ||
const rule = require('../lib/rules/no-element-handle'); | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
}); | ||
|
||
const wrapInTest = (input) => `test('verify noElementHandle rule', async () => { ${input} })`; | ||
|
||
const invalid = (code, output) => ({ | ||
code: wrapInTest(code), | ||
errors: [ | ||
{ | ||
messageId: 'noElementHandle', | ||
suggestions: [ | ||
{ | ||
messageId: code.includes('page.$$') ? 'replaceElementHandlesWithLocator' : 'replaceElementHandleWithLocator', | ||
output: wrapInTest(output), | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
const valid = (code) => ({ | ||
code: wrapInTest(code), | ||
}); | ||
|
||
new RuleTester().run('no-element-handle', rule, { | ||
invalid: [ | ||
// element handle as const | ||
invalid('const handle = await page.$("text=Submit");', 'const handle = page.locator("text=Submit");'), | ||
|
||
// element handle as let | ||
invalid('let handle = await page.$("text=Submit");', 'let handle = page.locator("text=Submit");'), | ||
|
||
// element handle as expression statement without await | ||
invalid('page.$("div")', 'page.locator("div")'), | ||
|
||
// element handles as expression statement without await | ||
invalid('page.$$("div")', 'page.locator("div")'), | ||
|
||
// element handle as expression statement | ||
invalid('await page.$("div")', 'page.locator("div")'), | ||
|
||
// element handle click | ||
invalid('await (await page.$$("div")).click();', 'await (page.locator("div")).click();'), | ||
|
||
// element handles as const | ||
invalid('const handles = await page.$$("a")', 'const handles = page.locator("a")'), | ||
|
||
// element handles as let | ||
invalid('let handles = await page.$$("a")', 'let handles = page.locator("a")'), | ||
|
||
// element handles as expression statement | ||
invalid('await page.$$("a")', 'page.locator("a")'), | ||
|
||
// return element handle without awaiting it | ||
invalid( | ||
'function getHandle() { return page.$("button"); }', | ||
'function getHandle() { return page.locator("button"); }' | ||
), | ||
|
||
// return element handles without awaiting it | ||
invalid( | ||
'function getHandle() { return page.$$("button"); }', | ||
'function getHandle() { return page.locator("button"); }' | ||
), | ||
|
||
// missed return for the element handle | ||
invalid('function getHandle() { page.$("button"); }', 'function getHandle() { page.locator("button"); }'), | ||
|
||
// arrow function return element handle without awaiting it | ||
invalid('const getHandles = () => page.$("links");', 'const getHandles = () => page.locator("links");'), | ||
|
||
// arrow function return element handles without awaiting it | ||
invalid('const getHandles = () => page.$$("links");', 'const getHandles = () => page.locator("links");'), | ||
], | ||
valid: [ | ||
// page locator | ||
valid('page.locator("a")'), | ||
|
||
// page locator with action | ||
valid('await page.locator("a").click();'), | ||
|
||
// const $ | ||
valid('const $ = "text";'), | ||
|
||
// $ as a method | ||
valid('$("a");'), | ||
|
||
// this.$ as a method | ||
valid('this.$("a");'), | ||
|
||
// internalPage.$ method | ||
valid('internalPage.$("a");'), | ||
|
||
// this.page.$$$ method | ||
valid('this.page.$$$("div");'), | ||
|
||
// page.$$$ method | ||
valid('page.$$$("div");'), | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.