Skip to content

Commit

Permalink
refactor: deduplicate isBooleanLiteral & getFirstMatcherArg utils
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jul 29, 2022
1 parent 7fae29f commit 517b0cb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 64 deletions.
19 changes: 2 additions & 17 deletions src/rules/prefer-comparison-matcher.ts
@@ -1,29 +1,14 @@
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import {
EqualityMatcher,
ParsedExpectFnCall,
createRule,
followTypeAssertionChain,
getAccessorValue,
getFirstMatcherArg,
isBooleanLiteral,
isStringNode,
parseJestFnCall,
} from './utils';

const isBooleanLiteral = (
node: TSESTree.Node,
): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';

const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
const [firstArg] = expectFnCall.args;

if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
return firstArg;
}

return followTypeAssertionChain(firstArg);
};

const isString = (node: TSESTree.Node) => {
return isStringNode(node) || node.type === AST_NODE_TYPES.TemplateLiteral;
};
Expand Down
21 changes: 3 additions & 18 deletions src/rules/prefer-equality-matcher.ts
@@ -1,29 +1,14 @@
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, TSESLint } from '@typescript-eslint/utils';
import {
EqualityMatcher,
ModifierName,
ParsedExpectFnCall,
createRule,
followTypeAssertionChain,
getAccessorValue,
getFirstMatcherArg,
isBooleanLiteral,
parseJestFnCall,
} from './utils';

const isBooleanLiteral = (
node: TSESTree.Node,
): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';

const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
const [firstArg] = expectFnCall.args;

if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
return firstArg;
}

return followTypeAssertionChain(firstArg);
};

export default createRule({
name: __filename,
meta: {
Expand Down
12 changes: 1 addition & 11 deletions src/rules/prefer-to-be.ts
Expand Up @@ -4,8 +4,8 @@ import {
EqualityMatcher,
ParsedExpectFnCall,
createRule,
followTypeAssertionChain,
getAccessorValue,
getFirstMatcherArg,
isIdentifier,
parseJestFnCall,
replaceAccessorFixer,
Expand Down Expand Up @@ -38,16 +38,6 @@ const shouldUseToBe = (expectFnCall: ParsedExpectFnCall): boolean => {
return firstArg.type === AST_NODE_TYPES.TemplateLiteral;
};

const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
const [firstArg] = expectFnCall.args;

if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
return firstArg;
}

return followTypeAssertionChain(firstArg);
};

type MessageId =
| 'useToBe'
| 'useToBeUndefined'
Expand Down
19 changes: 2 additions & 17 deletions src/rules/prefer-to-contain.ts
Expand Up @@ -4,30 +4,15 @@ import {
EqualityMatcher,
KnownCallExpression,
ModifierName,
ParsedExpectFnCall,
createRule,
followTypeAssertionChain,
getAccessorValue,
getFirstMatcherArg,
hasOnlyOneArgument,
isBooleanLiteral,
isSupportedAccessor,
parseJestFnCall,
} from './utils';

const isBooleanLiteral = (
node: TSESTree.Node,
): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';

const getFirstMatcherArg = (expectFnCall: ParsedExpectFnCall) => {
const [firstArg] = expectFnCall.args;

if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
return firstArg;
}

return followTypeAssertionChain(firstArg);
};

type FixableIncludesCallExpression = KnownCallExpression<'includes'> &
CallExpressionWithSingleArgument;

Expand Down
20 changes: 19 additions & 1 deletion src/rules/utils/misc.ts
Expand Up @@ -11,7 +11,8 @@ import {
getAccessorValue,
isSupportedAccessor,
} from './accessors';
import { isTypeOfJestFnCall } from './parseJestFnCall';
import { followTypeAssertionChain } from './followTypeAssertionChain';
import { ParsedExpectFnCall, isTypeOfJestFnCall } from './parseJestFnCall';

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

Expand Down Expand Up @@ -191,3 +192,20 @@ export const findTopMostCallExpression = (

return topMostCallExpression;
};

export const isBooleanLiteral = (
node: TSESTree.Node,
): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';

export const getFirstMatcherArg = (
expectFnCall: ParsedExpectFnCall,
): TSESTree.SpreadElement | TSESTree.Expression => {
const [firstArg] = expectFnCall.args;

if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
return firstArg;
}

return followTypeAssertionChain(firstArg);
};

0 comments on commit 517b0cb

Please sign in to comment.