Skip to content

Add no-useless-not rule #81

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 3 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions docs/rules/no-useless-not.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Disallow usage of `not` matchers when a specific matcher exists (`no-useless-not`)

Several Playwright matchers are complimentary such as `toBeVisible`/`toBeHidden`
and `toBeEnabled`/`toBeDisabled`. While the `not` variants of each of these
matchers can be used, it's preferred to use the complimentary matcher instead.

## Rule Details

Examples of **incorrect** code for this rule:

```javascript
expect(locator).not.toBeVisible();
expect(locator).not.toBeHidden();
expect(locator).not.toBeEnabled();
expect(locator).not.toBeDisabled();
```

Example of **correct** code for this rule:

```javascript
expect(locator).toBeHidden();
expect(locator).toBeVisible();
expect(locator).toBeDisabled();
expect(locator).toBeEnabled();
```
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import noWaitForTimeout from './rules/no-wait-for-timeout';
import noForceOption from './rules/no-force-option';
import maxNestedDescribe from './rules/max-nested-describe';
import noConditionalInTest from './rules/no-conditional-in-test';
import noUselessNot from './rules/no-useless-not';

export = {
configs: {
Expand All @@ -28,6 +29,7 @@ export = {
'playwright/no-force-option': 'warn',
'playwright/max-nested-describe': 'warn',
'playwright/no-conditional-in-test': 'warn',
'playwright/no-useless-not': 'warn',
},
},
'jest-playwright': {
Expand Down Expand Up @@ -74,5 +76,6 @@ export = {
'no-force-option': noForceOption,
'max-nested-describe': maxNestedDescribe,
'no-conditional-in-test': noConditionalInTest,
'no-useless-not': noUselessNot,
},
};
58 changes: 58 additions & 0 deletions src/rules/no-useless-not.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Rule } from 'eslint';
import { getNodeName, isIdentifier } from '../utils/ast';

const matcherMap = {
toBeVisible: 'toBeHidden',
toBeHidden: 'toBeVisible',
toBeEnabled: 'toBeDisabled',
toBeDisabled: 'toBeEnabled',
};

export default {
create(context) {
return {
MemberExpression(node) {
if (
node.object.type === 'MemberExpression' &&
node.object.object.type === 'CallExpression' &&
isIdentifier(node.object.object.callee, 'expect') &&
isIdentifier(node.object.property, 'not')
) {
const matcher = getNodeName(node.property) as
| keyof typeof matcherMap
| undefined;

if (matcher && matcher in matcherMap) {
const range = node.object.property.range!;

context.report({
fix: (fixer) => [
fixer.removeRange([range[0], range[1] + 1]),
fixer.replaceText(node.property, matcherMap[matcher]),
],
messageId: 'noUselessNot',
node: node,
data: {
old: matcher,
new: matcherMap[matcher],
},
});
}
}
},
};
},
meta: {
docs: {
category: 'Best Practices',
description: `Disallow usage of 'not' matchers when a more specific matcher exists`,
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-useless-not.md',
},
fixable: 'code',
messages: {
noUselessNot: 'Unexpected usage of not.{{old}}(). Use {{new}}() instead.',
},
type: 'problem',
},
} as Rule.RuleModule;
42 changes: 42 additions & 0 deletions test/spec/no-useless-not.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { runRuleTester } from '../utils/rule-tester';
import rule from '../../src/rules/no-useless-not';

const invalid = (oldMatcher: string, newMatcher: string) => ({
code: `expect(locator).not.${oldMatcher}()`,
output: `expect(locator).${newMatcher}()`,
errors: [
{
messageId: 'noUselessNot',
data: { old: oldMatcher, new: newMatcher },
},
],
});

runRuleTester('no-useless-not', rule, {
invalid: [
invalid('toBeVisible', 'toBeHidden'),
invalid('toBeHidden', 'toBeVisible'),
invalid('toBeEnabled', 'toBeDisabled'),
invalid('toBeDisabled', 'toBeEnabled'),
// Incomplete call expression
{
code: 'expect(locator).not.toBeHidden',
output: 'expect(locator).toBeVisible',
errors: [{ messageId: 'noUselessNot' }],
},
],
valid: [
'expect(locator).toBeVisible()',
'expect(locator).toBeHidden()',
'expect(locator).toBeEnabled()',
'expect(locator).toBeDisabled()',
// Incomplete call expression
'expect(locator).toBeVisible',
'expect(locator).toBeEnabled',
// Doesn't impact non-complimentary matchers
"expect(locator).not.toHaveText('foo')",
'expect(locator).not.toBeChecked()',
'expect(locator).not.toBeChecked({ checked: false })',
'expect(locator).not.toBeFocused()',
],
});