@kubb/ast 5.9.3 → 5.9.5

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.js CHANGED
@@ -2,10 +2,6 @@ import { t as __exportAll } from "./rolldown-runtime-CNktS9qV.js";
2
2
  import path from "node:path";
3
3
  import { hash } from "node:crypto";
4
4
  //#region src/constants.ts
5
- const visitorDepths = {
6
- shallow: "shallow",
7
- deep: "deep"
8
- };
9
5
  /**
10
6
  * Schema type discriminators used by all AST schema nodes.
11
7
  *
@@ -173,7 +169,6 @@ const VISITOR_KEYS = {
173
169
  /** Visitor callback names in traversal order, the keys of {@link Visitor}. */
174
170
  const visitorKeys = [
175
171
  "input",
176
- "output",
177
172
  "operation",
178
173
  "schema",
179
174
  "property",
@@ -185,14 +180,12 @@ const visitorKeys = [
185
180
  */
186
181
  const VISITOR_KEY_BY_KIND = {
187
182
  Input: "input",
188
- Output: "output",
189
183
  Operation: "operation",
190
184
  Schema: "schema",
191
185
  Property: "property",
192
186
  Parameter: "parameter",
193
187
  Response: "response"
194
188
  };
195
- const visitorKeysByKind = VISITOR_KEYS;
196
189
  /**
197
190
  * Returns `true` when `value` is an AST node (an object carrying a `kind`).
198
191
  */
@@ -206,14 +199,6 @@ function isNodeRecord(value) {
206
199
  return typeof value === "object" && value !== null && !Array.isArray(value);
207
200
  }
208
201
  /**
209
- * Returns the child fields of `node` to walk, from {@link VISITOR_KEYS}, or `undefined` when there
210
- * is nothing to walk. `Schema` children are only walked when `recurse` is `true`.
211
- */
212
- function childKeys(node, recurse) {
213
- if (node.kind === "Schema" && !recurse) return void 0;
214
- return visitorKeysByKind[node.kind];
215
- }
216
- /**
217
202
  * Returns the immediate traversable children of `node`.
218
203
  *
219
204
  * @example
@@ -223,7 +208,7 @@ function childKeys(node, recurse) {
223
208
  * ```
224
209
  */
225
210
  function* getChildren(node, recurse) {
226
- const keys = childKeys(node, recurse);
211
+ const keys = node.kind === "Schema" && !recurse ? void 0 : VISITOR_KEYS[node.kind];
227
212
  if (!keys) return;
228
213
  const record = node;
229
214
  for (const key of keys) {
@@ -282,7 +267,7 @@ function applyVisitor(node, visitor, parent) {
282
267
  */
283
268
  function transform(node, options) {
284
269
  const { depth, parent, ...visitor } = options;
285
- return transformNode(node, visitor, (depth ?? visitorDepths.deep) === visitorDepths.deep, parent);
270
+ return transformNode(node, visitor, (depth ?? "deep") === "deep", parent);
286
271
  }
287
272
  /**
288
273
  * Visits a single node, then immutably rebuilds its children. Returns the original
@@ -297,7 +282,7 @@ function transformNode(node, visitor, recurse, parent) {
297
282
  * (e.g. `additionalProperties: true`) intact.
298
283
  */
299
284
  function transformChildren(node, visitor, recurse) {
300
- const keys = childKeys(node, recurse);
285
+ const keys = node.kind === "Schema" && !recurse ? void 0 : VISITOR_KEYS[node.kind];
301
286
  if (!keys) return node;
302
287
  const record = node;
303
288
  let updates;
@@ -355,7 +340,7 @@ function transformChildren(node, visitor, recurse) {
355
340
  */
356
341
  function* collect(node, options) {
357
342
  const { depth, parent, ...visitor } = options;
358
- yield* collectNode(node, visitor, (depth ?? visitorDepths.deep) === visitorDepths.deep, parent);
343
+ yield* collectNode(node, visitor, (depth ?? "deep") === "deep", parent);
359
344
  }
360
345
  function* collectNode(node, visitor, recurse, parent) {
361
346
  const v = applyVisitor(node, visitor, parent);
@@ -485,18 +470,6 @@ function optionality(schema, required) {
485
470
  nullish: !required && nullable ? true : void 0
486
471
  };
487
472
  }
488
- /**
489
- * Defaults `required` to `false` and derives the schema's `optional`/`nullish` flags from it.
490
- * Shared build step of `createProperty` and `createParameter`.
491
- */
492
- function withRequired(props) {
493
- const required = props.required ?? false;
494
- return {
495
- ...props,
496
- required,
497
- schema: optionality(props.schema, required)
498
- };
499
- }
500
473
  //#endregion
501
474
  //#region src/createPrinter.ts
502
475
  /**
@@ -604,6 +577,10 @@ function extractStringsFromNodes(nodes) {
604
577
  }
605
578
  //#endregion
606
579
  //#region src/utils/refs.ts
580
+ /** Returns the last segment of a `$ref` pointer, for example `'Pet'` for `#/components/schemas/Pet`. */
581
+ function refName(ref) {
582
+ return ref.split("/").at(-1) ?? ref;
583
+ }
607
584
  /**
608
585
  * Resolves the emitted name of the schema a ref node points at. Prefers `targetName` (set when
609
586
  * the referenced schema was renamed, e.g. to break a collision), then the last segment of `ref`,
@@ -620,7 +597,7 @@ function extractStringsFromNodes(nodes) {
620
597
  function resolveRefName(node) {
621
598
  if (!node || node.type !== "ref") return null;
622
599
  if (node.targetName) return node.targetName;
623
- if (node.ref) return node.ref.split("/").at(-1) ?? node.ref;
600
+ if (node.ref) return refName(node.ref);
624
601
  return node.name ?? node.schema?.name ?? null;
625
602
  }
626
603
  /**
@@ -955,6 +932,17 @@ function inferDiscriminatorPropertyName(members) {
955
932
  }
956
933
  }
957
934
  //#endregion
935
+ //#region src/nodes/base.ts
936
+ /** Builds a `createX` factory for a node whose only computed field is `kind`. */
937
+ function nodeFactory(kind) {
938
+ return (input) => {
939
+ return {
940
+ ...input,
941
+ kind
942
+ };
943
+ };
944
+ }
945
+ //#endregion
958
946
  //#region src/nodes/code.ts
959
947
  /**
960
948
  * Creates a `ConstNode` representing a TypeScript `const` declaration.
@@ -965,12 +953,7 @@ function inferDiscriminatorPropertyName(members) {
965
953
  * // export const pets: Pet[] = ... as const
966
954
  * ```
967
955
  */
968
- function createConst(input) {
969
- return {
970
- ...input,
971
- kind: "Const"
972
- };
973
- }
956
+ const createConst = nodeFactory("Const");
974
957
  /**
975
958
  * Creates a `TypeNode` representing a TypeScript `type` alias declaration.
976
959
  *
@@ -980,12 +963,7 @@ function createConst(input) {
980
963
  * // export type Pet = ...
981
964
  * ```
982
965
  */
983
- function createType(input) {
984
- return {
985
- ...input,
986
- kind: "Type"
987
- };
988
- }
966
+ const createType = nodeFactory("Type");
989
967
  /**
990
968
  * Creates a `FunctionNode` representing a TypeScript `function` declaration.
991
969
  *
@@ -995,12 +973,7 @@ function createType(input) {
995
973
  * // export async function fetchPet(): Promise<Pet> { ... }
996
974
  * ```
997
975
  */
998
- function createFunction(input) {
999
- return {
1000
- ...input,
1001
- kind: "Function"
1002
- };
1003
- }
976
+ const createFunction = nodeFactory("Function");
1004
977
  /**
1005
978
  * Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
1006
979
  *
@@ -1010,12 +983,7 @@ function createFunction(input) {
1010
983
  * // export const double = (n: number) => ...
1011
984
  * ```
1012
985
  */
1013
- function createArrowFunction(input) {
1014
- return {
1015
- ...input,
1016
- kind: "ArrowFunction"
1017
- };
1018
- }
986
+ const createArrowFunction = nodeFactory("ArrowFunction");
1019
987
  /**
1020
988
  * Creates a {@link TextNode} representing a raw string fragment in the source output.
1021
989
  *
@@ -1063,12 +1031,7 @@ function createJsx(value) {
1063
1031
  /**
1064
1032
  * Creates a `ContentNode` for a single request-body or response content type.
1065
1033
  */
1066
- function createContent(input) {
1067
- return {
1068
- ...input,
1069
- kind: "Content"
1070
- };
1071
- }
1034
+ const createContent = nodeFactory("Content");
1072
1035
  //#endregion
1073
1036
  //#region src/utils/combineFileMembers.ts
1074
1037
  const IDENTIFIER_RUN = /[\w$]+/g;
@@ -1217,12 +1180,7 @@ function combineImports(imports, exports, source) {
1217
1180
  * // import { useState } from 'react'
1218
1181
  * ```
1219
1182
  */
1220
- function createImport(input) {
1221
- return {
1222
- ...input,
1223
- kind: "Import"
1224
- };
1225
- }
1183
+ const createImport = nodeFactory("Import");
1226
1184
  /**
1227
1185
  * Creates an `ExportNode` representing a language-agnostic export/public API declaration.
1228
1186
  *
@@ -1232,12 +1190,7 @@ function createImport(input) {
1232
1190
  * // export { Pet } from './Pet'
1233
1191
  * ```
1234
1192
  */
1235
- function createExport(input) {
1236
- return {
1237
- ...input,
1238
- kind: "Export"
1239
- };
1240
- }
1193
+ const createExport = nodeFactory("Export");
1241
1194
  /**
1242
1195
  * Creates a `SourceNode` representing a fragment of source code within a file.
1243
1196
  *
@@ -1246,12 +1199,7 @@ function createExport(input) {
1246
1199
  * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
1247
1200
  * ```
1248
1201
  */
1249
- function createSource(input) {
1250
- return {
1251
- ...input,
1252
- kind: "Source"
1253
- };
1254
- }
1202
+ const createSource = nodeFactory("Source");
1255
1203
  /**
1256
1204
  * Creates a fully resolved `FileNode` from a file input descriptor.
1257
1205
  *
@@ -1357,12 +1305,7 @@ function createInput(overrides = {}) {
1357
1305
  /**
1358
1306
  * Creates a `RequestBodyNode`.
1359
1307
  */
1360
- function createRequestBody(input) {
1361
- return {
1362
- ...input,
1363
- kind: "RequestBody"
1364
- };
1365
- }
1308
+ const createRequestBody = nodeFactory("RequestBody");
1366
1309
  //#endregion
1367
1310
  //#region src/nodes/operation.ts
1368
1311
  function createOperation(props) {
@@ -1379,28 +1322,10 @@ function createOperation(props) {
1379
1322
  };
1380
1323
  }
1381
1324
  //#endregion
1382
- //#region src/nodes/output.ts
1383
- /**
1384
- * Creates an `OutputNode` with a stable default for `files`.
1385
- *
1386
- * @example
1387
- * ```ts
1388
- * const output = createOutput()
1389
- * // { kind: 'Output', files: [] }
1390
- * ```
1391
- */
1392
- function createOutput(overrides = {}) {
1393
- return {
1394
- files: [],
1395
- ...overrides,
1396
- kind: "Output"
1397
- };
1398
- }
1399
- //#endregion
1400
1325
  //#region src/nodes/parameter.ts
1401
1326
  /**
1402
1327
  * Creates a `ParameterNode`. `required` defaults to `false`, and the schema's
1403
- * `optional`/`nullish` flags are derived from it through {@link withRequired}.
1328
+ * `optional`/`nullish` flags are derived from it.
1404
1329
  *
1405
1330
  * @example
1406
1331
  * ```ts
@@ -1413,8 +1338,11 @@ function createOutput(overrides = {}) {
1413
1338
  * ```
1414
1339
  */
1415
1340
  function createParameter(input) {
1341
+ const required = input.required ?? false;
1416
1342
  return {
1417
- ...withRequired(input),
1343
+ ...input,
1344
+ required,
1345
+ schema: optionality(input.schema, required),
1418
1346
  kind: "Parameter"
1419
1347
  };
1420
1348
  }
@@ -1422,7 +1350,7 @@ function createParameter(input) {
1422
1350
  //#region src/nodes/property.ts
1423
1351
  /**
1424
1352
  * Creates a `PropertyNode`. `required` defaults to `false`, and the schema's
1425
- * `optional`/`nullish` flags are derived from it through {@link withRequired}.
1353
+ * `optional`/`nullish` flags are derived from it.
1426
1354
  *
1427
1355
  * @example
1428
1356
  * ```ts
@@ -1435,8 +1363,11 @@ function createParameter(input) {
1435
1363
  * ```
1436
1364
  */
1437
1365
  function createProperty(input) {
1366
+ const required = input.required ?? false;
1438
1367
  return {
1439
- ...withRequired(input),
1368
+ ...input,
1369
+ required,
1370
+ schema: optionality(input.schema, required),
1440
1371
  kind: "Property"
1441
1372
  };
1442
1373
  }
@@ -1508,8 +1439,8 @@ function createSchema(props) {
1508
1439
  };
1509
1440
  }
1510
1441
  //#endregion
1511
- //#region src/factory.ts
1512
- var factory_exports = /* @__PURE__ */ __exportAll({
1442
+ //#region src/nodes/index.ts
1443
+ var nodes_exports = /* @__PURE__ */ __exportAll({
1513
1444
  createArrowFunction: () => createArrowFunction,
1514
1445
  createBreak: () => createBreak,
1515
1446
  createConst: () => createConst,
@@ -1521,7 +1452,6 @@ var factory_exports = /* @__PURE__ */ __exportAll({
1521
1452
  createInput: () => createInput,
1522
1453
  createJsx: () => createJsx,
1523
1454
  createOperation: () => createOperation,
1524
- createOutput: () => createOutput,
1525
1455
  createParameter: () => createParameter,
1526
1456
  createProperty: () => createProperty,
1527
1457
  createRequestBody: () => createRequestBody,
@@ -1529,32 +1459,8 @@ var factory_exports = /* @__PURE__ */ __exportAll({
1529
1459
  createSchema: () => createSchema,
1530
1460
  createSource: () => createSource,
1531
1461
  createText: () => createText,
1532
- createType: () => createType,
1533
- update: () => update
1462
+ createType: () => createType
1534
1463
  });
1535
- /**
1536
- * Identity-preserving node update: returns `node` unchanged when every field in
1537
- * `changes` already equals (by reference) the current value, otherwise a new node
1538
- * with the changes applied.
1539
- *
1540
- * Mirrors the TypeScript compiler's `factory.updateX` contract. Pair it with the
1541
- * structural sharing in {@link transform} so a no-op rewrite does not allocate and
1542
- * downstream passes can detect "nothing changed" by identity. Comparison is shallow,
1543
- * so a structurally equal but newly allocated array or object counts as a change.
1544
- *
1545
- * @example
1546
- * ```ts
1547
- * update(node, { name: node.name }) // -> same `node` reference
1548
- * update(node, { name: 'renamed' }) // -> new node, `name` replaced
1549
- * ```
1550
- */
1551
- function update(node, changes) {
1552
- for (const key in changes) if (changes[key] !== node[key]) return {
1553
- ...node,
1554
- ...changes
1555
- };
1556
- return node;
1557
- }
1558
1464
  //#endregion
1559
1465
  //#region src/index.ts
1560
1466
  var src_exports = /* @__PURE__ */ __exportAll({
@@ -1569,7 +1475,7 @@ var src_exports = /* @__PURE__ */ __exportAll({
1569
1475
  createPrinter: () => createPrinter,
1570
1476
  defineMacro: () => defineMacro,
1571
1477
  extractStringsFromNodes: () => extractStringsFromNodes,
1572
- factory: () => factory_exports,
1478
+ factory: () => nodes_exports,
1573
1479
  findCircularSchemas: () => findCircularSchemas,
1574
1480
  findCircularSchemasFromGraph: () => findCircularSchemasFromGraph,
1575
1481
  getSchemaLiteralValues: () => getSchemaLiteralValues,
@@ -1579,12 +1485,13 @@ var src_exports = /* @__PURE__ */ __exportAll({
1579
1485
  mergeRefWithSchema: () => mergeRefWithSchema,
1580
1486
  narrowSchema: () => narrowSchema,
1581
1487
  optionality: () => optionality,
1488
+ refName: () => refName,
1582
1489
  resolveRefName: () => resolveRefName,
1583
1490
  resolveSchemaProperties: () => resolveSchemaProperties,
1584
1491
  schemaTypes: () => schemaTypes,
1585
1492
  transform: () => transform
1586
1493
  });
1587
1494
  //#endregion
1588
- export { applyMacros, src_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, factory_exports as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
1495
+ export { applyMacros, src_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, nodes_exports as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
1589
1496
 
1590
1497
  //# sourceMappingURL=index.js.map