-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextractors.ts
More file actions
521 lines (456 loc) · 13.7 KB
/
extractors.ts
File metadata and controls
521 lines (456 loc) · 13.7 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import {
type DocMeta,
type PluginOptions,
type Relationship,
type RelationType,
type RenderOptions,
} from './types';
import {
type DataField,
type DataFieldAttribute,
type DataModel,
type DataModelAttribute,
isDataModel,
isEnum,
isTypeDef,
} from '@zenstackhq/language/ast';
import { getAllFields } from '@zenstackhq/language/utils';
import path from 'node:path';
/**
* Structural shape for both DataFieldType and FunctionParamType.
*/
export type TypeLike = {
array?: boolean;
optional?: boolean;
reference?: { readonly ref?: { readonly name: string } };
type?: string;
};
type AstLike = {
$container?: AstLike;
$cstNode?: {
root?: { element?: { $document?: { uri?: { fsPath?: string } } } };
};
$document?: { uri?: { fsPath?: string } };
};
/**
* Extracts the set of foreign key field names from `@relation(fields: [...])` attributes.
*/
export function collectFKFieldNames(fields: DataField[]): Set<string> {
const fkNames = new Set<string>();
for (const field of fields) {
const relationAttribute = field.attributes.find(
(a) => getAttrName(a) === '@relation',
);
if (!relationAttribute) {
continue;
}
const cstText = relationAttribute.$cstNode?.text ?? '';
const fieldsMatch = cstText.match(/fields:\s*\[([^\]]+)\]/u);
if (fieldsMatch) {
for (const name of fieldsMatch[1]!.split(',').map((s) => s.trim())) {
fkNames.add(name);
}
}
}
return fkNames;
}
/**
* Collects all relation fields across the given models into a flat list of `Relationship` entries.
*/
export function collectRelationships(models: DataModel[]): Relationship[] {
const modelByName = new Map<string, DataModel>();
for (const m of models) {
modelByName.set(m.name, m);
}
const rels: Relationship[] = [];
for (const model of models) {
for (const field of getAllFields(model)) {
if (field.type.reference?.ref && isDataModel(field.type.reference.ref)) {
const targetModel = modelByName.get(field.type.reference.ref.name);
if (!targetModel) {
continue;
}
const relType = inferRelationType(field, model, targetModel);
const relName = getRelationName(field);
rels.push({
field: field.name,
from: model.name,
relationName: relName,
to: targetModel.name,
type: relType,
});
}
}
}
return rels;
}
/**
* Extracts `@@meta('doc:category', ...)`, `doc:since`, and `doc:deprecated` values from model attributes.
*/
export function extractDocMeta(attributes: DataModelAttribute[]): DocMeta {
const meta: DocMeta = {};
for (const attribute of attributes) {
if (attribute.decl.ref?.name !== '@@meta') {
continue;
}
const key = stripQuotes(attribute.args[0]?.$cstNode?.text ?? '');
const value = stripQuotes(attribute.args[1]?.$cstNode?.text ?? '');
if (key === 'doc:category') {
meta.category = value;
} else if (key === 'doc:since') {
meta.since = value;
} else if (key === 'doc:deprecated') {
meta.deprecated = value;
}
}
return meta;
}
/**
* Extracts the `@meta('doc:example', '...')` value from a field, if present.
*/
export function extractFieldDocExample(field: DataField): string | undefined {
for (const attribute of field.attributes) {
if (getAttrName(attribute) !== '@meta') {
continue;
}
const key = stripQuotes(attribute.args[0]?.$cstNode?.text ?? '');
if (key === 'doc:example') {
return stripQuotes(attribute.args[1]?.$cstNode?.text ?? '');
}
}
return undefined;
}
/**
* Extracts `///` doc-comments preceding a procedure declaration from its CST text.
* Returns the comments joined by `joinWith` (newline by default, space for inline use).
*/
export function extractProcedureComments(
proc: { $cstNode?: { text?: string } },
joinWith: ' ' | '\n' = '\n',
): string {
const cstText = proc.$cstNode?.text;
if (!cstText) {
return '';
}
const commentLines: string[] = [];
for (const line of cstText.split('\n')) {
const trimmed = line.trim();
if (trimmed.startsWith('///')) {
commentLines.push(trimmed.replace(/^\/{3}\s?/u, ''));
} else {
break;
}
}
return commentLines.join(joinWith).trim();
}
/**
* Formats the argument list of a field attribute as a parenthesized string, e.g. `(cuid())`.
*/
export function formatAttrArgs(attribute: DataFieldAttribute): string {
if (attribute.args.length === 0) {
return '';
}
const parts = attribute.args.map((argument) => argument.$cstNode?.text ?? '');
return `(${parts.join(', ')})`;
}
/**
* Returns the resolved name of a field-level attribute (e.g. `@id`, `@default`).
*/
export function getAttrName(attribute: DataFieldAttribute): string {
return attribute.decl.ref?.name ?? '';
}
/**
* Returns the formatted default value for a field, or an em-dash if none.
*/
export function getDefaultValue(field: DataField): string {
const defaultAttribute = field.attributes.find(
(a) => getAttrName(a) === '@default',
);
const firstArgument = defaultAttribute?.args[0];
if (!firstArgument) {
return '\u2014';
}
return `\`${firstArgument.$cstNode?.text ?? ''}\``;
}
/**
* Returns a comma-separated string of field attributes, excluding `@default`, `@computed`, and `@meta`.
*/
export function getFieldAttributes(field: DataField): string {
const attributes = field.attributes
.filter((a) => {
const name = getAttrName(a);
return name !== '@default' && name !== '@computed' && name !== '@meta';
})
.map((a) => `\`${getAttrName(a)}${formatAttrArgs(a)}\``);
return attributes.length > 0 ? attributes.join(', ') : '\u2014';
}
/**
* Returns the display string for a field's type, optionally linking to the related model/enum page.
* Model field links use relative `./` paths (caller is in models/ dir).
*/
export function getFieldTypeName(field: DataField, linked: boolean): string {
let typeName: string;
if (field.type.reference?.ref) {
const ref = field.type.reference.ref;
if (linked) {
if (isDataModel(ref)) {
typeName = `[${ref.name}](./${ref.name}.md)`;
} else if (isEnum(ref)) {
typeName = `[${ref.name}](../enums/${ref.name}.md)`;
} else {
typeName = ref.name;
}
} else {
typeName = ref.name;
}
} else if (field.type.type) {
typeName = field.type.type;
} else {
typeName = 'Unknown';
}
if (field.type.array) {
typeName += '[]';
}
if (field.type.optional) {
typeName += '?';
}
const isScalar = !field.type.reference?.ref;
return isScalar ? `\`${typeName}\`` : typeName;
}
/**
* Extracts the relation name from `@relation("Name", ...)`, or undefined if absent.
*/
export function getRelationName(field: DataField): string | undefined {
const attribute = field.attributes.find(
(a) => getAttrName(a) === '@relation',
);
if (!attribute) {
return undefined;
}
const cstText = attribute.$cstNode?.text ?? '';
const m = cstText.match(/@relation\(\s*"([^"]+)"/u);
return m?.[1];
}
/**
* Returns the source file path relative to `schemaDir`, or just the basename if `schemaDir` is not set.
*/
export function getRelativeSourcePath(
node: AstLike,
schemaDir: string | undefined,
): string | undefined {
const absPath = getSourceFilePath(node);
if (!absPath) {
return undefined;
}
return schemaDir ? path.relative(schemaDir, absPath) : path.basename(absPath);
}
/**
* Resolves the absolute file system path of the source file that defines `node`.
*/
export function getSourceFilePath(node: AstLike): string | undefined {
const cstDocument = node.$cstNode?.root?.element?.$document?.uri?.fsPath;
if (cstDocument) {
return cstDocument;
}
let root: AstLike = node;
while (root.$container) {
root = root.$container;
}
return root.$document?.uri?.fsPath;
}
/**
* Returns `true` if the field is non-optional and non-array (i.e. required for creation).
*/
export function isFieldRequired(field: DataField): boolean {
return !field.type.optional && !field.type.array;
}
/**
* Returns `true` if the model has the `@@ignore` attribute.
*/
export function isIgnoredModel(model: DataModel): boolean {
if (model.attributes.some((a) => a.decl.ref?.name === '@@ignore')) {
return true;
}
// Support @@meta('doc:ignore', true)
for (const attribute of model.attributes) {
if (attribute.decl.ref?.name !== '@@meta') {
continue;
}
const key = stripQuotes(attribute.args[0]?.$cstNode?.text ?? '');
const value = stripQuotes(attribute.args[1]?.$cstNode?.text ?? '');
if (key === 'doc:ignore' && value === 'true') {
return true;
}
}
return false;
}
/**
* Converts plugin options into a typed `RenderOptions` with defaults.
*/
export function resolveRenderOptions(options: PluginOptions): RenderOptions {
return {
fieldOrder:
options.fieldOrder === 'alphabetical' ? 'alphabetical' : 'declaration',
includeIndexes: options.includeIndexes !== false,
includePolicies: options.includePolicies !== false,
includeRelationships: options.includeRelationships !== false,
includeValidation: options.includeValidation !== false,
};
}
/**
* Returns a markdown-formatted type link for use in documentation tables.
* When `linked` is true, reference types are rendered as markdown links to their pages.
* Scalar types are wrapped in backticks; array/optional suffixes are appended.
*/
export function resolveTypeLink(t: TypeLike, linked: boolean): string {
let typeName: string;
if (t.reference?.ref) {
const ref: unknown = t.reference.ref;
if (linked) {
if (isDataModel(ref)) {
typeName = `[${ref.name}](../models/${ref.name}.md)`;
} else if (isEnum(ref)) {
typeName = `[${ref.name}](../enums/${ref.name}.md)`;
} else if (isTypeDef(ref)) {
typeName = `[${ref.name}](../types/${ref.name}.md)`;
} else {
typeName = t.reference.ref.name;
}
} else {
typeName = t.reference.ref.name;
}
} else if (t.type) {
typeName = t.type;
} else {
typeName = 'Unknown';
}
if (t.array) {
typeName += '[]';
}
if (t.optional) {
typeName += '?';
}
const isScalar = !t.reference?.ref;
return isScalar ? `\`${typeName}\`` : typeName;
}
/**
* Returns the bare type name string (e.g. `"String"`, `"User"`) without markdown formatting.
*/
export function resolveTypeName(t: TypeLike): string {
return t.reference?.ref?.name ?? t.type ?? 'Unknown';
}
/**
* Strips leading `///` prefixes from ZModel doc-comment lines and joins them.
*/
export function stripCommentPrefix(comments: string[]): string {
return comments
.map((c) => c.replace(/^\/{3}\s?/u, ''))
.join('\n')
.trim();
}
/**
* Removes surrounding single or double quotes from a CST argument string.
*/
export function stripQuotes(s: string): string {
return s.replaceAll(/^["']|["']$/gu, '');
}
/**
* Finds the inverse (back-reference) field on `targetModel` that participates in the same relation as `field`.
*/
function findBackRef(
field: DataField,
model: DataModel,
targetModel: DataModel,
filter: (f: DataField) => boolean,
): DataField | undefined {
const relName = getRelationName(field);
return getAllFields(targetModel, true).find((f) => {
if (f === field) {
return false;
}
if (!f.type.reference?.ref || !isDataModel(f.type.reference.ref)) {
return false;
}
if (f.type.reference.ref.name !== model.name) {
return false;
}
if (relName && getRelationName(f) !== relName) {
return false;
}
return filter(f);
});
}
/**
* Returns the FK field names declared in a `@relation(fields: [...])` attribute on `field`.
*/
function getRelationFKNames(field: DataField): string[] {
const relationAttribute = field.attributes.find(
(a) => getAttrName(a) === '@relation',
);
if (!relationAttribute) {
return [];
}
const cstText = relationAttribute.$cstNode?.text ?? '';
const match = cstText.match(/fields:\s*\[([^\]]+)\]/u);
if (!match) {
return [];
}
return match[1]!.split(',').map((s) => s.trim());
}
/**
* Returns true if `field` has a `@relation(fields: [...])` where all FK columns are `@unique`.
*/
function hasUniqueForeignKey(field: DataField, model: DataModel): boolean {
const fkNames = getRelationFKNames(field);
if (fkNames.length === 0) {
return false;
}
const allFields = getAllFields(model, true);
return fkNames.every((fkName) => {
const fkField = allFields.find((f) => f.name === fkName);
return fkField?.attributes.some((a) => getAttrName(a) === '@unique');
});
}
/**
* Determines the cardinality of a relation field by inspecting FK uniqueness,
* array types, and the inverse field on the target model.
*/
function inferRelationType(
field: DataField,
model: DataModel,
targetModel: DataModel,
): RelationType {
if (field.type.array) {
if (isImplicitManyToManySide(field)) {
const arrayBackRef = findBackRef(
field,
model,
targetModel,
(f) => f.type.array,
);
if (arrayBackRef && isImplicitManyToManySide(arrayBackRef)) {
return 'Many\u2192Many';
}
}
return 'One\u2192Many';
}
if (hasUniqueForeignKey(field, model)) {
return field.type.optional ? 'One\u2192One?' : 'One\u2192One';
}
const scalarBackRef = findBackRef(
field,
model,
targetModel,
(f) => !f.type.array,
);
if (scalarBackRef && hasUniqueForeignKey(scalarBackRef, targetModel)) {
return field.type.optional ? 'One\u2192One?' : 'One\u2192One';
}
return field.type.optional ? 'Many\u2192One?' : 'Many\u2192One';
}
/**
* Returns true if `field` is an array relation with no explicit FK (implicit join table side).
*/
function isImplicitManyToManySide(field: DataField): boolean {
return field.type.array && getRelationFKNames(field).length === 0;
}