@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/README.md CHANGED
@@ -182,15 +182,22 @@ Generation validates canonical IR before emitting schemas. Invalid inputs now fa
182
182
 
183
183
  ### Handling Generation Failures
184
184
 
185
- Static generation APIs such as `generateSchemas()` still throw on invalid input, but the thrown error message now includes stable diagnostic codes that consumers can inspect or surface directly.
185
+ Static generation now uses explicit error reporting on the main entry points:
186
+
187
+ - `generateSchemas({ ..., errorReporting: "throw" })` and `generateSchemasFromProgram({ ..., errorReporting: "throw" })` keep the simple throw-on-error contract.
188
+ - `generateSchemas({ ..., errorReporting: "diagnostics" })` and `generateSchemasFromProgram({ ..., errorReporting: "diagnostics" })` return structured diagnostics instead of throwing for analysis and validation failures.
189
+ - `generateSchemasBatch()` and `generateSchemasBatchFromProgram()` continue to return per-target diagnostics across multiple targets.
190
+
191
+ Use the `"throw"` mode when you want "schema or failure" ergonomics. Use the `"diagnostics"` mode when you want to surface as much feedback as possible in one pass, especially in editor, CI, or migration tooling. The older `generateSchemasDetailed()` and `generateSchemasFromProgramDetailed()` wrappers remain available only as deprecated compatibility shims.
186
192
 
187
193
  ```ts
188
- import { generateSchemas } from "@formspec/build";
194
+ import { generateSchemas, generateSchemasBatch } from "@formspec/build";
189
195
 
190
196
  try {
191
197
  const { jsonSchema, uiSchema } = generateSchemas({
192
198
  filePath: "./src/forms.ts",
193
199
  typeName: "Invoice",
200
+ errorReporting: "throw",
194
201
  });
195
202
  } catch (error) {
196
203
  const message = error instanceof Error ? error.message : String(error);
@@ -205,6 +212,33 @@ try {
205
212
 
206
213
  throw error;
207
214
  }
215
+
216
+ const detailed = generateSchemas({
217
+ filePath: "./src/forms.ts",
218
+ typeName: "Invoice",
219
+ errorReporting: "diagnostics",
220
+ });
221
+
222
+ if (!detailed.ok) {
223
+ for (const diagnostic of detailed.diagnostics) {
224
+ console.error(`${diagnostic.code}: ${diagnostic.message}`);
225
+ }
226
+ }
227
+
228
+ const batch = generateSchemasBatch({
229
+ targets: [
230
+ { filePath: "./src/forms.ts", typeName: "Invoice" },
231
+ { filePath: "./src/forms.ts", typeName: "PaymentTerms" },
232
+ ],
233
+ });
234
+
235
+ for (const result of batch) {
236
+ if (!result.ok) {
237
+ console.error(
238
+ `${result.typeName} failed: ${result.diagnostics.map((diagnostic) => diagnostic.code).join(", ")}`
239
+ );
240
+ }
241
+ }
208
242
  ```
209
243
 
210
244
  The most relevant codes for extension-backed static analysis failures are:
@@ -212,6 +246,9 @@ The most relevant codes for extension-backed static analysis failures are:
212
246
  - `UNSUPPORTED_CUSTOM_TYPE_OVERRIDE` for extension custom types that conflict with unsupported TypeScript global built-ins.
213
247
  - `SYNTHETIC_SETUP_FAILURE` for invalid or conflicting extension custom type registrations and other synthetic compiler setup failures.
214
248
  - `TYPE_MISMATCH` for normal tag-on-type incompatibilities in author source.
249
+ - `TYPE_NOT_FOUND` when a requested exported target cannot be resolved.
250
+ - `UNSUPPORTED_ROOT_TYPE` and `DUPLICATE_ROOT_PROPERTIES` when a requested type alias cannot be treated as a schema root.
251
+ - `PROGRAM_CONTEXT_FAILURE` when the package cannot create or reuse a TypeScript program for the requested file.
215
252
 
216
253
  As a rule of thumb, `UNSUPPORTED_CUSTOM_TYPE_OVERRIDE` and `SYNTHETIC_SETUP_FAILURE` indicate extension configuration problems, while `TYPE_MISMATCH` usually indicates an authoring error in the analyzed source.
217
254
 
@@ -228,6 +265,8 @@ Low-level canonical IR generators, analyzer primitives, and validation helpers a
228
265
  - `generateSchemas(options)`
229
266
  - `generateSchemasFromClass(options)`
230
267
  - `generateSchemasFromProgram(options)`
268
+ - `generateSchemasBatch(options)`
269
+ - `generateSchemasBatchFromProgram(options)`
231
270
  - `buildMixedAuthoringSchemas(options)`
232
271
  - `createExtensionRegistry(extensions)`
233
272
 
@@ -5,6 +5,7 @@
5
5
  * using the project's tsconfig.json for compiler options.
6
6
  */
7
7
  import * as ts from "typescript";
8
+ import type { ConstraintSemanticDiagnostic } from "@formspec/analysis/internal";
8
9
  import { type DiscriminatorResolutionOptions, type IRClassAnalysis } from "./class-analyzer.js";
9
10
  import type { ExtensionRegistry } from "../extensions/index.js";
10
11
  import type { MetadataPolicyInput } from "@formspec/core";
@@ -19,6 +20,13 @@ export interface ProgramContext {
19
20
  /** The source file being analyzed */
20
21
  sourceFile: ts.SourceFile;
21
22
  }
23
+ export type AnalyzeNamedTypeToIRDetailedResult = {
24
+ readonly ok: true;
25
+ readonly analysis: IRClassAnalysis;
26
+ } | {
27
+ readonly ok: false;
28
+ readonly diagnostics: readonly ConstraintSemanticDiagnostic[];
29
+ };
22
30
  /**
23
31
  * Resolves a source file and checker from an existing TypeScript program.
24
32
  *
@@ -74,6 +82,11 @@ export declare function findTypeAliasByName(sourceFile: ts.SourceFile, aliasName
74
82
  * @returns IR analysis result
75
83
  */
76
84
  export declare function analyzeNamedTypeToIR(filePath: string, typeName: string, extensionRegistry?: ExtensionRegistry, metadataPolicy?: MetadataPolicyInput, discriminatorOptions?: DiscriminatorResolutionOptions): IRClassAnalysis;
85
+ /**
86
+ * Analyzes a named type from an existing program context and returns either an
87
+ * `IRClassAnalysis` or structured diagnostics instead of throwing.
88
+ */
89
+ export declare function analyzeNamedTypeToIRFromProgramContextDetailed(ctx: ProgramContext, filePath: string, typeName: string, extensionRegistry?: ExtensionRegistry, metadataPolicy?: MetadataPolicyInput, discriminatorOptions?: DiscriminatorResolutionOptions): AnalyzeNamedTypeToIRDetailedResult;
77
90
  /**
78
91
  * Analyzes a named type from an existing program context and returns an `IRClassAnalysis`.
79
92
  */
@@ -1 +1 @@
1
- {"version":3,"file":"program.d.ts","sourceRoot":"","sources":["../../src/analyzer/program.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAIjC,OAAO,EASL,KAAK,8BAA8B,EACnC,KAAK,eAAe,EAErB,MA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