@formspec/build 0.1.0-alpha.30 → 0.1.0-alpha.32
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/README.md +82 -5
- package/dist/analyzer/class-analyzer.d.ts +6 -0
- package/dist/analyzer/class-analyzer.d.ts.map +1 -1
- package/dist/browser.cjs +166 -24
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.js +166 -24
- package/dist/browser.js.map +1 -1
- package/dist/build-alpha.d.ts +180 -0
- package/dist/build-beta.d.ts +180 -0
- package/dist/build-internal.d.ts +180 -0
- package/dist/build.d.ts +180 -0
- package/dist/cli.cjs +584 -33
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +582 -33
- package/dist/cli.js.map +1 -1
- package/dist/generators/discovered-schema.d.ts +112 -0
- package/dist/generators/discovered-schema.d.ts.map +1 -0
- package/dist/index.cjs +573 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +565 -33
- package/dist/index.js.map +1 -1
- package/dist/internals.cjs +197 -33
- package/dist/internals.cjs.map +1 -1
- package/dist/internals.js +197 -33
- package/dist/internals.js.map +1 -1
- package/dist/static-build.d.ts +61 -0
- package/dist/static-build.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -990,9 +990,9 @@ function collectFields(elements, properties, required, ctx) {
|
|
|
990
990
|
for (const element of elements) {
|
|
991
991
|
switch (element.kind) {
|
|
992
992
|
case "field":
|
|
993
|
-
properties[
|
|
993
|
+
properties[getSerializedFieldName(element)] = generateFieldSchema(element, ctx);
|
|
994
994
|
if (element.required) {
|
|
995
|
-
required.push(
|
|
995
|
+
required.push(getSerializedFieldName(element));
|
|
996
996
|
}
|
|
997
997
|
break;
|
|
998
998
|
case "group":
|
|
@@ -1063,19 +1063,21 @@ function applyPathTargetedConstraints(schema, pathConstraints, ctx, typeNode) {
|
|
|
1063
1063
|
schema.items = applyPathTargetedConstraints(schema.items, pathConstraints, ctx, nestedType);
|
|
1064
1064
|
return schema;
|
|
1065
1065
|
}
|
|
1066
|
-
const
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1066
|
+
const propertyOverrides = buildPropertyOverrides(pathConstraints, typeNode, ctx);
|
|
1067
|
+
const nullableValueBranch = getNullableUnionValueSchema(schema);
|
|
1068
|
+
if (nullableValueBranch !== void 0) {
|
|
1069
|
+
const updatedNullableValueBranch = applyPathTargetedConstraints(
|
|
1070
|
+
nullableValueBranch,
|
|
1071
|
+
pathConstraints,
|
|
1072
|
+
ctx,
|
|
1073
|
+
resolveTraversableTypeNode(typeNode, ctx)
|
|
1074
|
+
);
|
|
1075
|
+
if (schema.oneOf !== void 0) {
|
|
1076
|
+
schema.oneOf = schema.oneOf.map(
|
|
1077
|
+
(branch) => branch === nullableValueBranch ? updatedNullableValueBranch : branch
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
return schema;
|
|
1079
1081
|
}
|
|
1080
1082
|
if (schema.$ref) {
|
|
1081
1083
|
const { $ref, ...rest } = schema;
|
|
@@ -1090,7 +1092,7 @@ function applyPathTargetedConstraints(schema, pathConstraints, ctx, typeNode) {
|
|
|
1090
1092
|
const missingOverrides = {};
|
|
1091
1093
|
for (const [target, overrideSchema] of Object.entries(propertyOverrides)) {
|
|
1092
1094
|
if (schema.properties[target]) {
|
|
1093
|
-
|
|
1095
|
+
mergeSchemaOverride(schema.properties[target], overrideSchema);
|
|
1094
1096
|
} else {
|
|
1095
1097
|
missingOverrides[target] = overrideSchema;
|
|
1096
1098
|
}
|
|
@@ -1164,7 +1166,7 @@ function generateObjectType(type, ctx) {
|
|
|
1164
1166
|
const properties = {};
|
|
1165
1167
|
const required = [];
|
|
1166
1168
|
for (const prop of type.properties) {
|
|
1167
|
-
const propertyName =
|
|
1169
|
+
const propertyName = getSerializedObjectPropertyName(prop);
|
|
1168
1170
|
properties[propertyName] = generatePropertySchema(prop, ctx);
|
|
1169
1171
|
if (!prop.optional) {
|
|
1170
1172
|
required.push(propertyName);
|
|
@@ -1218,7 +1220,16 @@ function isNullableUnion(type) {
|
|
|
1218
1220
|
return nullCount === 1;
|
|
1219
1221
|
}
|
|
1220
1222
|
function generateReferenceType(type, ctx) {
|
|
1221
|
-
return { $ref: `#/$defs/${
|
|
1223
|
+
return { $ref: `#/$defs/${getSerializedTypeName(type.name, ctx)}` };
|
|
1224
|
+
}
|
|
1225
|
+
function getSerializedFieldName(field) {
|
|
1226
|
+
return getSerializedName(field.name, field.metadata);
|
|
1227
|
+
}
|
|
1228
|
+
function getSerializedObjectPropertyName(property) {
|
|
1229
|
+
return getSerializedName(property.name, property.metadata);
|
|
1230
|
+
}
|
|
1231
|
+
function getSerializedTypeName(logicalName, ctx) {
|
|
1232
|
+
return ctx.typeNameMap[logicalName] ?? logicalName;
|
|
1222
1233
|
}
|
|
1223
1234
|
function applyResolvedMetadata(schema, metadata) {
|
|
1224
1235
|
const displayName = getDisplayName(metadata);
|
|
@@ -1229,17 +1240,148 @@ function applyResolvedMetadata(schema, metadata) {
|
|
|
1229
1240
|
function resolveReferencedType(type, ctx) {
|
|
1230
1241
|
return ctx.typeRegistry[type.name]?.type;
|
|
1231
1242
|
}
|
|
1243
|
+
function dereferenceTypeNode(typeNode, ctx) {
|
|
1244
|
+
if (typeNode?.kind !== "reference") {
|
|
1245
|
+
return typeNode;
|
|
1246
|
+
}
|
|
1247
|
+
return resolveReferencedType(typeNode, ctx);
|
|
1248
|
+
}
|
|
1249
|
+
function unwrapNullableTypeNode(typeNode) {
|
|
1250
|
+
if (typeNode?.kind !== "union" || !isNullableUnion(typeNode)) {
|
|
1251
|
+
return typeNode;
|
|
1252
|
+
}
|
|
1253
|
+
return typeNode.members.find(
|
|
1254
|
+
(member) => !(member.kind === "primitive" && member.primitiveKind === "null")
|
|
1255
|
+
);
|
|
1256
|
+
}
|
|
1257
|
+
function resolveTraversableTypeNode(typeNode, ctx) {
|
|
1258
|
+
const dereferenced = dereferenceTypeNode(typeNode, ctx);
|
|
1259
|
+
const unwrapped = unwrapNullableTypeNode(dereferenced);
|
|
1260
|
+
if (unwrapped !== dereferenced) {
|
|
1261
|
+
return resolveTraversableTypeNode(unwrapped, ctx);
|
|
1262
|
+
}
|
|
1263
|
+
return dereferenced;
|
|
1264
|
+
}
|
|
1232
1265
|
function resolveSerializedPropertyName(logicalName, typeNode, ctx) {
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
return
|
|
1266
|
+
const effectiveType = resolveTraversableTypeNode(typeNode, ctx);
|
|
1267
|
+
if (effectiveType?.kind === "array") {
|
|
1268
|
+
return resolveSerializedPropertyName(logicalName, effectiveType.items, ctx);
|
|
1236
1269
|
}
|
|
1237
|
-
if (
|
|
1238
|
-
const
|
|
1239
|
-
return
|
|
1270
|
+
if (effectiveType?.kind === "object") {
|
|
1271
|
+
const property = effectiveType.properties.find((candidate) => candidate.name === logicalName);
|
|
1272
|
+
return property === void 0 ? logicalName : getSerializedObjectPropertyName(property);
|
|
1240
1273
|
}
|
|
1241
1274
|
return logicalName;
|
|
1242
1275
|
}
|
|
1276
|
+
function resolveTargetTypeNode(logicalName, typeNode, ctx) {
|
|
1277
|
+
const effectiveType = resolveTraversableTypeNode(typeNode, ctx);
|
|
1278
|
+
if (effectiveType?.kind === "array") {
|
|
1279
|
+
return resolveTargetTypeNode(logicalName, effectiveType.items, ctx);
|
|
1280
|
+
}
|
|
1281
|
+
if (effectiveType?.kind !== "object") {
|
|
1282
|
+
return void 0;
|
|
1283
|
+
}
|
|
1284
|
+
return effectiveType.properties.find((candidate) => candidate.name === logicalName)?.type;
|
|
1285
|
+
}
|
|
1286
|
+
function buildPropertyOverrides(pathConstraints, typeNode, ctx) {
|
|
1287
|
+
const byTarget = /* @__PURE__ */ new Map();
|
|
1288
|
+
for (const constraint of pathConstraints) {
|
|
1289
|
+
const target = constraint.path?.segments[0];
|
|
1290
|
+
if (!target) {
|
|
1291
|
+
continue;
|
|
1292
|
+
}
|
|
1293
|
+
const grouped = byTarget.get(target) ?? [];
|
|
1294
|
+
grouped.push(constraint);
|
|
1295
|
+
byTarget.set(target, grouped);
|
|
1296
|
+
}
|
|
1297
|
+
const overrides = {};
|
|
1298
|
+
for (const [target, constraints] of byTarget) {
|
|
1299
|
+
overrides[resolveSerializedPropertyName(target, typeNode, ctx)] = buildPathOverrideSchema(
|
|
1300
|
+
constraints.map(stripLeadingPathSegment),
|
|
1301
|
+
resolveTargetTypeNode(target, typeNode, ctx),
|
|
1302
|
+
ctx
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
return overrides;
|
|
1306
|
+
}
|
|
1307
|
+
function buildPathOverrideSchema(constraints, typeNode, ctx) {
|
|
1308
|
+
const schema = {};
|
|
1309
|
+
const directConstraints = [];
|
|
1310
|
+
const nestedConstraints = [];
|
|
1311
|
+
for (const constraint of constraints) {
|
|
1312
|
+
if (constraint.path === void 0 || constraint.path.segments.length === 0) {
|
|
1313
|
+
directConstraints.push(constraint);
|
|
1314
|
+
} else {
|
|
1315
|
+
nestedConstraints.push(constraint);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
applyConstraints(schema, directConstraints, ctx);
|
|
1319
|
+
if (nestedConstraints.length === 0) {
|
|
1320
|
+
return schema;
|
|
1321
|
+
}
|
|
1322
|
+
const effectiveType = resolveTraversableTypeNode(typeNode, ctx);
|
|
1323
|
+
if (effectiveType?.kind === "array") {
|
|
1324
|
+
schema.items = buildPathOverrideSchema(nestedConstraints, effectiveType.items, ctx);
|
|
1325
|
+
return schema;
|
|
1326
|
+
}
|
|
1327
|
+
schema.properties = buildPropertyOverrides(nestedConstraints, effectiveType, ctx);
|
|
1328
|
+
return schema;
|
|
1329
|
+
}
|
|
1330
|
+
function mergeSchemaOverride(target, override) {
|
|
1331
|
+
const nullableValueBranch = getNullableUnionValueSchema(target);
|
|
1332
|
+
if (nullableValueBranch !== void 0) {
|
|
1333
|
+
mergeSchemaOverride(nullableValueBranch, override);
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
if (override.properties !== void 0) {
|
|
1337
|
+
const mergedProperties = target.properties ?? {};
|
|
1338
|
+
for (const [name, propertyOverride] of Object.entries(override.properties)) {
|
|
1339
|
+
const existing = mergedProperties[name];
|
|
1340
|
+
if (existing === void 0) {
|
|
1341
|
+
mergedProperties[name] = propertyOverride;
|
|
1342
|
+
} else {
|
|
1343
|
+
mergeSchemaOverride(existing, propertyOverride);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
target.properties = mergedProperties;
|
|
1347
|
+
}
|
|
1348
|
+
if (override.items !== void 0) {
|
|
1349
|
+
if (target.items === void 0) {
|
|
1350
|
+
target.items = override.items;
|
|
1351
|
+
} else {
|
|
1352
|
+
mergeSchemaOverride(target.items, override.items);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
for (const [key, value] of Object.entries(override)) {
|
|
1356
|
+
if (key === "properties" || key === "items") {
|
|
1357
|
+
continue;
|
|
1358
|
+
}
|
|
1359
|
+
target[key] = value;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
function stripLeadingPathSegment(constraint) {
|
|
1363
|
+
const segments = constraint.path?.segments;
|
|
1364
|
+
if (segments === void 0 || segments.length === 0) {
|
|
1365
|
+
return constraint;
|
|
1366
|
+
}
|
|
1367
|
+
const [, ...rest] = segments;
|
|
1368
|
+
if (rest.length === 0) {
|
|
1369
|
+
const { path: _path, ...stripped } = constraint;
|
|
1370
|
+
return stripped;
|
|
1371
|
+
}
|
|
1372
|
+
return {
|
|
1373
|
+
...constraint,
|
|
1374
|
+
path: { segments: rest }
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
function getNullableUnionValueSchema(schema) {
|
|
1378
|
+
if (schema.oneOf?.length !== 2) {
|
|
1379
|
+
return void 0;
|
|
1380
|
+
}
|
|
1381
|
+
const valueSchema = schema.oneOf.find((branch) => branch.type !== "null");
|
|
1382
|
+
const nullSchema = schema.oneOf.find((branch) => branch.type === "null");
|
|
1383
|
+
return valueSchema !== void 0 && nullSchema !== void 0 ? valueSchema : void 0;
|
|
1384
|
+
}
|
|
1243
1385
|
function generateDynamicType(type) {
|
|
1244
1386
|
if (type.dynamicKind === "enum") {
|
|
1245
1387
|
const schema = {
|
|
@@ -2850,6 +2992,27 @@ function resolveNodeMetadata(metadataPolicy, declarationKind, logicalName, node,
|
|
|
2850
2992
|
makeMetadataContext("tsdoc", declarationKind, logicalName, buildContext)
|
|
2851
2993
|
);
|
|
2852
2994
|
}
|
|
2995
|
+
function analyzeDeclarationRootInfo(declaration, checker, file = "", extensionRegistry, metadataPolicy) {
|
|
2996
|
+
const normalizedMetadataPolicy = normalizeMetadataPolicy(metadataPolicy);
|
|
2997
|
+
const declarationType = checker.getTypeAtLocation(declaration);
|
|
2998
|
+
const logicalName = ts3.isClassDeclaration(declaration) ? declaration.name?.text ?? "AnonymousClass" : declaration.name.text;
|
|
2999
|
+
const docResult = extractJSDocParseResult(
|
|
3000
|
+
declaration,
|
|
3001
|
+
file,
|
|
3002
|
+
makeParseOptions(extensionRegistry, void 0, checker, declarationType, declarationType)
|
|
3003
|
+
);
|
|
3004
|
+
const metadata = resolveNodeMetadata(normalizedMetadataPolicy, "type", logicalName, declaration, {
|
|
3005
|
+
checker,
|
|
3006
|
+
declaration,
|
|
3007
|
+
subjectType: declarationType,
|
|
3008
|
+
hostType: declarationType
|
|
3009
|
+
});
|
|
3010
|
+
return {
|
|
3011
|
+
...metadata !== void 0 && { metadata },
|
|
3012
|
+
annotations: docResult.annotations,
|
|
3013
|
+
diagnostics: docResult.diagnostics
|
|
3014
|
+
};
|
|
3015
|
+
}
|
|
2853
3016
|
function analyzeClassToIR(classDecl, checker, file = "", extensionRegistry, metadataPolicy) {
|
|
2854
3017
|
const normalizedMetadataPolicy = normalizeMetadataPolicy(metadataPolicy);
|
|
2855
3018
|
const name = classDecl.name?.text ?? "AnonymousClass";
|
|
@@ -3264,10 +3427,7 @@ function resolveLiteralDiscriminatorPropertyValue(boundType, fieldName, checker,
|
|
|
3264
3427
|
if (resolvedAnchorNode === null) {
|
|
3265
3428
|
return void 0;
|
|
3266
3429
|
}
|
|
3267
|
-
const propertyType = checker.getTypeOfSymbolAtLocation(
|
|
3268
|
-
propertySymbol,
|
|
3269
|
-
resolvedAnchorNode
|
|
3270
|
-
);
|
|
3430
|
+
const propertyType = checker.getTypeOfSymbolAtLocation(propertySymbol, resolvedAnchorNode);
|
|
3271
3431
|
if (propertyType.isStringLiteral()) {
|
|
3272
3432
|
return propertyType.value;
|
|
3273
3433
|
}
|
|
@@ -4226,14 +4386,39 @@ function resolveObjectType(type, checker, file, typeRegistry, visiting, sourceNo
|
|
|
4226
4386
|
collectedDiagnostics
|
|
4227
4387
|
);
|
|
4228
4388
|
const fieldNodeInfo = fieldInfoMap?.get(prop.name);
|
|
4389
|
+
const inlineFieldNodeInfo = fieldNodeInfo === void 0 ? ts3.isPropertySignature(declaration) ? analyzeInterfacePropertyToIR(
|
|
4390
|
+
declaration,
|
|
4391
|
+
checker,
|
|
4392
|
+
file,
|
|
4393
|
+
typeRegistry,
|
|
4394
|
+
visiting,
|
|
4395
|
+
collectedDiagnostics,
|
|
4396
|
+
type,
|
|
4397
|
+
metadataPolicy,
|
|
4398
|
+
extensionRegistry
|
|
4399
|
+
) : ts3.isPropertyDeclaration(declaration) ? analyzeFieldToIR(
|
|
4400
|
+
declaration,
|
|
4401
|
+
checker,
|
|
4402
|
+
file,
|
|
4403
|
+
typeRegistry,
|
|
4404
|
+
visiting,
|
|
4405
|
+
collectedDiagnostics,
|
|
4406
|
+
type,
|
|
4407
|
+
metadataPolicy,
|
|
4408
|
+
extensionRegistry
|
|
4409
|
+
) : null : null;
|
|
4410
|
+
const resolvedFieldNodeInfo = fieldNodeInfo ?? inlineFieldNodeInfo;
|
|
4411
|
+
const resolvedPropertyType = inlineFieldNodeInfo?.type ?? propTypeNode;
|
|
4229
4412
|
properties.push({
|
|
4230
4413
|
name: prop.name,
|
|
4231
|
-
...
|
|
4232
|
-
|
|
4414
|
+
...resolvedFieldNodeInfo?.metadata !== void 0 && {
|
|
4415
|
+
metadata: resolvedFieldNodeInfo.metadata
|
|
4416
|
+
},
|
|
4417
|
+
type: resolvedPropertyType,
|
|
4233
4418
|
optional,
|
|
4234
|
-
constraints:
|
|
4235
|
-
annotations:
|
|
4236
|
-
provenance:
|
|
4419
|
+
constraints: resolvedFieldNodeInfo?.constraints ?? [],
|
|
4420
|
+
annotations: resolvedFieldNodeInfo?.annotations ?? [],
|
|
4421
|
+
provenance: resolvedFieldNodeInfo?.provenance ?? provenanceForFile(file)
|
|
4237
4422
|
});
|
|
4238
4423
|
}
|
|
4239
4424
|
visiting.delete(type);
|
|
@@ -4878,6 +5063,360 @@ var init_class_schema = __esm({
|
|
|
4878
5063
|
}
|
|
4879
5064
|
});
|
|
4880
5065
|
|
|
5066
|
+
// src/static-build.ts
|
|
5067
|
+
import * as ts6 from "typescript";
|
|
5068
|
+
function toStaticBuildContext(context) {
|
|
5069
|
+
return context;
|
|
5070
|
+
}
|
|
5071
|
+
function createStaticBuildContext(filePath) {
|
|
5072
|
+
return toStaticBuildContext(createProgramContext(filePath));
|
|
5073
|
+
}
|
|
5074
|
+
function createStaticBuildContextFromProgram(program, filePath) {
|
|
5075
|
+
return toStaticBuildContext(createProgramContextFromProgram(program, filePath));
|
|
5076
|
+
}
|
|
5077
|
+
function getModuleSymbol(context) {
|
|
5078
|
+
const sourceFileWithSymbol = context.sourceFile;
|
|
5079
|
+
return context.checker.getSymbolAtLocation(context.sourceFile) ?? sourceFileWithSymbol.symbol;
|
|
5080
|
+
}
|
|
5081
|
+
function isSchemaSourceDeclaration(declaration) {
|
|
5082
|
+
return ts6.isClassDeclaration(declaration) || ts6.isInterfaceDeclaration(declaration) || ts6.isTypeAliasDeclaration(declaration);
|
|
5083
|
+
}
|
|
5084
|
+
function resolveModuleExport(context, exportName = "default") {
|
|
5085
|
+
const moduleSymbol = getModuleSymbol(context);
|
|
5086
|
+
if (moduleSymbol === void 0) {
|
|
5087
|
+
return null;
|
|
5088
|
+
}
|
|
5089
|
+
const exportSymbol = context.checker.getExportsOfModule(moduleSymbol).find((candidate) => candidate.name === exportName) ?? null;
|
|
5090
|
+
if (exportSymbol === null) {
|
|
5091
|
+
return null;
|
|
5092
|
+
}
|
|
5093
|
+
return exportSymbol.flags & ts6.SymbolFlags.Alias ? context.checker.getAliasedSymbol(exportSymbol) : exportSymbol;
|
|
5094
|
+
}
|
|
5095
|
+
function resolveModuleExportDeclaration(context, exportName = "default") {
|
|
5096
|
+
return resolveModuleExport(context, exportName)?.declarations?.find(isSchemaSourceDeclaration) ?? null;
|
|
5097
|
+
}
|
|
5098
|
+
var init_static_build = __esm({
|
|
5099
|
+
"src/static-build.ts"() {
|
|
5100
|
+
"use strict";
|
|
5101
|
+
init_program();
|
|
5102
|
+
}
|
|
5103
|
+
});
|
|
5104
|
+
|
|
5105
|
+
// src/generators/discovered-schema.ts
|
|
5106
|
+
import * as ts7 from "typescript";
|
|
5107
|
+
import { IR_VERSION as IR_VERSION3 } from "@formspec/core/internals";
|
|
5108
|
+
function toDiscoveredTypeSchemas(result) {
|
|
5109
|
+
return result;
|
|
5110
|
+
}
|
|
5111
|
+
function isNamedTypeDeclaration(declaration) {
|
|
5112
|
+
return ts7.isClassDeclaration(declaration) || ts7.isInterfaceDeclaration(declaration) || ts7.isTypeAliasDeclaration(declaration);
|
|
5113
|
+
}
|
|
5114
|
+
function hasConcreteTypeArguments(type, checker) {
|
|
5115
|
+
if ("aliasTypeArguments" in type && Array.isArray(type.aliasTypeArguments) && type.aliasTypeArguments.length > 0) {
|
|
5116
|
+
return true;
|
|
5117
|
+
}
|
|
5118
|
+
if ((type.flags & ts7.TypeFlags.Object) === 0) {
|
|
5119
|
+
return false;
|
|
5120
|
+
}
|
|
5121
|
+
const objectType = type;
|
|
5122
|
+
if ((objectType.objectFlags & ts7.ObjectFlags.Reference) === 0) {
|
|
5123
|
+
return false;
|
|
5124
|
+
}
|
|
5125
|
+
return checker.getTypeArguments(objectType).length > 0;
|
|
5126
|
+
}
|
|
5127
|
+
function getNamedTypeDeclaration2(type) {
|
|
5128
|
+
const symbol = type.getSymbol();
|
|
5129
|
+
if (symbol?.declarations !== void 0) {
|
|
5130
|
+
const declaration = symbol.declarations[0];
|
|
5131
|
+
if (declaration !== void 0 && isNamedTypeDeclaration(declaration)) {
|
|
5132
|
+
return declaration;
|
|
5133
|
+
}
|
|
5134
|
+
}
|
|
5135
|
+
const aliasDeclaration = type.aliasSymbol?.declarations?.find(ts7.isTypeAliasDeclaration);
|
|
5136
|
+
return aliasDeclaration;
|
|
5137
|
+
}
|
|
5138
|
+
function getFallbackName(sourceNode, fallback = "AnonymousType") {
|
|
5139
|
+
if (sourceNode !== void 0 && "name" in sourceNode) {
|
|
5140
|
+
const namedNode = sourceNode;
|
|
5141
|
+
if (namedNode.name !== void 0 && ts7.isIdentifier(namedNode.name)) {
|
|
5142
|
+
return namedNode.name.text;
|
|
5143
|
+
}
|
|
5144
|
+
}
|
|
5145
|
+
return fallback;
|
|
5146
|
+
}
|
|
5147
|
+
function createObjectRootAnalysis(name, properties, typeRegistry, metadata, annotations) {
|
|
5148
|
+
const fields = properties.map((property) => ({
|
|
5149
|
+
kind: "field",
|
|
5150
|
+
name: property.name,
|
|
5151
|
+
...property.metadata !== void 0 && { metadata: property.metadata },
|
|
5152
|
+
type: property.type,
|
|
5153
|
+
required: !property.optional,
|
|
5154
|
+
constraints: property.constraints,
|
|
5155
|
+
annotations: property.annotations,
|
|
5156
|
+
provenance: property.provenance
|
|
5157
|
+
}));
|
|
5158
|
+
return {
|
|
5159
|
+
name,
|
|
5160
|
+
...metadata !== void 0 && { metadata },
|
|
5161
|
+
fields,
|
|
5162
|
+
fieldLayouts: fields.map(() => ({})),
|
|
5163
|
+
typeRegistry,
|
|
5164
|
+
...annotations !== void 0 && annotations.length > 0 && { annotations },
|
|
5165
|
+
instanceMethods: [],
|
|
5166
|
+
staticMethods: [],
|
|
5167
|
+
diagnostics: []
|
|
5168
|
+
};
|
|
5169
|
+
}
|
|
5170
|
+
function omitApiName(metadata) {
|
|
5171
|
+
if (metadata?.apiName === void 0) {
|
|
5172
|
+
return metadata;
|
|
5173
|
+
}
|
|
5174
|
+
const { apiName: _apiName, ...rest } = metadata;
|
|
5175
|
+
return Object.keys(rest).length > 0 ? rest : void 0;
|
|
5176
|
+
}
|
|
5177
|
+
function describeRootType(rootType, typeRegistry, fallbackName) {
|
|
5178
|
+
if (rootType.kind !== "reference") {
|
|
5179
|
+
return {
|
|
5180
|
+
name: fallbackName,
|
|
5181
|
+
type: rootType
|
|
5182
|
+
};
|
|
5183
|
+
}
|
|
5184
|
+
const definition = typeRegistry[rootType.name];
|
|
5185
|
+
if (definition === void 0) {
|
|
5186
|
+
return {
|
|
5187
|
+
name: rootType.name,
|
|
5188
|
+
type: rootType
|
|
5189
|
+
};
|
|
5190
|
+
}
|
|
5191
|
+
return {
|
|
5192
|
+
name: definition.name,
|
|
5193
|
+
...definition.metadata !== void 0 && { metadata: definition.metadata },
|
|
5194
|
+
...definition.annotations !== void 0 && definition.annotations.length > 0 && { annotations: definition.annotations },
|
|
5195
|
+
type: definition.type
|
|
5196
|
+
};
|
|
5197
|
+
}
|
|
5198
|
+
function toStandaloneJsonSchema(root, typeRegistry, options) {
|
|
5199
|
+
const syntheticFieldMetadata = omitApiName(root.metadata);
|
|
5200
|
+
const syntheticField = {
|
|
5201
|
+
kind: "field",
|
|
5202
|
+
name: "__result",
|
|
5203
|
+
...syntheticFieldMetadata !== void 0 && { metadata: syntheticFieldMetadata },
|
|
5204
|
+
type: root.type,
|
|
5205
|
+
required: true,
|
|
5206
|
+
constraints: [],
|
|
5207
|
+
annotations: [...root.annotations ?? []],
|
|
5208
|
+
provenance: {
|
|
5209
|
+
surface: "tsdoc",
|
|
5210
|
+
file: "",
|
|
5211
|
+
line: 1,
|
|
5212
|
+
column: 0
|
|
5213
|
+
}
|
|
5214
|
+
};
|
|
5215
|
+
const schema = generateJsonSchemaFromIR(
|
|
5216
|
+
{
|
|
5217
|
+
kind: "form-ir",
|
|
5218
|
+
name: root.name,
|
|
5219
|
+
irVersion: IR_VERSION3,
|
|
5220
|
+
elements: [syntheticField],
|
|
5221
|
+
...root.metadata !== void 0 && { metadata: root.metadata },
|
|
5222
|
+
...root.annotations !== void 0 && root.annotations.length > 0 && { rootAnnotations: root.annotations },
|
|
5223
|
+
typeRegistry,
|
|
5224
|
+
provenance: syntheticField.provenance
|
|
5225
|
+
},
|
|
5226
|
+
{
|
|
5227
|
+
extensionRegistry: options?.extensionRegistry,
|
|
5228
|
+
vendorPrefix: options?.vendorPrefix
|
|
5229
|
+
}
|
|
5230
|
+
);
|
|
5231
|
+
const result = schema.properties?.["__result"];
|
|
5232
|
+
if (result === void 0) {
|
|
5233
|
+
throw new Error("FormSpec failed to extract the standalone schema root from the synthetic IR.");
|
|
5234
|
+
}
|
|
5235
|
+
if (schema.$defs === void 0 || Object.keys(schema.$defs).length === 0) {
|
|
5236
|
+
return {
|
|
5237
|
+
...schema.$schema !== void 0 && { $schema: schema.$schema },
|
|
5238
|
+
...result
|
|
5239
|
+
};
|
|
5240
|
+
}
|
|
5241
|
+
return {
|
|
5242
|
+
...schema.$schema !== void 0 && { $schema: schema.$schema },
|
|
5243
|
+
...result,
|
|
5244
|
+
$defs: schema.$defs
|
|
5245
|
+
};
|
|
5246
|
+
}
|
|
5247
|
+
function generateSchemasFromAnalysis(analysis, filePath, options) {
|
|
5248
|
+
return toDiscoveredTypeSchemas(
|
|
5249
|
+
generateClassSchemas(
|
|
5250
|
+
analysis,
|
|
5251
|
+
{ file: filePath },
|
|
5252
|
+
{
|
|
5253
|
+
extensionRegistry: options?.extensionRegistry,
|
|
5254
|
+
metadata: options?.metadata,
|
|
5255
|
+
vendorPrefix: options?.vendorPrefix
|
|
5256
|
+
}
|
|
5257
|
+
)
|
|
5258
|
+
);
|
|
5259
|
+
}
|
|
5260
|
+
function generateSchemasFromResolvedType(options, skipNamedDeclaration = false, rootOverride) {
|
|
5261
|
+
const namedDeclaration = skipNamedDeclaration || hasConcreteTypeArguments(options.type, options.context.checker) ? void 0 : getNamedTypeDeclaration2(options.type);
|
|
5262
|
+
if (namedDeclaration !== void 0) {
|
|
5263
|
+
return generateSchemasFromDeclaration({
|
|
5264
|
+
...options,
|
|
5265
|
+
declaration: namedDeclaration
|
|
5266
|
+
});
|
|
5267
|
+
}
|
|
5268
|
+
const filePath = options.sourceNode?.getSourceFile().fileName ?? options.context.sourceFile.fileName;
|
|
5269
|
+
const typeRegistry = {};
|
|
5270
|
+
const diagnostics = [];
|
|
5271
|
+
const rootType = resolveTypeNode(
|
|
5272
|
+
options.type,
|
|
5273
|
+
options.context.checker,
|
|
5274
|
+
filePath,
|
|
5275
|
+
typeRegistry,
|
|
5276
|
+
/* @__PURE__ */ new Set(),
|
|
5277
|
+
options.sourceNode,
|
|
5278
|
+
normalizeMetadataPolicy(options.metadata),
|
|
5279
|
+
options.extensionRegistry,
|
|
5280
|
+
diagnostics
|
|
5281
|
+
);
|
|
5282
|
+
if (diagnostics.length > 0) {
|
|
5283
|
+
const diagnosticDetails = diagnostics.map((diagnostic) => `${diagnostic.code}: ${diagnostic.message}`).join("; ");
|
|
5284
|
+
throw new Error(
|
|
5285
|
+
`FormSpec validation failed while generating discovered type schemas. ${diagnosticDetails}`
|
|
5286
|
+
);
|
|
5287
|
+
}
|
|
5288
|
+
const describedRoot = describeRootType(
|
|
5289
|
+
rootType,
|
|
5290
|
+
typeRegistry,
|
|
5291
|
+
options.name ?? getFallbackName(options.sourceNode)
|
|
5292
|
+
);
|
|
5293
|
+
const mergedMetadata = mergeResolvedMetadata(describedRoot.metadata, rootOverride?.metadata);
|
|
5294
|
+
const root = {
|
|
5295
|
+
...describedRoot,
|
|
5296
|
+
...rootOverride?.name !== void 0 && { name: rootOverride.name },
|
|
5297
|
+
...mergedMetadata !== void 0 && { metadata: mergedMetadata },
|
|
5298
|
+
...rootOverride?.annotations !== void 0 && { annotations: rootOverride.annotations }
|
|
5299
|
+
};
|
|
5300
|
+
if (root.type.kind === "object") {
|
|
5301
|
+
return generateSchemasFromAnalysis(
|
|
5302
|
+
createObjectRootAnalysis(
|
|
5303
|
+
options.name ?? root.name,
|
|
5304
|
+
root.type.properties,
|
|
5305
|
+
typeRegistry,
|
|
5306
|
+
root.metadata,
|
|
5307
|
+
root.annotations
|
|
5308
|
+
),
|
|
5309
|
+
filePath,
|
|
5310
|
+
options
|
|
5311
|
+
);
|
|
5312
|
+
}
|
|
5313
|
+
return {
|
|
5314
|
+
jsonSchema: toStandaloneJsonSchema(root, typeRegistry, options),
|
|
5315
|
+
uiSchema: null
|
|
5316
|
+
};
|
|
5317
|
+
}
|
|
5318
|
+
function generateSchemasFromDeclaration(options) {
|
|
5319
|
+
const filePath = options.declaration.getSourceFile().fileName;
|
|
5320
|
+
if (ts7.isClassDeclaration(options.declaration)) {
|
|
5321
|
+
return generateSchemasFromAnalysis(
|
|
5322
|
+
analyzeClassToIR(
|
|
5323
|
+
options.declaration,
|
|
5324
|
+
options.context.checker,
|
|
5325
|
+
filePath,
|
|
5326
|
+
options.extensionRegistry,
|
|
5327
|
+
options.metadata
|
|
5328
|
+
),
|
|
5329
|
+
filePath,
|
|
5330
|
+
options
|
|
5331
|
+
);
|
|
5332
|
+
}
|
|
5333
|
+
if (ts7.isInterfaceDeclaration(options.declaration)) {
|
|
5334
|
+
return generateSchemasFromAnalysis(
|
|
5335
|
+
analyzeInterfaceToIR(
|
|
5336
|
+
options.declaration,
|
|
5337
|
+
options.context.checker,
|
|
5338
|
+
filePath,
|
|
5339
|
+
options.extensionRegistry,
|
|
5340
|
+
options.metadata
|
|
5341
|
+
),
|
|
5342
|
+
filePath,
|
|
5343
|
+
options
|
|
5344
|
+
);
|
|
5345
|
+
}
|
|
5346
|
+
if (ts7.isTypeAliasDeclaration(options.declaration)) {
|
|
5347
|
+
const analyzedAlias = analyzeTypeAliasToIR(
|
|
5348
|
+
options.declaration,
|
|
5349
|
+
options.context.checker,
|
|
5350
|
+
filePath,
|
|
5351
|
+
options.extensionRegistry,
|
|
5352
|
+
options.metadata
|
|
5353
|
+
);
|
|
5354
|
+
if (analyzedAlias.ok) {
|
|
5355
|
+
return generateSchemasFromAnalysis(analyzedAlias.analysis, filePath, options);
|
|
5356
|
+
}
|
|
5357
|
+
const aliasRootInfo = analyzeDeclarationRootInfo(
|
|
5358
|
+
options.declaration,
|
|
5359
|
+
options.context.checker,
|
|
5360
|
+
filePath,
|
|
5361
|
+
options.extensionRegistry,
|
|
5362
|
+
options.metadata
|
|
5363
|
+
);
|
|
5364
|
+
if (aliasRootInfo.diagnostics.length > 0) {
|
|
5365
|
+
const diagnosticDetails = aliasRootInfo.diagnostics.map((diagnostic) => `${diagnostic.code}: ${diagnostic.message}`).join("; ");
|
|
5366
|
+
throw new Error(
|
|
5367
|
+
`FormSpec validation failed while generating discovered type schemas. ${diagnosticDetails}`
|
|
5368
|
+
);
|
|
5369
|
+
}
|
|
5370
|
+
return generateSchemasFromResolvedType(
|
|
5371
|
+
{
|
|
5372
|
+
...options,
|
|
5373
|
+
type: options.context.checker.getTypeAtLocation(options.declaration),
|
|
5374
|
+
sourceNode: options.declaration,
|
|
5375
|
+
name: options.declaration.name.text
|
|
5376
|
+
},
|
|
5377
|
+
true,
|
|
5378
|
+
{
|
|
5379
|
+
name: options.declaration.name.text,
|
|
5380
|
+
...aliasRootInfo.metadata !== void 0 && { metadata: aliasRootInfo.metadata },
|
|
5381
|
+
...aliasRootInfo.annotations.length > 0 && { annotations: aliasRootInfo.annotations }
|
|
5382
|
+
}
|
|
5383
|
+
);
|
|
5384
|
+
}
|
|
5385
|
+
const _exhaustive = options.declaration;
|
|
5386
|
+
return _exhaustive;
|
|
5387
|
+
}
|
|
5388
|
+
function generateSchemasFromType(options) {
|
|
5389
|
+
return generateSchemasFromResolvedType(options);
|
|
5390
|
+
}
|
|
5391
|
+
function generateSchemasFromParameter(options) {
|
|
5392
|
+
return generateSchemasFromResolvedType({
|
|
5393
|
+
...options,
|
|
5394
|
+
type: options.context.checker.getTypeAtLocation(options.parameter),
|
|
5395
|
+
sourceNode: options.parameter,
|
|
5396
|
+
name: getFallbackName(options.parameter, "Parameter")
|
|
5397
|
+
});
|
|
5398
|
+
}
|
|
5399
|
+
function generateSchemasFromReturnType(options) {
|
|
5400
|
+
const signature = options.context.checker.getSignatureFromDeclaration(options.declaration);
|
|
5401
|
+
const type = signature !== void 0 ? options.context.checker.getReturnTypeOfSignature(signature) : options.context.checker.getTypeAtLocation(options.declaration);
|
|
5402
|
+
const fallbackName = options.declaration.name !== void 0 && ts7.isIdentifier(options.declaration.name) ? `${options.declaration.name.text}ReturnType` : "ReturnType";
|
|
5403
|
+
return generateSchemasFromResolvedType({
|
|
5404
|
+
...options,
|
|
5405
|
+
type,
|
|
5406
|
+
sourceNode: options.declaration.type ?? options.declaration,
|
|
5407
|
+
name: fallbackName
|
|
5408
|
+
});
|
|
5409
|
+
}
|
|
5410
|
+
var init_discovered_schema = __esm({
|
|
5411
|
+
"src/generators/discovered-schema.ts"() {
|
|
5412
|
+
"use strict";
|
|
5413
|
+
init_class_analyzer();
|
|
5414
|
+
init_class_schema();
|
|
5415
|
+
init_ir_generator();
|
|
5416
|
+
init_metadata();
|
|
5417
|
+
}
|
|
5418
|
+
});
|
|
5419
|
+
|
|
4881
5420
|
// src/generators/mixed-authoring.ts
|
|
4882
5421
|
function buildMixedAuthoringSchemas(options) {
|
|
4883
5422
|
const { filePath, typeName, overlays, ...schemaOptions } = options;
|
|
@@ -5084,12 +5623,20 @@ __export(index_exports, {
|
|
|
5084
5623
|
buildFormSchemas: () => buildFormSchemas,
|
|
5085
5624
|
buildMixedAuthoringSchemas: () => buildMixedAuthoringSchemas,
|
|
5086
5625
|
createExtensionRegistry: () => createExtensionRegistry,
|
|
5626
|
+
createStaticBuildContext: () => createStaticBuildContext,
|
|
5627
|
+
createStaticBuildContextFromProgram: () => createStaticBuildContextFromProgram,
|
|
5087
5628
|
generateJsonSchema: () => generateJsonSchema,
|
|
5088
5629
|
generateSchemas: () => generateSchemas,
|
|
5089
5630
|
generateSchemasFromClass: () => generateSchemasFromClass,
|
|
5631
|
+
generateSchemasFromDeclaration: () => generateSchemasFromDeclaration,
|
|
5632
|
+
generateSchemasFromParameter: () => generateSchemasFromParameter,
|
|
5090
5633
|
generateSchemasFromProgram: () => generateSchemasFromProgram,
|
|
5634
|
+
generateSchemasFromReturnType: () => generateSchemasFromReturnType,
|
|
5635
|
+
generateSchemasFromType: () => generateSchemasFromType,
|
|
5091
5636
|
generateUiSchema: () => generateUiSchema,
|
|
5092
5637
|
jsonSchema7Schema: () => jsonSchema7Schema,
|
|
5638
|
+
resolveModuleExport: () => resolveModuleExport,
|
|
5639
|
+
resolveModuleExportDeclaration: () => resolveModuleExportDeclaration,
|
|
5093
5640
|
uiSchemaSchema: () => uiSchema,
|
|
5094
5641
|
writeSchemas: () => writeSchemas
|
|
5095
5642
|
});
|
|
@@ -5126,6 +5673,8 @@ var init_index = __esm({
|
|
|
5126
5673
|
init_generator();
|
|
5127
5674
|
init_generator2();
|
|
5128
5675
|
init_class_schema();
|
|
5676
|
+
init_static_build();
|
|
5677
|
+
init_discovered_schema();
|
|
5129
5678
|
init_mixed_authoring();
|
|
5130
5679
|
}
|
|
5131
5680
|
});
|