Skip to content

Commit 4592668

Browse files
feat: Add no-all-method rule to disallow .all() method calls
This commit introduces a new rule that prevents the use of `.all()` as a method call in the codebase. The rule is documented in the README and includes corresponding tests to validate its functionality.
1 parent f69316a commit 4592668

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ CLI option\
162162
| [require-hook](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | | |
163163
| [require-soft-assertions](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-soft-assertions.md) | Require assertions to use `expect.soft()` | | 🔧 | |
164164
| [require-to-pass-timeout](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-to-pass-timeout.md) | Require a timeout for `toPass()` | | | |
165+
| [no-all-method](https://github.com/playwright-community/eslint-plugin-playwright) | Disallow any use of `.all()` as a method call | | | |
165166
| [require-to-throw-message](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | | |
166167
| [require-top-level-describe](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `test.describe` block | | | |
167168
| [valid-describe-callback](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback || | |

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import expectExpect from './rules/expect-expect.js'
33
import maxExpects from './rules/max-expects.js'
44
import maxNestedDescribe from './rules/max-nested-describe.js'
55
import missingPlaywrightAwait from './rules/missing-playwright-await.js'
6+
import noAllMethod from './rules/no-all-method.js'
67
import noCommentedOutTests from './rules/no-commented-out-tests.js'
78
import noConditionalExpect from './rules/no-conditional-expect.js'
89
import noConditionalInTest from './rules/no-conditional-in-test.js'
@@ -57,6 +58,7 @@ const index = {
5758
'max-expects': maxExpects,
5859
'max-nested-describe': maxNestedDescribe,
5960
'missing-playwright-await': missingPlaywrightAwait,
61+
'no-all-method': noAllMethod,
6062
'no-commented-out-tests': noCommentedOutTests,
6163
'no-conditional-expect': noConditionalExpect,
6264
'no-conditional-in-test': noConditionalInTest,

src/rules/no-all-method.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { runRuleTester } from '../utils/rule-tester.js'
2+
import rule from './no-all-method.js'
3+
4+
runRuleTester('no-all-method', rule, {
5+
invalid: [
6+
{
7+
code: "await page.getByText('foo').all()",
8+
errors: [
9+
{
10+
messageId: 'noAllMethod',
11+
},
12+
],
13+
},
14+
{
15+
code: "obj['all']()",
16+
errors: [
17+
{
18+
messageId: 'noAllMethod',
19+
},
20+
],
21+
},
22+
{
23+
code: "foo.bar.all()",
24+
errors: [
25+
{
26+
messageId: 'noAllMethod',
27+
},
28+
],
29+
},
30+
],
31+
valid: [
32+
{ code: "const all = 1;" },
33+
{ code: "obj.all;" },
34+
{ code: "obj['all'];" },
35+
{ code: "const str = 'all';" },
36+
{ code: "function all() {}" },
37+
{ code: "let all = () => {};" },
38+
],
39+
})

src/rules/no-all-method.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { createRule } from '../utils/createRule.js'
2+
3+
export default createRule({
4+
create(context) {
5+
return {
6+
CallExpression(node) {
7+
if (
8+
node.callee &&
9+
node.callee.type === 'MemberExpression' &&
10+
node.callee.property &&
11+
((node.callee.property.type === 'Identifier' && node.callee.property.name === 'all') ||
12+
(node.callee.property.type === 'Literal' && node.callee.property.value === 'all'))
13+
) {
14+
context.report({
15+
messageId: 'noAllMethod',
16+
node: node.callee.property,
17+
})
18+
}
19+
},
20+
}
21+
},
22+
meta: {
23+
docs: {
24+
category: 'Best Practices',
25+
description: 'Disallow any use of .all() as a method call',
26+
recommended: false,
27+
url: 'https://github.com/playwright-community/eslint-plugin-playwright',
28+
},
29+
messages: {
30+
noAllMethod: 'Do not use .all() method.',
31+
},
32+
schema: [],
33+
type: 'problem',
34+
},
35+
})

0 commit comments

Comments
 (0)