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

key-replacer.js

160 lines5.0 KB

1// eslint-disable-next-line @typescript-eslint/no-var-requires
2const UnprocessableKeyError = require('../unprocessable-key-error')
3​
4module.exports = ({ jscodeshift, root, filePath, keyName = 'queryKey' }) => {
5 const isArrayExpression = (node) =>
6 jscodeshift.match(node, { type: jscodeshift.ArrayExpression.name })
7​
8 const isStringLiteral = (node) =>
9 jscodeshift.match(node, { type: jscodeshift.StringLiteral.name }) ||
10 jscodeshift.match(node, { type: jscodeshift.Literal.name })
11​
12 const isTemplateLiteral = (node) =>
13 jscodeshift.match(node, { type: jscodeshift.TemplateLiteral.name })
14​
15 const findVariableDeclaration = (node) => {
16 const declarations = root
17 .find(jscodeshift.VariableDeclarator, {
18 id: {
19 type: jscodeshift.Identifier.name,
20 name: node.name,
21 },
22 })
23 .paths()
24​
25 return declarations.length > 0 ? declarations[0] : null
26 }
27​
28 const createKeyValue = (node) => {
29 // When the node is a string literal we convert it into an array of strings.
30 if (isStringLiteral(node)) {
31 return jscodeshift.arrayExpression([
32 jscodeshift.stringLiteral(node.value),
33 ])
34 }
35​
36 // When the node is a template literal we convert it into an array of template literals.
37 if (isTemplateLiteral(node)) {
38 return jscodeshift.arrayExpression([
39 jscodeshift.templateLiteral(node.quasis, node.expressions),
40 ])
41 }
42​
43 if (jscodeshift.match(node, { type: jscodeshift.Identifier.name })) {
44 // When the node is an identifier at first, we try to find its declaration, because we will try
45 // to guess its type.
46 const variableDeclaration = findVariableDeclaration(node)
47​
48 if (!variableDeclaration) {
49 throw new UnprocessableKeyError(
50 `In file ${filePath} at line ${node.loc.start.line} the type of identifier \`${node.name}\` couldn't be recognized, so the codemod couldn't be applied. Please migrate manually.`,
51 )
52 }
53​
54 const initializer = variableDeclaration.value.init
55​
56 // When it's a string, we just wrap it into an array expression.
57 if (isStringLiteral(initializer) || isTemplateLiteral(initializer)) {
58 return jscodeshift.arrayExpression([node])
59 }
60 }
61​
62 throw new UnprocessableKeyError(
63 `In file ${filePath} at line ${node.loc.start.line} the type of the \`${keyName}\` couldn't be recognized, so the codemod couldn't be applied. Please migrate manually.`,
64 )
65 }
66​
67 const createKeyProperty = (node) =>
68 jscodeshift.property(
69 'init',
70 jscodeshift.identifier(keyName),
71 createKeyValue(node),
72 )
73​
74 const getPropertyFromObjectExpression = (objectExpression, propertyName) =>
75 objectExpression.properties.find(
76 (property) => property.key.name === propertyName,
77 ) ?? null
78​
79 const buildWithTypeArguments = (node, builder) => {
80 const newNode = builder(node)
81​
82 if (node.typeParameters) {
83 newNode.typeArguments = node.typeParameters
84 }
85​
86 return newNode
87 }
88​
89 return ({ node }) => {
90 // When the node doesn't have the 'original' property, that means the codemod has been already applied,
91 // so we don't need to do any changes.
92 if (!node.original) {
93 return node
94 }
95​
96 const methodArguments = node.arguments
97​
98 // The method call doesn't have any arguments, we have nothing to do in this case.
99 if (methodArguments.length === 0) {
100 return node
101 }
102​
103 try {
104 const [firstArgument, ...restOfTheArguments] = methodArguments
105​
106 if (
107 jscodeshift.match(firstArgument, {
108 type: jscodeshift.ObjectExpression.name,
109 })
110 ) {
111 const originalKey = getPropertyFromObjectExpression(
112 firstArgument,
113 keyName,
114 )
115​
116 if (!originalKey) {
117 throw new UnprocessableKeyError(
118 `In file ${filePath} at line ${node.loc.start.line} the \`${keyName}\` couldn't be found. Did you forget to add it?`,
119 )
120 }
121​
122 const restOfTheProperties = firstArgument.properties.filter(
123 (item) => item.key.name !== keyName,
124 )
125​
126 return buildWithTypeArguments(node, (originalNode) =>
127 jscodeshift.callExpression(originalNode.original.callee, [
128 jscodeshift.objectExpression([
129 createKeyProperty(originalKey.value),
130 ...restOfTheProperties,
131 ]),
132 ...restOfTheArguments,
133 ]),
134 )
135 }
136​
137 // When the node is an array expression we just simply return it because we want query keys to be arrays.
138 if (isArrayExpression(firstArgument)) {
139 return node
140 }
141​
142 return buildWithTypeArguments(node, (originalNode) =>
143 jscodeshift.callExpression(originalNode.original.callee, [
144 createKeyValue(firstArgument),
145 ...restOfTheArguments,
146 ]),
147 )
148 } catch (error) {
149 if (error.name === 'UnprocessableKeyError') {
150 if (process.env.NODE_ENV !== 'test') {
151 console.warn(error.message)
152 }
153​
154 return node
155 }
156​
157 throw error
158 }
159 }