Skip to content

Commit

Permalink
refactor: use flatMap instead of reduce (#1541)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 29, 2024
1 parent 8bba541 commit 295120e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 76 deletions.
9 changes: 3 additions & 6 deletions src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ describe('rules', () => {
expect(Object.keys(recommendedConfigs['flat/all'].rules)).toHaveLength(
ruleNames.length - deprecatedRules.length,
);
const allConfigRules = Object.values(recommendedConfigs)
.map(config => Object.keys(config.rules ?? {}))
.reduce((previousValue, currentValue) => [
...previousValue,
...currentValue,
]);
const allConfigRules = Object.values(recommendedConfigs).flatMap(config =>
Object.keys(config.rules ?? {}),
);

allConfigRules.forEach(rule => {
const ruleNamePrefix = 'jest/';
Expand Down
28 changes: 12 additions & 16 deletions src/rules/__tests__/no-deprecated-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,26 @@ describe('the rule', () => {
valid: [
'jest',
'require("fs")',
...allowedFunctions
.map(func => generateValidCases(jestVersion, func))
.reduce((acc, arr) => acc.concat(arr), []),
...allowedFunctions.flatMap(func =>
generateValidCases(jestVersion, func),
),
],
invalid: deprecations
.map(([, deprecation, replacement]) =>
generateInvalidCases(jestVersion, deprecation, replacement),
)
.reduce((acc, arr) => acc.concat(arr), []),
invalid: deprecations.flatMap(([, deprecation, replacement]) =>
generateInvalidCases(jestVersion, deprecation, replacement),
),
});

ruleTester.run('detected jest version', rule, {
valid: [
'jest',
'require("fs")',
...allowedFunctions
.map(func => generateValidCases(undefined, func))
.reduce((acc, arr) => acc.concat(arr), []),
...allowedFunctions.flatMap(func =>
generateValidCases(undefined, func),
),
],
invalid: deprecations
.map(([, deprecation, replacement]) =>
generateInvalidCases(undefined, deprecation, replacement),
)
.reduce((acc, arr) => acc.concat(arr), []),
invalid: deprecations.flatMap(([, deprecation, replacement]) =>
generateInvalidCases(undefined, deprecation, replacement),
),
});
});

Expand Down
70 changes: 27 additions & 43 deletions src/rules/__tests__/prefer-comparison-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,30 +205,24 @@ const generateValidStringLiteralCases = (operator: string, matcher: string) => {
['x', "'y'"],
['x', '`y`'],
['x', '`y${z}`'],
].reduce(
(cases, [a, b]) => [
...cases,
...[
`expect(${a} ${operator} ${b}).${matcher}(true)`,
`expect(${a} ${operator} ${b}).${matcher}(false)`,
`expect(${a} ${operator} ${b}).not.${matcher}(true)`,
`expect(${a} ${operator} ${b}).not.${matcher}(false)`,
`expect(${a} ${operator} ${b}).resolves.${matcher}(true)`,
`expect(${a} ${operator} ${b}).resolves.${matcher}(false)`,
`expect(${a} ${operator} ${b}).resolves.not.${matcher}(true)`,
`expect(${a} ${operator} ${b}).resolves.not.${matcher}(false)`,
`expect(${b} ${operator} ${a}).resolves.not.${matcher}(false)`,
`expect(${b} ${operator} ${a}).resolves.not.${matcher}(true)`,
`expect(${b} ${operator} ${a}).resolves.${matcher}(false)`,
`expect(${b} ${operator} ${a}).resolves.${matcher}(true)`,
`expect(${b} ${operator} ${a}).not.${matcher}(false)`,
`expect(${b} ${operator} ${a}).not.${matcher}(true)`,
`expect(${b} ${operator} ${a}).${matcher}(false)`,
`expect(${b} ${operator} ${a}).${matcher}(true)`,
],
],
[],
);
].flatMap(([a, b]) => [
`expect(${a} ${operator} ${b}).${matcher}(true)`,
`expect(${a} ${operator} ${b}).${matcher}(false)`,
`expect(${a} ${operator} ${b}).not.${matcher}(true)`,
`expect(${a} ${operator} ${b}).not.${matcher}(false)`,
`expect(${a} ${operator} ${b}).resolves.${matcher}(true)`,
`expect(${a} ${operator} ${b}).resolves.${matcher}(false)`,
`expect(${a} ${operator} ${b}).resolves.not.${matcher}(true)`,
`expect(${a} ${operator} ${b}).resolves.not.${matcher}(false)`,
`expect(${b} ${operator} ${a}).resolves.not.${matcher}(false)`,
`expect(${b} ${operator} ${a}).resolves.not.${matcher}(true)`,
`expect(${b} ${operator} ${a}).resolves.${matcher}(false)`,
`expect(${b} ${operator} ${a}).resolves.${matcher}(true)`,
`expect(${b} ${operator} ${a}).not.${matcher}(false)`,
`expect(${b} ${operator} ${a}).not.${matcher}(true)`,
`expect(${b} ${operator} ${a}).${matcher}(false)`,
`expect(${b} ${operator} ${a}).${matcher}(true)`,
]);
};

const testComparisonOperator = (
Expand All @@ -244,27 +238,17 @@ const testComparisonOperator = (
`expect(value).${preferredMatcherWhenNegated}(1);`,
`expect(value).not.${preferredMatcher}(1);`,
`expect(value).not.${preferredMatcherWhenNegated}(1);`,
...['toBe', 'toEqual', 'toStrictEqual'].reduce<string[]>(
(cases, equalityMatcher) => [
...cases,
...generateValidStringLiteralCases(operator, equalityMatcher),
],
[],
...['toBe', 'toEqual', 'toStrictEqual'].flatMap(equalityMatcher =>
generateValidStringLiteralCases(operator, equalityMatcher),
),
],
invalid: ['toBe', 'toEqual', 'toStrictEqual'].reduce<
Array<TSESLint.InvalidTestCase<'useToBeComparison', never>>
>(
(cases, equalityMatcher) => [
...cases,
...generateInvalidCases(
operator,
equalityMatcher,
preferredMatcher,
preferredMatcherWhenNegated,
),
],
[],
invalid: ['toBe', 'toEqual', 'toStrictEqual'].flatMap(equalityMatcher =>
generateInvalidCases(
operator,
equalityMatcher,
preferredMatcher,
preferredMatcherWhenNegated,
),
),
});
};
Expand Down
18 changes: 7 additions & 11 deletions src/rules/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,14 @@ export const getTestCallExpressionsFromDeclaredVariables = (
declaredVariables: readonly TSESLint.Scope.Variable[],
context: TSESLint.RuleContext<string, unknown[]>,
): TSESTree.CallExpression[] => {
return declaredVariables.reduce<TSESTree.CallExpression[]>(
(acc, { references }) =>
acc.concat(
references
.map(({ identifier }) => identifier.parent)
.filter(
(node): node is TSESTree.CallExpression =>
node?.type === AST_NODE_TYPES.CallExpression &&
isTypeOfJestFnCall(node, context, ['test']),
),
return declaredVariables.flatMap(({ references }) =>
references
.map(({ identifier }) => identifier.parent)
.filter(
(node): node is TSESTree.CallExpression =>
node?.type === AST_NODE_TYPES.CallExpression &&
isTypeOfJestFnCall(node, context, ['test']),
),
[],
);
};

Expand Down

0 comments on commit 295120e

Please sign in to comment.