@drincs/pixi-vn-json 1.13.15 → 1.13.17
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/dist/actions.cjs +2 -2
- package/dist/actions.d.cts +58 -2
- package/dist/actions.d.ts +58 -2
- package/dist/actions.mjs +2 -2
- package/dist/constants.cjs +1 -1
- package/dist/constants.mjs +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +2 -2
- package/dist/interpreter.cjs +1 -1
- package/dist/interpreter.mjs +1 -1
- package/package.json +7 -3
- package/scripts/generate-container-memory-schema.mjs +107 -0
- package/scripts/generate-entrance-transition-schemas.mjs +90 -0
- package/scripts/generate-schema.mjs +64 -0
- package/scripts/schema-generator.mjs +763 -0
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Generic TypeScript-interface → JSON-Schema generator, built on the TypeScript compiler API
|
|
4
|
+
* (AST traversal for "local" declarations, the type checker for everything else). Extracted from
|
|
5
|
+
* `generate-schema.mjs` (which used it, hardcoded, to generate `PixiVNJson`'s own schema) so it
|
|
6
|
+
* can also be pointed at arbitrary interfaces from *other* packages — e.g. `@drincs/pixi-vn`'s
|
|
7
|
+
* canvas transition prop types, consumed by `@drincs/pixi-vn-ink`.
|
|
8
|
+
*
|
|
9
|
+
* Two kinds of type get treated differently:
|
|
10
|
+
* - "Local" declarations — interfaces/type aliases found in a file under `interfaceDir` (when
|
|
11
|
+
* provided) — are walked directly from their AST (via `typeDecls`), fully, with no depth limit.
|
|
12
|
+
* This is how `PixiVNJson`'s own schema (all declared in this package's `src/schema`) has always
|
|
13
|
+
* been generated: every cross-reference between its own types resolves exactly, however deep.
|
|
14
|
+
* - Everything else ("external" types — from `@drincs/pixi-vn`, `pixi.js`, `motion`, or simply any
|
|
15
|
+
* type outside `interfaceDir`) is resolved via the type checker's fully-resolved `ts.Type`
|
|
16
|
+
* (so inherited members from `extends`/intersections are already flattened) and capped at
|
|
17
|
+
* {@link GenerateJsonSchemaOptions.maxDepth} nested object levels — see its docs for exactly
|
|
18
|
+
* what "depth" counts.
|
|
19
|
+
*
|
|
20
|
+
* A name in `rootTypeNames` can be either kind: a local type (e.g. `"PixiVNJson"`) is expanded
|
|
21
|
+
* with no cap, while a name that's only found by scanning `rootFiles`' own top-level declarations
|
|
22
|
+
* (not in `interfaceDir`) is generated via the capped "external" path from the start — this is how
|
|
23
|
+
* a shallow, `maxDepth: 1` schema for e.g. `MoveInOutProps` is produced.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { createRequire } from "node:module";
|
|
27
|
+
|
|
28
|
+
const require = createRequire(import.meta.url);
|
|
29
|
+
const ts = require("typescript");
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {object} GenerateJsonSchemaOptions
|
|
33
|
+
* @property {string[]} rootFiles Absolute paths of files that must be part of the TS program —
|
|
34
|
+
* both the source of any name in `rootTypeNames` not found under `interfaceDir`, and (when
|
|
35
|
+
* `interfaceDir` is omitted) the only files scanned for "local" declarations.
|
|
36
|
+
* @property {string} [tsconfigPath] Path to a tsconfig.json to read `compilerOptions` (and
|
|
37
|
+
* additional root files) from. When omitted, a minimal default (ES2022/ESNext/bundler
|
|
38
|
+
* resolution, `skipLibCheck: true`) is used — sufficient for reading plain `.d.ts` files.
|
|
39
|
+
* @property {object} [compilerOptions] Extra compiler options, merged over (and taking priority
|
|
40
|
+
* over) whatever `tsconfigPath` provided.
|
|
41
|
+
* @property {string} [interfaceDir] Absolute directory prefix. Declarations from source files
|
|
42
|
+
* under this directory are treated as "local" (see file doc above). Omit to treat every type as
|
|
43
|
+
* "external" (capped at `maxDepth`).
|
|
44
|
+
* @property {string[]} rootTypeNames Interface/type-alias names to generate a top-level schema
|
|
45
|
+
* for, one entry each in the returned `schemas` map.
|
|
46
|
+
* @property {number} [maxDepth=1] How many nested *object*-typed property levels get expanded for
|
|
47
|
+
* "external" types before collapsing to an opaque `{ type: "object" }`. The root type's own
|
|
48
|
+
* properties are always level 1 (so `maxDepth: 1`, the default, types every direct property —
|
|
49
|
+
* primitives, arrays, unions — correctly, but any property that is itself an object collapses
|
|
50
|
+
* without listing its members). `maxDepth: 2` expands one further level, and so on. Local types
|
|
51
|
+
* (see `interfaceDir`) are never subject to this cap.
|
|
52
|
+
* @property {boolean} [includeAllLocalDeclarations=false] When `true`, every declaration found
|
|
53
|
+
* under `interfaceDir` gets a `definitions` entry (even ones never referenced from
|
|
54
|
+
* `rootTypeNames`) — used by `generate-schema.mjs` so the published `PixiVNJson` schema bundle
|
|
55
|
+
* includes every local type, not just the ones reachable from the `PixiVNJson` root.
|
|
56
|
+
* @property {Record<string, object>} [typeNameOverrides] Schema to use verbatim whenever a
|
|
57
|
+
* `TypeReference` AST node by this exact name is encountered while walking a "local" declaration
|
|
58
|
+
* (see `interfaceDir`) — checked before both `typeDecls` and the external-type resolution
|
|
59
|
+
* fallback, so it works for a name that's ambiguous or expensive to resolve structurally (e.g.
|
|
60
|
+
* collapsing a large external union to a simple `{ type: [...] }`). Only applies on that
|
|
61
|
+
* AST-node path: a property discovered while enumerating an external object's own members via
|
|
62
|
+
* the type checker (i.e. anything nested inside a name in `rootTypeNames` that itself resolved
|
|
63
|
+
* as "external") is not affected.
|
|
64
|
+
* @returns {{ schemas: Record<string, object>, definitions: Record<string, object> }}
|
|
65
|
+
*/
|
|
66
|
+
export function generateJsonSchema(options) {
|
|
67
|
+
const {
|
|
68
|
+
rootFiles,
|
|
69
|
+
tsconfigPath,
|
|
70
|
+
compilerOptions: extraCompilerOptions = {},
|
|
71
|
+
interfaceDir,
|
|
72
|
+
rootTypeNames,
|
|
73
|
+
maxDepth = 1,
|
|
74
|
+
includeAllLocalDeclarations = false,
|
|
75
|
+
typeNameOverrides = {},
|
|
76
|
+
} = options;
|
|
77
|
+
|
|
78
|
+
// ─── TypeScript Program Setup ───────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
let baseOptions = {
|
|
81
|
+
target: ts.ScriptTarget.ES2022,
|
|
82
|
+
module: ts.ModuleKind.ESNext,
|
|
83
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
84
|
+
lib: ["lib.es2022.d.ts", "lib.dom.d.ts"],
|
|
85
|
+
skipLibCheck: true,
|
|
86
|
+
esModuleInterop: true,
|
|
87
|
+
allowJs: false,
|
|
88
|
+
strict: false,
|
|
89
|
+
};
|
|
90
|
+
let configRootNames = [];
|
|
91
|
+
|
|
92
|
+
if (tsconfigPath) {
|
|
93
|
+
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
|
|
94
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
95
|
+
configFile.config,
|
|
96
|
+
ts.sys,
|
|
97
|
+
require("node:path").dirname(tsconfigPath),
|
|
98
|
+
);
|
|
99
|
+
baseOptions = parsed.options;
|
|
100
|
+
configRootNames = parsed.fileNames;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const options_ = {
|
|
104
|
+
...baseOptions,
|
|
105
|
+
...extraCompilerOptions,
|
|
106
|
+
noEmit: true,
|
|
107
|
+
};
|
|
108
|
+
delete options_.outDir;
|
|
109
|
+
delete options_.declaration;
|
|
110
|
+
delete options_.declarationDir;
|
|
111
|
+
delete options_.declarationMap;
|
|
112
|
+
|
|
113
|
+
const rootNames = [...new Set([...configRootNames, ...rootFiles])];
|
|
114
|
+
const program = ts.createProgram({ rootNames, options: options_ });
|
|
115
|
+
const checker = program.getTypeChecker();
|
|
116
|
+
|
|
117
|
+
// ─── Collect "local" Type Declarations ──────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
/** Map of type name → AST declaration node, for types treated as "local" (unlimited depth). */
|
|
120
|
+
const typeDecls = new Map();
|
|
121
|
+
/** Map of type name → list of type parameter names, for generic local types. */
|
|
122
|
+
const typeParams = new Map();
|
|
123
|
+
|
|
124
|
+
if (interfaceDir) {
|
|
125
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
126
|
+
if (!sourceFile.fileName.startsWith(interfaceDir)) continue;
|
|
127
|
+
collectDeclarations(sourceFile);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function collectDeclarations(sourceFile) {
|
|
132
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
133
|
+
if (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) {
|
|
134
|
+
const name = node.name.text;
|
|
135
|
+
typeDecls.set(name, node);
|
|
136
|
+
if (node.typeParameters && node.typeParameters.length > 0) {
|
|
137
|
+
typeParams.set(
|
|
138
|
+
name,
|
|
139
|
+
node.typeParameters.map((tp) => tp.name.text),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ─── JSON Schema Generation ──────────────────────────────────────────────────
|
|
147
|
+
|
|
148
|
+
/** All definitions collected during traversal. */
|
|
149
|
+
const definitions = {};
|
|
150
|
+
/** Set of definition keys currently being processed (cycle detection). */
|
|
151
|
+
const processing = new Set();
|
|
152
|
+
/** Maximum key depth to prevent infinite expansion of recursive generics. */
|
|
153
|
+
const MAX_ARG_NESTING = 3;
|
|
154
|
+
|
|
155
|
+
function argKeyDepth(key) {
|
|
156
|
+
return (key.match(/Arr/g) || []).length;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function convertTypeNode(node, ctx) {
|
|
160
|
+
if (!node) return {};
|
|
161
|
+
|
|
162
|
+
switch (node.kind) {
|
|
163
|
+
case ts.SyntaxKind.StringKeyword:
|
|
164
|
+
return { type: "string" };
|
|
165
|
+
case ts.SyntaxKind.NumberKeyword:
|
|
166
|
+
return { type: "number" };
|
|
167
|
+
case ts.SyntaxKind.BooleanKeyword:
|
|
168
|
+
return { type: "boolean" };
|
|
169
|
+
case ts.SyntaxKind.NullKeyword:
|
|
170
|
+
return { type: "null" };
|
|
171
|
+
case ts.SyntaxKind.UndefinedKeyword:
|
|
172
|
+
return {};
|
|
173
|
+
case ts.SyntaxKind.AnyKeyword:
|
|
174
|
+
case ts.SyntaxKind.UnknownKeyword:
|
|
175
|
+
case ts.SyntaxKind.ObjectKeyword:
|
|
176
|
+
return {};
|
|
177
|
+
case ts.SyntaxKind.NeverKeyword:
|
|
178
|
+
return { not: {} };
|
|
179
|
+
case ts.SyntaxKind.VoidKeyword:
|
|
180
|
+
return {};
|
|
181
|
+
|
|
182
|
+
case ts.SyntaxKind.LiteralType: {
|
|
183
|
+
const lit = node.literal;
|
|
184
|
+
if (ts.isStringLiteral(lit)) return { const: lit.text };
|
|
185
|
+
if (ts.isNumericLiteral(lit)) return { const: Number(lit.text) };
|
|
186
|
+
if (lit.kind === ts.SyntaxKind.TrueKeyword) return { const: true };
|
|
187
|
+
if (lit.kind === ts.SyntaxKind.FalseKeyword) return { const: false };
|
|
188
|
+
if (lit.kind === ts.SyntaxKind.NullKeyword) return { type: "null" };
|
|
189
|
+
return {};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
case ts.SyntaxKind.ArrayType: {
|
|
193
|
+
const items = convertTypeNode(node.elementType, ctx);
|
|
194
|
+
return { type: "array", items };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
case ts.SyntaxKind.TupleType: {
|
|
198
|
+
const elements = node.elements.map((el) => convertTypeNode(el, ctx));
|
|
199
|
+
return { type: "array", items: elements };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
case ts.SyntaxKind.UnionType: {
|
|
203
|
+
const types = node.types.map((t) => convertTypeNode(t, ctx));
|
|
204
|
+
return mergeUnion(types);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
case ts.SyntaxKind.IntersectionType: {
|
|
208
|
+
const types = node.types.map((t) => convertTypeNode(t, ctx));
|
|
209
|
+
return { allOf: types };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
case ts.SyntaxKind.ParenthesizedType: {
|
|
213
|
+
return convertTypeNode(node.type, ctx);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
case ts.SyntaxKind.TypeLiteral: {
|
|
217
|
+
return convertTypeLiteral(node, ctx);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
case ts.SyntaxKind.MappedType: {
|
|
221
|
+
const valueSchema = node.type ? convertTypeNode(node.type, ctx) : {};
|
|
222
|
+
return { type: "object", additionalProperties: valueSchema };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
case ts.SyntaxKind.TypeReference: {
|
|
226
|
+
return convertTypeReference(node, ctx);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
case ts.SyntaxKind.ConditionalType:
|
|
230
|
+
return {};
|
|
231
|
+
|
|
232
|
+
case ts.SyntaxKind.IndexedAccessType:
|
|
233
|
+
return {};
|
|
234
|
+
|
|
235
|
+
case ts.SyntaxKind.TemplateLiteralType:
|
|
236
|
+
return { type: "string" };
|
|
237
|
+
|
|
238
|
+
case ts.SyntaxKind.RestType:
|
|
239
|
+
case ts.SyntaxKind.OptionalType:
|
|
240
|
+
return convertTypeNode(node.type, ctx);
|
|
241
|
+
|
|
242
|
+
case ts.SyntaxKind.InferType:
|
|
243
|
+
return {};
|
|
244
|
+
|
|
245
|
+
case ts.SyntaxKind.TypeQuery:
|
|
246
|
+
return {};
|
|
247
|
+
|
|
248
|
+
case ts.SyntaxKind.NamedTupleMember:
|
|
249
|
+
return convertTypeNode(node.type, ctx);
|
|
250
|
+
|
|
251
|
+
default:
|
|
252
|
+
return {};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function convertTypeLiteral(node, ctx) {
|
|
257
|
+
const properties = {};
|
|
258
|
+
const required = [];
|
|
259
|
+
let additionalProperties;
|
|
260
|
+
|
|
261
|
+
for (const member of node.members) {
|
|
262
|
+
if (ts.isPropertySignature(member)) {
|
|
263
|
+
const name = getMemberName(member);
|
|
264
|
+
if (!name) continue;
|
|
265
|
+
const schema = convertTypeNode(member.type, ctx);
|
|
266
|
+
properties[name] = getJsDocAnnotations(member, schema);
|
|
267
|
+
if (!member.questionToken) {
|
|
268
|
+
required.push(name);
|
|
269
|
+
}
|
|
270
|
+
} else if (ts.isIndexSignatureDeclaration(member)) {
|
|
271
|
+
additionalProperties = convertTypeNode(member.type, ctx);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const schema = { type: "object" };
|
|
276
|
+
if (Object.keys(properties).length > 0) schema.properties = properties;
|
|
277
|
+
if (required.length > 0) schema.required = required;
|
|
278
|
+
if (additionalProperties !== undefined) schema.additionalProperties = additionalProperties;
|
|
279
|
+
return schema;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function getMemberName(member) {
|
|
283
|
+
if (!member.name) return null;
|
|
284
|
+
if (ts.isIdentifier(member.name)) return member.name.text;
|
|
285
|
+
if (ts.isStringLiteral(member.name)) return member.name.text;
|
|
286
|
+
if (ts.isNumericLiteral(member.name)) return member.name.text;
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* `definitions` entry keys already used for external (named) types, keyed by
|
|
292
|
+
* `<declaring file>::<type name>::<depth>` — the depth is part of the key because the same
|
|
293
|
+
* named type expands to a shallower or deeper schema depending on how far from `maxDepth` it's
|
|
294
|
+
* first encountered.
|
|
295
|
+
*/
|
|
296
|
+
const externalTypeDefKeys = new Map();
|
|
297
|
+
|
|
298
|
+
function getExternalTypeCacheKey(type) {
|
|
299
|
+
const symbol = type.getSymbol?.() ?? type.aliasSymbol;
|
|
300
|
+
const name = symbol?.getName?.();
|
|
301
|
+
if (!name || name.startsWith("__")) return null;
|
|
302
|
+
const declaration = symbol.getDeclarations?.()?.[0];
|
|
303
|
+
const file = declaration ? declaration.getSourceFile().fileName : "";
|
|
304
|
+
return `${file}::${name}`;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Converts a resolved (semantic) `ts.Type` — not an AST node — to a JSON Schema fragment,
|
|
309
|
+
* capped at `maxDepth` nested object levels (see {@link GenerateJsonSchemaOptions.maxDepth}).
|
|
310
|
+
* The depth cap doubles as cycle protection for circular external types.
|
|
311
|
+
*/
|
|
312
|
+
function convertExternalType(type, depth) {
|
|
313
|
+
if (!type) return {};
|
|
314
|
+
const flags = type.flags;
|
|
315
|
+
|
|
316
|
+
if (flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) return {};
|
|
317
|
+
if (flags & ts.TypeFlags.String) return { type: "string" };
|
|
318
|
+
if (flags & ts.TypeFlags.Number) return { type: "number" };
|
|
319
|
+
if (flags & ts.TypeFlags.Boolean) return { type: "boolean" };
|
|
320
|
+
if (flags & ts.TypeFlags.Null) return { type: "null" };
|
|
321
|
+
if (flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Void)) return {};
|
|
322
|
+
if (flags & ts.TypeFlags.Never) return { not: {} };
|
|
323
|
+
if (flags & ts.TypeFlags.StringLiteral) return { const: type.value };
|
|
324
|
+
if (flags & ts.TypeFlags.NumberLiteral) return { const: type.value };
|
|
325
|
+
if (flags & ts.TypeFlags.BooleanLiteral) {
|
|
326
|
+
return { const: checker.typeToString(type) === "true" };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (flags & ts.TypeFlags.Object) {
|
|
330
|
+
if (depth <= maxDepth) {
|
|
331
|
+
const cacheKey = getExternalTypeCacheKey(type);
|
|
332
|
+
if (cacheKey) {
|
|
333
|
+
const existingDefKey = externalTypeDefKeys.get(`${cacheKey}@${depth}`);
|
|
334
|
+
if (existingDefKey) {
|
|
335
|
+
return { $ref: `#/definitions/${existingDefKey}` };
|
|
336
|
+
}
|
|
337
|
+
const defKey = `External_${checker.typeToString(type)}_d${depth}`.replace(
|
|
338
|
+
/[^A-Za-z0-9_]/g,
|
|
339
|
+
"_",
|
|
340
|
+
);
|
|
341
|
+
externalTypeDefKeys.set(`${cacheKey}@${depth}`, defKey);
|
|
342
|
+
definitions[defKey] = {}; // placeholder for cycle-safety
|
|
343
|
+
Object.assign(definitions[defKey], buildExternalObjectSchema(type, depth));
|
|
344
|
+
return { $ref: `#/definitions/${defKey}` };
|
|
345
|
+
}
|
|
346
|
+
return buildExternalObjectSchema(type, depth);
|
|
347
|
+
}
|
|
348
|
+
// Beyond the depth cap: still constrained to "an object", just opaque — see the
|
|
349
|
+
// module doc for why this is more useful than falling back to a bare `{}`.
|
|
350
|
+
return { type: "object" };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (type.isUnion()) {
|
|
354
|
+
return mergeUnion(type.types.map((t) => convertExternalType(t, depth)));
|
|
355
|
+
}
|
|
356
|
+
if (type.isIntersection()) {
|
|
357
|
+
return { allOf: type.types.map((t) => convertExternalType(t, depth)) };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (checker.isArrayType(type)) {
|
|
361
|
+
const [elementType] = checker.getTypeArguments(type);
|
|
362
|
+
return { type: "array", items: convertExternalType(elementType, depth + 1) };
|
|
363
|
+
}
|
|
364
|
+
if (checker.isTupleType(type)) {
|
|
365
|
+
const elementTypes = checker.getTypeArguments(type);
|
|
366
|
+
return {
|
|
367
|
+
type: "array",
|
|
368
|
+
items: elementTypes.map((t) => convertExternalType(t, depth + 1)),
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (type.getCallSignatures().length > 0 || type.getConstructSignatures().length > 0) {
|
|
373
|
+
return {};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return {};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function buildExternalObjectSchema(type, depth) {
|
|
380
|
+
const properties = {};
|
|
381
|
+
const required = [];
|
|
382
|
+
for (const prop of checker.getPropertiesOfType(type)) {
|
|
383
|
+
const declaration = prop.valueDeclaration ?? prop.declarations?.[0];
|
|
384
|
+
if (!declaration) continue;
|
|
385
|
+
const propType = checker.getTypeOfSymbolAtLocation(prop, declaration);
|
|
386
|
+
if (propType.getCallSignatures().length > 0) continue; // skip methods
|
|
387
|
+
properties[prop.name] = convertExternalType(propType, depth + 1);
|
|
388
|
+
if ((prop.flags & ts.SymbolFlags.Optional) === 0) {
|
|
389
|
+
required.push(prop.name);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
const schema = { type: "object" };
|
|
393
|
+
if (Object.keys(properties).length > 0) schema.properties = properties;
|
|
394
|
+
if (required.length > 0) schema.required = required;
|
|
395
|
+
const indexInfo =
|
|
396
|
+
checker.getIndexInfoOfType(type, ts.IndexKind.String) ??
|
|
397
|
+
checker.getIndexInfoOfType(type, ts.IndexKind.Number);
|
|
398
|
+
if (indexInfo) {
|
|
399
|
+
schema.additionalProperties = convertExternalType(indexInfo.type, depth + 1);
|
|
400
|
+
} else {
|
|
401
|
+
schema.additionalProperties = false;
|
|
402
|
+
}
|
|
403
|
+
return schema;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function convertTypeReference(node, ctx) {
|
|
407
|
+
const name = getTypeName(node.typeName);
|
|
408
|
+
|
|
409
|
+
if (ctx.has(name)) {
|
|
410
|
+
const schema = ctx.get(name);
|
|
411
|
+
if (schema && schema.__key !== undefined) {
|
|
412
|
+
const { __key, ...rest } = schema;
|
|
413
|
+
return rest;
|
|
414
|
+
}
|
|
415
|
+
return schema;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (Object.hasOwn(typeNameOverrides, name)) {
|
|
419
|
+
return typeNameOverrides[name];
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const builtinSchema = getBuiltinTypeSchema(name);
|
|
423
|
+
if (builtinSchema !== null) return builtinSchema;
|
|
424
|
+
|
|
425
|
+
if (typeDecls.has(name)) {
|
|
426
|
+
const decl = typeDecls.get(name);
|
|
427
|
+
const params = typeParams.get(name);
|
|
428
|
+
|
|
429
|
+
if (
|
|
430
|
+
params &&
|
|
431
|
+
params.length > 0 &&
|
|
432
|
+
node.typeArguments &&
|
|
433
|
+
node.typeArguments.length > 0
|
|
434
|
+
) {
|
|
435
|
+
return handleGenericApplication(name, decl, params, node.typeArguments, ctx);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
ensureDefinition(name, decl, new Map());
|
|
439
|
+
return { $ref: `#/definitions/${name}` };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
try {
|
|
443
|
+
const resolvedType = checker.getTypeFromTypeNode(node);
|
|
444
|
+
return convertExternalType(resolvedType, 1);
|
|
445
|
+
} catch {
|
|
446
|
+
return {};
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function handleGenericApplication(baseName, decl, paramNames, typeArgNodes, outerCtx) {
|
|
451
|
+
const argKeys = typeArgNodes.map((arg, i) => getTypeNodeKey(arg, outerCtx) || `T${i}`);
|
|
452
|
+
|
|
453
|
+
const safArgKeys = argKeys.map((k) => {
|
|
454
|
+
if (argKeyDepth(k) >= MAX_ARG_NESTING) {
|
|
455
|
+
return k.replace(/(Arr)+$/, "");
|
|
456
|
+
}
|
|
457
|
+
return k;
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const defKey = `${baseName}_${safArgKeys.join("_")}`.replace(/[^A-Za-z0-9_]/g, "_");
|
|
461
|
+
|
|
462
|
+
if (definitions[defKey] !== undefined) {
|
|
463
|
+
return { $ref: `#/definitions/${defKey}` };
|
|
464
|
+
}
|
|
465
|
+
if (processing.has(defKey)) {
|
|
466
|
+
return { $ref: `#/definitions/${defKey}` };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const argSchemas = typeArgNodes.map((arg) => convertTypeNode(arg, outerCtx));
|
|
470
|
+
|
|
471
|
+
const newCtx = new Map(outerCtx);
|
|
472
|
+
for (let i = 0; i < paramNames.length; i++) {
|
|
473
|
+
const argSchema = argSchemas[i] || {};
|
|
474
|
+
newCtx.set(paramNames[i], { ...argSchema, __key: safArgKeys[i] });
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
processing.add(defKey);
|
|
478
|
+
definitions[defKey] = {};
|
|
479
|
+
|
|
480
|
+
const raw = convertDeclaration(decl, newCtx);
|
|
481
|
+
const schema = removePrivateKeys(raw);
|
|
482
|
+
Object.assign(definitions[defKey], schema);
|
|
483
|
+
processing.delete(defKey);
|
|
484
|
+
|
|
485
|
+
return { $ref: `#/definitions/${defKey}` };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function removePrivateKeys(obj) {
|
|
489
|
+
return JSON.parse(JSON.stringify(obj, (key, val) => (key === "__key" ? undefined : val)));
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function getTypeNodeKey(node, ctx) {
|
|
493
|
+
if (!node) return "T";
|
|
494
|
+
|
|
495
|
+
switch (node.kind) {
|
|
496
|
+
case ts.SyntaxKind.StringKeyword:
|
|
497
|
+
return "string";
|
|
498
|
+
case ts.SyntaxKind.NumberKeyword:
|
|
499
|
+
return "number";
|
|
500
|
+
case ts.SyntaxKind.BooleanKeyword:
|
|
501
|
+
return "boolean";
|
|
502
|
+
case ts.SyntaxKind.NullKeyword:
|
|
503
|
+
return "null";
|
|
504
|
+
case ts.SyntaxKind.AnyKeyword:
|
|
505
|
+
case ts.SyntaxKind.UnknownKeyword:
|
|
506
|
+
return "any";
|
|
507
|
+
case ts.SyntaxKind.ArrayType: {
|
|
508
|
+
const inner = getTypeNodeKey(node.elementType, ctx);
|
|
509
|
+
return `${inner}Arr`;
|
|
510
|
+
}
|
|
511
|
+
case ts.SyntaxKind.TypeReference: {
|
|
512
|
+
const name = getTypeName(node.typeName);
|
|
513
|
+
if (ctx.has(name)) {
|
|
514
|
+
const resolved = ctx.get(name);
|
|
515
|
+
if (resolved?.__key) return resolved.__key;
|
|
516
|
+
return name;
|
|
517
|
+
}
|
|
518
|
+
if (node.typeArguments && node.typeArguments.length > 0) {
|
|
519
|
+
const args = node.typeArguments.map((a) => getTypeNodeKey(a, ctx)).join("_");
|
|
520
|
+
return `${name}_${args}`;
|
|
521
|
+
}
|
|
522
|
+
return name;
|
|
523
|
+
}
|
|
524
|
+
case ts.SyntaxKind.UnionType: {
|
|
525
|
+
const parts = node.types.slice(0, 3).map((t) => getTypeNodeKey(t, ctx));
|
|
526
|
+
return parts.join("_or_");
|
|
527
|
+
}
|
|
528
|
+
case ts.SyntaxKind.ParenthesizedType:
|
|
529
|
+
return getTypeNodeKey(node.type, ctx);
|
|
530
|
+
case ts.SyntaxKind.LiteralType: {
|
|
531
|
+
const lit = node.literal;
|
|
532
|
+
if (ts.isStringLiteral(lit)) return `lit_${lit.text}`;
|
|
533
|
+
if (ts.isNumericLiteral(lit)) return `lit_${lit.text}`;
|
|
534
|
+
return "lit";
|
|
535
|
+
}
|
|
536
|
+
default:
|
|
537
|
+
return "T";
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function ensureDefinition(name, decl, ctx) {
|
|
542
|
+
if (definitions[name] !== undefined) return;
|
|
543
|
+
if (processing.has(name)) return;
|
|
544
|
+
|
|
545
|
+
processing.add(name);
|
|
546
|
+
definitions[name] = {};
|
|
547
|
+
const schema = convertDeclaration(decl, ctx);
|
|
548
|
+
Object.assign(definitions[name], schema);
|
|
549
|
+
processing.delete(name);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function convertDeclaration(decl, ctx) {
|
|
553
|
+
if (ts.isInterfaceDeclaration(decl)) {
|
|
554
|
+
return convertInterface(decl, ctx);
|
|
555
|
+
}
|
|
556
|
+
if (ts.isTypeAliasDeclaration(decl)) {
|
|
557
|
+
const baseSchema = convertTypeNode(decl.type, ctx);
|
|
558
|
+
return getJsDocAnnotations(decl, baseSchema);
|
|
559
|
+
}
|
|
560
|
+
return {};
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function convertInterface(decl, ctx) {
|
|
564
|
+
const properties = {};
|
|
565
|
+
const required = [];
|
|
566
|
+
let additionalProperties;
|
|
567
|
+
|
|
568
|
+
const allOf = [];
|
|
569
|
+
if (decl.heritageClauses) {
|
|
570
|
+
for (const clause of decl.heritageClauses) {
|
|
571
|
+
if (clause.token === ts.SyntaxKind.ExtendsKeyword) {
|
|
572
|
+
for (const type of clause.types) {
|
|
573
|
+
const parentSchema = convertTypeReference(type, ctx);
|
|
574
|
+
if (parentSchema && Object.keys(parentSchema).length > 0) {
|
|
575
|
+
allOf.push(parentSchema);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
for (const member of decl.members) {
|
|
583
|
+
if (ts.isPropertySignature(member)) {
|
|
584
|
+
const name = getMemberName(member);
|
|
585
|
+
if (!name) continue;
|
|
586
|
+
const schema = convertTypeNode(member.type, ctx);
|
|
587
|
+
properties[name] = getJsDocAnnotations(member, schema);
|
|
588
|
+
if (!member.questionToken) {
|
|
589
|
+
required.push(name);
|
|
590
|
+
}
|
|
591
|
+
} else if (ts.isIndexSignatureDeclaration(member)) {
|
|
592
|
+
additionalProperties = convertTypeNode(member.type, ctx);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const schema = { type: "object" };
|
|
597
|
+
if (Object.keys(properties).length > 0) schema.properties = properties;
|
|
598
|
+
if (required.length > 0) schema.required = required;
|
|
599
|
+
if (additionalProperties !== undefined) schema.additionalProperties = additionalProperties;
|
|
600
|
+
|
|
601
|
+
if (allOf.length > 0) {
|
|
602
|
+
return { allOf: [...allOf, schema] };
|
|
603
|
+
}
|
|
604
|
+
return schema;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function getTypeName(entityName) {
|
|
608
|
+
if (ts.isIdentifier(entityName)) return entityName.text;
|
|
609
|
+
if (ts.isQualifiedName(entityName)) {
|
|
610
|
+
return `${getTypeName(entityName.left)}.${entityName.right.text}`;
|
|
611
|
+
}
|
|
612
|
+
return "";
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function getBuiltinTypeSchema(name) {
|
|
616
|
+
switch (name) {
|
|
617
|
+
case "string":
|
|
618
|
+
return { type: "string" };
|
|
619
|
+
case "number":
|
|
620
|
+
return { type: "number" };
|
|
621
|
+
case "boolean":
|
|
622
|
+
return { type: "boolean" };
|
|
623
|
+
case "null":
|
|
624
|
+
return { type: "null" };
|
|
625
|
+
case "undefined":
|
|
626
|
+
case "void":
|
|
627
|
+
return {};
|
|
628
|
+
case "any":
|
|
629
|
+
case "unknown":
|
|
630
|
+
case "object":
|
|
631
|
+
case "Object":
|
|
632
|
+
return {};
|
|
633
|
+
case "never":
|
|
634
|
+
return { not: {} };
|
|
635
|
+
case "Array":
|
|
636
|
+
case "ReadonlyArray":
|
|
637
|
+
return { type: "array" };
|
|
638
|
+
case "Record":
|
|
639
|
+
return { type: "object" };
|
|
640
|
+
case "Partial":
|
|
641
|
+
case "Required":
|
|
642
|
+
case "Readonly":
|
|
643
|
+
case "NonNullable":
|
|
644
|
+
case "Extract":
|
|
645
|
+
case "Exclude":
|
|
646
|
+
case "ReturnType":
|
|
647
|
+
case "Parameters":
|
|
648
|
+
case "InstanceType":
|
|
649
|
+
case "Awaited":
|
|
650
|
+
return null;
|
|
651
|
+
case "Pick":
|
|
652
|
+
case "Omit":
|
|
653
|
+
return {};
|
|
654
|
+
case "Map":
|
|
655
|
+
case "Set":
|
|
656
|
+
case "WeakMap":
|
|
657
|
+
case "WeakSet":
|
|
658
|
+
return {};
|
|
659
|
+
case "Promise":
|
|
660
|
+
return {};
|
|
661
|
+
case "Date":
|
|
662
|
+
return { type: "string", format: "date-time" };
|
|
663
|
+
case "RegExp":
|
|
664
|
+
return { type: "string", format: "regex" };
|
|
665
|
+
case "Function":
|
|
666
|
+
return {};
|
|
667
|
+
default:
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function mergeUnion(schemas) {
|
|
673
|
+
const nonEmpty = schemas.filter((s) => Object.keys(s).length > 0);
|
|
674
|
+
|
|
675
|
+
if (nonEmpty.length === 0) return {};
|
|
676
|
+
if (nonEmpty.length === 1) return nonEmpty[0];
|
|
677
|
+
|
|
678
|
+
const allTypes = nonEmpty.every((s) => s.type && Object.keys(s).length === 1);
|
|
679
|
+
if (allTypes) {
|
|
680
|
+
const types = [...new Set(nonEmpty.map((s) => s.type))];
|
|
681
|
+
if (types.length === 1) return { type: types[0] };
|
|
682
|
+
return { type: types };
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const allConsts = nonEmpty.every((s) => s.const !== undefined);
|
|
686
|
+
if (allConsts) {
|
|
687
|
+
return { enum: nonEmpty.map((s) => s.const) };
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
return { anyOf: nonEmpty };
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function getJsDocAnnotations(node, baseSchema) {
|
|
694
|
+
const comments = ts.getJSDocCommentsAndTags(node);
|
|
695
|
+
let description = "";
|
|
696
|
+
|
|
697
|
+
for (const comment of comments) {
|
|
698
|
+
if (ts.isJSDoc(comment) && comment.comment) {
|
|
699
|
+
const text =
|
|
700
|
+
typeof comment.comment === "string"
|
|
701
|
+
? comment.comment
|
|
702
|
+
: comment.comment.map((c) => c.text).join("");
|
|
703
|
+
description += text.trim();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (description) {
|
|
708
|
+
return { description, ...baseSchema };
|
|
709
|
+
}
|
|
710
|
+
return baseSchema;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/** Finds a top-level interface/type-alias declaration named `name` in any of `rootFiles`. */
|
|
714
|
+
function findRootFileDeclaration(name) {
|
|
715
|
+
for (const fileName of rootFiles) {
|
|
716
|
+
const sourceFile = program.getSourceFile(fileName);
|
|
717
|
+
if (!sourceFile) continue;
|
|
718
|
+
let found;
|
|
719
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
720
|
+
if (found) return;
|
|
721
|
+
if (
|
|
722
|
+
(ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) &&
|
|
723
|
+
node.name.text === name
|
|
724
|
+
) {
|
|
725
|
+
found = node;
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
if (found) return found;
|
|
729
|
+
}
|
|
730
|
+
return undefined;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// ─── Generate the Requested Root Schemas ─────────────────────────────────────
|
|
734
|
+
|
|
735
|
+
const schemas = {};
|
|
736
|
+
for (const name of rootTypeNames) {
|
|
737
|
+
if (typeDecls.has(name)) {
|
|
738
|
+
// "Local" type: expand fully via the AST-walk path (no depth cap).
|
|
739
|
+
ensureDefinition(name, typeDecls.get(name), new Map());
|
|
740
|
+
schemas[name] = { $ref: `#/definitions/${name}` };
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const declNode = findRootFileDeclaration(name);
|
|
745
|
+
if (!declNode) {
|
|
746
|
+
throw new Error(
|
|
747
|
+
`generateJsonSchema: type "${name}" was not found under interfaceDir nor as a top-level declaration in any rootFiles entry.`,
|
|
748
|
+
);
|
|
749
|
+
}
|
|
750
|
+
const type = checker.getTypeAtLocation(declNode.name);
|
|
751
|
+
schemas[name] = convertExternalType(type, 1);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (includeAllLocalDeclarations) {
|
|
755
|
+
for (const [name, decl] of typeDecls) {
|
|
756
|
+
if (!definitions[name]) {
|
|
757
|
+
ensureDefinition(name, decl, new Map());
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
return { schemas, definitions };
|
|
763
|
+
}
|