Skip to content

Commit 4a2591f

Browse files
authoredMar 26, 2023
feat: added strict equal (#128)
1 parent 9c5746b commit 4a2591f

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ To use the all configuration, extend it in your `.eslintrc` file:
103103
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | 🌐 | | |
104104
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in quality matchers | 🌐 | | 💡 |
105105
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase titles | 🌐 | 🔧 | |
106+
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Prefer strict equal over equal | 🌐 | | 💡 |
106107
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using toBe() || 🔧 | |
107108
| [prefer-to-be-falsy](docs/rules/prefer-to-be-falsy.md) | Suggest using toBeFalsy() | 🌐 | 🔧 | |
108109
| [prefer-to-be-object](docs/rules/prefer-to-be-object.md) | Prefer toBeObject() | 🌐 | 🔧 | |

‎docs/rules/prefer-strict-equal.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Prefer strict equal over equal (`vitest/prefer-strict-equal`)
2+
3+
⚠️ This rule _warns_ in the 🌐 `all` config.
4+
5+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
6+
7+
<!-- end auto-generated rule header -->
8+
9+
```ts
10+
// bad
11+
12+
expect(something).toEqual(somethingElse);
13+
14+
// good
15+
expect(something).toStrictEqual(somethingElse);
16+
17+
```

‎src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import preferToBeTruthy, { RULE_NAME as preferToBeTruthyName } from './rules/pre
3232
import preferToBeFalsy, { RULE_NAME as preferToBeFalsyName } from './rules/prefer-to-be-falsy'
3333
import preferToHaveLength, { RULE_NAME as preferToHaveLengthName } from './rules/prefer-to-have-length'
3434
import preferEqualityMatcher, { RULE_NAME as preferEqualityMatcherName } from './rules/prefer-equality-matcher'
35+
import preferStrictEqual, { RULE_NAME as preferStrictEqualName } from './rules/prefer-strict-equal'
3536

3637
const createConfig = (rules: Record<string, string>) => ({
3738
plugins: ['vitest'],
@@ -71,7 +72,8 @@ const allRules = {
7172
[preferToBeObjectName]: 'warn',
7273
[preferToBeTruthyName]: 'warn',
7374
[preferToHaveLengthName]: 'warn',
74-
[preferEqualityMatcherName]: 'warn'
75+
[preferEqualityMatcherName]: 'warn',
76+
[preferStrictEqualName]: 'warn'
7577
}
7678

7779
const recommended = {
@@ -118,7 +120,8 @@ export default {
118120
[preferToBeObjectName]: preferToBeObject,
119121
[preferToBeTruthyName]: preferToBeTruthy,
120122
[preferToHaveLengthName]: preferToHaveLength,
121-
[preferEqualityMatcherName]: preferEqualityMatcher
123+
[preferEqualityMatcherName]: preferEqualityMatcher,
124+
[preferStrictEqualName]: preferStrictEqual
122125
},
123126
configs: {
124127
all: createConfig(allRules),

‎src/rules/prefer-strict-equal.test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, it } from 'vitest'
2+
import ruleTester from '../utils/tester'
3+
import rule, { RULE_NAME } from './prefer-strict-equal'
4+
5+
describe(RULE_NAME, () => {
6+
it(RULE_NAME, () => {
7+
ruleTester.run(RULE_NAME, rule, {
8+
valid: [
9+
'expect(something).toStrictEqual(somethingElse);',
10+
'a().toEqual(\'b\')',
11+
'expect(a);'
12+
],
13+
invalid: [
14+
{
15+
code: 'expect(something).toEqual(somethingElse);',
16+
errors: [
17+
{
18+
messageId: 'useToStrictEqual',
19+
column: 19,
20+
line: 1,
21+
suggestions: [
22+
{
23+
messageId: 'suggestReplaceWithStrictEqual',
24+
output: 'expect(something).toStrictEqual(somethingElse);'
25+
}
26+
]
27+
}
28+
]
29+
},
30+
{
31+
code: 'expect(something).toEqual(somethingElse,);',
32+
parserOptions: { ecmaVersion: 2017 },
33+
errors: [
34+
{
35+
messageId: 'useToStrictEqual',
36+
column: 19,
37+
line: 1,
38+
suggestions: [
39+
{
40+
messageId: 'suggestReplaceWithStrictEqual',
41+
output: 'expect(something).toStrictEqual(somethingElse,);'
42+
}
43+
]
44+
}
45+
]
46+
},
47+
{
48+
code: 'expect(something)["toEqual"](somethingElse);',
49+
errors: [
50+
{
51+
messageId: 'useToStrictEqual',
52+
column: 19,
53+
line: 1,
54+
suggestions: [
55+
{
56+
messageId: 'suggestReplaceWithStrictEqual',
57+
output: 'expect(something)[\'toStrictEqual\'](somethingElse);'
58+
}
59+
]
60+
}
61+
]
62+
}
63+
]
64+
})
65+
})
66+
})

‎src/rules/prefer-strict-equal.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { createEslintRule, isSupportedAccessor, replaceAccessorFixer } from '../utils'
2+
import { parseVitestFnCall } from '../utils/parseVitestFnCall'
3+
import { EqualityMatcher } from '../utils/types'
4+
5+
export const RULE_NAME = 'prefer-strict-equal'
6+
export type MESSAGE_IDS = 'useToStrictEqual' | 'suggestReplaceWithStrictEqual'
7+
type Options = []
8+
9+
export default createEslintRule<Options, MESSAGE_IDS>({
10+
name: RULE_NAME,
11+
meta: {
12+
type: 'suggestion',
13+
docs: {
14+
description: 'Prefer strict equal over equal',
15+
recommended: 'warn'
16+
},
17+
messages: {
18+
useToStrictEqual: 'Use `toStrictEqual()` instead',
19+
suggestReplaceWithStrictEqual: 'Replace with `toStrictEqual()`'
20+
},
21+
schema: [],
22+
hasSuggestions: true
23+
},
24+
defaultOptions: [],
25+
create(context) {
26+
return {
27+
CallExpression(node) {
28+
const vitestFnCall = parseVitestFnCall(node, context)
29+
30+
if (vitestFnCall?.type !== 'expect') return
31+
32+
const { matcher } = vitestFnCall
33+
34+
if (isSupportedAccessor(matcher, 'toEqual')) {
35+
context.report({
36+
messageId: 'useToStrictEqual',
37+
node: matcher,
38+
suggest: [
39+
{
40+
messageId: 'suggestReplaceWithStrictEqual',
41+
fix: fixer => [
42+
replaceAccessorFixer(fixer, matcher, EqualityMatcher.toStrictEqual)
43+
]
44+
}
45+
]
46+
})
47+
}
48+
}
49+
}
50+
}
51+
})

0 commit comments

Comments
 (0)
Please sign in to comment.