@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.js CHANGED
@@ -5264,29 +5264,35 @@ function analyzeNamedTypeToIR(filePath, typeName, extensionRegistry, metadataPol
5264
5264
  discriminatorOptions
5265
5265
  );
5266
5266
  }
5267
- function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
5267
+ function analyzeNamedTypeToIRFromProgramContextDetailed(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
5268
5268
  const analysisFilePath = path.resolve(filePath);
5269
5269
  const classDecl = findClassByName(ctx.sourceFile, typeName);
5270
5270
  if (classDecl !== null) {
5271
- return analyzeClassToIR(
5272
- classDecl,
5273
- ctx.checker,
5274
- analysisFilePath,
5275
- extensionRegistry,
5276
- metadataPolicy,
5277
- discriminatorOptions
5278
- );
5271
+ return {
5272
+ ok: true,
5273
+ analysis: analyzeClassToIR(
5274
+ classDecl,
5275
+ ctx.checker,
5276
+ analysisFilePath,
5277
+ extensionRegistry,
5278
+ metadataPolicy,
5279
+ discriminatorOptions
5280
+ )
5281
+ };
5279
5282
  }
5280
5283
  const interfaceDecl = findInterfaceByName(ctx.sourceFile, typeName);
5281
5284
  if (interfaceDecl !== null) {
5282
- return analyzeInterfaceToIR(
5283
- interfaceDecl,
5284
- ctx.checker,
5285
- analysisFilePath,
5286
- extensionRegistry,
5287
- metadataPolicy,
5288
- discriminatorOptions
5289
- );
5285
+ return {
5286
+ ok: true,
5287
+ analysis: analyzeInterfaceToIR(
5288
+ interfaceDecl,
5289
+ ctx.checker,
5290
+ analysisFilePath,
5291
+ extensionRegistry,
5292
+ metadataPolicy,
5293
+ discriminatorOptions
5294
+ )
5295
+ };
5290
5296
  }
5291
5297
  const typeAlias = findTypeAliasByName(ctx.sourceFile, typeName);
5292
5298
  if (typeAlias !== null) {
@@ -5299,11 +5305,20 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
5299
5305
  discriminatorOptions
5300
5306
  );
5301
5307
  if (result.ok) {
5302
- return result.analysis;
5308
+ return { ok: true, analysis: result.analysis };
5303
5309
  }
5304
5310
  const fallbackEligible = result.kind === "not-object-like" && isResolvableObjectLikeAliasTypeNode(typeAlias.type) && containsTypeReferenceInObjectLikeAlias(typeAlias.type);
5305
5311
  if (!fallbackEligible) {
5306
- throw new Error(result.error);
5312
+ return {
5313
+ ok: false,
5314
+ diagnostics: [
5315
+ makeProgramDiagnostic(
5316
+ result.kind === "duplicate-properties" ? "DUPLICATE_ROOT_PROPERTIES" : "UNSUPPORTED_ROOT_TYPE",
5317
+ result.error,
5318
+ makeNodeProvenance(typeAlias, analysisFilePath)
5319
+ )
5320
+ ]
5321
+ };
5307
5322
  }
5308
5323
  const duplicatePropertyNames = findFallbackAliasDuplicatePropertyNames(
5309
5324
  typeAlias.type,
@@ -5312,9 +5327,16 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
5312
5327
  if (duplicatePropertyNames.length > 0) {
5313
5328
  const sourceFile = typeAlias.getSourceFile();
5314
5329
  const { line } = sourceFile.getLineAndCharacterOfPosition(typeAlias.getStart());
5315
- throw new Error(
5316
- `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} contains duplicate property names across object-like members: ${duplicatePropertyNames.join(", ")}`
5317
- );
5330
+ return {
5331
+ ok: false,
5332
+ diagnostics: [
5333
+ makeProgramDiagnostic(
5334
+ "DUPLICATE_ROOT_PROPERTIES",
5335
+ `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} contains duplicate property names across object-like members: ${duplicatePropertyNames.join(", ")}`,
5336
+ makeNodeProvenance(typeAlias, analysisFilePath)
5337
+ )
5338
+ ]
5339
+ };
5318
5340
  }
5319
5341
  const rootInfo = analyzeDeclarationRootInfo(
5320
5342
  typeAlias,
@@ -5344,13 +5366,71 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
5344
5366
  diagnostics
5345
5367
  );
5346
5368
  if (fallbackAnalysis !== null) {
5347
- return fallbackAnalysis;
5369
+ return { ok: true, analysis: fallbackAnalysis };
5348
5370
  }
5349
- throw new Error(result.error);
5371
+ return {
5372
+ ok: false,
5373
+ diagnostics: [
5374
+ makeProgramDiagnostic(
5375
+ "UNSUPPORTED_ROOT_TYPE",
5376
+ result.error,
5377
+ makeNodeProvenance(typeAlias, analysisFilePath)
5378
+ )
5379
+ ]
5380
+ };
5350
5381
  }
5351
- throw new Error(
5352
- `Type "${typeName}" not found as a class, interface, or type alias in ${analysisFilePath}`
5382
+ return {
5383
+ ok: false,
5384
+ diagnostics: [
5385
+ makeProgramDiagnostic(
5386
+ "TYPE_NOT_FOUND",
5387
+ `Type "${typeName}" not found as a class, interface, or type alias in ${analysisFilePath}`,
5388
+ makeFileProvenance(analysisFilePath)
5389
+ )
5390
+ ]
5391
+ };
5392
+ }
5393
+ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
5394
+ const result = analyzeNamedTypeToIRFromProgramContextDetailed(
5395
+ ctx,
5396
+ filePath,
5397
+ typeName,
5398
+ extensionRegistry,
5399
+ metadataPolicy,
5400
+ discriminatorOptions
5353
5401
  );
5402
+ if (result.ok) {
5403
+ return result.analysis;
5404
+ }
5405
+ throw new Error(result.diagnostics.map((diagnostic) => diagnostic.message).join("\n"));
5406
+ }
5407
+ function makeProgramDiagnostic(code, message, primaryLocation) {
5408
+ return {
5409
+ code,
5410
+ message,
5411
+ severity: "error",
5412
+ primaryLocation,
5413
+ relatedLocations: []
5414
+ };
5415
+ }
5416
+ function makeNodeProvenance(node, filePath) {
5417
+ const sourceFile = node.getSourceFile();
5418
+ const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
5419
+ return {
5420
+ surface: "tsdoc",
5421
+ file: filePath,
5422
+ line: position.line + 1,
5423
+ column: position.character,
5424
+ length: node.getWidth()
5425
+ };
5426
+ }
5427
+ function makeFileProvenance(filePath) {
5428
+ return {
5429
+ surface: "tsdoc",
5430
+ file: filePath,
5431
+ line: 1,
5432
+ column: 0
5433
+ };
5354
5434
  }
5355
5435
  var init_program = __esm({
5356
5436
  "src/analyzer/program.ts"() {
@@ -5448,13 +5528,27 @@ var init_validate = __esm({
5448
5528
  });
5449
5529
 
5450
5530
  // src/generators/class-schema.ts
5451
- import "typescript";
5531
+ import * as ts5 from "typescript";
5452
5532
  function generateClassSchemas(analysis, source, options) {
5453
- const errorDiagnostics = analysis.diagnostics?.filter(
5533
+ const result = generateClassSchemasDetailed(analysis, source, options);
5534
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5535
+ throw new Error(formatValidationError(result.diagnostics));
5536
+ }
5537
+ return {
5538
+ jsonSchema: result.jsonSchema,
5539
+ uiSchema: result.uiSchema
5540
+ };
5541
+ }
5542
+ function generateClassSchemasDetailed(analysis, source, options) {
5543
+ const analysisDiagnostics = analysis.diagnostics ?? [];
5544
+ const errorDiagnostics = analysisDiagnostics.filter(
5454
5545
  (diagnostic) => diagnostic.severity === "error"
5455
5546
  );
5456
- if (errorDiagnostics !== void 0 && errorDiagnostics.length > 0) {
5457
- throw new Error(formatValidationError(errorDiagnostics));
5547
+ if (errorDiagnostics.length > 0) {
5548
+ return {
5549
+ ok: false,
5550
+ diagnostics: analysisDiagnostics
5551
+ };
5458
5552
  }
5459
5553
  const ir = canonicalizeTSDoc(
5460
5554
  analysis,
@@ -5468,9 +5562,14 @@ function generateClassSchemas(analysis, source, options) {
5468
5562
  ...options?.vendorPrefix !== void 0 && { vendorPrefix: options.vendorPrefix }
5469
5563
  });
5470
5564
  if (!validationResult.valid) {
5471
- throw new Error(formatValidationError(validationResult.diagnostics));
5565
+ return {
5566
+ ok: false,
5567
+ diagnostics: [...analysisDiagnostics, ...validationResult.diagnostics]
5568
+ };
5472
5569
  }
5473
5570
  return {
5571
+ ok: true,
5572
+ diagnostics: [...analysisDiagnostics, ...validationResult.diagnostics],
5474
5573
  jsonSchema: generateJsonSchemaFromIR(ir, options),
5475
5574
  uiSchema: generateUiSchemaFromIR(ir)
5476
5575
  };
@@ -5512,25 +5611,127 @@ function generateSchemasFromClass(options) {
5512
5611
  );
5513
5612
  }
5514
5613
  function generateSchemas(options) {
5515
- const ctx = createProgramContext(options.filePath);
5614
+ const result = generateSchemasDetailedInternal(options);
5615
+ if (options.errorReporting === "diagnostics") {
5616
+ return result;
5617
+ }
5618
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5619
+ throw new Error(formatValidationError(result.diagnostics));
5620
+ }
5621
+ return {
5622
+ jsonSchema: result.jsonSchema,
5623
+ uiSchema: result.uiSchema
5624
+ };
5625
+ }
5626
+ function generateSchemasFromProgram(options) {
5627
+ const result = generateSchemasFromProgramDetailedInternal(options);
5628
+ if (options.errorReporting === "diagnostics") {
5629
+ return result;
5630
+ }
5631
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5632
+ throw new Error(formatValidationError(result.diagnostics));
5633
+ }
5634
+ return {
5635
+ jsonSchema: result.jsonSchema,
5636
+ uiSchema: result.uiSchema
5637
+ };
5638
+ }
5639
+ function generateSchemasDetailed(options) {
5640
+ return generateSchemas({
5641
+ ...options,
5642
+ errorReporting: "diagnostics"
5643
+ });
5644
+ }
5645
+ function generateSchemasDetailedInternal(options) {
5646
+ let ctx;
5647
+ try {
5648
+ ctx = createProgramContext(options.filePath);
5649
+ } catch (error) {
5650
+ return {
5651
+ ok: false,
5652
+ diagnostics: [createProgramContextFailureDiagnostic(options.filePath, error)]
5653
+ };
5654
+ }
5655
+ return generateSchemasFromDetailedProgramContext(ctx, options.filePath, options.typeName, options);
5656
+ }
5657
+ function generateSchemasFromProgramDetailed(options) {
5516
5658
  return generateSchemasFromProgram({
5517
5659
  ...options,
5518
- program: ctx.program
5660
+ errorReporting: "diagnostics"
5519
5661
  });
5520
5662
  }
5521
- function generateSchemasFromProgram(options) {
5522
- const ctx = createProgramContextFromProgram(options.program, options.filePath);
5523
- const analysis = analyzeNamedTypeToIRFromProgramContext(
5663
+ function generateSchemasFromProgramDetailedInternal(options) {
5664
+ let ctx;
5665
+ try {
5666
+ ctx = createProgramContextFromProgram(options.program, options.filePath);
5667
+ } catch (error) {
5668
+ return {
5669
+ ok: false,
5670
+ diagnostics: [createProgramContextFailureDiagnostic(options.filePath, error)]
5671
+ };
5672
+ }
5673
+ return generateSchemasFromDetailedProgramContext(ctx, options.filePath, options.typeName, options);
5674
+ }
5675
+ function generateSchemasBatch(options) {
5676
+ const contextCache = /* @__PURE__ */ new Map();
5677
+ return options.targets.map((target) => {
5678
+ let ctx;
5679
+ try {
5680
+ const cacheKey = ts5.sys.useCaseSensitiveFileNames ? target.filePath : target.filePath.toLowerCase();
5681
+ const cachedContext = contextCache.get(cacheKey);
5682
+ if (cachedContext === void 0) {
5683
+ ctx = createProgramContext(target.filePath);
5684
+ contextCache.set(cacheKey, ctx);
5685
+ } else {
5686
+ ctx = cachedContext;
5687
+ }
5688
+ } catch (error) {
5689
+ return withTarget(target, {
5690
+ ok: false,
5691
+ diagnostics: [createProgramContextFailureDiagnostic(target.filePath, error)]
5692
+ });
5693
+ }
5694
+ return withTarget(
5695
+ target,
5696
+ generateSchemasFromDetailedProgramContext(ctx, target.filePath, target.typeName, options)
5697
+ );
5698
+ });
5699
+ }
5700
+ function generateSchemasBatchFromProgram(options) {
5701
+ return options.targets.map((target) => {
5702
+ let ctx;
5703
+ try {
5704
+ ctx = createProgramContextFromProgram(options.program, target.filePath);
5705
+ } catch (error) {
5706
+ return withTarget(target, {
5707
+ ok: false,
5708
+ diagnostics: [createProgramContextFailureDiagnostic(target.filePath, error)]
5709
+ });
5710
+ }
5711
+ return withTarget(
5712
+ target,
5713
+ generateSchemasFromDetailedProgramContext(ctx, target.filePath, target.typeName, options)
5714
+ );
5715
+ });
5716
+ }
5717
+ function generateSchemasFromDetailedProgramContext(ctx, filePath, typeName, options) {
5718
+ const analysisResult = analyzeNamedTypeToIRFromProgramContextDetailed(
5524
5719
  ctx,
5525
- options.filePath,
5526
- options.typeName,
5720
+ filePath,
5721
+ typeName,
5527
5722
  options.extensionRegistry,
5528
5723
  options.metadata,
5529
5724
  options.discriminator
5530
5725
  );
5531
- return generateClassSchemas(
5532
- analysis,
5533
- { file: options.filePath },
5726
+ if (!analysisResult.ok) {
5727
+ return {
5728
+ ok: false,
5729
+ diagnostics: analysisResult.diagnostics
5730
+ };
5731
+ }
5732
+ return generateClassSchemasDetailed(
5733
+ analysisResult.analysis,
5734
+ { file: filePath },
5534
5735
  {
5535
5736
  extensionRegistry: options.extensionRegistry,
5536
5737
  metadata: options.metadata,
@@ -5538,6 +5739,27 @@ function generateSchemasFromProgram(options) {
5538
5739
  }
5539
5740
  );
5540
5741
  }
5742
+ function withTarget(target, result) {
5743
+ return {
5744
+ filePath: target.filePath,
5745
+ typeName: target.typeName,
5746
+ ...result
5747
+ };
5748
+ }
5749
+ function createProgramContextFailureDiagnostic(filePath, error) {
5750
+ return {
5751
+ code: "PROGRAM_CONTEXT_FAILURE",
5752
+ message: error instanceof Error ? error.message : String(error),
5753
+ severity: "error",
5754
+ primaryLocation: {
5755
+ surface: "tsdoc",
5756
+ file: filePath,
5757
+ line: 1,
5758
+ column: 0
5759
+ },
5760
+ relatedLocations: []
5761
+ };
5762
+ }
5541
5763
  var init_class_schema = __esm({
5542
5764
  "src/generators/class-schema.ts"() {
5543
5765
  "use strict";
@@ -5592,8 +5814,11 @@ var init_static_build = __esm({
5592
5814
  // src/generators/discovered-schema.ts
5593
5815
  import * as ts7 from "typescript";
5594
5816
  import { IR_VERSION as IR_VERSION3 } from "@formspec/core/internals";
5595
- function toDiscoveredTypeSchemas(result) {
5596
- return result;
5817
+ function toDiscoveredTypeSchemas(result, resolvedMetadata) {
5818
+ return {
5819
+ ...result,
5820
+ ...resolvedMetadata !== void 0 && { resolvedMetadata }
5821
+ };
5597
5822
  }
5598
5823
  function isNamedTypeDeclaration(declaration) {
5599
5824
  return ts7.isClassDeclaration(declaration) || ts7.isInterfaceDeclaration(declaration) || ts7.isTypeAliasDeclaration(declaration);
@@ -5741,7 +5966,8 @@ function generateSchemasFromAnalysis(analysis, filePath, options) {
5741
5966
  metadata: options?.metadata,
5742
5967
  vendorPrefix: options?.vendorPrefix
5743
5968
  }
5744
- )
5969
+ ),
5970
+ analysis.metadata
5745
5971
  );
5746
5972
  }
5747
5973
  function generateSchemasFromResolvedType(options, skipNamedDeclaration = false, rootOverride) {
@@ -5799,7 +6025,8 @@ function generateSchemasFromResolvedType(options, skipNamedDeclaration = false,
5799
6025
  }
5800
6026
  return {
5801
6027
  jsonSchema: toStandaloneJsonSchema(root, typeRegistry, options),
5802
- uiSchema: null
6028
+ uiSchema: null,
6029
+ ...root.metadata !== void 0 && { resolvedMetadata: root.metadata }
5803
6030
  };
5804
6031
  }
5805
6032
  function generateSchemasFromDeclaration(options) {
@@ -6139,10 +6366,14 @@ __export(index_exports, {
6139
6366
  createStaticBuildContextFromProgram: () => createStaticBuildContextFromProgram,
6140
6367
  generateJsonSchema: () => generateJsonSchema,
6141
6368
  generateSchemas: () => generateSchemas,
6369
+ generateSchemasBatch: () => generateSchemasBatch,
6370
+ generateSchemasBatchFromProgram: () => generateSchemasBatchFromProgram,
6371
+ generateSchemasDetailed: () => generateSchemasDetailed,
6142
6372
  generateSchemasFromClass: () => generateSchemasFromClass,
6143
6373
  generateSchemasFromDeclaration: () => generateSchemasFromDeclaration,
6144
6374
  generateSchemasFromParameter: () => generateSchemasFromParameter,
6145
6375
  generateSchemasFromProgram: () => generateSchemasFromProgram,
6376
+ generateSchemasFromProgramDetailed: () => generateSchemasFromProgramDetailed,
6146
6377
  generateSchemasFromReturnType: () => generateSchemasFromReturnType,
6147
6378
  generateSchemasFromType: () => generateSchemasFromType,
6148
6379
  generateUiSchema: () => generateUiSchema,
@@ -6185,6 +6416,9 @@ var init_index = __esm({
6185
6416
  init_generator();
6186
6417
  init_generator2();
6187
6418
  init_class_schema();
6419
+ init_class_schema();
6420
+ init_class_schema();
6421
+ init_class_schema();
6188
6422
  init_static_build();
6189
6423
  init_discovered_schema();
6190
6424
  init_mixed_authoring();