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