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

create-property-order-rule.ts

121 lines3.9 KB

1import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
2​
3import { getDocsUrl } from './get-docs-url'
4import { detectTanstackQueryImports } from './detect-react-query-imports'
5import { sortDataByOrder } from './sort-data-by-order'
6import type { ExtraRuleDocs } from '../types'
7​
8const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
9​
10/**
11 * Creates a rule that reports when the properties of the options object passed
12 * to the target functions are not in the required order.
13 *
14 * @param options - The rule's metadata, such as `name`, `meta`, and
15 * `defaultOptions`.
16 * @param targetFunctions - The names of the functions whose first argument is
17 * checked.
18 * @param orderRules - Pairs of property groups: the properties of the first
19 * group must come before those of the second.
20 * @returns An ESLint rule that reports out-of-order properties and fixes them.
21 */
22export function createPropertyOrderRule<
23 TFunc extends string,
24 TProp extends string,
25>(
26 options: Omit<Parameters<typeof createRule>[0], 'create'>,
27 targetFunctions: ReadonlyArray<TFunc> | Array<TFunc>,
28 orderRules: ReadonlyArray<
29 Readonly<[ReadonlyArray<TProp>, ReadonlyArray<TProp>]>
30 >,
31) {
32 const targetFunctionSet = new Set(targetFunctions)
33 /**
34 * Checks whether a function name is one of the target functions.
35 *
36 * @param node - The function name.
37 * @returns `true` if the name is a target function.
38 */
39 function isTargetFunction(node: any): node is TFunc {
40 return targetFunctionSet.has(node)
41 }
42​
43 return createRule({
44 ...options,
45 create: detectTanstackQueryImports((context) => {
46 return {
47 CallExpression(node) {
48 if (node.callee.type !== AST_NODE_TYPES.Identifier) {
49 return
50 }
51 const functions = node.callee.name
52 if (!isTargetFunction(functions)) {
53 return
54 }
55 const argument = node.arguments[0]
56 if (argument === undefined || argument.type !== 'ObjectExpression') {
57 return
58 }
59​
60 const allProperties = argument.properties
61​
62 // no need to sort if there is at max 1 property
63 if (allProperties.length < 2) {
64 return
65 }
66​
67 const properties = allProperties.flatMap((p, index) => {
68 if (
69 p.type === AST_NODE_TYPES.Property &&
70 p.key.type === AST_NODE_TYPES.Identifier
71 ) {
72 return { name: p.key.name, property: p }
73 } else return { name: `_property_${index}`, property: p }
74 })
75​
76 const sortedProperties = sortDataByOrder(
77 properties,
78 orderRules,
79 'name',
80 )
81 if (sortedProperties === null) {
82 return
83 }
84​
85 context.report({
86 node: argument,
87 data: { function: node.callee.name },
88 messageId: 'invalidOrder',
89 fix(fixer) {
90 const sourceCode = context.sourceCode
91​
92 const reorderedText = sortedProperties.reduce(
93 (sourceText, specifier, index) => {
94 let textBetweenProperties = ''
95 if (index < allProperties.length - 1) {
96 textBetweenProperties = sourceCode
97 .getText()
98 .slice(
99 allProperties[index]!.range[1],
100 allProperties[index + 1]!.range[0],
101 )
102 }
103 return (
104 sourceText +
105 sourceCode.getText(specifier.property) +
106 textBetweenProperties
107 )
108 },
109 '',
110 )
111 return fixer.replaceTextRange(
112 [allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],
113 reorderedText,
114 )
115 },
116 })
117 },
118 }
119 }),
120 })