Skip to content

Commit

Permalink
feat: added strict equal (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Mar 26, 2023
1 parent 9c5746b commit 4a2591f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -103,6 +103,7 @@ To use the all configuration, extend it in your `.eslintrc` file:
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | 🌐 | | |
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in quality matchers | 🌐 | | 💡 |
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase titles | 🌐 | 🔧 | |
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Prefer strict equal over equal | 🌐 | | 💡 |
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using toBe() || 🔧 | |
| [prefer-to-be-falsy](docs/rules/prefer-to-be-falsy.md) | Suggest using toBeFalsy() | 🌐 | 🔧 | |
| [prefer-to-be-object](docs/rules/prefer-to-be-object.md) | Prefer toBeObject() | 🌐 | 🔧 | |
Expand Down
17 changes: 17 additions & 0 deletions docs/rules/prefer-strict-equal.md
@@ -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);

```
7 changes: 5 additions & 2 deletions src/index.ts
Expand Up @@ -32,6 +32,7 @@ import preferToBeTruthy, { RULE_NAME as preferToBeTruthyName } from './rules/pre
import preferToBeFalsy, { RULE_NAME as preferToBeFalsyName } from './rules/prefer-to-be-falsy'
import preferToHaveLength, { RULE_NAME as preferToHaveLengthName } from './rules/prefer-to-have-length'
import preferEqualityMatcher, { RULE_NAME as preferEqualityMatcherName } from './rules/prefer-equality-matcher'
import preferStrictEqual, { RULE_NAME as preferStrictEqualName } from './rules/prefer-strict-equal'

const createConfig = (rules: Record<string, string>) => ({
plugins: ['vitest'],
Expand Down Expand Up @@ -71,7 +72,8 @@ const allRules = {
[preferToBeObjectName]: 'warn',
[preferToBeTruthyName]: 'warn',
[preferToHaveLengthName]: 'warn',
[preferEqualityMatcherName]: 'warn'
[preferEqualityMatcherName]: 'warn',
[preferStrictEqualName]: 'warn'
}

const recommended = {
Expand Down Expand Up @@ -118,7 +120,8 @@ export default {
[preferToBeObjectName]: preferToBeObject,
[preferToBeTruthyName]: preferToBeTruthy,
[preferToHaveLengthName]: preferToHaveLength,
[preferEqualityMatcherName]: preferEqualityMatcher
[preferEqualityMatcherName]: preferEqualityMatcher,
[preferStrictEqualName]: preferStrictEqual
},
configs: {
all: createConfig(allRules),
Expand Down
66 changes: 66 additions & 0 deletions src/rules/prefer-strict-equal.test.ts
@@ -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);'
}
]
}
]
}
]
})
})
})
51 changes: 51 additions & 0 deletions src/rules/prefer-strict-equal.ts
@@ -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)
]
}
]
})
}
}
}
}
})

0 comments on commit 4a2591f

Please sign in to comment.