File size: 3,263 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'

import { getDocsUrl } from './get-docs-url'
import { detectTanstackQueryImports } from './detect-react-query-imports'
import { sortDataByOrder } from './sort-data-by-order'
import type { ExtraRuleDocs } from '../types'

const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)

export function createPropertyOrderRule<
  TFunc extends string,
  TProp extends string,
>(
  options: Omit<Parameters<typeof createRule>[0], 'create'>,
  targetFunctions: ReadonlyArray<TFunc> | Array<TFunc>,
  orderRules: ReadonlyArray<
    Readonly<[ReadonlyArray<TProp>, ReadonlyArray<TProp>]>
  >,
) {
  const targetFunctionSet = new Set(targetFunctions)
  function isTargetFunction(node: any): node is TFunc {
    return targetFunctionSet.has(node)
  }

  return createRule({
    ...options,
    create: detectTanstackQueryImports((context) => {
      return {
        CallExpression(node) {
          if (node.callee.type !== AST_NODE_TYPES.Identifier) {
            return
          }
          const functions = node.callee.name
          if (!isTargetFunction(functions)) {
            return
          }
          const argument = node.arguments[0]
          if (argument === undefined || argument.type !== 'ObjectExpression') {
            return
          }

          const allProperties = argument.properties

          // no need to sort if there is at max 1 property
          if (allProperties.length < 2) {
            return
          }

          const properties = allProperties.flatMap((p, index) => {
            if (
              p.type === AST_NODE_TYPES.Property &&
              p.key.type === AST_NODE_TYPES.Identifier
            ) {
              return { name: p.key.name, property: p }
            } else return { name: `_property_${index}`, property: p }
          })

          const sortedProperties = sortDataByOrder(
            properties,
            orderRules,
            'name',
          )
          if (sortedProperties === null) {
            return
          }

          context.report({
            node: argument,
            data: { function: node.callee.name },
            messageId: 'invalidOrder',
            fix(fixer) {
              const sourceCode = context.sourceCode

              const reorderedText = sortedProperties.reduce(
                (sourceText, specifier, index) => {
                  let textBetweenProperties = ''
                  if (index < allProperties.length - 1) {
                    textBetweenProperties = sourceCode
                      .getText()
                      .slice(
                        allProperties[index]!.range[1],
                        allProperties[index + 1]!.range[0],
                      )
                  }
                  return (
                    sourceText +
                    sourceCode.getText(specifier.property) +
                    textBetweenProperties
                  )
                },
                '',
              )
              return fixer.replaceTextRange(
                [allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],
                reorderedText,
              )
            },
          })
        },
      }
    }),
  })
}