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

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) {
@@ -6728,6 +6789,8 @@ function annotationKey(annotation) {
6728
6789
 
6729
6790
  // src/index.ts
6730
6791
  function buildFormSchemas(form, options) {
6792
+ const logger = options?.logger ?? import_core3.noopLogger;
6793
+ logger.debug("buildFormSchemas: starting schema generation");
6731
6794
  return {
6732
6795
  jsonSchema: generateJsonSchema(form, options),
6733
6796
  uiSchema: generateUiSchema(form, options)
@@ -6740,12 +6803,15 @@ function writeSchemas(form, options) {
6740
6803
  indent = 2,
6741
6804
  vendorPrefix,
6742
6805
  enumSerialization,
6743
- metadata
6806
+ metadata,
6807
+ logger: rawLogger
6744
6808
  } = options;
6745
- const buildOptions = vendorPrefix === void 0 && enumSerialization === void 0 && metadata === void 0 ? void 0 : {
6809
+ const logger = (rawLogger ?? import_core3.noopLogger).child({ stage: "write" });
6810
+ const buildOptions = vendorPrefix === void 0 && enumSerialization === void 0 && metadata === void 0 ? { logger: rawLogger } : {
6746
6811
  ...vendorPrefix !== void 0 && { vendorPrefix },
6747
6812
  ...enumSerialization !== void 0 && { enumSerialization },
6748
- ...metadata !== void 0 && { metadata }
6813
+ ...metadata !== void 0 && { metadata },
6814
+ logger: rawLogger
6749
6815
  };
6750
6816
  const { jsonSchema, uiSchema: uiSchema2 } = buildFormSchemas(form, buildOptions);
6751
6817
  if (!fs.existsSync(outDir)) {
@@ -6753,7 +6819,9 @@ function writeSchemas(form, options) {
6753
6819
  }
6754
6820
  const jsonSchemaPath = path3.join(outDir, `${name}-schema.json`);
6755
6821
  const uiSchemaPath = path3.join(outDir, `${name}-uischema.json`);
6822
+ logger.debug("writing JSON Schema", { path: jsonSchemaPath });
6756
6823
  fs.writeFileSync(jsonSchemaPath, JSON.stringify(jsonSchema, null, indent));
6824
+ logger.debug("writing UI Schema", { path: uiSchemaPath });
6757
6825
  fs.writeFileSync(uiSchemaPath, JSON.stringify(uiSchema2, null, indent));
6758
6826
  return { jsonSchemaPath, uiSchemaPath };
6759
6827
  }