-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,14 @@ | ||
# Disallow usage of `nth` methods (`no-nth-methods`) | ||
|
||
This rule prevents the usage of `nth` methods (`first()`, `last()`, and | ||
`nth()`). These methods can be prone to flakiness if the DOM structure changes. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```javascript | ||
page.locator('button').first(); | ||
page.locator('button').last(); | ||
page.locator('button').nth(3); | ||
``` |
This file contains 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 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,38 @@ | ||
import { Rule } from 'eslint'; | ||
import { getStringValue } from '../utils/ast'; | ||
|
||
const methods = new Set(['first', 'last', 'nth']); | ||
|
||
export default { | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (node.callee.type !== 'MemberExpression') return; | ||
|
||
const method = getStringValue(node.callee.property); | ||
if (!methods.has(method)) return; | ||
|
||
context.report({ | ||
data: { method }, | ||
loc: { | ||
end: node.loc!.end, | ||
start: node.callee.property.loc!.start, | ||
}, | ||
messageId: 'noNthMethod', | ||
}); | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallow usage of nth methods', | ||
recommended: true, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-nth-methods.md', | ||
}, | ||
messages: { | ||
noNthMethod: 'Unexpected use of {{method}}()', | ||
}, | ||
type: 'problem', | ||
}, | ||
} as Rule.RuleModule; |
This file contains 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,73 @@ | ||
import rule from '../../src/rules/no-nth-methods'; | ||
import { runRuleTester } from '../utils/rule-tester'; | ||
|
||
const messageId = 'noNthMethod'; | ||
|
||
runRuleTester('no-nth-methods', rule, { | ||
invalid: [ | ||
// First | ||
{ | ||
code: 'page.locator("button").first()', | ||
errors: [{ column: 24, endColumn: 31, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'frame.locator("button").first()', | ||
errors: [{ column: 25, endColumn: 32, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'foo.locator("button").first()', | ||
errors: [{ column: 23, endColumn: 30, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'foo.first()', | ||
errors: [{ column: 5, endColumn: 12, line: 1, messageId }], | ||
}, | ||
|
||
// Last | ||
{ | ||
code: 'page.locator("button").last()', | ||
errors: [{ column: 24, endColumn: 30, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'frame.locator("button").last()', | ||
errors: [{ column: 25, endColumn: 31, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'foo.locator("button").last()', | ||
errors: [{ column: 23, endColumn: 29, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'foo.last()', | ||
errors: [{ column: 5, endColumn: 11, line: 1, messageId }], | ||
}, | ||
|
||
// nth | ||
{ | ||
code: 'page.locator("button").nth(3)', | ||
errors: [{ column: 24, endColumn: 30, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'frame.locator("button").nth(3)', | ||
errors: [{ column: 25, endColumn: 31, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'foo.locator("button").nth(3)', | ||
errors: [{ column: 23, endColumn: 29, line: 1, messageId }], | ||
}, | ||
{ | ||
code: 'foo.nth(32)', | ||
errors: [{ column: 5, endColumn: 12, line: 1, messageId }], | ||
}, | ||
], | ||
valid: [ | ||
'page', | ||
'page.locator("button")', | ||
'frame.locator("button")', | ||
'foo.locator("button")', | ||
|
||
'page.locator("button").click()', | ||
'frame.locator("button").click()', | ||
'foo.locator("button").click()', | ||
'foo.click()', | ||
], | ||
}); |