|
1
|
import ts from "typescript" |
|
2
|
import DefaultKindGenerator, { GetDocBlockOptions } from "./default.js" |
|
3
|
import getBasePath from "../../utils/get-base-path.js" |
|
4
|
import { getDmlOutputBasePath } from "../../utils/get-output-base-paths.js" |
|
5
|
import path from "path" |
|
6
|
import { |
|
7
|
camelToWords, |
|
8
|
RELATION_NAMES, |
|
9
|
snakeToPascal, |
|
10
|
camelToPascal, |
|
11
|
isSnakeCase, |
|
12
|
} from "utils" |
|
13
|
import toJsonFormatted from "../../utils/to-json-formatted.js" |
|
14
|
import { DmlFile, DmlObject } from "types" |
|
15
|
|
|
16
|
/** |
|
17
|
* DML generator for data models created with DML. |
|
18
|
*/ |
|
19
|
class DmlKindGenerator extends DefaultKindGenerator<ts.CallExpression> { |
|
20
|
protected allowedKinds: ts.SyntaxKind[] = [ts.SyntaxKind.CallExpression] |
|
21
|
public name = "dml" |
|
22
|
|
|
23
|
/** |
|
24
|
* Checks whether the node is a call expression, is `model.define`, and has at least |
|
25
|
* two arguments. |
|
26
|
* |
|
27
|
* @param node - The node to check |
|
28
|
* @returns Whether this kind generator can be used for this node. |
|
29
|
*/ |
|
30
|
isAllowed(node: ts.Node): node is ts.CallExpression { |
|
31
|
if (!super.isAllowed(node)) { |
|
32
|
return false |
|
33
|
} |
|
34
|
|
|
35
|
const isModelUtility = |
|
36
|
"expression" in node.expression && |
|
37
|
(node.expression.expression as ts.Node).getText() === "model" |
|
38
|
const isDefineMethod = |
|
39
|
"name" in node.expression && |
|
40
|
(node.expression.name as ts.Identifier).getText() === "define" |
|
41
|
const hasAllArguments = node.arguments.length >= 2 |
|
42
|
|
|
43
|
return isModelUtility && isDefineMethod && hasAllArguments |
|
44
|
} |
|
45
|
|
|
46
|
/** |
|
47
|
* Retrieve the DML object from the node's associated file, if it exists. |
|
48
|
* |
|
49
|
* @param node - The node to retrieve its DML object. |
|
50
|
* @param dataModelName - The data model's name |
|
51
|
* @returns The DML object, if available. |
|
52
|
*/ |
|
53
|
getExistingDmlObjectFromFile( |
|
54
|
node: ts.CallExpression, |
|
55
|
dataModelName: string |
|
56
|
): DmlObject | undefined { |
|
57
|
const filePath = this.getAssociatedFileName(node) |
|
58
|
const existingJson = ts.sys.readFile(filePath) |
|
59
|
|
|
60
|
if (!existingJson) { |
|
61
|
return |
|
62
|
} |
|
63
|
|
|
64
|
const parsedJson = JSON.parse(existingJson) as DmlFile |
|
65
|
if (!Object.hasOwn(parsedJson, dataModelName)) { |
|
66
|
return |
|
67
|
} |
|
68
|
|
|
69
|
return parsedJson[dataModelName].properties |
|
70
|
} |
|
71
|
|
|
72
|
/** |
|
73
|
* Get new or updated JSON dml object of the data model. |
|
74
|
* |
|
75
|
* @param node - The node to get its JSON DML object. |
|
76
|
* @param options - General options |
|
77
|
* @returns The JSON dml object. |
|
78
|
*/ |
|
79
|
async getDocBlock( |
|
80
|
node: ts.CallExpression | ts.Node, |
|
81
|
options?: GetDocBlockOptions |
|
82
|
): Promise<string> { |
|
83
|
if (!this.isAllowed(node)) { |
|
84
|
return await super.getDocBlock(node, options) |
|
85
|
} |
|
86
|
const dataModelName = this.getDataModelName(node.arguments[0]) |
|
87
|
|
|
88
|
const existingDmlObject = this.getExistingDmlObjectFromFile( |
|
89
|
node, |
|
90
|
dataModelName |
|
91
|
) |
|
92
|
|
|
93
|
const properties = existingDmlObject |
|
94
|
? this.updateExistingDmlObject({ |
|
95
|
node, |
|
96
|
dmlObject: existingDmlObject, |
|
97
|
dataModelName, |
|
98
|
}) |
|
99
|
: this.getNewDmlObject({ |
|
100
|
node, |
|
101
|
dataModelName, |
|
102
|
}) |
|
103
|
|
|
104
|
/** |
|
105
|
* Use parent to get tags like since, deprecated, featureFlag |
|
106
|
*/ |
|
107
|
const parent = node.parent?.parent?.parent |
|
108
|
|
|
109
|
const { sinceTag, deprecatedTag, featureFlagTag } = |
|
110
|
this.getInformationFromTags(parent) |
|
111
|
|
|
112
|
const dmlFile: DmlFile = { |
|
113
|
[dataModelName]: { |
|
114
|
filePath: getBasePath(node.getSourceFile().fileName), |
|
115
|
properties, |
|
116
|
since: this.formatJSDocTag(sinceTag), |
|
117
|
deprecated: { |
|
118
|
is_deprecated: !!deprecatedTag, |
|
119
|
description: this.formatJSDocTag(deprecatedTag), |
|
120
|
}, |
|
121
|
featureFlag: this.formatJSDocTag(featureFlagTag), |
|
122
|
}, |
|
123
|
} |
|
124
|
|
|
125
|
return toJsonFormatted(dmlFile) |
|
126
|
} |
|
127
|
|
|
128
|
/** |
|
129
|
* Get a new DML object for a node. |
|
130
|
* |
|
131
|
* @param param0 - The node and data model's details |
|
132
|
* @returns The DML object. |
|
133
|
*/ |
|
134
|
getNewDmlObject({ |
|
135
|
node, |
|
136
|
dataModelName, |
|
137
|
}: { |
|
138
|
node: ts.CallExpression |
|
139
|
dataModelName: string |
|
140
|
}): DmlObject { |
|
141
|
return this.getPropertiesFromNode(node, dataModelName) |
|
142
|
} |
|
143
|
|
|
144
|
/** |
|
145
|
* Update existing dml object by removing properties no longer |
|
146
|
* available and adding new ones. |
|
147
|
* |
|
148
|
* @param param0 - The node and data model's details. |
|
149
|
* @returns The updated DML object. |
|
150
|
*/ |
|
151
|
updateExistingDmlObject({ |
|
152
|
node, |
|
153
|
dmlObject, |
|
154
|
dataModelName, |
|
155
|
}: { |
|
156
|
node: ts.CallExpression |
|
157
|
dmlObject: DmlObject |
|
158
|
dataModelName: string |
|
159
|
}): DmlObject { |
|
160
|
const newDmlObject = Object.assign({}, dmlObject) |
|
161
|
const newProperties = this.getPropertiesFromNode(node, dataModelName) |
|
162
|
|
|
163
|
const newPropertyNames = Object.keys(newProperties) |
|
164
|
const oldPropertyNames = Object.keys(newDmlObject) |
|
165
|
|
|
166
|
// delete properties not available anymore |
|
167
|
oldPropertyNames.forEach((oldPropertyName) => { |
|
168
|
if (!newPropertyNames.includes(oldPropertyName)) { |
|
169
|
delete newDmlObject[oldPropertyName] |
|
170
|
} |
|
171
|
}) |
|
172
|
|
|
173
|
// add new properties |
|
174
|
newPropertyNames.forEach((newPropertyName) => { |
|
175
|
if (oldPropertyNames.includes(newPropertyName)) { |
|
176
|
return |
|
177
|
} |
|
178
|
|
|
179
|
newDmlObject[newPropertyName] = newProperties[newPropertyName] |
|
180
|
}) |
|
181
|
|
|
182
|
return newDmlObject |
|
183
|
} |
|
184
|
|
|
185
|
/** |
|
186
|
* Get the data model's name. It's either a string passed to `model.define`, or |
|
187
|
* in the `name` property of the object passed as a parameter to `model.define`. |
|
188
|
* |
|
189
|
* @param node - The node to retrieve its data model name. |
|
190
|
* @returns The data model's name. |
|
191
|
*/ |
|
192
|
getDataModelName(node: ts.Node): string { |
|
193
|
let name = node.getText() |
|
194
|
if (ts.isObjectLiteralExpression(node)) { |
|
195
|
const nameProperty = node.properties.find((propertyNode) => { |
|
196
|
return ( |
|
197
|
propertyNode.name?.getText() === "name" && |
|
198
|
"initializer" in propertyNode |
|
199
|
) |
|
200
|
}) as ts.ObjectLiteralElementLike & { |
|
201
|
initializer: ts.Node |
|
202
|
} |
|
203
|
|
|
204
|
if (nameProperty) { |
|
205
|
name = nameProperty.initializer.getText() |
|
206
|
} |
|
207
|
} |
|
208
|
|
|
209
|
name = name.replace(/^"/, "").replace(/"$/, "") |
|
210
|
|
|
211
|
return isSnakeCase(name) ? snakeToPascal(name) : camelToPascal(name) |
|
212
|
} |
|
213
|
|
|
214
|
/** |
|
215
|
* Get the properties of a node. |
|
216
|
* |
|
217
|
* @param node - The node to get its properties. |
|
218
|
* @param dataModelName - The data model's name. |
|
219
|
* @returns The properties and their description. |
|
220
|
*/ |
|
221
|
getPropertiesFromNode( |
|
222
|
node: ts.CallExpression, |
|
223
|
dataModelName: string |
|
224
|
): DmlObject { |
|
225
|
const formattedDataModelName = this.formatDataModelName(dataModelName) |
|
226
|
const propertyNodes = |
|
227
|
"properties" in node.arguments[1] |
|
228
|
? (node.arguments[1].properties as ts.Node[]) |
|
229
|
: [] |
|
230
|
|
|
231
|
const properties: DmlObject = {} |
|
232
|
|
|
233
|
propertyNodes.forEach((propertyNode) => { |
|
234
|
const propertyName = |
|
235
|
"name" in propertyNode |
|
236
|
? (propertyNode.name as ts.Identifier).getText() |
|
237
|
: propertyNode.getText() |
|
238
|
const propertyType = this.checker.getTypeAtLocation(propertyNode) |
|
239
|
const propertyTypeStr = this.checker.typeToString(propertyType) |
|
240
|
|
|
241
|
const isRelation = RELATION_NAMES.some((relationName) => |
|
242
|
propertyTypeStr.includes(relationName) |
|
243
|
) |
|
244
|
const isBoolean = propertyTypeStr.includes("BooleanProperty") |
|
245
|
const relationName = isRelation ? camelToWords(propertyName) : undefined |
|
246
|
const { summary, sinceTag, deprecatedTag, featureFlagTag, example } = |
|
247
|
this.getInformationFromTags(propertyNode) |
|
248
|
|
|
249
|
let propertyDescription = |
|
250
|
summary || |
|
251
|
this.knowledgeBaseFactory.tryToGetObjectPropertySummary({ |
|
252
|
retrieveOptions: { |
|
253
|
str: propertyName, |
|
254
|
kind: propertyNode.kind, |
|
255
|
templateOptions: { |
|
256
|
parentName: formattedDataModelName, |
|
257
|
rawParentName: dataModelName, |
|
258
|
}, |
|
259
|
}, |
|
260
|
propertyDetails: { |
|
261
|
isClassOrInterface: isRelation, |
|
262
|
isBoolean, |
|
263
|
classOrInterfaceName: relationName, |
|
264
|
}, |
|
265
|
}) |
|
266
|
|
|
267
|
if (isRelation) { |
|
268
|
propertyDescription += `\n\n@expandable` |
|
269
|
} |
|
270
|
|
|
271
|
if (sinceTag) { |
|
272
|
propertyDescription += `\n\n@since ${ |
|
273
|
this.formatJSDocTag(sinceTag) ?? "" |
|
274
|
}` |
|
275
|
} |
|
276
|
|
|
277
|
if (featureFlagTag) { |
|
278
|
propertyDescription += `\n\n@featureFlag ${ |
|
279
|
this.formatJSDocTag(featureFlagTag) ?? "" |
|
280
|
}` |
|
281
|
} |
|
282
|
|
|
283
|
if (deprecatedTag) { |
|
284
|
propertyDescription += `\n\n@deprecated ${ |
|
285
|
this.formatJSDocTag(deprecatedTag) ?? "" |
|
286
|
}` |
|
287
|
} |
|
288
|
|
|
289
|
if (example) { |
|
290
|
propertyDescription += `\n\n@example ${ |
|
291
|
this.formatJSDocTag(example) ?? "" |
|
292
|
}` |
|
293
|
} |
|
294
|
|
|
295
|
properties[propertyName] = propertyDescription |
|
296
|
}) |
|
297
|
|
|
298
|
return properties |
|
299
|
} |
|
300
|
|
|
301
|
/** |
|
302
|
* Get the name of the output JSON file associated with a node. |
|
303
|
* |
|
304
|
* @param node - The node to get its file name. |
|
305
|
* @returns The file name. |
|
306
|
*/ |
|
307
|
getAssociatedFileName(node: ts.Node): string { |
|
308
|
const filePath = getBasePath(node.getSourceFile().fileName).split("/") |
|
309
|
// since modules are at packages/modules/<name>, the name should be at index 2 |
|
310
|
const moduleName = filePath[2] |
|
311
|
|
|
312
|
return path.join(getDmlOutputBasePath(), `${moduleName}.json`) |
|
313
|
} |
|
314
|
|
|
315
|
/** |
|
316
|
* Format the data model name for presentation purposes. |
|
317
|
* |
|
318
|
* @param name - The raw data model's name. |
|
319
|
* @returns The formatted name. |
|
320
|
*/ |
|
321
|
formatDataModelName(name: string): string { |
|
322
|
return camelToWords(name) |
|
323
|
} |
|
324
|
} |
|
325
|
|
|
326
|
export default DmlKindGenerator |