-
Notifications
You must be signed in to change notification settings - Fork 43
Feature add no force option rule #47
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 22 commits into
playwright-community:master
from
elaichenkov:feature-add-no-force-option-rule
Apr 28, 2022
Merged
Changes from 19 commits
Commits
Show all changes
22 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 1651e94
Merge branch 'playwright-community:master' into no-element-handle-rule
elaichenkov 5e9c6e8
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov 813fc04
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov 2f7fec8
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov bd0899f
rule: add noForceOption
elaichenkov 27aba43
docs: change Example > Examples
elaichenkov 96058de
test: add wrapInTest function
elaichenkov c6f6a3b
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov fafc23f
chore: use helper function
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,52 @@ | ||
function isForceOptionEnabled({ parent }) { | ||
return ( | ||
parent && | ||
parent.arguments && | ||
parent.arguments.length && | ||
parent.arguments.some( | ||
(argument) => | ||
argument.type === 'ObjectExpression' && | ||
argument.properties.some(({ key, value }) => key && key.name === 'force' && value && value.value === true) | ||
) | ||
); | ||
} | ||
|
||
// https://playwright.dev/docs/api/class-locator | ||
const methodsWithForceOption = new Set([ | ||
'check', | ||
'uncheck', | ||
'click', | ||
'dblclick', | ||
'dragTo', | ||
'fill', | ||
'hover', | ||
'selectOption', | ||
'selectText', | ||
'setChecked', | ||
'tap', | ||
]); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create(context) { | ||
return { | ||
MemberExpression(node) { | ||
if (node.property && methodsWithForceOption.has(node.property.name) && isForceOptionEnabled(node)) { | ||
context.report({ messageId: 'noForceOption', node }); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Prevent usage of `{ force: true }` option.', | ||
recommended: true, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright#no-force-option', | ||
}, | ||
messages: { | ||
noForceOption: 'Unexpected use of { force: true } option.', | ||
}, | ||
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,58 @@ | ||
const { RuleTester } = require('eslint'); | ||
const rule = require('../lib/rules/no-force-option'); | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
}); | ||
|
||
const invalid = (code) => ({ | ||
code, | ||
errors: [{ messageId: 'noForceOption' }], | ||
}); | ||
|
||
new RuleTester().run('no-force-option', rule, { | ||
invalid: [ | ||
invalid(`test('should work', async ({ page }) => { await page.locator('check').check({ force: true }) });`), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('check').uncheck({ force: true }) });`), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('button').click({ force: true }) });`), | ||
invalid( | ||
`test('should work', async ({ page }) => { const button = page.locator('button'); await button.click({ force: true }) });` | ||
), | ||
invalid( | ||
`test('should work', async ({ page }) => { await page.locator('button').locator('btn').click({ force: true }) });` | ||
), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('button').dblclick({ force: true }) });`), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('input').dragTo({ force: true }) });`), | ||
invalid( | ||
`test('should work', async ({ page }) => { await page.locator('input').fill('something', { force: true }) });` | ||
), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('elm').hover({ force: true }) });`), | ||
invalid( | ||
`test('should work', async ({ page }) => { await page.locator('select').selectOption({ label: 'Blue' }, { force: true }) });` | ||
), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('select').selectText({ force: true }) });`), | ||
invalid( | ||
`test('should work', async ({ page }) => { await page.locator('checkbox').setChecked(true, { force: true }) });` | ||
), | ||
invalid(`test('should work', async ({ page }) => { await page.locator('button').tap({ force: true }) });`), | ||
], | ||
valid: [ | ||
`test('should work', async ({ page }) => { await page.locator('check').check() });`, | ||
elaichenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`test('should work', async ({ page }) => { await page.locator('check').uncheck() });`, | ||
`test('should work', async ({ page }) => { await page.locator('button').click() });`, | ||
`test('should work', async ({ page }) => { await page.locator('button').locator('btn').click() });`, | ||
`test('should work', async ({ page }) => { await page.locator('button').click({ delay: 500, noWaitAfter: true }) });`, | ||
`test('should work', async ({ page }) => { await page.locator('button').dblclick() });`, | ||
`test('should work', async ({ page }) => { await page.locator('input').dragTo() });`, | ||
`test('should work', async ({ page }) => { await page.locator('input').fill('something', { timeout: 1000 }) });`, | ||
`test('should work', async ({ page }) => { await page.locator('elm').hover() });`, | ||
`test('should work', async ({ page }) => { await page.locator('select').selectOption({ label: 'Blue' }) });`, | ||
`test('should work', async ({ page }) => { await page.locator('select').selectText() });`, | ||
`test('should work', async ({ page }) => { await page.locator('checkbox').setChecked(true) });`, | ||
`test('should work', async ({ page }) => { await page.locator('button').tap() });`, | ||
`doSomething({ force: true });`, | ||
`test('should work', async ({ page }) => { await doSomething({ force: true }) });`, | ||
], | ||
}); |
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.
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.
This one, I'm not sure about for the default rules, I can see both sides. @mxschmitt Thoughts?
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.
usually you never have to use force. Only when Playwright has bugs or the application which you are testing does not follow the accessibility standards. So I'd vote for emitting a warning, if users scream, we can change it anyway or they disable the rule manually.