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

index.cjs

122 lines3.6 KB

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