@formspec/build 0.1.0-alpha.52 → 0.1.0-alpha.54

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/index.cjs CHANGED
@@ -56,6 +56,10 @@ __export(index_exports, {
56
56
  writeSchemas: () => writeSchemas
57
57
  });
58
58
  module.exports = __toCommonJS(index_exports);
59
+ var import_core3 = require("@formspec/core");
60
+
61
+ // src/json-schema/generator.ts
62
+ var import_core = require("@formspec/core");
59
63
 
60
64
  // src/canonicalize/chain-dsl-canonicalizer.ts
61
65
  var import_internals = require("@formspec/core/internals");
@@ -1713,10 +1717,14 @@ function assignVendorPrefixedExtensionKeywords(schema, extensionSchema, vendorPr
1713
1717
 
1714
1718
  // src/json-schema/generator.ts
1715
1719
  function generateJsonSchema(form, options) {
1720
+ const logger = (options?.logger ?? import_core.noopLogger).child({ stage: "ir" });
1716
1721
  const metadata = options?.metadata;
1717
1722
  const vendorPrefix = options?.vendorPrefix;
1718
1723
  const enumSerialization = options?.enumSerialization;
1724
+ logger.debug("canonicalizing chain DSL to IR");
1719
1725
  const ir = canonicalizeChainDSL(form, metadata !== void 0 ? { metadata } : void 0);
1726
+ const schemaLogger = (options?.logger ?? import_core.noopLogger).child({ stage: "schema" });
1727
+ schemaLogger.debug("generating JSON Schema from IR");
1720
1728
  const internalOptions = vendorPrefix === void 0 && enumSerialization === void 0 ? void 0 : {
1721
1729
  ...vendorPrefix !== void 0 && { vendorPrefix },
1722
1730
  ...enumSerialization !== void 0 && { enumSerialization }
@@ -1724,6 +1732,9 @@ function generateJsonSchema(form, options) {
1724
1732
  return generateJsonSchemaFromIR(ir, internalOptions);
1725
1733
  }
1726
1734
 
1735
+ // src/ui-schema/generator.ts
1736
+ var import_core2 = require("@formspec/core");
1737
+
1727
1738
  // src/ui-schema/schema.ts
1728
1739
  var import_zod = require("zod");
1729
1740
  var jsonPointerSchema = import_zod.z.string();
@@ -1983,10 +1994,14 @@ function collectFieldNameMap(elements) {
1983
1994
 
1984
1995
  // src/ui-schema/generator.ts
1985
1996
  function generateUiSchema(form, options) {
1997
+ const logger = (options?.logger ?? import_core2.noopLogger).child({ stage: "ir" });
1998
+ logger.debug("canonicalizing chain DSL to IR for UI Schema generation");
1986
1999
  const ir = canonicalizeChainDSL(
1987
2000
  form,
1988
2001
  options?.metadata !== void 0 ? { metadata: options.metadata } : void 0
1989
2002
  );
2003
+ const schemaLogger = (options?.logger ?? import_core2.noopLogger).child({ stage: "schema" });
2004
+ schemaLogger.debug("generating UI Schema from IR");
1990
2005
  return generateUiSchemaFromIR(ir);
1991
2006
  }
1992
2007
 
@@ -2419,39 +2434,85 @@ function isNonReferenceIdentifier(node) {
2419
2434
  }
2420
2435
  return false;
2421
2436
  }
2422
- function statementReferencesImportedName(statement, importedNames) {
2437
+ function astReferencesImportedName(root, importedNames) {
2423
2438
  if (importedNames.size === 0) {
2424
2439
  return false;
2425
2440
  }
2426
- let referencesImportedName = false;
2441
+ let found = false;
2427
2442
  const visit = (node) => {
2428
- if (referencesImportedName) {
2429
- return;
2430
- }
2443
+ if (found) return;
2431
2444
  if (ts4.isIdentifier(node) && importedNames.has(node.text) && !isNonReferenceIdentifier(node)) {
2432
- referencesImportedName = true;
2445
+ found = true;
2433
2446
  return;
2434
2447
  }
2435
2448
  ts4.forEachChild(node, visit);
2436
2449
  };
2437
- visit(statement);
2438
- return referencesImportedName;
2450
+ visit(root);
2451
+ return found;
2452
+ }
2453
+ function getObjectMembers(statement) {
2454
+ if (ts4.isInterfaceDeclaration(statement)) {
2455
+ return statement.members;
2456
+ }
2457
+ if (ts4.isTypeLiteralNode(statement.type)) {
2458
+ return statement.type.members;
2459
+ }
2460
+ return void 0;
2461
+ }
2462
+ function rewriteImportedMemberTypes(statement, sourceFile, importedNames) {
2463
+ const members = getObjectMembers(statement);
2464
+ if (members === void 0) {
2465
+ return null;
2466
+ }
2467
+ const replacements = [];
2468
+ for (const member of members) {
2469
+ if (!ts4.isPropertySignature(member)) {
2470
+ if (astReferencesImportedName(member, importedNames)) {
2471
+ return null;
2472
+ }
2473
+ continue;
2474
+ }
2475
+ const typeAnnotation = member.type;
2476
+ if (typeAnnotation === void 0) continue;
2477
+ if (astReferencesImportedName(typeAnnotation, importedNames)) {
2478
+ replacements.push({
2479
+ start: typeAnnotation.getStart(sourceFile),
2480
+ end: typeAnnotation.getEnd()
2481
+ });
2482
+ }
2483
+ }
2484
+ if (replacements.length === 0) {
2485
+ return statement.getText(sourceFile);
2486
+ }
2487
+ const stmtStart = statement.getStart(sourceFile);
2488
+ let result = statement.getText(sourceFile);
2489
+ for (const { start, end } of [...replacements].reverse()) {
2490
+ result = result.slice(0, start - stmtStart) + "unknown" + result.slice(end - stmtStart);
2491
+ }
2492
+ return result;
2439
2493
  }
2440
2494
  function buildSupportingDeclarations(sourceFile, extensionTypeNames) {
2441
2495
  const importedNames = collectImportedNames(sourceFile);
2442
2496
  const importedNamesToSkip = new Set(
2443
2497
  [...importedNames].filter((name) => !extensionTypeNames.has(name))
2444
2498
  );
2445
- return sourceFile.statements.filter((statement) => {
2446
- if (ts4.isImportDeclaration(statement)) return false;
2447
- if (ts4.isImportEqualsDeclaration(statement)) return false;
2448
- if (ts4.isExportDeclaration(statement) && statement.moduleSpecifier !== void 0)
2449
- return false;
2450
- if (statementReferencesImportedName(statement, importedNamesToSkip)) {
2451
- return false;
2499
+ const result = [];
2500
+ for (const statement of sourceFile.statements) {
2501
+ if (ts4.isImportDeclaration(statement)) continue;
2502
+ if (ts4.isImportEqualsDeclaration(statement)) continue;
2503
+ if (ts4.isExportDeclaration(statement) && statement.moduleSpecifier !== void 0) continue;
2504
+ if (!astReferencesImportedName(statement, importedNamesToSkip)) {
2505
+ result.push(statement.getText(sourceFile));
2506
+ continue;
2452
2507
  }
2453
- return true;
2454
- }).map((statement) => statement.getText(sourceFile));
2508
+ if (ts4.isInterfaceDeclaration(statement) || ts4.isTypeAliasDeclaration(statement)) {
2509
+ const rewritten = rewriteImportedMemberTypes(statement, sourceFile, importedNamesToSkip);
2510
+ if (rewritten !== null) {
2511
+ result.push(rewritten);
2512
+ }
2513
+ }
2514
+ }
2515
+ return result;
2455
2516
  }
2456
2517
  function pushUniqueCompilerDiagnostics(target, additions) {
2457
2518
  for (const diagnostic of additions) {
@@ -4295,10 +4356,22 @@ function resolveTypeNode(type, checker, file, typeRegistry, visiting, sourceNode
4295
4356
  sourceNode
4296
4357
  );
4297
4358
  if (customTypeLookup !== null) {
4359
+ const typeId = customTypeIdFromLookup(customTypeLookup);
4360
+ let payload = null;
4361
+ if (customTypeLookup.registration.extractPayload !== void 0) {
4362
+ try {
4363
+ payload = customTypeLookup.registration.extractPayload(type, checker) ?? null;
4364
+ } catch (cause) {
4365
+ throw new Error(
4366
+ `extractPayload for custom type "${customTypeLookup.registration.typeName}" in extension "${customTypeLookup.extensionId}" threw`,
4367
+ { cause }
4368
+ );
4369
+ }
4370
+ }
4298
4371
  return {
4299
4372
  kind: "custom",
4300
- typeId: customTypeIdFromLookup(customTypeLookup),
4301
- payload: null
4373
+ typeId,
4374
+ payload
4302
4375
  };
4303
4376
  }
4304
4377
  const primitiveAlias = tryResolveNamedPrimitiveAlias(
@@ -6728,6 +6801,8 @@ function annotationKey(annotation) {
6728
6801
 
6729
6802
  // src/index.ts
6730
6803
  function buildFormSchemas(form, options) {
6804
+ const logger = options?.logger ?? import_core3.noopLogger;
6805
+ logger.debug("buildFormSchemas: starting schema generation");
6731
6806
  return {
6732
6807
  jsonSchema: generateJsonSchema(form, options),
6733
6808
  uiSchema: generateUiSchema(form, options)
@@ -6740,12 +6815,15 @@ function writeSchemas(form, options) {
6740
6815
  indent = 2,
6741
6816
  vendorPrefix,
6742
6817
  enumSerialization,
6743
- metadata
6818
+ metadata,
6819
+ logger: rawLogger
6744
6820
  } = options;
6745
- const buildOptions = vendorPrefix === void 0 && enumSerialization === void 0 && metadata === void 0 ? void 0 : {
6821
+ const logger = (rawLogger ?? import_core3.noopLogger).child({ stage: "write" });
6822
+ const buildOptions = vendorPrefix === void 0 && enumSerialization === void 0 && metadata === void 0 ? { logger: rawLogger } : {
6746
6823
  ...vendorPrefix !== void 0 && { vendorPrefix },
6747
6824
  ...enumSerialization !== void 0 && { enumSerialization },
6748
- ...metadata !== void 0 && { metadata }
6825
+ ...metadata !== void 0 && { metadata },
6826
+ logger: rawLogger
6749
6827
  };
6750
6828
  const { jsonSchema, uiSchema: uiSchema2 } = buildFormSchemas(form, buildOptions);
6751
6829
  if (!fs.existsSync(outDir)) {
@@ -6753,7 +6831,9 @@ function writeSchemas(form, options) {
6753
6831
  }
6754
6832
  const jsonSchemaPath = path3.join(outDir, `${name}-schema.json`);
6755
6833
  const uiSchemaPath = path3.join(outDir, `${name}-uischema.json`);
6834
+ logger.debug("writing JSON Schema", { path: jsonSchemaPath });
6756
6835
  fs.writeFileSync(jsonSchemaPath, JSON.stringify(jsonSchema, null, indent));
6836
+ logger.debug("writing UI Schema", { path: uiSchemaPath });
6757
6837
  fs.writeFileSync(uiSchemaPath, JSON.stringify(uiSchema2, null, indent));
6758
6838
  return { jsonSchemaPath, uiSchemaPath };
6759
6839
  }