@kubb/ast 5.0.0-beta.30 → 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.cjs +452 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +220 -101
- package/dist/index.js +450 -56
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dedupe.ts +200 -0
- package/src/guards.ts +1 -46
- package/src/index.ts +2 -0
- package/src/refs.ts +0 -49
- package/src/signature.ts +232 -0
- package/src/types.ts +1 -0
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -224,60 +224,6 @@ const mediaTypes = {
|
|
|
224
224
|
videoMp4: "video/mp4"
|
|
225
225
|
};
|
|
226
226
|
//#endregion
|
|
227
|
-
//#region src/dialect.ts
|
|
228
|
-
/**
|
|
229
|
-
* Identity helper that types a {@link SchemaDialect} for an adapter. Like
|
|
230
|
-
* `defineParser`, it adds no runtime behavior — it pins the dialect's type for
|
|
231
|
-
* inference and gives adapter authors a discoverable anchor.
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* ```ts
|
|
235
|
-
* export const oasDialect = defineSchemaDialect({
|
|
236
|
-
* name: 'oas',
|
|
237
|
-
* isNullable,
|
|
238
|
-
* isReference,
|
|
239
|
-
* isDiscriminator,
|
|
240
|
-
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
241
|
-
* resolveRef,
|
|
242
|
-
* })
|
|
243
|
-
* ```
|
|
244
|
-
*/
|
|
245
|
-
function defineSchemaDialect(dialect) {
|
|
246
|
-
return dialect;
|
|
247
|
-
}
|
|
248
|
-
//#endregion
|
|
249
|
-
//#region src/dispatch.ts
|
|
250
|
-
/**
|
|
251
|
-
* Walks an ordered list of {@link DispatchRule}s and returns the first node produced.
|
|
252
|
-
*
|
|
253
|
-
* This is the shared backbone for spec adapters (OpenAPI today, AsyncAPI and others later).
|
|
254
|
-
* The contract an adapter follows is intentionally minimal:
|
|
255
|
-
*
|
|
256
|
-
* context → [rule.match → rule.convert] → node
|
|
257
|
-
*
|
|
258
|
-
* An adapter derives a context from a source spec node, then declares an ordered table of
|
|
259
|
-
* rules mapping spec shapes onto Kubb AST nodes. To add support for a new spec, write a new
|
|
260
|
-
* context type and a new rules table — the traversal here is reused unchanged.
|
|
261
|
-
*
|
|
262
|
-
* Order is significant: earlier rules win, so list higher-precedence or more specific shapes
|
|
263
|
-
* first (e.g. composition keywords before plain `type`). A rule whose `match` returns `true`
|
|
264
|
-
* may still `convert` to `null` to defer to later rules. When no rule produces a node this
|
|
265
|
-
* returns `null`, leaving the caller to apply its own fallback.
|
|
266
|
-
*
|
|
267
|
-
* @example
|
|
268
|
-
* ```ts
|
|
269
|
-
* const node = dispatch(schemaRules, schemaContext) ?? createSchema({ type: fallbackType })
|
|
270
|
-
* ```
|
|
271
|
-
*/
|
|
272
|
-
function dispatch(rules, context) {
|
|
273
|
-
for (const rule of rules) {
|
|
274
|
-
if (!rule.match(context)) continue;
|
|
275
|
-
const node = rule.convert(context);
|
|
276
|
-
if (node !== null && node !== void 0) return node;
|
|
277
|
-
}
|
|
278
|
-
return null;
|
|
279
|
-
}
|
|
280
|
-
//#endregion
|
|
281
227
|
//#region ../../internals/utils/src/casing.ts
|
|
282
228
|
/**
|
|
283
229
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -2138,6 +2084,454 @@ function createJsx(value) {
|
|
|
2138
2084
|
};
|
|
2139
2085
|
}
|
|
2140
2086
|
//#endregion
|
|
2087
|
+
//#region src/signature.ts
|
|
2088
|
+
/**
|
|
2089
|
+
* The shape-affecting flags shared by every node kind: base primitive, format, and `nullable`.
|
|
2090
|
+
* Documentation and usage-slot flags (`optional`/`nullish`/`readOnly`/`writeOnly`) are
|
|
2091
|
+
* intentionally excluded — they describe the property slot, not the type.
|
|
2092
|
+
*/
|
|
2093
|
+
function flagsDescriptor(node) {
|
|
2094
|
+
return `${node.primitive ?? ""};${node.format ?? ""};${node.nullable ? 1 : 0}`;
|
|
2095
|
+
}
|
|
2096
|
+
function refTargetName(node) {
|
|
2097
|
+
if (node.ref) return extractRefName(node.ref);
|
|
2098
|
+
return node.name ?? "";
|
|
2099
|
+
}
|
|
2100
|
+
const arrayTupleFields = [
|
|
2101
|
+
{
|
|
2102
|
+
kind: "children",
|
|
2103
|
+
key: "items",
|
|
2104
|
+
prefix: "i"
|
|
2105
|
+
},
|
|
2106
|
+
{
|
|
2107
|
+
kind: "child",
|
|
2108
|
+
key: "rest",
|
|
2109
|
+
prefix: "r"
|
|
2110
|
+
},
|
|
2111
|
+
{
|
|
2112
|
+
kind: "scalar",
|
|
2113
|
+
key: "min",
|
|
2114
|
+
prefix: "mn"
|
|
2115
|
+
},
|
|
2116
|
+
{
|
|
2117
|
+
kind: "scalar",
|
|
2118
|
+
key: "max",
|
|
2119
|
+
prefix: "mx"
|
|
2120
|
+
},
|
|
2121
|
+
{
|
|
2122
|
+
kind: "bool",
|
|
2123
|
+
key: "unique",
|
|
2124
|
+
prefix: "u"
|
|
2125
|
+
}
|
|
2126
|
+
];
|
|
2127
|
+
const numericFields = [
|
|
2128
|
+
{
|
|
2129
|
+
kind: "scalar",
|
|
2130
|
+
key: "min",
|
|
2131
|
+
prefix: "mn"
|
|
2132
|
+
},
|
|
2133
|
+
{
|
|
2134
|
+
kind: "scalar",
|
|
2135
|
+
key: "max",
|
|
2136
|
+
prefix: "mx"
|
|
2137
|
+
},
|
|
2138
|
+
{
|
|
2139
|
+
kind: "scalar",
|
|
2140
|
+
key: "exclusiveMinimum",
|
|
2141
|
+
prefix: "emn"
|
|
2142
|
+
},
|
|
2143
|
+
{
|
|
2144
|
+
kind: "scalar",
|
|
2145
|
+
key: "exclusiveMaximum",
|
|
2146
|
+
prefix: "emx"
|
|
2147
|
+
},
|
|
2148
|
+
{
|
|
2149
|
+
kind: "scalar",
|
|
2150
|
+
key: "multipleOf",
|
|
2151
|
+
prefix: "mo"
|
|
2152
|
+
}
|
|
2153
|
+
];
|
|
2154
|
+
const rangeFields = [{
|
|
2155
|
+
kind: "scalar",
|
|
2156
|
+
key: "min",
|
|
2157
|
+
prefix: "mn"
|
|
2158
|
+
}, {
|
|
2159
|
+
kind: "scalar",
|
|
2160
|
+
key: "max",
|
|
2161
|
+
prefix: "mx"
|
|
2162
|
+
}];
|
|
2163
|
+
/**
|
|
2164
|
+
* Maps each schema node `type` to the ordered list of shape-contributing fields.
|
|
2165
|
+
* Node types absent from this map (scalar types like boolean, null, any, etc.) fall
|
|
2166
|
+
* back to `${type}|${flags}` with no additional fields.
|
|
2167
|
+
*/
|
|
2168
|
+
const SHAPE_KEYS = {
|
|
2169
|
+
object: [
|
|
2170
|
+
{ kind: "objectProps" },
|
|
2171
|
+
{ kind: "additionalProps" },
|
|
2172
|
+
{ kind: "patternProps" },
|
|
2173
|
+
{
|
|
2174
|
+
kind: "scalar",
|
|
2175
|
+
key: "minProperties",
|
|
2176
|
+
prefix: "mn"
|
|
2177
|
+
},
|
|
2178
|
+
{
|
|
2179
|
+
kind: "scalar",
|
|
2180
|
+
key: "maxProperties",
|
|
2181
|
+
prefix: "mx"
|
|
2182
|
+
}
|
|
2183
|
+
],
|
|
2184
|
+
array: arrayTupleFields,
|
|
2185
|
+
tuple: arrayTupleFields,
|
|
2186
|
+
union: [
|
|
2187
|
+
{
|
|
2188
|
+
kind: "scalar",
|
|
2189
|
+
key: "strategy",
|
|
2190
|
+
prefix: "s"
|
|
2191
|
+
},
|
|
2192
|
+
{
|
|
2193
|
+
kind: "scalar",
|
|
2194
|
+
key: "discriminatorPropertyName",
|
|
2195
|
+
prefix: "d"
|
|
2196
|
+
},
|
|
2197
|
+
{
|
|
2198
|
+
kind: "children",
|
|
2199
|
+
key: "members",
|
|
2200
|
+
prefix: "m"
|
|
2201
|
+
}
|
|
2202
|
+
],
|
|
2203
|
+
intersection: [{
|
|
2204
|
+
kind: "children",
|
|
2205
|
+
key: "members",
|
|
2206
|
+
prefix: "m"
|
|
2207
|
+
}],
|
|
2208
|
+
enum: [{ kind: "enumValues" }],
|
|
2209
|
+
ref: [{ kind: "refTarget" }],
|
|
2210
|
+
string: [
|
|
2211
|
+
{
|
|
2212
|
+
kind: "scalar",
|
|
2213
|
+
key: "min",
|
|
2214
|
+
prefix: "mn"
|
|
2215
|
+
},
|
|
2216
|
+
{
|
|
2217
|
+
kind: "scalar",
|
|
2218
|
+
key: "max",
|
|
2219
|
+
prefix: "mx"
|
|
2220
|
+
},
|
|
2221
|
+
{
|
|
2222
|
+
kind: "scalar",
|
|
2223
|
+
key: "pattern",
|
|
2224
|
+
prefix: "pt"
|
|
2225
|
+
}
|
|
2226
|
+
],
|
|
2227
|
+
number: numericFields,
|
|
2228
|
+
integer: numericFields,
|
|
2229
|
+
bigint: numericFields,
|
|
2230
|
+
url: [
|
|
2231
|
+
{
|
|
2232
|
+
kind: "scalar",
|
|
2233
|
+
key: "path",
|
|
2234
|
+
prefix: "path"
|
|
2235
|
+
},
|
|
2236
|
+
{
|
|
2237
|
+
kind: "scalar",
|
|
2238
|
+
key: "min",
|
|
2239
|
+
prefix: "mn"
|
|
2240
|
+
},
|
|
2241
|
+
{
|
|
2242
|
+
kind: "scalar",
|
|
2243
|
+
key: "max",
|
|
2244
|
+
prefix: "mx"
|
|
2245
|
+
}
|
|
2246
|
+
],
|
|
2247
|
+
uuid: rangeFields,
|
|
2248
|
+
email: rangeFields,
|
|
2249
|
+
datetime: [{
|
|
2250
|
+
kind: "bool",
|
|
2251
|
+
key: "offset",
|
|
2252
|
+
prefix: "o"
|
|
2253
|
+
}, {
|
|
2254
|
+
kind: "bool",
|
|
2255
|
+
key: "local",
|
|
2256
|
+
prefix: "l"
|
|
2257
|
+
}],
|
|
2258
|
+
date: [{
|
|
2259
|
+
kind: "scalar",
|
|
2260
|
+
key: "representation",
|
|
2261
|
+
prefix: "rep"
|
|
2262
|
+
}],
|
|
2263
|
+
time: [{
|
|
2264
|
+
kind: "scalar",
|
|
2265
|
+
key: "representation",
|
|
2266
|
+
prefix: "rep"
|
|
2267
|
+
}]
|
|
2268
|
+
};
|
|
2269
|
+
function serializeShapeField(field, node, record) {
|
|
2270
|
+
switch (field.kind) {
|
|
2271
|
+
case "scalar": return `${field.prefix}:${record[field.key] ?? ""}`;
|
|
2272
|
+
case "bool": return `${field.prefix}:${record[field.key] ? 1 : 0}`;
|
|
2273
|
+
case "child": {
|
|
2274
|
+
const child = record[field.key];
|
|
2275
|
+
return `${field.prefix}:${child ? signatureOf(child) : ""}`;
|
|
2276
|
+
}
|
|
2277
|
+
case "children": {
|
|
2278
|
+
const children = record[field.key] ?? [];
|
|
2279
|
+
return `${field.prefix}[${children.map((c) => signatureOf(c)).join(",")}]`;
|
|
2280
|
+
}
|
|
2281
|
+
case "objectProps": return `p[${(node.properties ?? []).map((prop) => `${prop.name}${prop.required ? "!" : "?"}${signatureOf(prop.schema)}`).join(",")}]`;
|
|
2282
|
+
case "additionalProps": {
|
|
2283
|
+
const obj = node;
|
|
2284
|
+
if (typeof obj.additionalProperties === "boolean") return `ab:${obj.additionalProperties}`;
|
|
2285
|
+
if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`;
|
|
2286
|
+
return "";
|
|
2287
|
+
}
|
|
2288
|
+
case "patternProps": {
|
|
2289
|
+
const obj = node;
|
|
2290
|
+
return `pp[${obj.patternProperties ? Object.keys(obj.patternProperties).sort().map((key) => `${key}=${signatureOf(obj.patternProperties[key])}`).join(",") : ""}]`;
|
|
2291
|
+
}
|
|
2292
|
+
case "enumValues": {
|
|
2293
|
+
const en = node;
|
|
2294
|
+
let values = "";
|
|
2295
|
+
if (en.namedEnumValues?.length) values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(",");
|
|
2296
|
+
else if (en.enumValues?.length) values = en.enumValues.map((value) => `${value === null ? "null" : typeof value}:${String(value)}`).join(",");
|
|
2297
|
+
return `v[${values}]`;
|
|
2298
|
+
}
|
|
2299
|
+
case "refTarget": return `->${refTargetName(node)}`;
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
/**
|
|
2303
|
+
* Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
|
|
2304
|
+
* children's signatures. {@link signatureOf} hashes this string; children contribute their
|
|
2305
|
+
* fixed-length signature rather than their own full descriptor, which keeps the result bounded.
|
|
2306
|
+
*/
|
|
2307
|
+
function describeShape(node) {
|
|
2308
|
+
const flags = flagsDescriptor(node);
|
|
2309
|
+
const fields = SHAPE_KEYS[node.type];
|
|
2310
|
+
if (!fields) return `${node.type}|${flags}`;
|
|
2311
|
+
const record = node;
|
|
2312
|
+
const parts = [`${node.type}|${flags}`];
|
|
2313
|
+
for (const field of fields) parts.push(serializeShapeField(field, node, record));
|
|
2314
|
+
return parts.join("|");
|
|
2315
|
+
}
|
|
2316
|
+
/**
|
|
2317
|
+
* Persistent hash-consing cache: `SchemaNode` → signature digest, keyed by node identity.
|
|
2318
|
+
*
|
|
2319
|
+
* A `WeakMap` so entries are released once the node is garbage-collected, and so a node hashed
|
|
2320
|
+
* during dedupe planning is not re-hashed when the same tree is rewritten during streaming
|
|
2321
|
+
* (where `schemaSignature` and `applyDedupe` would otherwise each walk it from scratch). Reuse
|
|
2322
|
+
* across calls is sound because a signature depends only on a node's content, and schema nodes
|
|
2323
|
+
* are immutable once created — transforms allocate new objects rather than mutating in place.
|
|
2324
|
+
*/
|
|
2325
|
+
const signatureCache = /* @__PURE__ */ new WeakMap();
|
|
2326
|
+
/**
|
|
2327
|
+
* Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
|
|
2328
|
+
* children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
|
|
2329
|
+
* full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
|
|
2330
|
+
* digest is identical across calls because it depends only on content — never on traversal
|
|
2331
|
+
* order. This keeps the keys built during planning consistent with the ones recomputed later
|
|
2332
|
+
* during streaming. {@link signatureCache} memoizes node → digest across every computation.
|
|
2333
|
+
*/
|
|
2334
|
+
function signatureOf(node) {
|
|
2335
|
+
const cached = signatureCache.get(node);
|
|
2336
|
+
if (cached !== void 0) return cached;
|
|
2337
|
+
const signature = (0, node_crypto.createHash)("sha256").update(describeShape(node)).digest("hex");
|
|
2338
|
+
signatureCache.set(node, signature);
|
|
2339
|
+
return signature;
|
|
2340
|
+
}
|
|
2341
|
+
/**
|
|
2342
|
+
* Computes a deterministic, shape-only signature (a fixed-length content hash) for a schema node.
|
|
2343
|
+
*
|
|
2344
|
+
* Two schemas share a signature when they are structurally identical, ignoring
|
|
2345
|
+
* documentation (`name`, `title`, `description`, `example`, `default`, `deprecated`)
|
|
2346
|
+
* and usage-slot flags (`optional`, `nullish`, `readOnly`, `writeOnly`). `nullable`
|
|
2347
|
+
* is kept because it changes the produced type. `ref` nodes compare by target name,
|
|
2348
|
+
* which also keeps the algorithm terminating on circular shapes.
|
|
2349
|
+
*
|
|
2350
|
+
* @example Two enums with different descriptions share a signature
|
|
2351
|
+
* ```ts
|
|
2352
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
|
|
2353
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
|
|
2354
|
+
* ```
|
|
2355
|
+
*/
|
|
2356
|
+
function schemaSignature(node) {
|
|
2357
|
+
return signatureOf(node);
|
|
2358
|
+
}
|
|
2359
|
+
/**
|
|
2360
|
+
* Returns `true` when two schema nodes are structurally identical under shape-only equality.
|
|
2361
|
+
*
|
|
2362
|
+
* @example
|
|
2363
|
+
* ```ts
|
|
2364
|
+
* isSchemaEqual(a, b) // a and b produce the same TypeScript type
|
|
2365
|
+
* ```
|
|
2366
|
+
*/
|
|
2367
|
+
function isSchemaEqual(a, b) {
|
|
2368
|
+
return schemaSignature(a) === schemaSignature(b);
|
|
2369
|
+
}
|
|
2370
|
+
//#endregion
|
|
2371
|
+
//#region src/dedupe.ts
|
|
2372
|
+
/**
|
|
2373
|
+
* Builds the shared `ref` replacement for a duplicate occurrence, carrying the
|
|
2374
|
+
* usage-slot and documentation fields that are not part of the canonical type.
|
|
2375
|
+
*/
|
|
2376
|
+
function createRefNode(node, canonical) {
|
|
2377
|
+
return createSchema({
|
|
2378
|
+
type: "ref",
|
|
2379
|
+
name: canonical.name,
|
|
2380
|
+
ref: canonical.ref,
|
|
2381
|
+
optional: node.optional,
|
|
2382
|
+
nullish: node.nullish,
|
|
2383
|
+
readOnly: node.readOnly,
|
|
2384
|
+
writeOnly: node.writeOnly,
|
|
2385
|
+
deprecated: node.deprecated,
|
|
2386
|
+
description: node.description,
|
|
2387
|
+
default: node.default,
|
|
2388
|
+
example: node.example
|
|
2389
|
+
});
|
|
2390
|
+
}
|
|
2391
|
+
function applyDedupe(node, canonicalBySignature, skipRootMatch = false) {
|
|
2392
|
+
if (canonicalBySignature.size === 0) return node;
|
|
2393
|
+
const root = node;
|
|
2394
|
+
return transform(node, { schema(schemaNode) {
|
|
2395
|
+
const signature = signatureOf(schemaNode);
|
|
2396
|
+
if (skipRootMatch && schemaNode === root) return void 0;
|
|
2397
|
+
const canonical = canonicalBySignature.get(signature);
|
|
2398
|
+
if (!canonical) return void 0;
|
|
2399
|
+
return createRefNode(schemaNode, canonical);
|
|
2400
|
+
} });
|
|
2401
|
+
}
|
|
2402
|
+
/**
|
|
2403
|
+
* Strips usage-slot flags from a hoisted definition and applies its canonical name.
|
|
2404
|
+
* A standalone definition is never optional, so `optional`/`nullish` are cleared.
|
|
2405
|
+
*/
|
|
2406
|
+
function cleanDefinition(node, name) {
|
|
2407
|
+
return {
|
|
2408
|
+
...node,
|
|
2409
|
+
name,
|
|
2410
|
+
optional: void 0,
|
|
2411
|
+
nullish: void 0
|
|
2412
|
+
};
|
|
2413
|
+
}
|
|
2414
|
+
/**
|
|
2415
|
+
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
|
|
2416
|
+
*
|
|
2417
|
+
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
|
|
2418
|
+
* is a named top-level schema, that name becomes the canonical (so other top-level duplicates
|
|
2419
|
+
* and inline copies turn into references to it); otherwise a new definition is hoisted using
|
|
2420
|
+
* `nameFor`. The plan is then applied per node with {@link applyDedupe}.
|
|
2421
|
+
*
|
|
2422
|
+
* @example
|
|
2423
|
+
* ```ts
|
|
2424
|
+
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
|
|
2425
|
+
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
|
|
2426
|
+
* nameFor: (node) => node.name ?? null,
|
|
2427
|
+
* refFor: (name) => `#/components/schemas/${name}`,
|
|
2428
|
+
* })
|
|
2429
|
+
* ```
|
|
2430
|
+
*/
|
|
2431
|
+
function buildDedupePlan(roots, options) {
|
|
2432
|
+
const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options;
|
|
2433
|
+
const topLevelNodes = /* @__PURE__ */ new Set();
|
|
2434
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2435
|
+
function record(schemaNode) {
|
|
2436
|
+
const signature = signatureOf(schemaNode);
|
|
2437
|
+
if (!isCandidate(schemaNode)) return;
|
|
2438
|
+
const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name;
|
|
2439
|
+
const group = groups.get(signature);
|
|
2440
|
+
if (group) {
|
|
2441
|
+
group.count++;
|
|
2442
|
+
if (isTopLevel && !group.topLevelName) group.topLevelName = schemaNode.name;
|
|
2443
|
+
} else groups.set(signature, {
|
|
2444
|
+
count: 1,
|
|
2445
|
+
representative: schemaNode,
|
|
2446
|
+
topLevelName: isTopLevel ? schemaNode.name : void 0
|
|
2447
|
+
});
|
|
2448
|
+
}
|
|
2449
|
+
for (const root of roots) {
|
|
2450
|
+
if (root.kind === "Schema") topLevelNodes.add(root);
|
|
2451
|
+
for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
2452
|
+
}
|
|
2453
|
+
const canonicalBySignature = /* @__PURE__ */ new Map();
|
|
2454
|
+
const pendingHoists = [];
|
|
2455
|
+
for (const [signature, group] of groups) {
|
|
2456
|
+
if (group.count < minOccurrences) continue;
|
|
2457
|
+
if (group.topLevelName) {
|
|
2458
|
+
canonicalBySignature.set(signature, {
|
|
2459
|
+
name: group.topLevelName,
|
|
2460
|
+
ref: refFor(group.topLevelName)
|
|
2461
|
+
});
|
|
2462
|
+
continue;
|
|
2463
|
+
}
|
|
2464
|
+
const name = nameFor(group.representative, signature);
|
|
2465
|
+
if (!name) continue;
|
|
2466
|
+
canonicalBySignature.set(signature, {
|
|
2467
|
+
name,
|
|
2468
|
+
ref: refFor(name)
|
|
2469
|
+
});
|
|
2470
|
+
pendingHoists.push({
|
|
2471
|
+
name,
|
|
2472
|
+
representative: group.representative
|
|
2473
|
+
});
|
|
2474
|
+
}
|
|
2475
|
+
return {
|
|
2476
|
+
canonicalBySignature,
|
|
2477
|
+
hoisted: pendingHoists.map(({ name, representative }) => cleanDefinition(applyDedupe(representative, canonicalBySignature, true), name))
|
|
2478
|
+
};
|
|
2479
|
+
}
|
|
2480
|
+
//#endregion
|
|
2481
|
+
//#region src/dialect.ts
|
|
2482
|
+
/**
|
|
2483
|
+
* Identity helper that types a {@link SchemaDialect} for an adapter. Like
|
|
2484
|
+
* `defineParser`, it adds no runtime behavior — it pins the dialect's type for
|
|
2485
|
+
* inference and gives adapter authors a discoverable anchor.
|
|
2486
|
+
*
|
|
2487
|
+
* @example
|
|
2488
|
+
* ```ts
|
|
2489
|
+
* export const oasDialect = defineSchemaDialect({
|
|
2490
|
+
* name: 'oas',
|
|
2491
|
+
* isNullable,
|
|
2492
|
+
* isReference,
|
|
2493
|
+
* isDiscriminator,
|
|
2494
|
+
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
2495
|
+
* resolveRef,
|
|
2496
|
+
* })
|
|
2497
|
+
* ```
|
|
2498
|
+
*/
|
|
2499
|
+
function defineSchemaDialect(dialect) {
|
|
2500
|
+
return dialect;
|
|
2501
|
+
}
|
|
2502
|
+
//#endregion
|
|
2503
|
+
//#region src/dispatch.ts
|
|
2504
|
+
/**
|
|
2505
|
+
* Walks an ordered list of {@link DispatchRule}s and returns the first node produced.
|
|
2506
|
+
*
|
|
2507
|
+
* This is the shared backbone for spec adapters (OpenAPI today, AsyncAPI and others later).
|
|
2508
|
+
* The contract an adapter follows is intentionally minimal:
|
|
2509
|
+
*
|
|
2510
|
+
* context → [rule.match → rule.convert] → node
|
|
2511
|
+
*
|
|
2512
|
+
* An adapter derives a context from a source spec node, then declares an ordered table of
|
|
2513
|
+
* rules mapping spec shapes onto Kubb AST nodes. To add support for a new spec, write a new
|
|
2514
|
+
* context type and a new rules table — the traversal here is reused unchanged.
|
|
2515
|
+
*
|
|
2516
|
+
* Order is significant: earlier rules win, so list higher-precedence or more specific shapes
|
|
2517
|
+
* first (e.g. composition keywords before plain `type`). A rule whose `match` returns `true`
|
|
2518
|
+
* may still `convert` to `null` to defer to later rules. When no rule produces a node this
|
|
2519
|
+
* returns `null`, leaving the caller to apply its own fallback.
|
|
2520
|
+
*
|
|
2521
|
+
* @example
|
|
2522
|
+
* ```ts
|
|
2523
|
+
* const node = dispatch(schemaRules, schemaContext) ?? createSchema({ type: fallbackType })
|
|
2524
|
+
* ```
|
|
2525
|
+
*/
|
|
2526
|
+
function dispatch(rules, context) {
|
|
2527
|
+
for (const rule of rules) {
|
|
2528
|
+
if (!rule.match(context)) continue;
|
|
2529
|
+
const node = rule.convert(context);
|
|
2530
|
+
if (node !== null && node !== void 0) return node;
|
|
2531
|
+
}
|
|
2532
|
+
return null;
|
|
2533
|
+
}
|
|
2534
|
+
//#endregion
|
|
2141
2535
|
//#region src/printer.ts
|
|
2142
2536
|
/**
|
|
2143
2537
|
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
@@ -2354,6 +2748,8 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
2354
2748
|
return propNode;
|
|
2355
2749
|
}
|
|
2356
2750
|
//#endregion
|
|
2751
|
+
exports.applyDedupe = applyDedupe;
|
|
2752
|
+
exports.buildDedupePlan = buildDedupePlan;
|
|
2357
2753
|
exports.caseParams = caseParams;
|
|
2358
2754
|
exports.childName = childName;
|
|
2359
2755
|
exports.collect = collect;
|
|
@@ -2404,6 +2800,7 @@ exports.isInputNode = isInputNode;
|
|
|
2404
2800
|
exports.isOperationNode = isOperationNode;
|
|
2405
2801
|
exports.isOutputNode = isOutputNode;
|
|
2406
2802
|
exports.isScalarPrimitive = isScalarPrimitive;
|
|
2803
|
+
exports.isSchemaEqual = isSchemaEqual;
|
|
2407
2804
|
exports.isSchemaNode = isSchemaNode;
|
|
2408
2805
|
exports.isStringType = isStringType;
|
|
2409
2806
|
exports.mediaTypes = mediaTypes;
|
|
@@ -2412,6 +2809,7 @@ exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
|
|
|
2412
2809
|
exports.narrowSchema = narrowSchema;
|
|
2413
2810
|
exports.nodeKinds = nodeKinds;
|
|
2414
2811
|
exports.resolveRefName = resolveRefName;
|
|
2812
|
+
exports.schemaSignature = schemaSignature;
|
|
2415
2813
|
exports.schemaTypes = schemaTypes;
|
|
2416
2814
|
exports.setDiscriminatorEnum = setDiscriminatorEnum;
|
|
2417
2815
|
exports.setEnumName = setEnumName;
|