From 517b0cbab45fd3338594123a9f68c128fe29d122 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 30 Jul 2022 08:36:18 +1200 Subject: [PATCH] refactor: deduplicate `isBooleanLiteral` & `getFirstMatcherArg` utils --- src/rules/prefer-comparison-matcher.ts | 19 ++----------------- src/rules/prefer-equality-matcher.ts | 21 +++------------------ src/rules/prefer-to-be.ts | 12 +----------- src/rules/prefer-to-contain.ts | 19 ++----------------- src/rules/utils/misc.ts | 20 +++++++++++++++++++- 5 files changed, 27 insertions(+), 64 deletions(-) diff --git a/src/rules/prefer-comparison-matcher.ts b/src/rules/prefer-comparison-matcher.ts index eb12e4f70..d46362f80 100644 --- a/src/rules/prefer-comparison-matcher.ts +++ b/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; }; diff --git a/src/rules/prefer-equality-matcher.ts b/src/rules/prefer-equality-matcher.ts index 35d74082d..4b1fa8e3c 100644 --- a/src/rules/prefer-equality-matcher.ts +++ b/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: { diff --git a/src/rules/prefer-to-be.ts b/src/rules/prefer-to-be.ts index 0111222b7..b4991d687 100644 --- a/src/rules/prefer-to-be.ts +++ b/src/rules/prefer-to-be.ts @@ -4,8 +4,8 @@ import { EqualityMatcher, ParsedExpectFnCall, createRule, - followTypeAssertionChain, getAccessorValue, + getFirstMatcherArg, isIdentifier, parseJestFnCall, replaceAccessorFixer, @@ -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' diff --git a/src/rules/prefer-to-contain.ts b/src/rules/prefer-to-contain.ts index 691065956..f8af5e1da 100644 --- a/src/rules/prefer-to-contain.ts +++ b/src/rules/prefer-to-contain.ts @@ -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; diff --git a/src/rules/utils/misc.ts b/src/rules/utils/misc.ts index 47b1d922f..109a51781 100644 --- a/src/rules/utils/misc.ts +++ b/src/rules/utils/misc.ts @@ -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'; @@ -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); +};