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

index.cjs

123 lines3.6 KB

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