-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: updated * feat: updated
- Loading branch information
Showing
5 changed files
with
141 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,24 @@ | ||
# Suggest using toBeFalsy() (`vitest/prefer-to-be-false`) | ||
|
||
⚠️ 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 --> | ||
|
||
## Rule Details | ||
|
||
This rule aims to enforce the use of `toBeFalsy()` over `toBeFalsy()`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
expect(foo).toBe(false) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
expect(foo).toBeFalsy() | ||
``` | ||
|
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,54 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils' | ||
import { createEslintRule, getAccessorValue } from '../utils/index' | ||
import { getFirstMatcherArg, parseVitestFnCall } from '../utils/parseVitestFnCall' | ||
import { EqualityMatcher } from '../utils/types' | ||
|
||
export const RULE_NAME = 'prefer-to-be-false' | ||
export type MESSAGE_ID = 'preferToBeFalse'; | ||
export type Options = [] | ||
|
||
interface FalseLiteral extends TSESTree.BooleanLiteral { | ||
value: false; | ||
} | ||
|
||
const isFalseLiteral = (node: TSESTree.Node): node is FalseLiteral => node.type === AST_NODE_TYPES.Literal && node.value === false | ||
|
||
export default createEslintRule<Options, MESSAGE_ID>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Suggest using toBeFalsy()', | ||
recommended: false | ||
}, | ||
messages: { | ||
preferToBeFalse: 'Prefer toBe(false) over toBeFalsy()' | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const vitestFnCall = parseVitestFnCall(node, context) | ||
|
||
if (vitestFnCall?.type !== 'expect') return | ||
|
||
if (vitestFnCall.args.length === 1 && | ||
isFalseLiteral(getFirstMatcherArg(vitestFnCall)) && | ||
// eslint-disable-next-line no-prototype-builtins | ||
EqualityMatcher.hasOwnProperty(getAccessorValue(vitestFnCall.matcher))) { | ||
context.report({ | ||
node: vitestFnCall.matcher, | ||
messageId: 'preferToBeFalse', | ||
fix: fixer => [ | ||
fixer.replaceText(vitestFnCall.matcher, 'toBeFalsy'), | ||
fixer.remove(vitestFnCall.args[0]) | ||
] | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) |
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,57 @@ | ||
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint' | ||
import { it, describe } from 'vitest' | ||
import rule, { RULE_NAME } from './prefer-to-be-false' | ||
|
||
const ruleTester: RuleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser') | ||
}) | ||
|
||
describe(RULE_NAME, () => { | ||
it(RULE_NAME, () => { | ||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
'[].push(false)', | ||
'expect("something");', | ||
'expect(true).toBeTruthy();', | ||
'expect(false).toBeTruthy();', | ||
'expect(false).toBeFalsy();', | ||
'expect(true).toBeFalsy();', | ||
'expect(value).toEqual();', | ||
'expect(value).not.toBeFalsy();', | ||
'expect(value).not.toEqual();' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(true).toBe(false);', | ||
output: 'expect(true).toBeFalsy();', | ||
errors: [{ messageId: 'preferToBeFalse', column: 14, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect(wasSuccessful).toEqual(false);', | ||
output: 'expect(wasSuccessful).toBeFalsy();', | ||
errors: [{ messageId: 'preferToBeFalse', column: 23, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect(fs.existsSync(\'/path/to/file\')).toStrictEqual(false);', | ||
output: 'expect(fs.existsSync(\'/path/to/file\')).toBeFalsy();', | ||
errors: [{ messageId: 'preferToBeFalse', column: 40, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect("a string").not.toBe(false);', | ||
output: 'expect("a string").not.toBeFalsy();', | ||
errors: [{ messageId: 'preferToBeFalse', column: 24, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect("a string").not.toEqual(false);', | ||
output: 'expect("a string").not.toBeFalsy();', | ||
errors: [{ messageId: 'preferToBeFalse', column: 24, line: 1 }] | ||
}, | ||
{ | ||
code: 'expect("a string").not.toStrictEqual(false);', | ||
output: 'expect("a string").not.toBeFalsy();', | ||
errors: [{ messageId: 'preferToBeFalse', column: 24, line: 1 }] | ||
} | ||
] | ||
}) | ||
}) | ||
}) |