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

index.cjs

124 lines3.7 KB

1// eslint-disable-next-line @typescript-eslint/no-var-requires
2const UnknownUsageError = require('./unknown-usage-error.cjs')
3​
4module.exports = ({ jscodeshift, utils }) => {
5 /**
6 *
7 * @param {import('jscodeshift').ObjectExpression} source
8 * @param {import('jscodeshift').ObjectExpression} target
9 * @param {(node: import('jscodeshift').Node) => boolean} predicate
10 */
11 const copyPropertiesFromSource = (source, target, predicate) => {
12 source.properties.forEach((property) => {
13 if (predicate(property)) {
14 target.properties.push(property)
15 }
16 })
17 }
18​
19 /**
20 * @param {import('jscodeshift').NodePath} path
21 * @param {string} argumentName
22 * @param {string} filePath
23 * @returns {*}
24 */
25 const getBindingFromScope = (path, argumentName, filePath) => {
26 /**
27 * If the current scope contains the declaration then we can use the actual one else we attempt to find the
28 * binding from above.
29 */
30 const scope = path.scope.declares(argumentName)
31 ? path.scope
32 : path.scope.lookup(argumentName)
33​
34 /**
35 * The declaration couldn't be found for some reason, time to move on. We warn the user it needs to be rewritten
36 * by themselves.
37 */
38 if (!scope) {
39 return undefined
40 }
41​
42 const binding = scope.bindings[argumentName]
43 .filter((item) => utils.isIdentifier(item.value))
44 .map((item) => item.parentPath.value)
45 .at(0)
46​
47 if (!binding) {
48 throw new UnknownUsageError(path.node, filePath)
49 }
50​
51 return binding
52 }
53​
54 /**
55 * @param {import('jscodeshift').VariableDeclarator} binding
56 * @returns {import('jscodeshift').Node|undefined}
57 */
58 const getInitializerByDeclarator = (binding) => {
59 const isVariableDeclaration = jscodeshift.match(binding, {
60 type: jscodeshift.VariableDeclarator.name,
61 })
62​
63 if (!isVariableDeclaration) {
64 return undefined
65 }
66​
67 const isTSAsExpression = jscodeshift.match(binding.init, {
68 type: jscodeshift.TSAsExpression.name,
69 })
70​
71 return isTSAsExpression ? binding.init.expression : binding.init
72 }
73​
74 /**
75 * @param {import('jscodeshift').Node} node
76 * @returns {boolean}
77 */
78 const isArrayExpressionVariable = (node) =>
79 jscodeshift.match(node, {
80 type: jscodeshift.VariableDeclarator.name,
81 init: {
82 type: jscodeshift.ArrayExpression.name,
83 },
84 })
85​
86 /**
87 * @param {import('jscodeshift').NodePath} path
88 * @param {import('jscodeshift').Node} node
89 * @param {"queryKey"|"mutationKey"} keyName
90 * @param {string} filePath
91 * @returns {import('jscodeshift').Property|undefined}
92 */
93 const transformArgumentToKey = (path, node, keyName, filePath) => {
94 // If the first argument is an identifier we have to infer its type if possible.
95 if (utils.isIdentifier(node)) {
96 const binding = getBindingFromScope(path, node.name, filePath)
97​
98 if (isArrayExpressionVariable(binding)) {
99 return jscodeshift.property(
100 'init',
101 jscodeshift.identifier(keyName),
102 jscodeshift.identifier(binding.id.name),
103 )
104 }
105 }
106​
107 // If the first argument is an array, then it matches the following overload:
108 // methodName(queryKey?: QueryKey, firstObject?: TFirstObject, secondObject?: TSecondObject)
109 if (utils.isArrayExpression(node)) {
110 // Then we create the 'queryKey' property based on it, because it will be passed to the first argument
111 // that should be an object according to the new signature.
112 return jscodeshift.property('init', jscodeshift.identifier(keyName), node)
113 }
114​
115 return undefined
116 }
117​
118 return {
119 copyPropertiesFromSource,
120 getInitializerByDeclarator,
121 getBindingFromScope,
122 transformArgumentToKey,
123 }