-
Notifications
You must be signed in to change notification settings - Fork 43
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
148 additions
and
2 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,17 @@ | ||
# Suggest using `toBeTruthy` (`vitest/prefer-to-be-truthy`) | ||
|
||
⚠️ This rule _warns_ in the 🌐 `all` config. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
```js | ||
// bad | ||
expect(foo).toBe(true) | ||
expectTypeOf(foo).toBe(true) | ||
|
||
// good | ||
expect(foo).toBeTruthy() | ||
expectTypeOf(foo).toBeTruthy() | ||
``` |
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,70 @@ | ||
import { describe, it } from 'vitest' | ||
import ruleTester from '../utils/tester' | ||
import rule, { RULE_NAME } from './prefer-to-be-truthy' | ||
|
||
const messageId = 'preferToBeTruthy' | ||
|
||
describe(RULE_NAME, () => { | ||
it(RULE_NAME, () => { | ||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
'[].push(true)', | ||
'expect("something");', | ||
'expect(true).toBeTrue();', | ||
'expect(false).toBeTrue();', | ||
'expect(fal,se).toBeFalse();', | ||
'expect(true).toBeFalse();', | ||
'expect(value).toEqual();', | ||
'expect(value).not.toBeTrue();', | ||
'expect(value).not.toEqual();', | ||
'expect(value).toBe(undefined);', | ||
'expect(value).not.toBe(undefined);', | ||
'expect(true).toBe(false)', | ||
'expect(value).toBe();', | ||
'expect(true).toMatchSnapshot();', | ||
'expect("a string").toMatchSnapshot(true);', | ||
'expect("a string").not.toMatchSnapshot();', | ||
'expect(something).toEqual(\'a string\');', | ||
'expect(true).toBe', | ||
'expectTypeOf(true).toBe()' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(false).toBe(true);', | ||
output: 'expect(false).toBeTruthy();', | ||
errors: [{ messageId, column: 15, line: 1 }] | ||
}, | ||
{ | ||
code: 'expectTypeOf(false).toBe(true);', | ||
output: 'expectTypeOf(false).toBeTruthy();', | ||
errors: [{ messageId, column: 21, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect(wasSuccessful).toEqual(true);', | ||
output: 'expect(wasSuccessful).toBeTruthy();', | ||
errors: [{ messageId, column: 23, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect(fs.existsSync(\'/path/to/file\')).toStrictEqual(true);', | ||
output: 'expect(fs.existsSync(\'/path/to/file\')).toBeTruthy();', | ||
errors: [{ messageId, column: 40, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect("a string").not.toBe(true);', | ||
output: 'expect("a string").not.toBeTruthy();', | ||
errors: [{ messageId, column: 24, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect("a string").not.toEqual(true);', | ||
output: 'expect("a string").not.toBeTruthy();', | ||
errors: [{ messageId, column: 24, line: 1 }] | ||
}, | ||
{ | ||
code: 'expectTypeOf("a string").not.toStrictEqual(true);', | ||
output: 'expectTypeOf("a string").not.toBeTruthy();', | ||
errors: [{ messageId, column: 30, line: 1 }] | ||
} | ||
] | ||
}) | ||
}) | ||
}) |
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,55 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils' | ||
import { createEslintRule, getAccessorValue } from '../utils' | ||
import { getFirstMatcherArg, parseVitestFnCall } from '../utils/parseVitestFnCall' | ||
import { EqualityMatcher } from '../utils/types' | ||
|
||
type MESSAGE_IDS = 'preferToBeTruthy' | ||
export const RULE_NAME = 'prefer-to-be-truthy' | ||
type Options = [] | ||
|
||
interface TrueLiteral extends TSESTree.BooleanLiteral { | ||
value: true; | ||
} | ||
|
||
const isTrueLiteral = (node: TSESTree.Node): node is TrueLiteral => | ||
node.type === AST_NODE_TYPES.Literal && node.value === true | ||
|
||
export default createEslintRule<Options, MESSAGE_IDS>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Suggest using `toBeTruthy`', | ||
recommended: 'warn' | ||
}, | ||
messages: { | ||
preferToBeTruthy: 'Prefer using `toBeTruthy` to test value is `true`' | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const vitestFnCall = parseVitestFnCall(node, context) | ||
|
||
if (!(vitestFnCall?.type === 'expect' || vitestFnCall?.type === 'expectTypeOf')) return | ||
|
||
if (vitestFnCall.args.length === 1 && | ||
isTrueLiteral(getFirstMatcherArg(vitestFnCall)) && | ||
// eslint-disable-next-line no-prototype-builtins | ||
EqualityMatcher.hasOwnProperty(getAccessorValue(vitestFnCall.matcher))) { | ||
context.report({ | ||
node: vitestFnCall.matcher, | ||
messageId: 'preferToBeTruthy', | ||
fix: fixer => [ | ||
fixer.replaceText(vitestFnCall.matcher, 'toBeTruthy'), | ||
fixer.remove(vitestFnCall.args[0]) | ||
] | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) |