@kubb/ast 5.0.0-beta.31 → 5.0.0-beta.32

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
1
+ import { t as __name } from "./chunk-C0LytTxp.js";
2
2
 
3
3
  //#region src/constants.d.ts
4
4
  /**
@@ -3873,5 +3873,5 @@ declare function containsCircularRef(node: SchemaNode | undefined, {
3873
3873
  excludeName?: string;
3874
3874
  }): boolean;
3875
3875
  //#endregion
3876
- export { type ArraySchemaNode, type ArrowFunctionNode, AsyncVisitor, type BaseNode, type BreakNode, BuildDedupePlanOptions, type CodeNode, CollectOptions, CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, DedupeCanonical, DedupePlan, DispatchRule, DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type HttpStatusCode, type ImportNode, InferSchema, InferSchemaNode, type InputMeta, type InputNode, type InputStreamNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type OperationNodeBase, OperationParamsResolver, type OperationProtocol, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, ParentOf, ParserOptions, type PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, type PropertyNode, RefMap, type RefSchemaNode, type ResponseNode, ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions, applyDedupe, buildDedupePlan, caseParams, childName, collect, collectImports, collectLazy, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createContent, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createRequestBody, createResponse, createSchema, createSource, createStreamInput, createText, createType, definePrinter, defineSchemaDialect, dispatch, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaEqual, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, mergeAdjacentObjectsLazy, narrowSchema, nodeKinds, resolveRefName, schemaSignature, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, update, walk };
3876
+ export { type ArraySchemaNode, type ArrowFunctionNode, type AsyncVisitor, type BaseNode, type BreakNode, type BuildDedupePlanOptions, type CodeNode, type CollectOptions, type CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, type DedupeCanonical, type DedupePlan, type DispatchRule, type DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type HttpStatusCode, type ImportNode, type InferSchema, type InferSchemaNode, type InputMeta, type InputNode, type InputStreamNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, type Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type OperationNodeBase, type OperationParamsResolver, type OperationProtocol, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefMap, type RefSchemaNode, type ResponseNode, type ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, type SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, type VisitorDepth, type WalkOptions, applyDedupe, buildDedupePlan, caseParams, childName, collect, collectImports, collectLazy, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createContent, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createRequestBody, createResponse, createSchema, createSource, createStreamInput, createText, createType, definePrinter, defineSchemaDialect, dispatch, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaEqual, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, mergeAdjacentObjectsLazy, narrowSchema, nodeKinds, resolveRefName, schemaSignature, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, update, walk };
3877
3877
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./chunk--u3MIqq1.js";
1
+ import "./chunk-C0LytTxp.js";
2
2
  import { createHash } from "node:crypto";
3
3
  import path from "node:path";
4
4
  //#region src/constants.ts
@@ -2074,66 +2074,245 @@ function refTargetName(node) {
2074
2074
  if (node.ref) return extractRefName(node.ref);
2075
2075
  return node.name ?? "";
2076
2076
  }
2077
- /**
2078
- * Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
2079
- * children's signatures. {@link signatureOf} hashes this string; children contribute their
2080
- * fixed-length signature rather than their own full descriptor, which keeps the result bounded.
2081
- */
2082
- function describeShape(node, signatures) {
2083
- const flags = flagsDescriptor(node);
2084
- switch (node.type) {
2085
- case "object": {
2086
- const props = (node.properties ?? []).map((prop) => `${prop.name}${prop.required ? "!" : "?"}${signatureOf(prop.schema, signatures)}`).join(",");
2087
- let additional = "";
2088
- if (typeof node.additionalProperties === "boolean") additional = `ab:${node.additionalProperties}`;
2089
- else if (node.additionalProperties) additional = `as:${signatureOf(node.additionalProperties, signatures)}`;
2090
- const pattern = node.patternProperties ? Object.keys(node.patternProperties).sort().map((key) => `${key}=${signatureOf(node.patternProperties[key], signatures)}`).join(",") : "";
2091
- return `object|${flags}|p[${props}]|${additional}|pp[${pattern}]|mn:${node.minProperties ?? ""}|mx:${node.maxProperties ?? ""}`;
2077
+ const arrayTupleFields = [
2078
+ {
2079
+ kind: "children",
2080
+ key: "items",
2081
+ prefix: "i"
2082
+ },
2083
+ {
2084
+ kind: "child",
2085
+ key: "rest",
2086
+ prefix: "r"
2087
+ },
2088
+ {
2089
+ kind: "scalar",
2090
+ key: "min",
2091
+ prefix: "mn"
2092
+ },
2093
+ {
2094
+ kind: "scalar",
2095
+ key: "max",
2096
+ prefix: "mx"
2097
+ },
2098
+ {
2099
+ kind: "bool",
2100
+ key: "unique",
2101
+ prefix: "u"
2102
+ }
2103
+ ];
2104
+ const numericFields = [
2105
+ {
2106
+ kind: "scalar",
2107
+ key: "min",
2108
+ prefix: "mn"
2109
+ },
2110
+ {
2111
+ kind: "scalar",
2112
+ key: "max",
2113
+ prefix: "mx"
2114
+ },
2115
+ {
2116
+ kind: "scalar",
2117
+ key: "exclusiveMinimum",
2118
+ prefix: "emn"
2119
+ },
2120
+ {
2121
+ kind: "scalar",
2122
+ key: "exclusiveMaximum",
2123
+ prefix: "emx"
2124
+ },
2125
+ {
2126
+ kind: "scalar",
2127
+ key: "multipleOf",
2128
+ prefix: "mo"
2129
+ }
2130
+ ];
2131
+ const rangeFields = [{
2132
+ kind: "scalar",
2133
+ key: "min",
2134
+ prefix: "mn"
2135
+ }, {
2136
+ kind: "scalar",
2137
+ key: "max",
2138
+ prefix: "mx"
2139
+ }];
2140
+ /**
2141
+ * Maps each schema node `type` to the ordered list of shape-contributing fields.
2142
+ * Node types absent from this map (scalar types like boolean, null, any, etc.) fall
2143
+ * back to `${type}|${flags}` with no additional fields.
2144
+ */
2145
+ const SHAPE_KEYS = {
2146
+ object: [
2147
+ { kind: "objectProps" },
2148
+ { kind: "additionalProps" },
2149
+ { kind: "patternProps" },
2150
+ {
2151
+ kind: "scalar",
2152
+ key: "minProperties",
2153
+ prefix: "mn"
2154
+ },
2155
+ {
2156
+ kind: "scalar",
2157
+ key: "maxProperties",
2158
+ prefix: "mx"
2159
+ }
2160
+ ],
2161
+ array: arrayTupleFields,
2162
+ tuple: arrayTupleFields,
2163
+ union: [
2164
+ {
2165
+ kind: "scalar",
2166
+ key: "strategy",
2167
+ prefix: "s"
2168
+ },
2169
+ {
2170
+ kind: "scalar",
2171
+ key: "discriminatorPropertyName",
2172
+ prefix: "d"
2173
+ },
2174
+ {
2175
+ kind: "children",
2176
+ key: "members",
2177
+ prefix: "m"
2178
+ }
2179
+ ],
2180
+ intersection: [{
2181
+ kind: "children",
2182
+ key: "members",
2183
+ prefix: "m"
2184
+ }],
2185
+ enum: [{ kind: "enumValues" }],
2186
+ ref: [{ kind: "refTarget" }],
2187
+ string: [
2188
+ {
2189
+ kind: "scalar",
2190
+ key: "min",
2191
+ prefix: "mn"
2192
+ },
2193
+ {
2194
+ kind: "scalar",
2195
+ key: "max",
2196
+ prefix: "mx"
2197
+ },
2198
+ {
2199
+ kind: "scalar",
2200
+ key: "pattern",
2201
+ prefix: "pt"
2202
+ }
2203
+ ],
2204
+ number: numericFields,
2205
+ integer: numericFields,
2206
+ bigint: numericFields,
2207
+ url: [
2208
+ {
2209
+ kind: "scalar",
2210
+ key: "path",
2211
+ prefix: "path"
2212
+ },
2213
+ {
2214
+ kind: "scalar",
2215
+ key: "min",
2216
+ prefix: "mn"
2217
+ },
2218
+ {
2219
+ kind: "scalar",
2220
+ key: "max",
2221
+ prefix: "mx"
2092
2222
  }
2093
- case "array":
2094
- case "tuple": {
2095
- const items = (node.items ?? []).map((item) => signatureOf(item, signatures)).join(",");
2096
- const rest = node.rest ? signatureOf(node.rest, signatures) : "";
2097
- return `${node.type}|${flags}|i[${items}]|r:${rest}|mn:${node.min ?? ""}|mx:${node.max ?? ""}|u:${node.unique ? 1 : 0}`;
2223
+ ],
2224
+ uuid: rangeFields,
2225
+ email: rangeFields,
2226
+ datetime: [{
2227
+ kind: "bool",
2228
+ key: "offset",
2229
+ prefix: "o"
2230
+ }, {
2231
+ kind: "bool",
2232
+ key: "local",
2233
+ prefix: "l"
2234
+ }],
2235
+ date: [{
2236
+ kind: "scalar",
2237
+ key: "representation",
2238
+ prefix: "rep"
2239
+ }],
2240
+ time: [{
2241
+ kind: "scalar",
2242
+ key: "representation",
2243
+ prefix: "rep"
2244
+ }]
2245
+ };
2246
+ function serializeShapeField(field, node, record) {
2247
+ switch (field.kind) {
2248
+ case "scalar": return `${field.prefix}:${record[field.key] ?? ""}`;
2249
+ case "bool": return `${field.prefix}:${record[field.key] ? 1 : 0}`;
2250
+ case "child": {
2251
+ const child = record[field.key];
2252
+ return `${field.prefix}:${child ? signatureOf(child) : ""}`;
2098
2253
  }
2099
- case "union": {
2100
- const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(",");
2101
- return `union|${flags}|s:${node.strategy ?? ""}|d:${node.discriminatorPropertyName ?? ""}|m[${members}]`;
2254
+ case "children": {
2255
+ const children = record[field.key] ?? [];
2256
+ return `${field.prefix}[${children.map((c) => signatureOf(c)).join(",")}]`;
2102
2257
  }
2103
- case "intersection": return `intersection|${flags}|m[${(node.members ?? []).map((member) => signatureOf(member, signatures)).join(",")}]`;
2104
- case "enum": {
2258
+ case "objectProps": return `p[${(node.properties ?? []).map((prop) => `${prop.name}${prop.required ? "!" : "?"}${signatureOf(prop.schema)}`).join(",")}]`;
2259
+ case "additionalProps": {
2260
+ const obj = node;
2261
+ if (typeof obj.additionalProperties === "boolean") return `ab:${obj.additionalProperties}`;
2262
+ if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`;
2263
+ return "";
2264
+ }
2265
+ case "patternProps": {
2266
+ const obj = node;
2267
+ return `pp[${obj.patternProperties ? Object.keys(obj.patternProperties).sort().map((key) => `${key}=${signatureOf(obj.patternProperties[key])}`).join(",") : ""}]`;
2268
+ }
2269
+ case "enumValues": {
2270
+ const en = node;
2105
2271
  let values = "";
2106
- if (node.namedEnumValues?.length) values = node.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(",");
2107
- else if (node.enumValues?.length) values = node.enumValues.map((value) => `${value === null ? "null" : typeof value}:${String(value)}`).join(",");
2108
- return `enum|${flags}|v[${values}]`;
2272
+ if (en.namedEnumValues?.length) values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(",");
2273
+ else if (en.enumValues?.length) values = en.enumValues.map((value) => `${value === null ? "null" : typeof value}:${String(value)}`).join(",");
2274
+ return `v[${values}]`;
2109
2275
  }
2110
- case "ref": return `ref|${flags}|->${refTargetName(node)}`;
2111
- case "string": return `string|${flags}|mn:${node.min ?? ""}|mx:${node.max ?? ""}|pt:${node.pattern ?? ""}`;
2112
- case "number":
2113
- case "integer":
2114
- case "bigint": return `${node.type}|${flags}|mn:${node.min ?? ""}|mx:${node.max ?? ""}|emn:${node.exclusiveMinimum ?? ""}|emx:${node.exclusiveMaximum ?? ""}|mo:${node.multipleOf ?? ""}`;
2115
- case "url": return `url|${flags}|path:${node.path ?? ""}|mn:${node.min ?? ""}|mx:${node.max ?? ""}`;
2116
- case "uuid":
2117
- case "email": return `${node.type}|${flags}|mn:${node.min ?? ""}|mx:${node.max ?? ""}`;
2118
- case "datetime": return `datetime|${flags}|o:${node.offset ? 1 : 0}|l:${node.local ? 1 : 0}`;
2119
- case "date":
2120
- case "time": return `${node.type}|${flags}|rep:${node.representation}`;
2121
- default: return `${node.type}|${flags}`;
2276
+ case "refTarget": return `->${refTargetName(node)}`;
2122
2277
  }
2123
2278
  }
2124
2279
  /**
2280
+ * Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
2281
+ * children's signatures. {@link signatureOf} hashes this string; children contribute their
2282
+ * fixed-length signature rather than their own full descriptor, which keeps the result bounded.
2283
+ */
2284
+ function describeShape(node) {
2285
+ const flags = flagsDescriptor(node);
2286
+ const fields = SHAPE_KEYS[node.type];
2287
+ if (!fields) return `${node.type}|${flags}`;
2288
+ const record = node;
2289
+ const parts = [`${node.type}|${flags}`];
2290
+ for (const field of fields) parts.push(serializeShapeField(field, node, record));
2291
+ return parts.join("|");
2292
+ }
2293
+ /**
2294
+ * Persistent hash-consing cache: `SchemaNode` → signature digest, keyed by node identity.
2295
+ *
2296
+ * A `WeakMap` so entries are released once the node is garbage-collected, and so a node hashed
2297
+ * during dedupe planning is not re-hashed when the same tree is rewritten during streaming
2298
+ * (where `schemaSignature` and `applyDedupe` would otherwise each walk it from scratch). Reuse
2299
+ * across calls is sound because a signature depends only on a node's content, and schema nodes
2300
+ * are immutable once created — transforms allocate new objects rather than mutating in place.
2301
+ */
2302
+ const signatureCache = /* @__PURE__ */ new WeakMap();
2303
+ /**
2125
2304
  * Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
2126
2305
  * children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
2127
2306
  * full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
2128
2307
  * digest is identical across calls because it depends only on content — never on traversal
2129
2308
  * order. This keeps the keys built during planning consistent with the ones recomputed later
2130
- * during streaming. `signatures` memoizes node → digest within a single computation.
2309
+ * during streaming. {@link signatureCache} memoizes node → digest across every computation.
2131
2310
  */
2132
- function signatureOf(node, signatures) {
2133
- const cached = signatures.get(node);
2311
+ function signatureOf(node) {
2312
+ const cached = signatureCache.get(node);
2134
2313
  if (cached !== void 0) return cached;
2135
- const signature = createHash("sha256").update(describeShape(node, signatures)).digest("hex");
2136
- signatures.set(node, signature);
2314
+ const signature = createHash("sha256").update(describeShape(node)).digest("hex");
2315
+ signatureCache.set(node, signature);
2137
2316
  return signature;
2138
2317
  }
2139
2318
  /**
@@ -2152,7 +2331,7 @@ function signatureOf(node, signatures) {
2152
2331
  * ```
2153
2332
  */
2154
2333
  function schemaSignature(node) {
2155
- return signatureOf(node, /* @__PURE__ */ new Map());
2334
+ return signatureOf(node);
2156
2335
  }
2157
2336
  /**
2158
2337
  * Returns `true` when two schema nodes are structurally identical under shape-only equality.
@@ -2188,10 +2367,9 @@ function createRefNode(node, canonical) {
2188
2367
  }
2189
2368
  function applyDedupe(node, canonicalBySignature, skipRootMatch = false) {
2190
2369
  if (canonicalBySignature.size === 0) return node;
2191
- const signatures = /* @__PURE__ */ new Map();
2192
2370
  const root = node;
2193
2371
  return transform(node, { schema(schemaNode) {
2194
- const signature = signatureOf(schemaNode, signatures);
2372
+ const signature = signatureOf(schemaNode);
2195
2373
  if (skipRootMatch && schemaNode === root) return void 0;
2196
2374
  const canonical = canonicalBySignature.get(signature);
2197
2375
  if (!canonical) return void 0;
@@ -2229,11 +2407,10 @@ function cleanDefinition(node, name) {
2229
2407
  */
2230
2408
  function buildDedupePlan(roots, options) {
2231
2409
  const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options;
2232
- const signatures = /* @__PURE__ */ new Map();
2233
2410
  const topLevelNodes = /* @__PURE__ */ new Set();
2234
2411
  const groups = /* @__PURE__ */ new Map();
2235
2412
  function record(schemaNode) {
2236
- const signature = signatureOf(schemaNode, signatures);
2413
+ const signature = signatureOf(schemaNode);
2237
2414
  if (!isCandidate(schemaNode)) return;
2238
2415
  const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name;
2239
2416
  const group = groups.get(signature);