@burnsred/entity-sync 0.0.0-stage → 3.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +11 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/agents/entity-editor.md +16 -0
- package/bin/entity-sync.mjs +36 -0
- package/dist/cli.d.mts +20 -0
- package/dist/commands/asserts.d.mts +4 -0
- package/dist/commands/check.d.mts +6 -0
- package/dist/commands/inspect.d.mts +30 -0
- package/dist/commands/plan.d.mts +28 -0
- package/dist/commands/schema.d.mts +39 -0
- package/dist/config.d.mts +175 -0
- package/dist/index.d.mts +5 -0
- package/dist/lib/cache.d.mts +120 -0
- package/dist/lib/diff.d.mts +228 -0
- package/dist/lib/field-map.d.mts +93 -0
- package/dist/lib/gen-asserts.d.mts +70 -0
- package/dist/lib/graph.d.mts +90 -0
- package/dist/lib/parse-entities.d.mts +257 -0
- package/dist/lib/parse-schema.d.mts +98 -0
- package/dist/lib/resolve.d.mts +135 -0
- package/dist/project.d.mts +60 -0
- package/hooks/editor-guard.mjs +86 -0
- package/hooks/hooks.json +15 -0
- package/package.json +54 -4
- package/skills/entity/SKILL.md +105 -0
- package/skills/entity/reference/create.md +74 -0
- package/skills/entity/reference/editor.md +80 -0
- package/skills/entity/reference/enums.md +89 -0
- package/skills/entity/reference/fields.md +101 -0
- package/skills/entity/reference/sync.md +191 -0
- package/src/cli.mjs +110 -0
- package/src/commands/asserts.mjs +52 -0
- package/src/commands/check.mjs +88 -0
- package/src/commands/inspect.mjs +290 -0
- package/src/commands/plan.mjs +542 -0
- package/src/commands/schema.mjs +206 -0
- package/src/config.mjs +205 -0
- package/src/index.mjs +19 -0
- package/src/lib/cache.mjs +323 -0
- package/src/lib/diff.mjs +706 -0
- package/src/lib/field-map.mjs +438 -0
- package/src/lib/gen-asserts.mjs +478 -0
- package/src/lib/graph.mjs +284 -0
- package/src/lib/parse-entities.mjs +935 -0
- package/src/lib/parse-schema.mjs +667 -0
- package/src/lib/resolve.mjs +455 -0
- package/src/project.mjs +115 -0
|
@@ -0,0 +1,935 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Parse `src/entities/**` into an inventory of every Entity subclass: its field
|
|
4
|
+
* declarations, its `Fields`/`Record` type members, its duck, and the API path it
|
|
5
|
+
* is served from.
|
|
6
|
+
*
|
|
7
|
+
* This is the other half of the join `plan.mjs` performs - `parse-schema.mjs`
|
|
8
|
+
* describes what the API says, this describes what the code says.
|
|
9
|
+
*
|
|
10
|
+
* ## Shapes this has to tolerate
|
|
11
|
+
*
|
|
12
|
+
* The entity files are hand-written, so unlike `schema.d.ts` they vary:
|
|
13
|
+
*
|
|
14
|
+
* - several Entity subclasses in one file (`Answer.ts`, `AmsApp.ts`)
|
|
15
|
+
* - `static fields` with and without an `EntityFields<T>` annotation
|
|
16
|
+
* - class names that don't end in `Entity` (`AmsApp`, `BasicEntity`), and type
|
|
17
|
+
* names that don't match the class (`AmsApp` uses `AppEntityFields`)
|
|
18
|
+
* - `apiBase` written as a plain string, a template literal, or a bare
|
|
19
|
+
* `settings.*` reference
|
|
20
|
+
* - screen-state entities (`filter/`) with a `DuckScreen` and no `apiBase` at all
|
|
21
|
+
*
|
|
22
|
+
* Anything unrecognised is recorded as unresolved rather than guessed at, so
|
|
23
|
+
* `plan.mjs` can report it instead of acting on a wrong assumption.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { readFileSync, readdirSync } from 'node:fs';
|
|
27
|
+
import { join, relative } from 'node:path';
|
|
28
|
+
|
|
29
|
+
import ts from 'typescript';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {object} EntityFieldDecl
|
|
33
|
+
* @property {string} name
|
|
34
|
+
* @property {string | null} fieldClass e.g. 'CharField'; null if not a `new …Field()`
|
|
35
|
+
* @property {Record<string, string>} opts option name -> source text
|
|
36
|
+
* @property {string | null} entityRef identifier passed as the `entity:` option
|
|
37
|
+
* @property {boolean} many `many: true` was passed
|
|
38
|
+
* @property {boolean} readOnly `readOnly: true` was passed
|
|
39
|
+
* @property {boolean} blank `blank: true` was passed
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {object} TypeMember
|
|
44
|
+
* @property {string} name
|
|
45
|
+
* @property {string} typeText the declared type, verbatim
|
|
46
|
+
* @property {boolean} optional
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @typedef {object} ParsedEntity
|
|
51
|
+
* @property {string} file repo-relative path
|
|
52
|
+
* @property {string} className
|
|
53
|
+
* @property {boolean} isDefaultExport
|
|
54
|
+
* @property {string | null} schemaAnnotation from a `@schema <Component>` JSDoc tag
|
|
55
|
+
* @property {string[]} [schemaAlso] from `@schemaAlso <Component>` tags: other components this entity also models
|
|
56
|
+
* @property {string | null} noSchema reason from `@noSchema`; '' if tagged without one
|
|
57
|
+
* @property {Record<string, string>} schemaIgnores field -> reason, from `@schemaIgnore`
|
|
58
|
+
* @property {string | null} schemaSubset reason from `@schemaSubset`; a deliberate projection
|
|
59
|
+
* @property {string | null} apiBaseRaw source text of the `apiBase` expression
|
|
60
|
+
* @property {string | null} apiBase resolved URL path, matching schema `paths` keys
|
|
61
|
+
* @property {string | null} apiBaseNote why resolution failed, when it did
|
|
62
|
+
* @property {boolean} paginated
|
|
63
|
+
* @property {string | null} duck duck class name, e.g. 'DjangoRestFramework'
|
|
64
|
+
* @property {string | null} fieldsTypeName from `EntityFields<T>`, else conventional
|
|
65
|
+
* @property {EntityFieldDecl[]} fields
|
|
66
|
+
* @property {TypeMember[]} fieldsTypeMembers
|
|
67
|
+
* @property {string | null} recordTypeName
|
|
68
|
+
* @property {string | null} recordTypeText
|
|
69
|
+
* @property {boolean} fieldsTypeExported can `gen-asserts.mjs` import it?
|
|
70
|
+
* @property {boolean} recordTypeExported
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A `export const X = { … } as const` choices dict.
|
|
75
|
+
*
|
|
76
|
+
* The entity library has no first-class enum - `EnumField` is backed by
|
|
77
|
+
* `EntityEnum` and changes the runtime value to an Immutable Map - so a broker
|
|
78
|
+
* enum is modelled as a plain frozen object plus a type alias of the same name.
|
|
79
|
+
* That gives `Status.draft` at call sites and `Status` as a type, from one
|
|
80
|
+
* import.
|
|
81
|
+
*
|
|
82
|
+
* A dict is only schema-backed if it carries `@schemaEnum <Component>`. Several
|
|
83
|
+
* enums here have no broker counterpart at all (the policy-snapshot widget and
|
|
84
|
+
* rule enums are a client-side convention), so absence of the tag means "local",
|
|
85
|
+
* not "unresolved".
|
|
86
|
+
*
|
|
87
|
+
* @typedef {object} ParsedEnum
|
|
88
|
+
* @property {string} file repo-relative path
|
|
89
|
+
* @property {string} name
|
|
90
|
+
* @property {string | null} schemaEnum component name from `@schemaEnum`
|
|
91
|
+
* @property {Record<string, string>} members key -> string value, in source order
|
|
92
|
+
* @property {string[]} values
|
|
93
|
+
* @property {boolean} exported
|
|
94
|
+
* @property {string | null} typeName an exported type alias the dict derives
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Maps `settings.*` API URL constants to the path prefix that appears in the
|
|
99
|
+
* schema's `paths` keys, e.g. `{ BASE_API_URL: '/api/v1' }`.
|
|
100
|
+
*
|
|
101
|
+
* Projects supply this as `settingsPrefixes` in `entity-sync.config.mjs`,
|
|
102
|
+
* mirroring their `src/settings/settings.ts`, where each constant is composed as
|
|
103
|
+
* `${BASE_API_ORIGIN}<prefix>`. Only the path part matters here, because schema
|
|
104
|
+
* `paths` keys are origin-relative.
|
|
105
|
+
*
|
|
106
|
+
* Kept as data so a drift between the map and settings.ts shows up as an
|
|
107
|
+
* unresolved `apiBase` in the report rather than a silent mismatch. Empty by
|
|
108
|
+
* default: a project-specific prefix is never guessed.
|
|
109
|
+
*
|
|
110
|
+
* @type {Readonly<Record<string, string>>}
|
|
111
|
+
*/
|
|
112
|
+
export const DEFAULT_SETTINGS_PREFIXES = Object.freeze({});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Prefix applied to a bare relative `apiBase` such as `'/application/'`.
|
|
116
|
+
*
|
|
117
|
+
* Typically the path of whatever `axiosInstance` uses as `baseURL`; projects set
|
|
118
|
+
* it with `apiBasePrefix` in their config. Empty by default.
|
|
119
|
+
*/
|
|
120
|
+
export const DEFAULT_API_BASE_PREFIX = '';
|
|
121
|
+
|
|
122
|
+
// ── Small AST helpers ──────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {ts.ClassElement | ts.ObjectLiteralElementLike | ts.PropertySignature} member
|
|
126
|
+
* @returns {string | null}
|
|
127
|
+
*/
|
|
128
|
+
function memberName(member) {
|
|
129
|
+
const name = member.name;
|
|
130
|
+
if (!name) return null;
|
|
131
|
+
if (ts.isIdentifier(name) || ts.isStringLiteral(name)) return name.text;
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {ts.ClassElement} member
|
|
137
|
+
*/
|
|
138
|
+
function isStatic(member) {
|
|
139
|
+
return Boolean(
|
|
140
|
+
ts.canHaveModifiers(member) &&
|
|
141
|
+
ts
|
|
142
|
+
.getModifiers(member)
|
|
143
|
+
?.some((m) => m.kind === ts.SyntaxKind.StaticKeyword),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Find a static property on a class by name.
|
|
149
|
+
*
|
|
150
|
+
* @param {ts.ClassDeclaration} node
|
|
151
|
+
* @param {string} name
|
|
152
|
+
* @returns {ts.PropertyDeclaration | null}
|
|
153
|
+
*/
|
|
154
|
+
function findStaticProperty(node, name) {
|
|
155
|
+
for (const member of node.members) {
|
|
156
|
+
if (!ts.isPropertyDeclaration(member)) continue;
|
|
157
|
+
if (!isStatic(member)) continue;
|
|
158
|
+
if (memberName(member) === name) return member;
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Does this class extend `Entity`?
|
|
165
|
+
*
|
|
166
|
+
* Deliberately narrow: an entity that extends something else would need its own
|
|
167
|
+
* handling, and misclassifying it is worse than omitting it.
|
|
168
|
+
*
|
|
169
|
+
* @param {ts.ClassDeclaration} node
|
|
170
|
+
*/
|
|
171
|
+
function extendsEntity(node) {
|
|
172
|
+
return Boolean(
|
|
173
|
+
node.heritageClauses?.some(
|
|
174
|
+
(clause) =>
|
|
175
|
+
clause.token === ts.SyntaxKind.ExtendsKeyword &&
|
|
176
|
+
clause.types.some(
|
|
177
|
+
(type) =>
|
|
178
|
+
ts.isIdentifier(type.expression) &&
|
|
179
|
+
type.expression.text === 'Entity',
|
|
180
|
+
),
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Read the comment text of a named JSDoc tag.
|
|
187
|
+
*
|
|
188
|
+
* Tag names must be single identifiers: TypeScript splits `@schema-none` into
|
|
189
|
+
* the tag `schema` with comment `-none`, which would collide with `@schema`.
|
|
190
|
+
* Hence `@noSchema` rather than a hyphenated name.
|
|
191
|
+
*
|
|
192
|
+
* @param {ts.Node} node
|
|
193
|
+
* @param {string} tagName
|
|
194
|
+
* @returns {{ present: boolean, comment: string }}
|
|
195
|
+
*/
|
|
196
|
+
function readJsDocTag(node, tagName) {
|
|
197
|
+
const [first] = readJsDocTags(node, tagName);
|
|
198
|
+
return first ?? { present: false, comment: '' };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Read every occurrence of a named JSDoc tag.
|
|
203
|
+
*
|
|
204
|
+
* Needed for `@schemaIgnore`, which is expected more than once per class.
|
|
205
|
+
*
|
|
206
|
+
* @param {ts.Node} node
|
|
207
|
+
* @param {string} tagName
|
|
208
|
+
* @returns {{ present: true, comment: string }[]}
|
|
209
|
+
*/
|
|
210
|
+
function readJsDocTags(node, tagName) {
|
|
211
|
+
const jsDocs = /** @type {{ jsDoc?: ts.JSDoc[] }} */ (node).jsDoc ?? [];
|
|
212
|
+
|
|
213
|
+
/** @type {{ present: true, comment: string }[]} */
|
|
214
|
+
const found = [];
|
|
215
|
+
|
|
216
|
+
for (const doc of jsDocs) {
|
|
217
|
+
for (const tag of doc.tags ?? []) {
|
|
218
|
+
if (tag.tagName.text !== tagName) continue;
|
|
219
|
+
found.push({
|
|
220
|
+
present: true,
|
|
221
|
+
// Collapse whitespace: a reason spanning several JSDoc lines arrives
|
|
222
|
+
// with newlines in it, which would break a markdown table row.
|
|
223
|
+
comment:
|
|
224
|
+
typeof tag.comment === 'string'
|
|
225
|
+
? tag.comment.replace(/\s+/g, ' ').trim()
|
|
226
|
+
: '',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return found;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Read a `@schemaSubset <reason>` tag.
|
|
236
|
+
*
|
|
237
|
+
* Marks an entity as a deliberate *projection* of its component: it models some
|
|
238
|
+
* of the serializer's fields and knowingly omits the rest. `ApplicationRowEntity`
|
|
239
|
+
* is a table-row view of `Application`; `UserEntity` is the auth whoami view of
|
|
240
|
+
* `User`.
|
|
241
|
+
*
|
|
242
|
+
* For these, the key-set check becomes one-directional - every field the entity
|
|
243
|
+
* declares must exist upstream, but it need not declare all of them. Extra or
|
|
244
|
+
* misnamed fields are still caught, which is the part that matters.
|
|
245
|
+
*
|
|
246
|
+
* @param {ts.Node} node
|
|
247
|
+
* @returns {string | null} the reason, '' if tagged without one, null if absent
|
|
248
|
+
*/
|
|
249
|
+
function readSchemaSubset(node) {
|
|
250
|
+
const { present, comment } = readJsDocTag(node, 'schemaSubset');
|
|
251
|
+
return present ? comment : null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Read `@schemaIgnore <field> <reason>` tags.
|
|
256
|
+
*
|
|
257
|
+
* These record the cases where the *schema* is wrong rather than the entity -
|
|
258
|
+
* most often a DRF `SerializerMethodField`, which drf-spectacular types as
|
|
259
|
+
* `string` unless annotated with `@extend_schema_field`, even when it returns a
|
|
260
|
+
* bool or an int.
|
|
261
|
+
*
|
|
262
|
+
* A reason is expected: without one this becomes a way to silence findings,
|
|
263
|
+
* which is what the tool exists to prevent. `plan.mjs` lists every suppression
|
|
264
|
+
* under `## Suppressions`, with its reason and the ops it is holding back, and
|
|
265
|
+
* marks the ones the schema has since caught up with as redundant.
|
|
266
|
+
*
|
|
267
|
+
* @param {ts.Node} node
|
|
268
|
+
* @returns {Record<string, string>} field name -> reason
|
|
269
|
+
*/
|
|
270
|
+
function readSchemaIgnores(node) {
|
|
271
|
+
/** @type {Record<string, string>} */
|
|
272
|
+
const ignores = {};
|
|
273
|
+
|
|
274
|
+
for (const { comment } of readJsDocTags(node, 'schemaIgnore')) {
|
|
275
|
+
const [field, ...rest] = comment.split(/\s+/);
|
|
276
|
+
if (!field) continue;
|
|
277
|
+
ignores[field] = rest.join(' ');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return ignores;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Read a `@schema <Component>` JSDoc tag off a node.
|
|
285
|
+
*
|
|
286
|
+
* This is how the entity-to-component mapping is persisted: decided once, then
|
|
287
|
+
* read from the file it describes rather than re-derived on every sync.
|
|
288
|
+
*
|
|
289
|
+
* @param {ts.Node} node
|
|
290
|
+
* @returns {string | null}
|
|
291
|
+
*/
|
|
292
|
+
function readSchemaAnnotation(node) {
|
|
293
|
+
const { comment } = readJsDocTag(node, 'schema');
|
|
294
|
+
return comment ? comment.split(/\s+/)[0] : null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Read every `@schemaAlso <Component>` JSDoc tag off a class: other components
|
|
299
|
+
* the entity also models, such as the narrower `NestedTrust` for `Trust`, so a
|
|
300
|
+
* field embedding one is checked against the entity. Never changes what the
|
|
301
|
+
* entity itself resolves to.
|
|
302
|
+
*
|
|
303
|
+
* @param {ts.Node} node
|
|
304
|
+
* @returns {string[]}
|
|
305
|
+
*/
|
|
306
|
+
function readSchemaAlsoAnnotations(node) {
|
|
307
|
+
return readJsDocTags(node, 'schemaAlso')
|
|
308
|
+
.map(({ comment }) => comment.split(/\s+/)[0])
|
|
309
|
+
.filter(Boolean);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Read a `@schemaEnum <Component>` JSDoc tag off a choices dict.
|
|
314
|
+
*
|
|
315
|
+
* A separate tag from `@schema`, on purpose. `@schema` means "this class models
|
|
316
|
+
* that component" and drives entity resolution; overloading it for a const would
|
|
317
|
+
* make a mis-parse able to silently re-map an entity, which is the failure mode
|
|
318
|
+
* the tag-name warnings in the skill doc exist to prevent.
|
|
319
|
+
*
|
|
320
|
+
* It also carries the name back: the convention drops the `Enum` suffix from the
|
|
321
|
+
* dict (`ApplicationStatusEnum` -> `ApplicationStatus`), so the tag is the only
|
|
322
|
+
* record of which component it came from.
|
|
323
|
+
*
|
|
324
|
+
* @param {ts.Node} node
|
|
325
|
+
* @returns {string | null}
|
|
326
|
+
*/
|
|
327
|
+
function readSchemaEnumAnnotation(node) {
|
|
328
|
+
const { comment } = readJsDocTag(node, 'schemaEnum');
|
|
329
|
+
return comment ? comment.split(/\s+/)[0] : null;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Read a `@noSchema <reason>` JSDoc tag, marking an entity as having no schema
|
|
334
|
+
* counterpart by design.
|
|
335
|
+
*
|
|
336
|
+
* Distinct from an entity that merely failed to resolve: this is a recorded
|
|
337
|
+
* decision, so it belongs in a separate section of the report rather than
|
|
338
|
+
* appearing as a failure on every run.
|
|
339
|
+
*
|
|
340
|
+
* Returns the reason, or an empty string when the tag is present without one,
|
|
341
|
+
* or null when absent - so callers can tell "declared, no reason" from
|
|
342
|
+
* "not declared".
|
|
343
|
+
*
|
|
344
|
+
* @param {ts.Node} node
|
|
345
|
+
* @returns {string | null}
|
|
346
|
+
*/
|
|
347
|
+
function readNoSchemaAnnotation(node) {
|
|
348
|
+
const { present, comment } = readJsDocTag(node, 'noSchema');
|
|
349
|
+
return present ? comment : null;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ── apiBase resolution ────────────────────────────────────────────────────────
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Resolve the `apiBase` expression to a path that can be looked up in the
|
|
356
|
+
* schema's `paths` index.
|
|
357
|
+
*
|
|
358
|
+
* Handles the three forms in use:
|
|
359
|
+
*
|
|
360
|
+
* '/application/' -> prefixed with the axios baseURL
|
|
361
|
+
* `${settings.POLICY_API_URL}/member_policy/` -> prefix substituted
|
|
362
|
+
* settings.AUTH_URL -> prefix substituted
|
|
363
|
+
*
|
|
364
|
+
* @param {ts.Expression} node
|
|
365
|
+
* @param {Record<string, string>} prefixes
|
|
366
|
+
* @param {string} basePrefix
|
|
367
|
+
* @returns {{ resolved: string | null, note: string | null }}
|
|
368
|
+
*/
|
|
369
|
+
function resolveApiBase(node, prefixes, basePrefix) {
|
|
370
|
+
// `settings.X`
|
|
371
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
372
|
+
const key = node.name.text;
|
|
373
|
+
const prefix = prefixes[key];
|
|
374
|
+
if (!prefix) {
|
|
375
|
+
return { resolved: null, note: `unknown settings key: settings.${key}` };
|
|
376
|
+
}
|
|
377
|
+
return { resolved: prefix, note: null };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// `'/application/'`
|
|
381
|
+
if (ts.isStringLiteral(node)) {
|
|
382
|
+
return { resolved: withBase(node.text, basePrefix), note: null };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// A template with no substitutions is just a string.
|
|
386
|
+
if (ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
387
|
+
return { resolved: withBase(node.text, basePrefix), note: null };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// `` `${settings.POLICY_API_URL}/member_policy/` ``
|
|
391
|
+
if (ts.isTemplateExpression(node)) {
|
|
392
|
+
let out = node.head.text;
|
|
393
|
+
|
|
394
|
+
for (const span of node.templateSpans) {
|
|
395
|
+
const expression = span.expression;
|
|
396
|
+
if (!ts.isPropertyAccessExpression(expression)) {
|
|
397
|
+
return {
|
|
398
|
+
resolved: null,
|
|
399
|
+
note: `unsupported template substitution: ${expression.getText()}`,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const key = expression.name.text;
|
|
404
|
+
const prefix = prefixes[key];
|
|
405
|
+
if (!prefix) {
|
|
406
|
+
return {
|
|
407
|
+
resolved: null,
|
|
408
|
+
note: `unknown settings key: settings.${key}`,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
out += prefix + span.literal.text;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Already absolute via the substituted prefix, so no base is applied.
|
|
416
|
+
return { resolved: out, note: null };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return { resolved: null, note: `unsupported apiBase expression` };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Apply the default axios `baseURL` prefix to a relative apiBase.
|
|
424
|
+
*
|
|
425
|
+
* @param {string} value
|
|
426
|
+
* @param {string} basePrefix
|
|
427
|
+
*/
|
|
428
|
+
function withBase(value, basePrefix) {
|
|
429
|
+
// DRF convention: anything under /api/ is already origin-relative and absolute.
|
|
430
|
+
if (value.startsWith('/api/')) return value;
|
|
431
|
+
if (basePrefix && value.startsWith(basePrefix)) return value;
|
|
432
|
+
return `${basePrefix}${value.startsWith('/') ? '' : '/'}${value}`;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// ── Field declarations ────────────────────────────────────────────────────────
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Parse one `name: new Fields.XField({ … })` entry.
|
|
439
|
+
*
|
|
440
|
+
* @param {ts.ObjectLiteralElementLike} property
|
|
441
|
+
* @returns {EntityFieldDecl | null}
|
|
442
|
+
*/
|
|
443
|
+
function parseFieldDecl(property) {
|
|
444
|
+
if (!ts.isPropertyAssignment(property)) return null;
|
|
445
|
+
|
|
446
|
+
const name = memberName(property);
|
|
447
|
+
if (!name) return null;
|
|
448
|
+
|
|
449
|
+
/** @type {EntityFieldDecl} */
|
|
450
|
+
const decl = {
|
|
451
|
+
name,
|
|
452
|
+
fieldClass: null,
|
|
453
|
+
opts: {},
|
|
454
|
+
entityRef: null,
|
|
455
|
+
many: false,
|
|
456
|
+
readOnly: false,
|
|
457
|
+
blank: false,
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
const initializer = property.initializer;
|
|
461
|
+
if (!ts.isNewExpression(initializer)) {
|
|
462
|
+
// e.g. a shared field instance or a spread; recorded with no class so the
|
|
463
|
+
// report can flag it rather than the diff silently retyping it.
|
|
464
|
+
return decl;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// `Fields.CharField` or a bare `CustomField`
|
|
468
|
+
const callee = initializer.expression;
|
|
469
|
+
if (ts.isPropertyAccessExpression(callee)) {
|
|
470
|
+
decl.fieldClass = callee.name.text;
|
|
471
|
+
} else if (ts.isIdentifier(callee)) {
|
|
472
|
+
decl.fieldClass = callee.text;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const [options] = initializer.arguments ?? [];
|
|
476
|
+
if (options && ts.isObjectLiteralExpression(options)) {
|
|
477
|
+
for (const option of options.properties) {
|
|
478
|
+
if (!ts.isPropertyAssignment(option)) continue;
|
|
479
|
+
const optionName = memberName(option);
|
|
480
|
+
if (!optionName) continue;
|
|
481
|
+
|
|
482
|
+
decl.opts[optionName] = option.initializer.getText();
|
|
483
|
+
|
|
484
|
+
if (optionName === 'entity' && ts.isIdentifier(option.initializer)) {
|
|
485
|
+
decl.entityRef = option.initializer.text;
|
|
486
|
+
}
|
|
487
|
+
if (option.initializer.kind === ts.SyntaxKind.TrueKeyword) {
|
|
488
|
+
if (optionName === 'many') decl.many = true;
|
|
489
|
+
if (optionName === 'readOnly') decl.readOnly = true;
|
|
490
|
+
if (optionName === 'blank') decl.blank = true;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return decl;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* @param {ts.PropertyDeclaration} fieldsProperty
|
|
500
|
+
* @returns {EntityFieldDecl[]}
|
|
501
|
+
*/
|
|
502
|
+
function parseFields(fieldsProperty) {
|
|
503
|
+
const initializer = fieldsProperty.initializer;
|
|
504
|
+
if (!initializer || !ts.isObjectLiteralExpression(initializer)) return [];
|
|
505
|
+
|
|
506
|
+
/** @type {EntityFieldDecl[]} */
|
|
507
|
+
const fields = [];
|
|
508
|
+
for (const property of initializer.properties) {
|
|
509
|
+
const decl = parseFieldDecl(property);
|
|
510
|
+
if (decl) fields.push(decl);
|
|
511
|
+
}
|
|
512
|
+
return fields;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Read the type argument of an `EntityFields<T>` annotation.
|
|
517
|
+
*
|
|
518
|
+
* Preferred over deriving `${className}Fields`, because the two disagree in
|
|
519
|
+
* practice - `AmsApp` is annotated `EntityFields<AppEntityFields>`.
|
|
520
|
+
*
|
|
521
|
+
* @param {ts.PropertyDeclaration} fieldsProperty
|
|
522
|
+
* @returns {string | null}
|
|
523
|
+
*/
|
|
524
|
+
function readFieldsTypeName(fieldsProperty) {
|
|
525
|
+
const type = fieldsProperty.type;
|
|
526
|
+
if (!type || !ts.isTypeReferenceNode(type)) return null;
|
|
527
|
+
if (!ts.isIdentifier(type.typeName)) return null;
|
|
528
|
+
if (type.typeName.text !== 'EntityFields') return null;
|
|
529
|
+
|
|
530
|
+
const [argument] = type.typeArguments ?? [];
|
|
531
|
+
if (!argument || !ts.isTypeReferenceNode(argument)) return null;
|
|
532
|
+
if (!ts.isIdentifier(argument.typeName)) return null;
|
|
533
|
+
|
|
534
|
+
return argument.typeName.text;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// ── File-level collection ─────────────────────────────────────────────────────
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Collect `export type X = { … }` members and raw text, keyed by type name.
|
|
541
|
+
*
|
|
542
|
+
* `exported` is tracked because `gen-asserts.mjs` can only assert against a type
|
|
543
|
+
* it can import; an unexported one has to be skipped and reported.
|
|
544
|
+
*
|
|
545
|
+
* @param {ts.SourceFile} sourceFile
|
|
546
|
+
* @returns {Map<string, { members: TypeMember[], text: string, exported: boolean }>}
|
|
547
|
+
*/
|
|
548
|
+
function collectTypeAliases(sourceFile) {
|
|
549
|
+
/** @type {Map<string, { members: TypeMember[], text: string, exported: boolean }>} */
|
|
550
|
+
const aliases = new Map();
|
|
551
|
+
|
|
552
|
+
for (const statement of sourceFile.statements) {
|
|
553
|
+
if (!ts.isTypeAliasDeclaration(statement)) continue;
|
|
554
|
+
|
|
555
|
+
/** @type {TypeMember[]} */
|
|
556
|
+
const members = [];
|
|
557
|
+
|
|
558
|
+
if (ts.isTypeLiteralNode(statement.type)) {
|
|
559
|
+
for (const member of statement.type.members) {
|
|
560
|
+
if (!ts.isPropertySignature(member) || !member.type) continue;
|
|
561
|
+
const name = memberName(member);
|
|
562
|
+
if (!name) continue;
|
|
563
|
+
members.push({
|
|
564
|
+
name,
|
|
565
|
+
// Verbatim text, so the convention check can see whether a nested
|
|
566
|
+
// field was written as `XEntityFields` or `XEntityRecord`.
|
|
567
|
+
typeText: member.type.getText(),
|
|
568
|
+
optional: Boolean(member.questionToken),
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
aliases.set(statement.name.text, {
|
|
574
|
+
members,
|
|
575
|
+
text: statement.type.getText(),
|
|
576
|
+
exported: Boolean(
|
|
577
|
+
ts
|
|
578
|
+
.getModifiers(statement)
|
|
579
|
+
?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword),
|
|
580
|
+
),
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
return aliases;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Collect `export const X = { … } as const` choices dicts.
|
|
589
|
+
*
|
|
590
|
+
* Only all-string-valued object literals qualify. That excludes the label and
|
|
591
|
+
* description sidecars (`SupplierStatusLabel`, `ApplicationStatusChoices`),
|
|
592
|
+
* which are also `as const` but are keyed *by* an enum value rather than being
|
|
593
|
+
* one - they are derived from the dict, so asserting them against the broker
|
|
594
|
+
* would be asserting the same thing twice. Ordering arrays such as
|
|
595
|
+
* `SupplierStatusOrder` fall out too, being array literals rather than objects.
|
|
596
|
+
*
|
|
597
|
+
* The JSDoc hangs off the `VariableStatement`, not the `VariableDeclaration`, so
|
|
598
|
+
* the tag has to be read from the statement.
|
|
599
|
+
*
|
|
600
|
+
* @param {ts.SourceFile} sourceFile
|
|
601
|
+
* @param {string} file
|
|
602
|
+
* @param {Map<string, { members: TypeMember[], text: string, exported: boolean }>} aliases
|
|
603
|
+
* @returns {ParsedEnum[]}
|
|
604
|
+
*/
|
|
605
|
+
function collectEnums(sourceFile, file, aliases) {
|
|
606
|
+
/** @type {ParsedEnum[]} */
|
|
607
|
+
const enums = [];
|
|
608
|
+
|
|
609
|
+
for (const statement of sourceFile.statements) {
|
|
610
|
+
if (!ts.isVariableStatement(statement)) continue;
|
|
611
|
+
|
|
612
|
+
const exported = Boolean(
|
|
613
|
+
ts
|
|
614
|
+
.getModifiers(statement)
|
|
615
|
+
?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword),
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
619
|
+
if (!ts.isIdentifier(declaration.name)) continue;
|
|
620
|
+
if (!declaration.initializer) continue;
|
|
621
|
+
|
|
622
|
+
// `{ … } as const` - unwrap the assertion to reach the literal.
|
|
623
|
+
const initializer = declaration.initializer;
|
|
624
|
+
if (!ts.isAsExpression(initializer)) continue;
|
|
625
|
+
if (!ts.isObjectLiteralExpression(initializer.expression)) continue;
|
|
626
|
+
|
|
627
|
+
/** @type {Record<string, string>} */
|
|
628
|
+
const members = {};
|
|
629
|
+
let allStrings = true;
|
|
630
|
+
|
|
631
|
+
for (const property of initializer.expression.properties) {
|
|
632
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
633
|
+
allStrings = false;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
// A computed key means the object is keyed by another enum's values -
|
|
637
|
+
// a label map, not an enum.
|
|
638
|
+
if (ts.isComputedPropertyName(property.name)) {
|
|
639
|
+
allStrings = false;
|
|
640
|
+
break;
|
|
641
|
+
}
|
|
642
|
+
const key = memberName(property);
|
|
643
|
+
if (!key || !ts.isStringLiteral(property.initializer)) {
|
|
644
|
+
allStrings = false;
|
|
645
|
+
break;
|
|
646
|
+
}
|
|
647
|
+
members[key] = property.initializer.text;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
if (!allStrings || !Object.keys(members).length) continue;
|
|
651
|
+
|
|
652
|
+
const name = declaration.name.text;
|
|
653
|
+
|
|
654
|
+
// The convention is declaration merging - the type alias shares the
|
|
655
|
+
// dict's name - but three older shapes predate it and are still valid:
|
|
656
|
+
// `QuestionWidget`/`QuestionWidgetValue`, and the const literally named
|
|
657
|
+
// `SupplierStatusEnum` whose type dropped the suffix. Resolve whichever
|
|
658
|
+
// is present rather than forcing a rename, since the assertion compares
|
|
659
|
+
// the union either way and the name has no bearing on correctness.
|
|
660
|
+
const typeName =
|
|
661
|
+
[name, `${name}Value`, name.replace(/Enum$/, '')].find(
|
|
662
|
+
(candidate) => aliases.get(candidate)?.exported,
|
|
663
|
+
) ?? null;
|
|
664
|
+
|
|
665
|
+
enums.push({
|
|
666
|
+
file,
|
|
667
|
+
name,
|
|
668
|
+
schemaEnum: readSchemaEnumAnnotation(statement),
|
|
669
|
+
members,
|
|
670
|
+
values: Object.values(members),
|
|
671
|
+
exported,
|
|
672
|
+
typeName,
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
return enums;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Collect `SomeEntity.duck = new DuckClass({ … })` assignments.
|
|
682
|
+
*
|
|
683
|
+
* @param {ts.SourceFile} sourceFile
|
|
684
|
+
* @returns {Map<string, string>} className -> duck class name
|
|
685
|
+
*/
|
|
686
|
+
function collectDucks(sourceFile) {
|
|
687
|
+
/** @type {Map<string, string>} */
|
|
688
|
+
const ducks = new Map();
|
|
689
|
+
|
|
690
|
+
for (const statement of sourceFile.statements) {
|
|
691
|
+
if (!ts.isExpressionStatement(statement)) continue;
|
|
692
|
+
|
|
693
|
+
const expression = statement.expression;
|
|
694
|
+
if (!ts.isBinaryExpression(expression)) continue;
|
|
695
|
+
if (expression.operatorToken.kind !== ts.SyntaxKind.EqualsToken) continue;
|
|
696
|
+
|
|
697
|
+
const target = expression.left;
|
|
698
|
+
if (!ts.isPropertyAccessExpression(target)) continue;
|
|
699
|
+
if (target.name.text !== 'duck') continue;
|
|
700
|
+
if (!ts.isIdentifier(target.expression)) continue;
|
|
701
|
+
|
|
702
|
+
const value = expression.right;
|
|
703
|
+
if (!ts.isNewExpression(value)) continue;
|
|
704
|
+
|
|
705
|
+
const callee = value.expression;
|
|
706
|
+
const duckName = ts.isIdentifier(callee)
|
|
707
|
+
? callee.text
|
|
708
|
+
: ts.isPropertyAccessExpression(callee)
|
|
709
|
+
? callee.name.text
|
|
710
|
+
: null;
|
|
711
|
+
|
|
712
|
+
if (duckName) ducks.set(target.expression.text, duckName);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
return ducks;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Find the identifier in `export default X`.
|
|
720
|
+
*
|
|
721
|
+
* @param {ts.SourceFile} sourceFile
|
|
722
|
+
* @returns {string | null}
|
|
723
|
+
*/
|
|
724
|
+
function findDefaultExportName(sourceFile) {
|
|
725
|
+
for (const statement of sourceFile.statements) {
|
|
726
|
+
if (!ts.isExportAssignment(statement)) continue;
|
|
727
|
+
if (ts.isIdentifier(statement.expression)) return statement.expression.text;
|
|
728
|
+
}
|
|
729
|
+
return null;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// ── Entry points ──────────────────────────────────────────────────────────────
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Parse a single entity source file.
|
|
736
|
+
*
|
|
737
|
+
* @param {string} source
|
|
738
|
+
* @param {string} file repo-relative path, recorded on each entity
|
|
739
|
+
* @param {{ prefixes?: Record<string, string>, basePrefix?: string }} [options]
|
|
740
|
+
* @returns {ParsedEntity[]}
|
|
741
|
+
*/
|
|
742
|
+
export function parseEntitySource(source, file, options = {}) {
|
|
743
|
+
const prefixes = options.prefixes ?? DEFAULT_SETTINGS_PREFIXES;
|
|
744
|
+
const basePrefix = options.basePrefix ?? DEFAULT_API_BASE_PREFIX;
|
|
745
|
+
|
|
746
|
+
const sourceFile = ts.createSourceFile(
|
|
747
|
+
file,
|
|
748
|
+
source,
|
|
749
|
+
ts.ScriptTarget.ES2022,
|
|
750
|
+
true,
|
|
751
|
+
);
|
|
752
|
+
|
|
753
|
+
const aliases = collectTypeAliases(sourceFile);
|
|
754
|
+
const ducks = collectDucks(sourceFile);
|
|
755
|
+
const defaultExport = findDefaultExportName(sourceFile);
|
|
756
|
+
|
|
757
|
+
/** @type {ParsedEntity[]} */
|
|
758
|
+
const entities = [];
|
|
759
|
+
|
|
760
|
+
for (const statement of sourceFile.statements) {
|
|
761
|
+
if (!ts.isClassDeclaration(statement)) continue;
|
|
762
|
+
if (!statement.name || !extendsEntity(statement)) continue;
|
|
763
|
+
|
|
764
|
+
const className = statement.name.text;
|
|
765
|
+
|
|
766
|
+
const fieldsProperty = findStaticProperty(statement, 'fields');
|
|
767
|
+
const fieldsTypeName = fieldsProperty
|
|
768
|
+
? (readFieldsTypeName(fieldsProperty) ?? `${className}Fields`)
|
|
769
|
+
: null;
|
|
770
|
+
|
|
771
|
+
// Two naming conventions are both in use, so try the suffix swap first and
|
|
772
|
+
// fall back to the class name. `Notifications.ts` pairs
|
|
773
|
+
// `ActionNotificationFields` with `ActionNotificationEntityRecord`, which
|
|
774
|
+
// only the second form finds.
|
|
775
|
+
const recordTypeName = fieldsTypeName
|
|
776
|
+
? ([
|
|
777
|
+
fieldsTypeName.replace(/Fields$/, 'Record'),
|
|
778
|
+
`${className}Record`,
|
|
779
|
+
].find((candidate) => aliases.has(candidate)) ??
|
|
780
|
+
fieldsTypeName.replace(/Fields$/, 'Record'))
|
|
781
|
+
: null;
|
|
782
|
+
|
|
783
|
+
/** @type {string | null} */
|
|
784
|
+
let apiBaseRaw = null;
|
|
785
|
+
/** @type {string | null} */
|
|
786
|
+
let apiBase = null;
|
|
787
|
+
/** @type {string | null} */
|
|
788
|
+
let apiBaseNote = null;
|
|
789
|
+
|
|
790
|
+
const pathsProperty = findStaticProperty(statement, 'paths');
|
|
791
|
+
if (pathsProperty?.initializer) {
|
|
792
|
+
if (ts.isObjectLiteralExpression(pathsProperty.initializer)) {
|
|
793
|
+
const apiBaseProperty = pathsProperty.initializer.properties.find(
|
|
794
|
+
(p) => memberName(p) === 'apiBase',
|
|
795
|
+
);
|
|
796
|
+
if (apiBaseProperty && ts.isPropertyAssignment(apiBaseProperty)) {
|
|
797
|
+
apiBaseRaw = apiBaseProperty.initializer.getText();
|
|
798
|
+
const resolution = resolveApiBase(
|
|
799
|
+
apiBaseProperty.initializer,
|
|
800
|
+
prefixes,
|
|
801
|
+
basePrefix,
|
|
802
|
+
);
|
|
803
|
+
apiBase = resolution.resolved;
|
|
804
|
+
apiBaseNote = resolution.note;
|
|
805
|
+
}
|
|
806
|
+
} else {
|
|
807
|
+
apiBaseRaw = pathsProperty.initializer.getText();
|
|
808
|
+
apiBaseNote = 'static paths is not an object literal';
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
const paginatedProperty = findStaticProperty(statement, 'paginated');
|
|
813
|
+
const paginated =
|
|
814
|
+
paginatedProperty?.initializer?.kind === ts.SyntaxKind.TrueKeyword;
|
|
815
|
+
|
|
816
|
+
entities.push({
|
|
817
|
+
file,
|
|
818
|
+
className,
|
|
819
|
+
isDefaultExport: className === defaultExport,
|
|
820
|
+
schemaAnnotation: readSchemaAnnotation(statement),
|
|
821
|
+
schemaAlso: readSchemaAlsoAnnotations(statement),
|
|
822
|
+
noSchema: readNoSchemaAnnotation(statement),
|
|
823
|
+
schemaIgnores: readSchemaIgnores(statement),
|
|
824
|
+
schemaSubset: readSchemaSubset(statement),
|
|
825
|
+
apiBaseRaw,
|
|
826
|
+
apiBase,
|
|
827
|
+
apiBaseNote,
|
|
828
|
+
paginated,
|
|
829
|
+
duck: ducks.get(className) ?? null,
|
|
830
|
+
fieldsTypeName,
|
|
831
|
+
fields: fieldsProperty ? parseFields(fieldsProperty) : [],
|
|
832
|
+
fieldsTypeMembers: fieldsTypeName
|
|
833
|
+
? (aliases.get(fieldsTypeName)?.members ?? [])
|
|
834
|
+
: [],
|
|
835
|
+
recordTypeName,
|
|
836
|
+
recordTypeText: recordTypeName
|
|
837
|
+
? (aliases.get(recordTypeName)?.text ?? null)
|
|
838
|
+
: null,
|
|
839
|
+
fieldsTypeExported: Boolean(
|
|
840
|
+
fieldsTypeName && aliases.get(fieldsTypeName)?.exported,
|
|
841
|
+
),
|
|
842
|
+
recordTypeExported: Boolean(
|
|
843
|
+
recordTypeName && aliases.get(recordTypeName)?.exported,
|
|
844
|
+
),
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
return entities;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Parse the choices dicts out of a single entity source file.
|
|
853
|
+
*
|
|
854
|
+
* Separate from `parseEntitySource` because enums are not owned by a class -
|
|
855
|
+
* they sit at file scope, and `ApplicationPolicySnapshot.ts` declares six of
|
|
856
|
+
* them and no entity at all.
|
|
857
|
+
*
|
|
858
|
+
* @param {string} source
|
|
859
|
+
* @param {string} file repo-relative path, recorded on each enum
|
|
860
|
+
* @returns {ParsedEnum[]}
|
|
861
|
+
*/
|
|
862
|
+
export function parseEnumSource(source, file) {
|
|
863
|
+
const sourceFile = ts.createSourceFile(
|
|
864
|
+
file,
|
|
865
|
+
source,
|
|
866
|
+
ts.ScriptTarget.ES2022,
|
|
867
|
+
true,
|
|
868
|
+
);
|
|
869
|
+
|
|
870
|
+
return collectEnums(sourceFile, file, collectTypeAliases(sourceFile));
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Walk every `.ts` file under a directory, recursing into subdirectories such
|
|
875
|
+
* as `filter/`.
|
|
876
|
+
*
|
|
877
|
+
* Test files are skipped; index barrels contribute no declarations of either
|
|
878
|
+
* kind, so they fall out naturally.
|
|
879
|
+
*
|
|
880
|
+
* @param {string} dir absolute path to walk
|
|
881
|
+
* @param {string} repoRoot absolute path, used to make `file` relative
|
|
882
|
+
* @param {(source: string, file: string) => void} visit
|
|
883
|
+
*/
|
|
884
|
+
function walkTsFiles(dir, repoRoot, visit) {
|
|
885
|
+
for (const dirent of readdirSync(dir, { withFileTypes: true })) {
|
|
886
|
+
const path = join(dir, dirent.name);
|
|
887
|
+
|
|
888
|
+
if (dirent.isDirectory()) {
|
|
889
|
+
walkTsFiles(path, repoRoot, visit);
|
|
890
|
+
continue;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
if (!dirent.name.endsWith('.ts')) continue;
|
|
894
|
+
if (dirent.name.includes('.test.')) continue;
|
|
895
|
+
|
|
896
|
+
visit(readFileSync(path, 'utf8'), relative(repoRoot, path));
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Parse every `.ts` entity file under a directory.
|
|
902
|
+
*
|
|
903
|
+
* @param {string} entitiesDir absolute path to `src/entities`
|
|
904
|
+
* @param {string} repoRoot absolute path, used to make `file` relative
|
|
905
|
+
* @param {{ prefixes?: Record<string, string>, basePrefix?: string }} [options]
|
|
906
|
+
* @returns {ParsedEntity[]}
|
|
907
|
+
*/
|
|
908
|
+
export function parseEntitiesDir(entitiesDir, repoRoot, options = {}) {
|
|
909
|
+
/** @type {ParsedEntity[]} */
|
|
910
|
+
const entities = [];
|
|
911
|
+
|
|
912
|
+
walkTsFiles(entitiesDir, repoRoot, (source, file) => {
|
|
913
|
+
entities.push(...parseEntitySource(source, file, options));
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
return entities;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* Parse every choices dict under a directory.
|
|
921
|
+
*
|
|
922
|
+
* @param {string} entitiesDir absolute path to `src/entities`
|
|
923
|
+
* @param {string} repoRoot absolute path, used to make `file` relative
|
|
924
|
+
* @returns {ParsedEnum[]}
|
|
925
|
+
*/
|
|
926
|
+
export function parseEnumsDir(entitiesDir, repoRoot) {
|
|
927
|
+
/** @type {ParsedEnum[]} */
|
|
928
|
+
const enums = [];
|
|
929
|
+
|
|
930
|
+
walkTsFiles(entitiesDir, repoRoot, (source, file) => {
|
|
931
|
+
enums.push(...parseEnumSource(source, file));
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
return enums;
|
|
935
|
+
}
|