-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathurl-to-withrouter.ts
393 lines (345 loc) · 11.8 KB
/
url-to-withrouter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// One-time usage file. You can delete me after running the codemod!
function addWithRouterImport(j, root) {
// We create an import specifier, this is the value of an import, eg:
// import {withRouter} from 'next/router
// The specifier would be `withRouter`
const withRouterSpecifier = j.importSpecifier(j.identifier('withRouter'))
// Check if this file is already import `next/router`
// so that we can just attach `withRouter` instead of creating a new `import` node
const originalRouterImport = root.find(j.ImportDeclaration, {
source: {
value: 'next/router',
},
})
if (originalRouterImport.length > 0) {
// Check if `withRouter` is already imported. In that case we don't have to do anything
if (
originalRouterImport.find(j.ImportSpecifier, {
imported: { name: 'withRouter' },
}).length > 0
) {
return
}
// Attach `withRouter` to the existing `next/router` import node
originalRouterImport.forEach((node) => {
node.value.specifiers.push(withRouterSpecifier)
})
return
}
// Create import node
// import {withRouter} from 'next/router'
const withRouterImport = j.importDeclaration(
[withRouterSpecifier],
j.stringLiteral('next/router')
)
// Find the Program, this is the top level AST node
const Program = root.find(j.Program)
// Attach the import at the top of the body
Program.forEach((node) => {
node.value.body.unshift(withRouterImport)
})
}
function getThisPropsUrlNodes(j, tree) {
return tree.find(j.MemberExpression, {
object: {
type: 'MemberExpression',
object: { type: 'ThisExpression' },
property: { name: 'props' },
},
property: { name: 'url' },
})
}
function getPropsUrlNodes(j, tree, name) {
return tree.find(j.MemberExpression, {
object: { name },
property: { name: 'url' },
})
}
// Wraps the provided node in a function call
// For example if `functionName` is `withRouter` it will wrap the provided node in `withRouter(NODE_CONTENT)`
function wrapNodeInFunction(j, functionName, args) {
const mappedArgs = args.map((node) => {
// If the node is a ClassDeclaration we have to turn it into a ClassExpression
// since ClassDeclarations can't be wrapped in a function
if (node.type === 'ClassDeclaration') {
node.type = 'ClassExpression'
}
return node
})
return j.callExpression(j.identifier(functionName), mappedArgs)
}
function turnUrlIntoRouter(j, tree) {
tree.find(j.Identifier, { name: 'url' }).replaceWith(j.identifier('router'))
}
export default function transformer(file, api) {
// j is just a shorthand for the jscodeshift api
const j = api.jscodeshift
// this is the AST root on which we can call methods like `.find`
const root = j(file.source)
// We search for `export default`
const defaultExports = root.find(j.ExportDefaultDeclaration)
// We loop over the `export default` instances
// This is just how jscodeshift works, there can only be one export default instance
defaultExports.forEach((rule) => {
// rule.value is an AST node
const { value: node } = rule
// declaration holds the AST node for what comes after `export default`
const { declaration } = node
function wrapDefaultExportInWithRouter() {
if (
j(rule).find(j.CallExpression, { callee: { name: 'withRouter' } })
.length > 0
) {
return
}
j(rule).replaceWith(
j.exportDefaultDeclaration(
wrapNodeInFunction(j, 'withRouter', [declaration])
)
)
}
// The `Identifier` type is given in this case:
// export default Test
// where `Test` is the identifier
if (declaration.type === 'Identifier') {
// the variable name
const { name } = declaration
// find the implementation of the variable, can be a class, function, etc
let implementation = root.find(j.Declaration, { id: { name } })
if (implementation.length === 0) {
implementation = root.find(j.VariableDeclarator, { id: { name } })
}
implementation
.find(j.Property, { key: { name: 'url' } })
.forEach((propertyRule) => {
const isThisPropsDestructure = j(propertyRule).closest(
j.VariableDeclarator,
{
init: {
object: {
type: 'ThisExpression',
},
property: { name: 'props' },
},
}
)
if (isThisPropsDestructure.length === 0) {
return
}
const originalKeyValue = propertyRule.value.value.name
propertyRule.value.key.name = 'router'
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
// If the property is reassigned to another variable we don't have to transform it
if (originalKeyValue !== 'url') {
return
}
propertyRule.value.value.name = 'router'
j(propertyRule)
.closest(j.BlockStatement)
.find(j.Identifier, (identifierNode) => {
if (identifierNode.type === 'JSXIdentifier') {
return false
}
if (identifierNode.name !== 'url') {
return false
}
return true
})
.replaceWith(j.identifier('router'))
})
// Find usage of `this.props.url`
const thisPropsUrlUsage = getThisPropsUrlNodes(j, implementation)
if (thisPropsUrlUsage.length === 0) {
return
}
// rename `url` to `router`
turnUrlIntoRouter(j, thisPropsUrlUsage)
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
return
}
const arrowFunctions = j(rule).find(j.ArrowFunctionExpression)
;(() => {
if (arrowFunctions.length === 0) {
return
}
arrowFunctions.forEach((r) => {
// This makes sure we don't match nested functions, only the top one
if (j(r).closest(j.Expression).length !== 0) {
return
}
if (!r.value.params || !r.value.params[0]) {
return
}
const name = r.value.params[0].name
const propsUrlUsage = getPropsUrlNodes(j, j(r), name)
if (propsUrlUsage.length === 0) {
return
}
turnUrlIntoRouter(j, propsUrlUsage)
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
})
return
})()
if (declaration.type === 'CallExpression') {
j(rule)
.find(j.CallExpression, (haystack) => {
const firstArgument = haystack.arguments[0] || {}
if (firstArgument.type === 'Identifier') {
return true
}
return false
})
.forEach((callRule) => {
const { name } = callRule.value.arguments[0]
// find the implementation of the variable, can be a class, function, etc
let implementation = root.find(j.Declaration, { id: { name } })
if (implementation.length === 0) {
implementation = root.find(j.VariableDeclarator, { id: { name } })
}
// Find usage of `this.props.url`
const thisPropsUrlUsage = getThisPropsUrlNodes(j, implementation)
implementation
.find(j.Property, { key: { name: 'url' } })
.forEach((propertyRule) => {
const isThisPropsDestructure = j(propertyRule).closest(
j.VariableDeclarator,
{
init: {
object: {
type: 'ThisExpression',
},
property: { name: 'props' },
},
}
)
if (isThisPropsDestructure.length === 0) {
return
}
const originalKeyValue = propertyRule.value.value.name
propertyRule.value.key.name = 'router'
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
// If the property is reassigned to another variable we don't have to transform it
if (originalKeyValue !== 'url') {
return
}
propertyRule.value.value.name = 'router'
j(propertyRule)
.closest(j.BlockStatement)
.find(j.Identifier, (identifierNode) => {
if (identifierNode.type === 'JSXIdentifier') {
return false
}
if (identifierNode.name !== 'url') {
return false
}
return true
})
.replaceWith(j.identifier('router'))
})
if (thisPropsUrlUsage.length === 0) {
return
}
// rename `url` to `router`
turnUrlIntoRouter(j, thisPropsUrlUsage)
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
return
})
}
j(rule)
.find(j.Property, { key: { name: 'url' } })
.forEach((propertyRule) => {
const isThisPropsDestructure = j(propertyRule).closest(
j.VariableDeclarator,
{
init: {
object: {
type: 'ThisExpression',
},
property: { name: 'props' },
},
}
)
if (isThisPropsDestructure.length === 0) {
return
}
const originalKeyValue = propertyRule.value.value.name
propertyRule.value.key.name = 'router'
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
// If the property is reassigned to another variable we don't have to transform it
if (originalKeyValue !== 'url') {
return
}
propertyRule.value.value.name = 'router'
j(propertyRule)
.closest(j.BlockStatement)
.find(j.Identifier, (identifierNode) => {
if (identifierNode.type === 'JSXIdentifier') {
return false
}
if (identifierNode.name !== 'url') {
return false
}
return true
})
.replaceWith(j.identifier('router'))
})
j(rule)
.find(j.MethodDefinition, { key: { name: 'componentWillReceiveProps' } })
.forEach((methodRule) => {
const func = methodRule.value.value
if (!func.params[0]) {
return
}
const firstArgumentName = func.params[0].name
const propsUrlUsage = getPropsUrlNodes(
j,
j(methodRule),
firstArgumentName
)
turnUrlIntoRouter(j, propsUrlUsage)
if (propsUrlUsage.length === 0) {
return
}
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
})
j(rule)
.find(j.MethodDefinition, { key: { name: 'componentDidUpdate' } })
.forEach((methodRule) => {
const func = methodRule.value.value
if (!func.params[0]) {
return
}
const firstArgumentName = func.params[0].name
const propsUrlUsage = getPropsUrlNodes(
j,
j(methodRule),
firstArgumentName
)
turnUrlIntoRouter(j, propsUrlUsage)
if (propsUrlUsage.length === 0) {
return
}
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
})
const thisPropsUrlUsage = getThisPropsUrlNodes(j, j(rule))
const propsUrlUsage = getPropsUrlNodes(j, j(rule), 'props')
// rename `url` to `router`
turnUrlIntoRouter(j, thisPropsUrlUsage)
turnUrlIntoRouter(j, propsUrlUsage)
if (thisPropsUrlUsage.length === 0 && propsUrlUsage.length === 0) {
return
}
wrapDefaultExportInWithRouter()
addWithRouterImport(j, root)
return
})
return root.toSource()
}