@formspec/build 0.1.0-alpha.38 → 0.1.0-alpha.39

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/cli.cjs CHANGED
@@ -5251,29 +5251,35 @@ function analyzeNamedTypeToIR(filePath, typeName, extensionRegistry, metadataPol
5251
5251
  discriminatorOptions
5252
5252
  );
5253
5253
  }
5254
- function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
5254
+ function analyzeNamedTypeToIRFromProgramContextDetailed(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
5255
5255
  const analysisFilePath = path.resolve(filePath);
5256
5256
  const classDecl = findClassByName(ctx.sourceFile, typeName);
5257
5257
  if (classDecl !== null) {
5258
- return analyzeClassToIR(
5259
- classDecl,
5260
- ctx.checker,
5261
- analysisFilePath,
5262
- extensionRegistry,
5263
- metadataPolicy,
5264
- discriminatorOptions
5265
- );
5258
+ return {
5259
+ ok: true,
5260
+ analysis: analyzeClassToIR(
5261
+ classDecl,
5262
+ ctx.checker,
5263
+ analysisFilePath,
5264
+ extensionRegistry,
5265
+ metadataPolicy,
5266
+ discriminatorOptions
5267
+ )
5268
+ };
5266
5269
  }
5267
5270
  const interfaceDecl = findInterfaceByName(ctx.sourceFile, typeName);
5268
5271
  if (interfaceDecl !== null) {
5269
- return analyzeInterfaceToIR(
5270
- interfaceDecl,
5271
- ctx.checker,
5272
- analysisFilePath,
5273
- extensionRegistry,
5274
- metadataPolicy,
5275
- discriminatorOptions
5276
- );
5272
+ return {
5273
+ ok: true,
5274
+ analysis: analyzeInterfaceToIR(
5275
+ interfaceDecl,
5276
+ ctx.checker,
5277
+ analysisFilePath,
5278
+ extensionRegistry,
5279
+ metadataPolicy,
5280
+ discriminatorOptions
5281
+ )
5282
+ };
5277
5283
  }
5278
5284
  const typeAlias = findTypeAliasByName(ctx.sourceFile, typeName);
5279
5285
  if (typeAlias !== null) {
@@ -5286,11 +5292,20 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
5286
5292
  discriminatorOptions
5287
5293
  );
5288
5294
  if (result.ok) {
5289
- return result.analysis;
5295
+ return { ok: true, analysis: result.analysis };
5290
5296
  }
5291
5297
  const fallbackEligible = result.kind === "not-object-like" && isResolvableObjectLikeAliasTypeNode(typeAlias.type) && containsTypeReferenceInObjectLikeAlias(typeAlias.type);
5292
5298
  if (!fallbackEligible) {
5293
- throw new Error(result.error);
5299
+ return {
5300
+ ok: false,
5301
+ diagnostics: [
5302
+ makeProgramDiagnostic(
5303
+ result.kind === "duplicate-properties" ? "DUPLICATE_ROOT_PROPERTIES" : "UNSUPPORTED_ROOT_TYPE",
5304
+ result.error,
5305
+ makeNodeProvenance(typeAlias, analysisFilePath)
5306
+ )
5307
+ ]
5308
+ };
5294
5309
  }
5295
5310
  const duplicatePropertyNames = findFallbackAliasDuplicatePropertyNames(
5296
5311
  typeAlias.type,
@@ -5299,9 +5314,16 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
5299
5314
  if (duplicatePropertyNames.length > 0) {
5300
5315
  const sourceFile = typeAlias.getSourceFile();
5301
5316
  const { line } = sourceFile.getLineAndCharacterOfPosition(typeAlias.getStart());
5302
- throw new Error(
5303
- `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} contains duplicate property names across object-like members: ${duplicatePropertyNames.join(", ")}`
5304
- );
5317
+ return {
5318
+ ok: false,
5319
+ diagnostics: [
5320
+ makeProgramDiagnostic(
5321
+ "DUPLICATE_ROOT_PROPERTIES",
5322
+ `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} contains duplicate property names across object-like members: ${duplicatePropertyNames.join(", ")}`,
5323
+ makeNodeProvenance(typeAlias, analysisFilePath)
5324
+ )
5325
+ ]
5326
+ };
5305
5327
  }
5306
5328
  const rootInfo = analyzeDeclarationRootInfo(
5307
5329
  typeAlias,
@@ -5331,13 +5353,71 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
5331
5353
  diagnostics
5332
5354
  );
5333
5355
  if (fallbackAnalysis !== null) {
5334
- return fallbackAnalysis;
5356
+ return { ok: true, analysis: fallbackAnalysis };
5335
5357
  }
5336
- throw new Error(result.error);
5358
+ return {
5359
+ ok: false,
5360
+ diagnostics: [
5361
+ makeProgramDiagnostic(
5362
+ "UNSUPPORTED_ROOT_TYPE",
5363
+ result.error,
5364
+ makeNodeProvenance(typeAlias, analysisFilePath)
5365
+ )
5366
+ ]
5367
+ };
5337
5368
  }
5338
- throw new Error(
5339
- `Type "${typeName}" not found as a class, interface, or type alias in ${analysisFilePath}`
5369
+ return {
5370
+ ok: false,
5371
+ diagnostics: [
5372
+ makeProgramDiagnostic(
5373
+ "TYPE_NOT_FOUND",
5374
+ `Type "${typeName}" not found as a class, interface, or type alias in ${analysisFilePath}`,
5375
+ makeFileProvenance(analysisFilePath)
5376
+ )
5377
+ ]
5378
+ };
5379
+ }
5380
+ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
5381
+ const result = analyzeNamedTypeToIRFromProgramContextDetailed(
5382
+ ctx,
5383
+ filePath,
5384
+ typeName,
5385
+ extensionRegistry,
5386
+ metadataPolicy,
5387
+ discriminatorOptions
5340
5388
  );
5389
+ if (result.ok) {
5390
+ return result.analysis;
5391
+ }
5392
+ throw new Error(result.diagnostics.map((diagnostic) => diagnostic.message).join("\n"));
5393
+ }
5394
+ function makeProgramDiagnostic(code, message, primaryLocation) {
5395
+ return {
5396
+ code,
5397
+ message,
5398
+ severity: "error",
5399
+ primaryLocation,
5400
+ relatedLocations: []
5401
+ };
5402
+ }
5403
+ function makeNodeProvenance(node, filePath) {
5404
+ const sourceFile = node.getSourceFile();
5405
+ const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
5406
+ return {
5407
+ surface: "tsdoc",
5408
+ file: filePath,
5409
+ line: position.line + 1,
5410
+ column: position.character,
5411
+ length: node.getWidth()
5412
+ };
5413
+ }
5414
+ function makeFileProvenance(filePath) {
5415
+ return {
5416
+ surface: "tsdoc",
5417
+ file: filePath,
5418
+ line: 1,
5419
+ column: 0
5420
+ };
5341
5421
  }
5342
5422
  var ts4, path;
5343
5423
  var init_program = __esm({
@@ -5438,11 +5518,25 @@ var init_validate = __esm({
5438
5518
 
5439
5519
  // src/generators/class-schema.ts
5440
5520
  function generateClassSchemas(analysis, source, options) {
5441
- const errorDiagnostics = analysis.diagnostics?.filter(
5521
+ const result = generateClassSchemasDetailed(analysis, source, options);
5522
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5523
+ throw new Error(formatValidationError(result.diagnostics));
5524
+ }
5525
+ return {
5526
+ jsonSchema: result.jsonSchema,
5527
+ uiSchema: result.uiSchema
5528
+ };
5529
+ }
5530
+ function generateClassSchemasDetailed(analysis, source, options) {
5531
+ const analysisDiagnostics = analysis.diagnostics ?? [];
5532
+ const errorDiagnostics = analysisDiagnostics.filter(
5442
5533
  (diagnostic) => diagnostic.severity === "error"
5443
5534
  );
5444
- if (errorDiagnostics !== void 0 && errorDiagnostics.length > 0) {
5445
- throw new Error(formatValidationError(errorDiagnostics));
5535
+ if (errorDiagnostics.length > 0) {
5536
+ return {
5537
+ ok: false,
5538
+ diagnostics: analysisDiagnostics
5539
+ };
5446
5540
  }
5447
5541
  const ir = canonicalizeTSDoc(
5448
5542
  analysis,
@@ -5456,9 +5550,14 @@ function generateClassSchemas(analysis, source, options) {
5456
5550
  ...options?.vendorPrefix !== void 0 && { vendorPrefix: options.vendorPrefix }
5457
5551
  });
5458
5552
  if (!validationResult.valid) {
5459
- throw new Error(formatValidationError(validationResult.diagnostics));
5553
+ return {
5554
+ ok: false,
5555
+ diagnostics: [...analysisDiagnostics, ...validationResult.diagnostics]
5556
+ };
5460
5557
  }
5461
5558
  return {
5559
+ ok: true,
5560
+ diagnostics: [...analysisDiagnostics, ...validationResult.diagnostics],
5462
5561
  jsonSchema: generateJsonSchemaFromIR(ir, options),
5463
5562
  uiSchema: generateUiSchemaFromIR(ir)
5464
5563
  };
@@ -5500,25 +5599,127 @@ function generateSchemasFromClass(options) {
5500
5599
  );
5501
5600
  }
5502
5601
  function generateSchemas(options) {
5503
- const ctx = createProgramContext(options.filePath);
5602
+ const result = generateSchemasDetailedInternal(options);
5603
+ if (options.errorReporting === "diagnostics") {
5604
+ return result;
5605
+ }
5606
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5607
+ throw new Error(formatValidationError(result.diagnostics));
5608
+ }
5609
+ return {
5610
+ jsonSchema: result.jsonSchema,
5611
+ uiSchema: result.uiSchema
5612
+ };
5613
+ }
5614
+ function generateSchemasFromProgram(options) {
5615
+ const result = generateSchemasFromProgramDetailedInternal(options);
5616
+ if (options.errorReporting === "diagnostics") {
5617
+ return result;
5618
+ }
5619
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5620
+ throw new Error(formatValidationError(result.diagnostics));
5621
+ }
5622
+ return {
5623
+ jsonSchema: result.jsonSchema,
5624
+ uiSchema: result.uiSchema
5625
+ };
5626
+ }
5627
+ function generateSchemasDetailed(options) {
5628
+ return generateSchemas({
5629
+ ...options,
5630
+ errorReporting: "diagnostics"
5631
+ });
5632
+ }
5633
+ function generateSchemasDetailedInternal(options) {
5634
+ let ctx;
5635
+ try {
5636
+ ctx = createProgramContext(options.filePath);
5637
+ } catch (error) {
5638
+ return {
5639
+ ok: false,
5640
+ diagnostics: [createProgramContextFailureDiagnostic(options.filePath, error)]
5641
+ };
5642
+ }
5643
+ return generateSchemasFromDetailedProgramContext(ctx, options.filePath, options.typeName, options);
5644
+ }
5645
+ function generateSchemasFromProgramDetailed(options) {
5504
5646
  return generateSchemasFromProgram({
5505
5647
  ...options,
5506
- program: ctx.program
5648
+ errorReporting: "diagnostics"
5507
5649
  });
5508
5650
  }
5509
- function generateSchemasFromProgram(options) {
5510
- const ctx = createProgramContextFromProgram(options.program, options.filePath);
5511
- const analysis = analyzeNamedTypeToIRFromProgramContext(
5651
+ function generateSchemasFromProgramDetailedInternal(options) {
5652
+ let ctx;
5653
+ try {
5654
+ ctx = createProgramContextFromProgram(options.program, options.filePath);
5655
+ } catch (error) {
5656
+ return {
5657
+ ok: false,
5658
+ diagnostics: [createProgramContextFailureDiagnostic(options.filePath, error)]
5659
+ };
5660
+ }
5661
+ return generateSchemasFromDetailedProgramContext(ctx, options.filePath, options.typeName, options);
5662
+ }
5663
+ function generateSchemasBatch(options) {
5664
+ const contextCache = /* @__PURE__ */ new Map();
5665
+ return options.targets.map((target) => {
5666
+ let ctx;
5667
+ try {
5668
+ const cacheKey = ts5.sys.useCaseSensitiveFileNames ? target.filePath : target.filePath.toLowerCase();
5669
+ const cachedContext = contextCache.get(cacheKey);
5670
+ if (cachedContext === void 0) {
5671
+ ctx = createProgramContext(target.filePath);
5672
+ contextCache.set(cacheKey, ctx);
5673
+ } else {
5674
+ ctx = cachedContext;
5675
+ }
5676
+ } catch (error) {
5677
+ return withTarget(target, {
5678
+ ok: false,
5679
+ diagnostics: [createProgramContextFailureDiagnostic(target.filePath, error)]
5680
+ });
5681
+ }
5682
+ return withTarget(
5683
+ target,
5684
+ generateSchemasFromDetailedProgramContext(ctx, target.filePath, target.typeName, options)
5685
+ );
5686
+ });
5687
+ }
5688
+ function generateSchemasBatchFromProgram(options) {
5689
+ return options.targets.map((target) => {
5690
+ let ctx;
5691
+ try {
5692
+ ctx = createProgramContextFromProgram(options.program, target.filePath);
5693
+ } catch (error) {
5694
+ return withTarget(target, {
5695
+ ok: false,
5696
+ diagnostics: [createProgramContextFailureDiagnostic(target.filePath, error)]
5697
+ });
5698
+ }
5699
+ return withTarget(
5700
+ target,
5701
+ generateSchemasFromDetailedProgramContext(ctx, target.filePath, target.typeName, options)
5702
+ );
5703
+ });
5704
+ }
5705
+ function generateSchemasFromDetailedProgramContext(ctx, filePath, typeName, options) {
5706
+ const analysisResult = analyzeNamedTypeToIRFromProgramContextDetailed(
5512
5707
  ctx,
5513
- options.filePath,
5514
- options.typeName,
5708
+ filePath,
5709
+ typeName,
5515
5710
  options.extensionRegistry,
5516
5711
  options.metadata,
5517
5712
  options.discriminator
5518
5713
  );
5519
- return generateClassSchemas(
5520
- analysis,
5521
- { file: options.filePath },
5714
+ if (!analysisResult.ok) {
5715
+ return {
5716
+ ok: false,
5717
+ diagnostics: analysisResult.diagnostics
5718
+ };
5719
+ }
5720
+ return generateClassSchemasDetailed(
5721
+ analysisResult.analysis,
5722
+ { file: filePath },
5522
5723
  {
5523
5724
  extensionRegistry: options.extensionRegistry,
5524
5725
  metadata: options.metadata,
@@ -5526,11 +5727,32 @@ function generateSchemasFromProgram(options) {
5526
5727
  }
5527
5728
  );
5528
5729
  }
5730
+ function withTarget(target, result) {
5731
+ return {
5732
+ filePath: target.filePath,
5733
+ typeName: target.typeName,
5734
+ ...result
5735
+ };
5736
+ }
5737
+ function createProgramContextFailureDiagnostic(filePath, error) {
5738
+ return {
5739
+ code: "PROGRAM_CONTEXT_FAILURE",
5740
+ message: error instanceof Error ? error.message : String(error),
5741
+ severity: "error",
5742
+ primaryLocation: {
5743
+ surface: "tsdoc",
5744
+ file: filePath,
5745
+ line: 1,
5746
+ column: 0
5747
+ },
5748
+ relatedLocations: []
5749
+ };
5750
+ }
5529
5751
  var ts5;
5530
5752
  var init_class_schema = __esm({
5531
5753
  "src/generators/class-schema.ts"() {
5532
5754
  "use strict";
5533
- ts5 = require("typescript");
5755
+ ts5 = __toESM(require("typescript"), 1);
5534
5756
  init_program();
5535
5757
  init_class_analyzer();
5536
5758
  init_canonicalize();
@@ -5581,8 +5803,11 @@ var init_static_build = __esm({
5581
5803
  });
5582
5804
 
5583
5805
  // src/generators/discovered-schema.ts
5584
- function toDiscoveredTypeSchemas(result) {
5585
- return result;
5806
+ function toDiscoveredTypeSchemas(result, resolvedMetadata) {
5807
+ return {
5808
+ ...result,
5809
+ ...resolvedMetadata !== void 0 && { resolvedMetadata }
5810
+ };
5586
5811
  }
5587
5812
  function isNamedTypeDeclaration(declaration) {
5588
5813
  return ts7.isClassDeclaration(declaration) || ts7.isInterfaceDeclaration(declaration) || ts7.isTypeAliasDeclaration(declaration);
@@ -5730,7 +5955,8 @@ function generateSchemasFromAnalysis(analysis, filePath, options) {
5730
5955
  metadata: options?.metadata,
5731
5956
  vendorPrefix: options?.vendorPrefix
5732
5957
  }
5733
- )
5958
+ ),
5959
+ analysis.metadata
5734
5960
  );
5735
5961
  }
5736
5962
  function generateSchemasFromResolvedType(options, skipNamedDeclaration = false, rootOverride) {
@@ -5788,7 +6014,8 @@ function generateSchemasFromResolvedType(options, skipNamedDeclaration = false,
5788
6014
  }
5789
6015
  return {
5790
6016
  jsonSchema: toStandaloneJsonSchema(root, typeRegistry, options),
5791
- uiSchema: null
6017
+ uiSchema: null,
6018
+ ...root.metadata !== void 0 && { resolvedMetadata: root.metadata }
5792
6019
  };
5793
6020
  }
5794
6021
  function generateSchemasFromDeclaration(options) {
@@ -6131,10 +6358,14 @@ __export(index_exports, {
6131
6358
  createStaticBuildContextFromProgram: () => createStaticBuildContextFromProgram,
6132
6359
  generateJsonSchema: () => generateJsonSchema,
6133
6360
  generateSchemas: () => generateSchemas,
6361
+ generateSchemasBatch: () => generateSchemasBatch,
6362
+ generateSchemasBatchFromProgram: () => generateSchemasBatchFromProgram,
6363
+ generateSchemasDetailed: () => generateSchemasDetailed,
6134
6364
  generateSchemasFromClass: () => generateSchemasFromClass,
6135
6365
  generateSchemasFromDeclaration: () => generateSchemasFromDeclaration,
6136
6366
  generateSchemasFromParameter: () => generateSchemasFromParameter,
6137
6367
  generateSchemasFromProgram: () => generateSchemasFromProgram,
6368
+ generateSchemasFromProgramDetailed: () => generateSchemasFromProgramDetailed,
6138
6369
  generateSchemasFromReturnType: () => generateSchemasFromReturnType,
6139
6370
  generateSchemasFromType: () => generateSchemasFromType,
6140
6371
  generateUiSchema: () => generateUiSchema,
@@ -6178,6 +6409,9 @@ var init_index = __esm({
6178
6409
  init_generator();
6179
6410
  init_generator2();
6180
6411
  init_class_schema();
6412
+ init_class_schema();
6413
+ init_class_schema();
6414
+ init_class_schema();
6181
6415
  init_static_build();
6182
6416
  init_discovered_schema();
6183
6417
  init_mixed_authoring();