@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.
@@ -33,6 +33,7 @@ __export(internals_exports, {
33
33
  analyzeClassToIR: () => analyzeClassToIR,
34
34
  analyzeInterfaceToIR: () => analyzeInterfaceToIR,
35
35
  analyzeNamedTypeToIRFromProgramContext: () => analyzeNamedTypeToIRFromProgramContext,
36
+ analyzeNamedTypeToIRFromProgramContextDetailed: () => analyzeNamedTypeToIRFromProgramContextDetailed,
36
37
  analyzeTypeAliasToIR: () => analyzeTypeAliasToIR,
37
38
  canonicalizeChainDSL: () => canonicalizeChainDSL,
38
39
  canonicalizeTSDoc: () => canonicalizeTSDoc,
@@ -44,6 +45,7 @@ __export(internals_exports, {
44
45
  findInterfaceByName: () => findInterfaceByName,
45
46
  findTypeAliasByName: () => findTypeAliasByName,
46
47
  generateClassSchemas: () => generateClassSchemas,
48
+ generateClassSchemasDetailed: () => generateClassSchemasDetailed,
47
49
  generateJsonSchemaFromIR: () => generateJsonSchemaFromIR,
48
50
  generateMethodSchemas: () => generateMethodSchemas,
49
51
  generateUiSchemaFromIR: () => generateUiSchemaFromIR,
@@ -3906,29 +3908,35 @@ function findFallbackAliasDuplicatePropertyNames(typeNode, checker) {
3906
3908
  }
3907
3909
  return [...duplicates].sort();
3908
3910
  }
3909
- function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
3911
+ function analyzeNamedTypeToIRFromProgramContextDetailed(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
3910
3912
  const analysisFilePath = path.resolve(filePath);
3911
3913
  const classDecl = findClassByName(ctx.sourceFile, typeName);
3912
3914
  if (classDecl !== null) {
3913
- return analyzeClassToIR(
3914
- classDecl,
3915
- ctx.checker,
3916
- analysisFilePath,
3917
- extensionRegistry,
3918
- metadataPolicy,
3919
- discriminatorOptions
3920
- );
3915
+ return {
3916
+ ok: true,
3917
+ analysis: analyzeClassToIR(
3918
+ classDecl,
3919
+ ctx.checker,
3920
+ analysisFilePath,
3921
+ extensionRegistry,
3922
+ metadataPolicy,
3923
+ discriminatorOptions
3924
+ )
3925
+ };
3921
3926
  }
3922
3927
  const interfaceDecl = findInterfaceByName(ctx.sourceFile, typeName);
3923
3928
  if (interfaceDecl !== null) {
3924
- return analyzeInterfaceToIR(
3925
- interfaceDecl,
3926
- ctx.checker,
3927
- analysisFilePath,
3928
- extensionRegistry,
3929
- metadataPolicy,
3930
- discriminatorOptions
3931
- );
3929
+ return {
3930
+ ok: true,
3931
+ analysis: analyzeInterfaceToIR(
3932
+ interfaceDecl,
3933
+ ctx.checker,
3934
+ analysisFilePath,
3935
+ extensionRegistry,
3936
+ metadataPolicy,
3937
+ discriminatorOptions
3938
+ )
3939
+ };
3932
3940
  }
3933
3941
  const typeAlias = findTypeAliasByName(ctx.sourceFile, typeName);
3934
3942
  if (typeAlias !== null) {
@@ -3941,11 +3949,20 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
3941
3949
  discriminatorOptions
3942
3950
  );
3943
3951
  if (result.ok) {
3944
- return result.analysis;
3952
+ return { ok: true, analysis: result.analysis };
3945
3953
  }
3946
3954
  const fallbackEligible = result.kind === "not-object-like" && isResolvableObjectLikeAliasTypeNode(typeAlias.type) && containsTypeReferenceInObjectLikeAlias(typeAlias.type);
3947
3955
  if (!fallbackEligible) {
3948
- throw new Error(result.error);
3956
+ return {
3957
+ ok: false,
3958
+ diagnostics: [
3959
+ makeProgramDiagnostic(
3960
+ result.kind === "duplicate-properties" ? "DUPLICATE_ROOT_PROPERTIES" : "UNSUPPORTED_ROOT_TYPE",
3961
+ result.error,
3962
+ makeNodeProvenance(typeAlias, analysisFilePath)
3963
+ )
3964
+ ]
3965
+ };
3949
3966
  }
3950
3967
  const duplicatePropertyNames = findFallbackAliasDuplicatePropertyNames(
3951
3968
  typeAlias.type,
@@ -3954,9 +3971,16 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
3954
3971
  if (duplicatePropertyNames.length > 0) {
3955
3972
  const sourceFile = typeAlias.getSourceFile();
3956
3973
  const { line } = sourceFile.getLineAndCharacterOfPosition(typeAlias.getStart());
3957
- throw new Error(
3958
- `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} contains duplicate property names across object-like members: ${duplicatePropertyNames.join(", ")}`
3959
- );
3974
+ return {
3975
+ ok: false,
3976
+ diagnostics: [
3977
+ makeProgramDiagnostic(
3978
+ "DUPLICATE_ROOT_PROPERTIES",
3979
+ `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} contains duplicate property names across object-like members: ${duplicatePropertyNames.join(", ")}`,
3980
+ makeNodeProvenance(typeAlias, analysisFilePath)
3981
+ )
3982
+ ]
3983
+ };
3960
3984
  }
3961
3985
  const rootInfo = analyzeDeclarationRootInfo(
3962
3986
  typeAlias,
@@ -3986,17 +4010,75 @@ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensi
3986
4010
  diagnostics
3987
4011
  );
3988
4012
  if (fallbackAnalysis !== null) {
3989
- return fallbackAnalysis;
4013
+ return { ok: true, analysis: fallbackAnalysis };
3990
4014
  }
3991
- throw new Error(result.error);
4015
+ return {
4016
+ ok: false,
4017
+ diagnostics: [
4018
+ makeProgramDiagnostic(
4019
+ "UNSUPPORTED_ROOT_TYPE",
4020
+ result.error,
4021
+ makeNodeProvenance(typeAlias, analysisFilePath)
4022
+ )
4023
+ ]
4024
+ };
3992
4025
  }
3993
- throw new Error(
3994
- `Type "${typeName}" not found as a class, interface, or type alias in ${analysisFilePath}`
4026
+ return {
4027
+ ok: false,
4028
+ diagnostics: [
4029
+ makeProgramDiagnostic(
4030
+ "TYPE_NOT_FOUND",
4031
+ `Type "${typeName}" not found as a class, interface, or type alias in ${analysisFilePath}`,
4032
+ makeFileProvenance(analysisFilePath)
4033
+ )
4034
+ ]
4035
+ };
4036
+ }
4037
+ function analyzeNamedTypeToIRFromProgramContext(ctx, filePath, typeName, extensionRegistry, metadataPolicy, discriminatorOptions) {
4038
+ const result = analyzeNamedTypeToIRFromProgramContextDetailed(
4039
+ ctx,
4040
+ filePath,
4041
+ typeName,
4042
+ extensionRegistry,
4043
+ metadataPolicy,
4044
+ discriminatorOptions
3995
4045
  );
4046
+ if (result.ok) {
4047
+ return result.analysis;
4048
+ }
4049
+ throw new Error(result.diagnostics.map((diagnostic) => diagnostic.message).join("\n"));
4050
+ }
4051
+ function makeProgramDiagnostic(code, message, primaryLocation) {
4052
+ return {
4053
+ code,
4054
+ message,
4055
+ severity: "error",
4056
+ primaryLocation,
4057
+ relatedLocations: []
4058
+ };
4059
+ }
4060
+ function makeNodeProvenance(node, filePath) {
4061
+ const sourceFile = node.getSourceFile();
4062
+ const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
4063
+ return {
4064
+ surface: "tsdoc",
4065
+ file: filePath,
4066
+ line: position.line + 1,
4067
+ column: position.character,
4068
+ length: node.getWidth()
4069
+ };
4070
+ }
4071
+ function makeFileProvenance(filePath) {
4072
+ return {
4073
+ surface: "tsdoc",
4074
+ file: filePath,
4075
+ line: 1,
4076
+ column: 0
4077
+ };
3996
4078
  }
3997
4079
 
3998
4080
  // src/generators/class-schema.ts
3999
- var ts5 = require("typescript");
4081
+ var ts5 = __toESM(require("typescript"), 1);
4000
4082
 
4001
4083
  // src/metadata/collision-guards.ts
4002
4084
  function assertUniqueSerializedNames(entries, scope) {
@@ -5058,11 +5140,25 @@ function validateIR(ir, options) {
5058
5140
 
5059
5141
  // src/generators/class-schema.ts
5060
5142
  function generateClassSchemas(analysis, source, options) {
5061
- const errorDiagnostics = analysis.diagnostics?.filter(
5143
+ const result = generateClassSchemasDetailed(analysis, source, options);
5144
+ if (!result.ok || result.jsonSchema === void 0 || result.uiSchema === void 0) {
5145
+ throw new Error(formatValidationError(result.diagnostics));
5146
+ }
5147
+ return {
5148
+ jsonSchema: result.jsonSchema,
5149
+ uiSchema: result.uiSchema
5150
+ };
5151
+ }
5152
+ function generateClassSchemasDetailed(analysis, source, options) {
5153
+ const analysisDiagnostics = analysis.diagnostics ?? [];
5154
+ const errorDiagnostics = analysisDiagnostics.filter(
5062
5155
  (diagnostic) => diagnostic.severity === "error"
5063
5156
  );
5064
- if (errorDiagnostics !== void 0 && errorDiagnostics.length > 0) {
5065
- throw new Error(formatValidationError(errorDiagnostics));
5157
+ if (errorDiagnostics.length > 0) {
5158
+ return {
5159
+ ok: false,
5160
+ diagnostics: analysisDiagnostics
5161
+ };
5066
5162
  }
5067
5163
  const ir = canonicalizeTSDoc(
5068
5164
  analysis,
@@ -5076,9 +5172,14 @@ function generateClassSchemas(analysis, source, options) {
5076
5172
  ...options?.vendorPrefix !== void 0 && { vendorPrefix: options.vendorPrefix }
5077
5173
  });
5078
5174
  if (!validationResult.valid) {
5079
- throw new Error(formatValidationError(validationResult.diagnostics));
5175
+ return {
5176
+ ok: false,
5177
+ diagnostics: [...analysisDiagnostics, ...validationResult.diagnostics]
5178
+ };
5080
5179
  }
5081
5180
  return {
5181
+ ok: true,
5182
+ diagnostics: [...analysisDiagnostics, ...validationResult.diagnostics],
5082
5183
  jsonSchema: generateJsonSchemaFromIR(ir, options),
5083
5184
  uiSchema: generateUiSchemaFromIR(ir)
5084
5185
  };
@@ -5358,6 +5459,7 @@ function collectFormSpecReferences(methods) {
5358
5459
  analyzeClassToIR,
5359
5460
  analyzeInterfaceToIR,
5360
5461
  analyzeNamedTypeToIRFromProgramContext,
5462
+ analyzeNamedTypeToIRFromProgramContextDetailed,
5361
5463
  analyzeTypeAliasToIR,
5362
5464
  canonicalizeChainDSL,
5363
5465
  canonicalizeTSDoc,
@@ -5369,6 +5471,7 @@ function collectFormSpecReferences(methods) {
5369
5471
  findInterfaceByName,
5370
5472
  findTypeAliasByName,
5371
5473
  generateClassSchemas,
5474
+ generateClassSchemasDetailed,
5372
5475
  generateJsonSchemaFromIR,
5373
5476
  generateMethodSchemas,
5374
5477
  generateUiSchemaFromIR,