Skip to content

Commit 2bb0737

Browse files
committed
refactor: deduplicate isBooleanLiteral & getFirstMatcherArg utils
1 parent 6d7142b commit 2bb0737

File tree

5 files changed

+27
-64
lines changed

5 files changed

+27
-64
lines changed

src/rules/prefer-comparison-matcher.ts

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
11
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
22
import {
33
EqualityMatcher,
4-
ParsedExpectFnCall,
54
createRule,
6-
followTypeAssertionChain,
75
getAccessorValue,
6+
getFirstMatcherArg,
7+
isBooleanLiteral,
88
isStringNode,
99
parseJestFnCall,
1010
} from './utils';
1111

12-
const isBooleanLiteral = (
13-
node: TSESTree.Node,
14-
): node is TSESTree.BooleanLiteral =>
15-
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
16-
17-
const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
18-
const [firstArg] = expectFnCall.args;
19-
20-
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
21-
return firstArg;
22-
}
23-
24-
return followTypeAssertionChain(firstArg);
25-
};
26-
2712
const isString = (node: TSESTree.Node) => {
2813
return isStringNode(node) || node.type === AST_NODE_TYPES.TemplateLiteral;
2914
};

src/rules/prefer-equality-matcher.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
1+
import { AST_NODE_TYPES, TSESLint } from '@typescript-eslint/utils';
22
import {
33
EqualityMatcher,
44
ModifierName,
5-
ParsedExpectFnCall,
65
createRule,
7-
followTypeAssertionChain,
86
getAccessorValue,
7+
getFirstMatcherArg,
8+
isBooleanLiteral,
99
parseJestFnCall,
1010
} from './utils';
1111

12-
const isBooleanLiteral = (
13-
node: TSESTree.Node,
14-
): node is TSESTree.BooleanLiteral =>
15-
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
16-
17-
const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
18-
const [firstArg] = expectFnCall.args;
19-
20-
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
21-
return firstArg;
22-
}
23-
24-
return followTypeAssertionChain(firstArg);
25-
};
26-
2712
export default createRule({
2813
name: __filename,
2914
meta: {

src/rules/prefer-to-be.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
EqualityMatcher,
55
ParsedExpectFnCall,
66
createRule,
7-
followTypeAssertionChain,
87
getAccessorValue,
8+
getFirstMatcherArg,
99
isIdentifier,
1010
parseJestFnCall,
1111
replaceAccessorFixer,
@@ -38,16 +38,6 @@ const shouldUseToBe = (expectFnCall: ParsedExpectFnCall): boolean => {
3838
return firstArg.type === AST_NODE_TYPES.TemplateLiteral;
3939
};
4040

41-
const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
42-
const [firstArg] = expectFnCall.args;
43-
44-
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
45-
return firstArg;
46-
}
47-
48-
return followTypeAssertionChain(firstArg);
49-
};
50-
5141
type MessageId =
5242
| 'useToBe'
5343
| 'useToBeUndefined'

src/rules/prefer-to-contain.ts

+2-17
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@ import {
44
EqualityMatcher,
55
KnownCallExpression,
66
ModifierName,
7-
ParsedExpectFnCall,
87
createRule,
9-
followTypeAssertionChain,
108
getAccessorValue,
9+
getFirstMatcherArg,
1110
hasOnlyOneArgument,
11+
isBooleanLiteral,
1212
isSupportedAccessor,
1313
parseJestFnCall,
1414
} from './utils';
1515

16-
const isBooleanLiteral = (
17-
node: TSESTree.Node,
18-
): node is TSESTree.BooleanLiteral =>
19-
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
20-
21-
const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
22-
const [firstArg] = expectFnCall.args;
23-
24-
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
25-
return firstArg;
26-
}
27-
28-
return followTypeAssertionChain(firstArg);
29-
};
30-
3116
type FixableIncludesCallExpression = KnownCallExpression<'includes'> &
3217
CallExpressionWithSingleArgument;
3318

src/rules/utils/misc.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
getAccessorValue,
1212
isSupportedAccessor,
1313
} from './accessors';
14-
import { isTypeOfJestFnCall } from './parseJestFnCall';
14+
import { followTypeAssertionChain } from './followTypeAssertionChain';
15+
import { ParsedExpectFnCall, isTypeOfJestFnCall } from './parseJestFnCall';
1516

1617
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
1718

@@ -191,3 +192,20 @@ export const findTopMostCallExpression = (
191192

192193
return topMostCallExpression;
193194
};
195+
196+
export const isBooleanLiteral = (
197+
node: TSESTree.Node,
198+
): node is TSESTree.BooleanLiteral =>
199+
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
200+
201+
export const getFirstMatcherArg = (
202+
expectFnCall: ParsedExpectFnCall,
203+
): TSESTree.SpreadElement | TSESTree.Expression => {
204+
const [firstArg] = expectFnCall.args;
205+
206+
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
207+
return firstArg;
208+
}
209+
210+
return followTypeAssertionChain(firstArg);
211+
};

0 commit comments

Comments
 (0)