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