-
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
140 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 @@ | ||
# Prefer strict equal over equal (`vitest/prefer-strict-equal`) | ||
|
||
⚠️ This rule _warns_ in the 🌐 `all` config. | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
```ts | ||
// bad | ||
|
||
expect(something).toEqual(somethingElse); | ||
|
||
// good | ||
expect(something).toStrictEqual(somethingElse); | ||
|
||
``` |
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,66 @@ | ||
import { describe, it } from 'vitest' | ||
import ruleTester from '../utils/tester' | ||
import rule, { RULE_NAME } from './prefer-strict-equal' | ||
|
||
describe(RULE_NAME, () => { | ||
it(RULE_NAME, () => { | ||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
'expect(something).toStrictEqual(somethingElse);', | ||
'a().toEqual(\'b\')', | ||
'expect(a);' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(something).toEqual(somethingElse);', | ||
errors: [ | ||
{ | ||
messageId: 'useToStrictEqual', | ||
column: 19, | ||
line: 1, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
output: 'expect(something).toStrictEqual(somethingElse);' | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect(something).toEqual(somethingElse,);', | ||
parserOptions: { ecmaVersion: 2017 }, | ||
errors: [ | ||
{ | ||
messageId: 'useToStrictEqual', | ||
column: 19, | ||
line: 1, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
output: 'expect(something).toStrictEqual(somethingElse,);' | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect(something)["toEqual"](somethingElse);', | ||
errors: [ | ||
{ | ||
messageId: 'useToStrictEqual', | ||
column: 19, | ||
line: 1, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
output: 'expect(something)[\'toStrictEqual\'](somethingElse);' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
}) | ||
}) |
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,51 @@ | ||
import { createEslintRule, isSupportedAccessor, replaceAccessorFixer } from '../utils' | ||
import { parseVitestFnCall } from '../utils/parseVitestFnCall' | ||
import { EqualityMatcher } from '../utils/types' | ||
|
||
export const RULE_NAME = 'prefer-strict-equal' | ||
export type MESSAGE_IDS = 'useToStrictEqual' | 'suggestReplaceWithStrictEqual' | ||
type Options = [] | ||
|
||
export default createEslintRule<Options, MESSAGE_IDS>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefer strict equal over equal', | ||
recommended: 'warn' | ||
}, | ||
messages: { | ||
useToStrictEqual: 'Use `toStrictEqual()` instead', | ||
suggestReplaceWithStrictEqual: 'Replace with `toStrictEqual()`' | ||
}, | ||
schema: [], | ||
hasSuggestions: true | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const vitestFnCall = parseVitestFnCall(node, context) | ||
|
||
if (vitestFnCall?.type !== 'expect') return | ||
|
||
const { matcher } = vitestFnCall | ||
|
||
if (isSupportedAccessor(matcher, 'toEqual')) { | ||
context.report({ | ||
messageId: 'useToStrictEqual', | ||
node: matcher, | ||
suggest: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
fix: fixer => [ | ||
replaceAccessorFixer(fixer, matcher, EqualityMatcher.toStrictEqual) | ||
] | ||
} | ||
] | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) |