Skip to content

Commit

Permalink
fix(eslint-plugin-query): correctly handle call-expression spread (#8090
Browse files Browse the repository at this point in the history
)
  • Loading branch information
schiller-manuel authored Sep 24, 2024
1 parent 9da00d6 commit f1aab70
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ import {
generateInterleavedCombinations,
generatePartialCombinations,
generatePermutations,
normalizeIndent,
} from './test-utils'
import type { InfiniteQueryFunctions } from '../rules/infinite-query-property-order/constants'

const ruleTester = new RuleTester()

type CheckedProperties = (typeof checkedProperties)[number]
const orderIndependentProps = ['queryKey', '...foo'] as const
const orderIndependentProps = [
'queryKey',
'...objectExpressionSpread',
'...callExpressionSpread',
] as const
type OrderIndependentProps = (typeof orderIndependentProps)[number]

interface TestCase {
Expand Down Expand Up @@ -84,7 +89,7 @@ export function generateInvalidPermutations(
Math.abs(
invalid.indexOf('getNextPageParam') -
invalid.indexOf('getPreviousPageParam'),
) !== 1 && !invalid.includes('queryFn'),
) !== 1,
),
)
}
Expand Down Expand Up @@ -122,17 +127,26 @@ const invalidTestMatrix = combinate({
properties: interleavedInvalidPermutations,
})

const callExpressionSpread = normalizeIndent`
...communitiesQuery({
filters: {
...fieldValues,
placementFormats: [],
},
})`

function getCode({
infiniteQueryFunction: infiniteQueryFunction,
properties,
}: TestCase) {
function getPropertyCode(
property: CheckedProperties | OrderIndependentProps,
) {
if (property.startsWith('...')) {
return property
}
switch (property) {
case '...objectExpressionSpread':
return `...objectExpressionSpread`
case '...callExpressionSpread':
return callExpressionSpread
case 'queryKey':
return `queryKey: ['projects']`
case 'queryFn':
Expand All @@ -142,8 +156,6 @@ function getCode({
case 'getNextPageParam':
return 'getNextPageParam: (lastPage) => lastPage.nextId ?? undefined'
}

return `${property}: () => null`
}
return `
import { ${infiniteQueryFunction} } from '@tanstack/react-query'
Expand Down Expand Up @@ -180,3 +192,28 @@ ruleTester.run(name, rule, {
valid: validTestCases,
invalid: invalidTestCases,
})

// regression tests

const regressionTestCases = {
valid: [
{
name: 'should pass with call expression spread',
code: normalizeIndent`
import { useInfiniteQuery } from '@tanstack/react-query'
const { data, isFetching, isLoading, hasNextPage, fetchNextPage } =
useInfiniteQuery({
...communitiesQuery({
filters: {
...fieldValues,
placementFormats: [],
},
}),
refetchOnMount: false,
})`,
},
],
invalid: [],
}

ruleTester.run(name, rule, regressionTestCases)
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ export const rule = createRule({
} else if (p.type === AST_NODE_TYPES.SpreadElement) {
if (p.argument.type === AST_NODE_TYPES.Identifier) {
return { name: p.argument.name, property: p }
} else {
throw new Error('Unsupported spread element')
} else if (p.argument.type === AST_NODE_TYPES.CallExpression) {
if (p.argument.callee.type === AST_NODE_TYPES.Identifier) {
return { name: p.argument.callee.name, property: p }
}
}
throw new Error('Unsupported spread element')
}
return []
})
Expand Down

0 comments on commit f1aab70

Please sign in to comment.