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