-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.ts
126 lines (119 loc) · 3.69 KB
/
index.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
import type {
Transform,
MemberExpression,
ObjectExpression,
RestElement,
JSCodeshift
} from 'jscodeshift'
type Prop = Extract<
ObjectExpression['properties'][number],
{ value: unknown }
>['value']
const createAddTagTypesCall = (
j: JSCodeshift,
object: MemberExpression['object'],
addTagTypes: Prop
) => {
const newCall = j.callExpression(
j.memberExpression(object, j.identifier('addTagTypes')),
[]
)
if (addTagTypes.type === 'Identifier') {
newCall.arguments.push(j.spreadElement(addTagTypes))
} else if (addTagTypes.type === 'ArrayExpression') {
newCall.arguments = addTagTypes.elements.filter(
(el): el is Exclude<typeof el, RestElement | null> =>
!!(el && el.type !== 'RestElement')
)
}
return newCall
}
const transform: Transform = (file, api) => {
const j = api.jscodeshift
const root = j(file.source)
return root
.find(j.CallExpression, {
callee: {
property: {
name: 'enhanceEndpoints'
}
}
})
.forEach((path) => {
const callee = path.value.callee as MemberExpression
const [config] = path.value.arguments
if (config.type === 'ObjectExpression') {
let addTagTypes: Prop | undefined = undefined
let endpoints: Prop | undefined = undefined
for (const property of config.properties) {
if (
(property.type === 'ObjectProperty' ||
property.type === 'Property') &&
property.key.type === 'Identifier'
) {
switch (property.key.name) {
case 'addTagTypes':
addTagTypes = property.value
break
case 'endpoints':
endpoints = property.value
break
}
}
}
if (!endpoints) {
if (!addTagTypes) {
return
}
// no endpoints - we can go ahead and replace
path.replace(createAddTagTypesCall(j, callee.object, addTagTypes))
} else {
let calleeObject = addTagTypes
? createAddTagTypesCall(j, callee.object, addTagTypes)
: callee.object
if (endpoints.type === 'ObjectExpression') {
for (const endpointProp of endpoints.properties) {
if (endpointProp.type === 'ObjectProperty') {
const endpointName =
endpointProp.key.type === 'Identifier' &&
!endpointProp.computed
? j.stringLiteral(endpointProp.key.name)
: endpointProp.key
calleeObject = j.callExpression(
j.memberExpression(
calleeObject,
j.identifier('enhanceEndpoint')
),
[endpointName, endpointProp.value as any]
)
} else if (endpointProp.type === 'ObjectMethod') {
const endpointName =
endpointProp.key.type === 'Identifier'
? j.stringLiteral(endpointProp.key.name)
: endpointProp.key
calleeObject = j.callExpression(
j.memberExpression(
calleeObject,
j.identifier('enhanceEndpoint')
),
[
endpointName,
j.arrowFunctionExpression(
endpointProp.params,
endpointProp.body
)
]
)
}
}
}
path.replace(calleeObject)
}
}
})
.toSource({
arrowParensAlways: true
})
}
export const parser = 'tsx'
export default transform