-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransform.ts
More file actions
297 lines (291 loc) · 12.2 KB
/
transform.ts
File metadata and controls
297 lines (291 loc) · 12.2 KB
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
import {JSCodeshift, Transform} from "jscodeshift/src/core";
function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const transform: Transform = (file, api) => {
const j = api.jscodeshift;
const {statement} = j.template;
const root = j(file.source);
const objects = root
.find(j.CallExpression).filter(p => {
const callee = p.value.callee;
return callee.type === 'Identifier' && (callee.name === 'objectType' || callee.name === 'interfaceType' || callee.name === 'inputObjectType');
});
const enums = root
.find(j.CallExpression).filter(p => {
const callee = p.value.callee;
return callee.type === 'Identifier' && callee.name === 'enumType';
});
const queriesMutations = root
.find(j.CallExpression).filter(p => {
const callee = p.value.callee;
const functionNames = ['mutationField', 'queryField'];
return callee.type === 'Identifier' && functionNames.includes(callee.name) && p.value.arguments[0].type === 'StringLiteral';
});
const relayQueries = root
.find(j.CallExpression).filter(p => {
const callee = p.value.callee;
const functionNames = ['mutationField', 'queryField'];
// matches arrow functions
return callee.type === 'Identifier' && functionNames.includes(callee.name) && p.value.arguments[0].type === 'ArrowFunctionExpression';
});
enums.replaceWith(p => {
const name = p.value.arguments[0].properties.find((p: any) => p.key.name === 'name').value;
return statement`builder.enumType(${name}, {
values: ${p.value.arguments[0].properties.find((p: any) => p.key.name === 'members').value} as const
})`;
})
queriesMutations.replaceWith(p => {
const functionName = p.value.callee.name;
const args = p.value.arguments;
if (args.length === 0) {
return p.value;
}
const name = args[0].value;
const config = args[1];
const auth = config.properties.find(p => p.key.name === 'authorize');
if (auth) {
auth.key.name = "authScopes";
}
const newArgs = config.properties.find(p => p.key.name === 'args');
if (newArgs) {
newArgs.value.properties = newArgs.value.properties.map(p => {
const name = p.key.name;
const nonNullable = p.value.callee.name === 'nonNull';
const params = []
if (nonNullable) {
params.push(j.property('init', j.identifier('required'), j.booleanLiteral(true)))
}
const val = p.value.arguments[0] ?? p.value;
let newArg;
if (val.type === 'Identifier') {
params.unshift(j.property('init', j.identifier('type'), val))
newArg = j.callExpression(
j.memberExpression(j.identifier('t'), j.identifier('arg')),
[j.objectExpression(params)]
);
} else if (val.type === 'ExpressionStatement') {
params.unshift(j.property('init', j.identifier('type'), val.expression))
newArg = j.callExpression(
j.memberExpression(j.identifier('t'), j.identifier('arg')),
[j.objectExpression(params)]
);
} else {
const type = val.callee.name.match(/[a-z]+/g)[0];
newArg = j.callExpression(
j.memberExpression(j.memberExpression(j.identifier('t'), j.identifier('arg')), j.identifier(type)),
params.length ? [j.objectExpression(params)] : []
);
}
return j.property('init', j.identifier(name), newArg);
});
}
const typeProperty = config.properties.find(p => p.key.name === 'type')
const isNullable = typeProperty.value.type === 'CallExpression' && typeProperty.value.callee.name === 'nullable'
if (isNullable) {
if (typeProperty.value.arguments[0]?.callee?.name === 'list') {
typeProperty.value = j.arrayExpression([typeProperty.value.arguments[0].arguments[0]]);
} else {
typeProperty.value = typeProperty.value.arguments[0];
}
} else if (typeProperty.value.type === 'CallExpression' && typeProperty.value.callee.name === 'list') {
typeProperty.value = j.arrayExpression([typeProperty.value.arguments[0]]);
}
return statement`builder.${functionName}(${j.stringLiteral(name)}, t => t.field(${j.objectExpression([
typeProperty,
isNullable ? j.property('init', j.identifier('nullable'), j.booleanLiteral(true)) : null,
config.properties.find(p => p.key.name === 'args'),
auth,
config.properties.find(p => p.key.name === 'resolve')].filter(Boolean))}))`;
});
relayQueries.replaceWith(p => {
const functionName = p.value.callee.name;
const args = p.value.arguments;
if (args.length === 0) {
return p.value;
}
const connectionFieldArguments = args[0].body.body[0].expression.arguments;
const name = connectionFieldArguments[0].value;
const config = connectionFieldArguments[1];
const typeProperty = config.properties.find(p => p.key.name === 'type');
const isNullable = typeProperty.value.type === 'CallExpression' && typeProperty.value.callee.name === 'nullable';
if (isNullable) {
typeProperty.value = typeProperty.value.arguments[0];
}
const additionalArgs = config.properties.find(p => p.key.name === 'additionalArgs');
if (additionalArgs) {
additionalArgs.key.name = 'args';
}
const nodes = config.properties.find(p => p.key.name === 'nodes');
if (nodes) {
nodes.key.name = 'resolve';
const previousBody = nodes.body;
const isAsync = nodes.async;
nodes.async = false;
nodes.body = j.blockStatement([
j.returnStatement(
j.callExpression(
j.identifier('resolveOffsetConnection'),
[
j.objectExpression([
j.property.from({
kind: 'init',
key: j.identifier('args'),
value: j.identifier('args'),
shorthand: true
})
]),
j.arrowFunctionExpression.from({
params: [
j.objectPattern([
j.property.from({
kind: 'init',
key: j.identifier('limit'),
value: j.identifier('limit'),
shorthand: true
}),
j.property.from({
kind: 'init',
key: j.identifier('offset'),
value: j.identifier('offset'),
shorthand: true
})
])
],
body: previousBody,
async: isAsync
}
)
]
)
)
]);
}
const auth = config.properties.find(p => p.key.name === 'authorize');
if (auth) {
auth.key.name = "authScopes";
}
return statement`builder.${functionName}(${j.stringLiteral(name)}, t => t.connection(${j.objectExpression([
typeProperty,
isNullable ? j.property('init', j.identifier('nullable'), j.booleanLiteral(true)) : null,
additionalArgs,
auth ? auth : null,
nodes
].filter(Boolean))}))`;
});
objects.replaceWith(p => {
const object = p.value.arguments[0];
const type = object.properties.find(p => p.key.name === 'name').value;
const definitions = object.properties.find(p => p.key.name === 'definition').body.body;
const resolveType = object.properties.find(p => p.key.name === 'resolveType');
let objectType = 'objectRef';
if(p.node.callee.name === 'interfaceType') {
objectType = 'interfaceRef';
} else if(p.node.callee.name === 'inputObjectType') {
objectType = 'inputType';
}
const implementsInterfaces = [];
const fields = definitions.map(node => {
const functionName = node.expression.callee.property.name;
if (functionName === 'implements') {
implementsInterfaces.push(node.expression.arguments[0].name);
return null;
}
const isNullable = node.expression.callee.object.property?.name === 'nullable'
if(objectType === 'interfaceRef' || objectType === 'inputType' || (node.expression.arguments.length === 2 && node.expression.callee.property.name !== 'field')){
const functionName = node.expression.callee.property.name;
const args = [];
let name;
if (functionName === 'field') {
name = node.expression.arguments[0].properties.find(p => p.key.name === 'name').value.value
j.property('init', j.identifier('nullable'), j.booleanLiteral(true))
const props = node.expression.arguments[0].properties.filter(p => p.key.name !== 'name');
if(isNullable){
props.unshift(j.property('init', j.identifier('nullable'), j.booleanLiteral(true)))
}
args.push(j.objectExpression(props));
} else if(node.expression.arguments[1]){
name = node.expression.arguments[0].value
const props = node.expression.arguments[1].properties;
if(isNullable){
props.unshift(j.property('init', j.identifier('nullable'), j.booleanLiteral(true)))
}
args.push(j.objectExpression(props));
} else {
name = node.expression.arguments[0].value
}
return j.property('init', j.identifier(name), j.callExpression(j.memberExpression(j.identifier('t'), j.identifier(functionName)), args));
}
let propertyName;
let type;
let resolve;
if (functionName === 'field' && node.expression.arguments[0].type === 'ObjectExpression') {
propertyName = node.expression.arguments[0].properties.find(p => p.key.name === 'name').value;
type = node.expression.arguments[0].properties.find(p => p.key.name === 'type').value;
resolve = node.expression.arguments[0].properties.find(p => p.key.name === 'resolve');
} else if (functionName === 'field') {
propertyName = node.expression.arguments[0];
type = node.expression.arguments[1].properties.find(p => p.key.name === 'type').value;
resolve = node.expression.arguments[1].properties.find(p => p.key.name === 'resolve')
} else {
propertyName = node.expression.arguments[0];
type = functionName;
resolve = node.expression.arguments[1]?.properties.find(p => p.key.name === 'resolve')
}
let exposeName;
const functionArguments = []
if(functionName === 'field') {
if(resolve) {
exposeName = 'field'
} else {
exposeName = 'expose'
functionArguments.push(propertyName)
}
} else {
exposeName = `expose${capitalizeFirstLetter(type === 'id' ? 'ID' : type)}`;
functionArguments.push(propertyName)
}
const objectProps = [];
if (functionName === 'field') {
let finalType = type;
if(type.type === 'CallExpression' && type.callee.name === 'list') {
finalType = j.arrayExpression([type.arguments[0]])
}
objectProps.push(j.property('init', j.identifier('type'), finalType))
}
if (isNullable) {
objectProps.push(j.property('init', j.identifier('nullable'), j.booleanLiteral(true)))
}
if (resolve) {
objectProps.push(resolve)
}
if(objectProps.length) {
functionArguments.push(j.objectExpression(objectProps))
}
return j.property('init', j.identifier(propertyName.value), j.callExpression(
j.memberExpression(j.identifier('t'), j.identifier(exposeName)),
functionArguments
));
}).filter(Boolean);
const objectProps = [];
if (implementsInterfaces.length) {
objectProps.push(j.property('init', j.identifier('interfaces'), j.arrayExpression(implementsInterfaces.map(i => j.identifier(i)))));
}
if (fields.length) {
objectProps.push(j.property('init', j.identifier('fields'), j.arrowFunctionExpression(
[j.identifier('t')],
j.objectExpression(fields)
)));
}
if (resolveType) {
objectProps.push(resolveType);
}
if(objectType === 'inputType') {
return statement`builder.${objectType}(${type}, ${j.objectExpression(objectProps)})`
}
return statement`builder.${objectType}<any>(${type})
.implement(${j.objectExpression(objectProps)})`;
});
return root.toSource();
};
export default transform;