Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

no-rest-destructuring.utils.ts

60 lines1.9 KB

1import { AST_NODE_TYPES } from '@typescript-eslint/utils'
2import type {
3 ParserServices,
4 ParserServicesWithTypeInformation,
5 TSESTree,
6} from '@typescript-eslint/utils'
7​
8type TypeChecker = ReturnType<
9 ParserServicesWithTypeInformation['program']['getTypeChecker']
11type Type = ReturnType<TypeChecker['getTypeAtLocation']>
12​
13const QUERY_RESULT_TYPE_NAMES = new Set([
14 'UseBaseQueryResult',
15 'UseQueryResult',
16 'UseSuspenseQueryResult',
17 'DefinedUseQueryResult',
18 'UseInfiniteQueryResult',
19 'UseSuspenseInfiniteQueryResult',
20 'DefinedUseInfiniteQueryResult',
21 'QueryObserverResult',
22 'InfiniteQueryObserverResult',
23])
24​
25/**
26 * Checks whether a type is one of the query result types, or a union that contains one.
27 * @param type - The type to check.
28 * @returns `true` if the type's alias or symbol name is a known query result type.
29 */
30function isQueryResultType(type: Type): boolean {
31 if (type.aliasSymbol && QUERY_RESULT_TYPE_NAMES.has(type.aliasSymbol.name)) {
32 return true
33 }
34 const symbol = type.getSymbol()
35 if (symbol && QUERY_RESULT_TYPE_NAMES.has(symbol.name)) {
36 return true
37 }
38 return type.isUnion() && type.types.some(isQueryResultType)
40​
41export const NoRestDestructuringUtils = {
42 isObjectRestDestructuring(node: TSESTree.Node): boolean {
43 if (node.type !== AST_NODE_TYPES.ObjectPattern) {
44 return false
45 }
46 return node.properties.some((p) => p.type === AST_NODE_TYPES.RestElement)
47 },
48 isQueryResultCall(
49 node: TSESTree.CallExpression,
50 parserServices: Partial<ParserServices> | null | undefined,
51 ): boolean {
52 if (!parserServices?.program || !parserServices.esTreeNodeToTSNodeMap) {
53 return false
54 }
55 const checker = parserServices.program.getTypeChecker()
56 const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.callee)
57 const signatures = checker.getTypeAtLocation(tsNode).getCallSignatures()
58 return signatures.some((sig) => isQueryResultType(sig.getReturnType()))
59 },