-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenum-page.ts
More file actions
152 lines (136 loc) · 3.89 KB
/
enum-page.ts
File metadata and controls
152 lines (136 loc) · 3.89 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
import { stripCommentPrefix } from '../extractors';
import { type EnumPageProps } from '../types';
import {
breadcrumbs,
generatedHeader,
navigationFooter,
referencesSection,
renderDescription,
renderSourceAndDeclaration,
sectionHeading,
} from './common';
import { type DataModel, type Enum, isEnum } from '@zenstackhq/language/ast';
import { getAllFields } from '@zenstackhq/language/utils';
type EnumUsage = {
fieldNames: string[];
isView: boolean;
modelName: string;
};
/**
* Renders a full documentation page for an enum, including values, usage, and a class diagram.
*/
export function renderEnumPage(props: EnumPageProps): string {
const usages = collectEnumUsage(props.enumDecl, props.allModels);
return [
...renderHeader(props),
...renderDescription(props.enumDecl.comments),
...renderSourceAndDeclaration(props.enumDecl, props.options.schemaDir),
...renderValuesSection(props.enumDecl),
...renderUsedBySection(props.enumDecl, usages),
...renderUsageDiagram(props.enumDecl, usages),
...referencesSection('enum'),
...navigationFooter(props.navigation),
].join('\n');
}
/**
* Finds all models/views that reference this enum and which fields use it.
*/
function collectEnumUsage(enumDecl: Enum, allModels: DataModel[]): EnumUsage[] {
const usages: EnumUsage[] = [];
for (const m of allModels) {
const fields = getAllFields(m)
.filter(
(f) =>
f.type.reference?.ref &&
isEnum(f.type.reference.ref) &&
f.type.reference.ref.name === enumDecl.name,
)
.map((f) => f.name);
if (fields.length > 0) {
usages.push({
fieldNames: fields,
isView: Boolean(m.isView),
modelName: m.name,
});
}
}
return usages.sort((a, b) => a.modelName.localeCompare(b.modelName));
}
/**
* Renders the page header with breadcrumb, title, and enum badge.
*/
function renderHeader(props: EnumPageProps): string[] {
return [
...generatedHeader(
props.options.genCtx,
props.options.includeGeneratedHeader,
),
breadcrumbs('Enums', props.enumDecl.name, '../'),
'',
`# ${props.enumDecl.name} <kbd>Enum</kbd>`,
'',
];
}
/**
* Renders a Mermaid class diagram showing which models reference this enum.
*/
function renderUsageDiagram(enumDecl: Enum, usages: EnumUsage[]): string[] {
if (usages.length === 0) {
return [];
}
const lines = [
'```mermaid',
'classDiagram',
` class ${enumDecl.name} {`,
` <<enumeration>>`,
];
for (const field of enumDecl.fields) {
lines.push(` ${field.name}`);
}
lines.push(' }');
for (const { fieldNames, modelName } of usages) {
lines.push(
` ${modelName} --> ${enumDecl.name} : ${fieldNames.join(', ')}`,
);
}
lines.push('```', '');
return lines;
}
/**
* Renders cross-reference links to models/views that use this enum.
*/
function renderUsedBySection(_enumDecl: Enum, usages: EnumUsage[]): string[] {
if (usages.length === 0) {
return [];
}
const lines = [...sectionHeading('Used By'), ''];
for (const { fieldNames, isView, modelName } of usages) {
const dir = isView ? 'views' : 'models';
const fieldLinks = fieldNames
.map((f) => `[\`${f}\`](../${dir}/${modelName}.md#field-${f})`)
.join(', ');
lines.push(`- [${modelName}](../${dir}/${modelName}.md) — ${fieldLinks}`);
}
lines.push('');
return lines;
}
/**
* Renders the enum values as a two-column table (value + description).
*/
function renderValuesSection(enumDecl: Enum): string[] {
if (enumDecl.fields.length === 0) {
return [];
}
const lines = [
...sectionHeading('Values'),
'',
'| Value | Description |',
'| --- | --- |',
];
for (const field of enumDecl.fields) {
const fieldDesc = stripCommentPrefix(field.comments) || '—';
lines.push(`| \`${field.name}\` | ${fieldDesc} |`);
}
lines.push('');
return lines;
}