Skip to content

Utils: add runRuleTester helper function #48

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
83ca91d
rule: add new noElementHandle rule
elaichenkov Apr 10, 2022
09acdee
chore: move end range to const
elaichenkov Apr 10, 2022
bf274cc
fix: getRange method for non-awaiting statements
elaichenkov Apr 10, 2022
6ba89e1
test: add additional tests for the noElementHandle rule
elaichenkov Apr 10, 2022
e151e3c
chore: improve noElementHandle rule
elaichenkov Apr 10, 2022
fc8c9b3
docs: update the wording of noElementHandle rule
elaichenkov Apr 11, 2022
3e8c4e8
rule: update message to match with other rule naming conventions
elaichenkov Apr 11, 2022
54725f3
rule: change noElementHandle to warn level
elaichenkov Apr 11, 2022
bad4c71
rule: noElementHandle change fixable to suggestion
elaichenkov Apr 11, 2022
4890c75
test: add valid tests for noElementHandle rule
elaichenkov Apr 11, 2022
7bb335b
rule: change type for noElementHandle to suggestion
elaichenkov Apr 11, 2022
03f016e
rule: update noElementHandle rule's message for suggestions
elaichenkov Apr 11, 2022
c776eed
rule: update noElementHandle with getRange method
elaichenkov Apr 11, 2022
5d81362
devops: add NPM publish workflow (#42)
mxschmitt Apr 11, 2022
a709eb9
Add no eval rule (#41)
elaichenkov Apr 12, 2022
f906083
Add new "noFocusedTest" rule (#44)
elaichenkov Apr 13, 2022
edfff56
Add new "noWaitForTimeout" rule (#46)
elaichenkov Apr 15, 2022
d4129b0
Add new "noSkippedTest" rule (#45)
elaichenkov Apr 15, 2022
40cf01f
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov Apr 23, 2022
4c5bbd6
utils: add runRuleTest helper function
elaichenkov Apr 23, 2022
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
Prev Previous commit
Next Next commit
rule: noElementHandle change fixable to suggestion
  • Loading branch information
elaichenkov committed Apr 11, 2022
commit bad4c71dc8a181355d621dd8cd576f61f32e687d
10 changes: 8 additions & 2 deletions lib/rules/no-element-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ module.exports = {
if (isPageIdentifier(node) && (isElementHandleIdentifier(node) || isElementHandlesIdentifier(node))) {
context.report({
messageId: 'noElementHandle',
fix: (fixer) => fixer.replaceTextRange(getRange(node), 'page.locator'),
suggest: [
{
messageId: 'replaceWithLocator',
fix: (fixer) => fixer.replaceTextRange(getRange(node), 'page.locator'),
},
],
node,
});
}
Expand All @@ -68,9 +73,10 @@ module.exports = {
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright#no-element-handle',
},
fixable: 'code',
hasSuggestions: true,
messages: {
noElementHandle: 'Unexpected use of element handles.',
replaceWithLocator: 'Replace `page.$` and `page.$$` with `page.locator`',
},
type: 'problem',
},
Expand Down
23 changes: 15 additions & 8 deletions test/no-element-handle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ const wrapInTest = (input) => `test('verify noElementHandle rule', async () => {

const invalid = (code, output) => ({
code: wrapInTest(code),
errors: [{ messageId: 'noElementHandle' }],
output: wrapInTest(output),
errors: [
{
messageId: 'noElementHandle',
suggestions: [{ messageId: 'replaceWithLocator', output: wrapInTest(output) }],
},
],
});

const valid = (code) => ({
Expand All @@ -22,10 +26,16 @@ const valid = (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');"),
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');"),
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")'),
Expand Down Expand Up @@ -55,10 +65,7 @@ new RuleTester().run('no-element-handle', rule, {
),

// missed return for the element handle
invalid(
'function getHandle() { page.$("button"); }',
'function getHandle() { page.locator("button"); }'
),
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");'),
Expand Down