Skip to content

Commit a8a2ea4

Browse files
committed
chore: Add shared equality matchers
1 parent 51fca42 commit a8a2ea4

7 files changed

+13
-20
lines changed

src/rules/prefer-comparison-matcher.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Rule } from 'eslint';
22
import * as ESTree from 'estree';
33
import {
4+
equalityMatchers,
45
getParent,
56
getRawValue,
67
getStringValue,
@@ -9,8 +10,6 @@ import {
910
} from '../utils/ast';
1011
import { parseExpectCall } from '../utils/parseExpectCall';
1112

12-
const equalityMatchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);
13-
1413
const isString = (node: ESTree.Node) => {
1514
return isStringLiteral(node) || node.type === 'TemplateLiteral';
1615
};

src/rules/prefer-to-be.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Rule } from 'eslint';
22
import ESTree from 'estree';
3-
import { getStringValue, isIdentifier } from '../utils/ast';
3+
import { equalityMatchers, getStringValue, isIdentifier } from '../utils/ast';
44
import { replaceAccessorFixer } from '../utils/fixer';
55
import { ParsedExpectCall, parseExpectCall } from '../utils/parseExpectCall';
66

@@ -69,10 +69,8 @@ export default {
6969
);
7070
}
7171

72-
const argumentMatchers = ['toBe', 'toEqual', 'toStrictEqual'];
7372
const firstArg = expectCall.args[0];
74-
75-
if (!argumentMatchers.includes(expectCall.matcherName) || !firstArg) {
73+
if (!equalityMatchers.has(expectCall.matcherName) || !firstArg) {
7674
return;
7775
}
7876

src/rules/prefer-to-contain.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Rule } from 'eslint';
22
import ESTree from 'estree';
33
import {
4+
equalityMatchers,
45
getStringValue,
56
isBooleanLiteral,
67
isPropertyAccessor,
78
} from '../utils/ast';
89
import { parseExpectCall } from '../utils/parseExpectCall';
910
import { KnownCallExpression } from '../utils/types';
1011

11-
const matchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);
12-
1312
type FixableIncludesCallExpression = KnownCallExpression;
1413

1514
const isFixableIncludesCallExpression = (
@@ -35,7 +34,7 @@ export default {
3534
if (
3635
!includesCall ||
3736
matcherArg.type === 'SpreadElement' ||
38-
!matchers.has(matcherName) ||
37+
!equalityMatchers.has(matcherName) ||
3938
!isBooleanLiteral(matcherArg) ||
4039
!isFixableIncludesCallExpression(includesCall)
4140
) {

src/rules/prefer-to-have-count.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Rule } from 'eslint';
2-
import { isPropertyAccessor } from '../utils/ast';
2+
import { equalityMatchers, isPropertyAccessor } from '../utils/ast';
33
import { replaceAccessorFixer } from '../utils/fixer';
44
import { parseExpectCall } from '../utils/parseExpectCall';
55

6-
const matchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);
7-
86
export default {
97
create(context) {
108
return {
119
CallExpression(node) {
1210
const expectCall = parseExpectCall(context, node);
13-
if (!expectCall || !matchers.has(expectCall.matcherName)) {
11+
if (!expectCall || !equalityMatchers.has(expectCall.matcherName)) {
1412
return;
1513
}
1614

src/rules/prefer-to-have-length.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Rule } from 'eslint';
2-
import { isPropertyAccessor } from '../utils/ast';
2+
import { equalityMatchers, isPropertyAccessor } from '../utils/ast';
33
import { replaceAccessorFixer } from '../utils/fixer';
44
import { parseExpectCall } from '../utils/parseExpectCall';
55

6-
const lengthMatchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);
7-
86
export default {
97
create(context) {
108
return {
119
CallExpression(node) {
1210
const expectCall = parseExpectCall(context, node);
13-
if (!expectCall || !lengthMatchers.has(expectCall.matcherName)) {
11+
if (!expectCall || !equalityMatchers.has(expectCall.matcherName)) {
1412
return;
1513
}
1614

src/utils/ast.ts

+2
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,5 @@ export function isFunction(node: ESTree.Node): node is FunctionExpression {
229229
node.type === 'FunctionExpression'
230230
);
231231
}
232+
233+
export const equalityMatchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);

test/spec/prefer-comparison-matcher.spec.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RuleTester } from 'eslint';
22
import rule from '../../src/rules/prefer-comparison-matcher';
3+
import { equalityMatchers } from '../../src/utils/ast';
34
import { runRuleTester } from '../utils/rule-tester';
45

56
const generateInvalidCases = (
@@ -248,9 +249,7 @@ const testComparisonOperator = (
248249
preferredMatcherWhenNegated: string,
249250
) => {
250251
runRuleTester(`prefer-comparision-matcher: ${operator}`, rule, {
251-
invalid: ['toBe', 'toEqual', 'toStrictEqual'].reduce<
252-
RuleTester.InvalidTestCase[]
253-
>(
252+
invalid: [...equalityMatchers.keys()].reduce<RuleTester.InvalidTestCase[]>(
254253
(cases, equalityMatcher) => [
255254
...cases,
256255
...generateInvalidCases(

0 commit comments

Comments
 (0)