@kubb/ast 5.0.0-beta.57 → 5.0.0-beta.59

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.
Files changed (69) hide show
  1. package/README.md +21 -7
  2. package/dist/casing-BE2R1RXg.cjs +88 -0
  3. package/dist/casing-BE2R1RXg.cjs.map +1 -0
  4. package/dist/chunk-CNktS9qV.js +17 -0
  5. package/dist/extractStringsFromNodes-WMYJ8nQL.d.ts +82 -0
  6. package/dist/factory-BmcGBdeg.cjs +1251 -0
  7. package/dist/factory-BmcGBdeg.cjs.map +1 -0
  8. package/dist/factory-Du7nEP4B.js +282 -0
  9. package/dist/factory-Du7nEP4B.js.map +1 -0
  10. package/dist/factory.cjs +28 -0
  11. package/dist/factory.d.ts +62 -0
  12. package/dist/factory.js +3 -0
  13. package/dist/{types-C5aVnRE1.d.ts → index-BzjwdK2M.d.ts} +94 -1146
  14. package/dist/index.cjs +220 -1481
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.ts +96 -58
  17. package/dist/index.js +182 -1920
  18. package/dist/index.js.map +1 -1
  19. package/dist/operationParams-BZ07xDm0.d.ts +204 -0
  20. package/dist/response-DKxTr522.js +683 -0
  21. package/dist/response-DKxTr522.js.map +1 -0
  22. package/dist/types-olVl9v5p.d.ts +764 -0
  23. package/dist/types.d.ts +5 -2
  24. package/dist/utils-BCtRXfhI.cjs +275 -0
  25. package/dist/utils-BCtRXfhI.cjs.map +1 -0
  26. package/dist/utils-SdZU0F3H.js +1336 -0
  27. package/dist/utils-SdZU0F3H.js.map +1 -0
  28. package/dist/utils.cjs +129 -13
  29. package/dist/utils.d.ts +127 -76
  30. package/dist/utils.js +3 -2
  31. package/package.json +5 -1
  32. package/src/constants.ts +8 -14
  33. package/src/dedupe.ts +8 -0
  34. package/src/factory.ts +18 -1
  35. package/src/guards.ts +1 -1
  36. package/src/index.ts +7 -50
  37. package/src/node.ts +1 -1
  38. package/src/nodes/base.ts +0 -10
  39. package/src/nodes/code.ts +29 -47
  40. package/src/nodes/content.ts +2 -2
  41. package/src/nodes/file.ts +28 -23
  42. package/src/nodes/function.ts +2 -17
  43. package/src/nodes/index.ts +1 -1
  44. package/src/nodes/input.ts +20 -19
  45. package/src/nodes/operation.ts +1 -1
  46. package/src/nodes/parameter.ts +0 -3
  47. package/src/nodes/property.ts +0 -3
  48. package/src/nodes/requestBody.ts +3 -6
  49. package/src/nodes/schema.ts +3 -2
  50. package/src/printer.ts +4 -4
  51. package/src/registry.ts +31 -26
  52. package/src/signature.ts +3 -3
  53. package/src/transformers.ts +28 -1
  54. package/src/types.ts +2 -53
  55. package/src/utils/codegen.ts +104 -0
  56. package/src/utils/extractStringsFromNodes.ts +34 -0
  57. package/src/utils/fileMerge.ts +184 -0
  58. package/src/utils/index.ts +8 -296
  59. package/src/utils/operationParams.ts +353 -0
  60. package/src/utils/refs.ts +112 -0
  61. package/src/utils/schemaGraph.ts +169 -0
  62. package/src/utils/strings.ts +139 -0
  63. package/src/visitor.ts +43 -19
  64. package/dist/chunk-C0LytTxp.js +0 -8
  65. package/dist/utils-0p8ZO287.js +0 -570
  66. package/dist/utils-0p8ZO287.js.map +0 -1
  67. package/dist/utils-cdQ6Pzyi.cjs +0 -726
  68. package/dist/utils-cdQ6Pzyi.cjs.map +0 -1
  69. package/src/utils/ast.ts +0 -879
package/README.md CHANGED
@@ -28,10 +28,12 @@ Defines the node tree, visitor pattern, factory functions, and type guards used
28
28
 
29
29
  ## Imports
30
30
 
31
- | Path | Contents |
32
- | ----------------- | ------------------------------------------------------------------- |
33
- | `@kubb/ast` | Runtime: factory functions, guards, visitor, ref helpers, constants |
34
- | `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types |
31
+ | Path | Contents |
32
+ | ------------------- | ---------------------------------------------------------------------------------------- |
33
+ | `@kubb/ast` | Runtime: node definitions, guards, visitor, transformers, constants |
34
+ | `@kubb/ast/factory` | Node constructors (`createSchema`, `createFile`, and friends), the `ts.factory` analogue |
35
+ | `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types |
36
+ | `@kubb/ast/utils` | Spec-agnostic string and identifier helpers, ref helpers |
35
37
 
36
38
  ## Node tree
37
39
 
@@ -56,10 +58,12 @@ SchemaNode (discriminated union)
56
58
 
57
59
  ### Factory
58
60
 
61
+ Constructors live on the `@kubb/ast/factory` subpath, mirroring `ts.factory.createX`. Through `@kubb/core` the same set is reachable as `ast.factory.createSchema(...)`.
62
+
59
63
  ```ts
60
- import { createRoot, createOperation, createSchema, createProperty } from '@kubb/ast'
64
+ import { createInput, createSchema, createProperty } from '@kubb/ast/factory'
61
65
 
62
- const root = createRoot({
66
+ const root = createInput({
63
67
  schemas: [
64
68
  createSchema({
65
69
  name: 'Pet',
@@ -125,11 +129,21 @@ function process(node: Node) {
125
129
  ### Refs
126
130
 
127
131
  ```ts
128
- import { extractRefName } from '@kubb/ast'
132
+ import { extractRefName } from '@kubb/ast/utils'
129
133
 
130
134
  extractRefName('#/components/schemas/Pet') // 'Pet'
131
135
  ```
132
136
 
137
+ ## Adding a node
138
+
139
+ Adding a node touches three files. The barrels and visitor tables derive the rest.
140
+
141
+ 1. Define the node in its own `src/nodes/*.ts` file. Call `defineNode` and export the resulting `fooDef`, the `createFoo` constructor, and the node's type.
142
+ 2. Add `fooDef` to the `nodeDefs` array in `src/registry.ts`.
143
+ 3. Re-export `createFoo` from `src/factory.ts`.
144
+
145
+ Everything else follows from there. `@kubb/ast/types` picks up the node type through `export type *`, the `@kubb/ast` barrel picks up `fooDef` through `export * from './registry.ts'`, and the visitor tables (`VISITOR_KEYS`, `VISITOR_KEY_BY_KIND`, `nodeRebuilders`) come from the def's `children`, `visitorKey`, and `rebuild` fields. `registry.test.ts` fails when a def has no matching `factory.create*`, so missing wiring is caught in CI.
146
+
133
147
  ## Supporting Kubb
134
148
 
135
149
  Kubb is an open source project, and its development is funded entirely by sponsors. If you would like to become a sponsor, please consider:
@@ -0,0 +1,88 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", {
5
+ value,
6
+ configurable: true
7
+ });
8
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
+ var __getOwnPropNames = Object.getOwnPropertyNames;
10
+ var __getProtoOf = Object.getPrototypeOf;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __exportAll = (all, no_symbols) => {
13
+ let target = {};
14
+ for (var name in all) __defProp(target, name, {
15
+ get: all[name],
16
+ enumerable: true
17
+ });
18
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
19
+ return target;
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
23
+ key = keys[i];
24
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
25
+ get: ((k) => from[k]).bind(null, key),
26
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
27
+ });
28
+ }
29
+ return to;
30
+ };
31
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
32
+ value: mod,
33
+ enumerable: true
34
+ }) : target, mod));
35
+ //#endregion
36
+ //#region ../../internals/utils/src/casing.ts
37
+ /**
38
+ * Shared implementation for camelCase and PascalCase conversion.
39
+ * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
40
+ * and capitalizes each word according to `pascal`.
41
+ *
42
+ * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
43
+ */
44
+ function toCamelOrPascal(text, pascal) {
45
+ return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
46
+ if (word.length > 1 && word === word.toUpperCase()) return word;
47
+ return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
48
+ }).join("").replace(/[^a-zA-Z0-9]/g, "");
49
+ }
50
+ /**
51
+ * Converts `text` to PascalCase.
52
+ *
53
+ * @example Word boundaries
54
+ * `pascalCase('hello-world') // 'HelloWorld'`
55
+ *
56
+ * @example With a suffix
57
+ * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
58
+ */
59
+ function pascalCase(text, { prefix = "", suffix = "" } = {}) {
60
+ return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
61
+ }
62
+ //#endregion
63
+ Object.defineProperty(exports, "__exportAll", {
64
+ enumerable: true,
65
+ get: function() {
66
+ return __exportAll;
67
+ }
68
+ });
69
+ Object.defineProperty(exports, "__name", {
70
+ enumerable: true,
71
+ get: function() {
72
+ return __name;
73
+ }
74
+ });
75
+ Object.defineProperty(exports, "__toESM", {
76
+ enumerable: true,
77
+ get: function() {
78
+ return __toESM;
79
+ }
80
+ });
81
+ Object.defineProperty(exports, "pascalCase", {
82
+ enumerable: true,
83
+ get: function() {
84
+ return pascalCase;
85
+ }
86
+ });
87
+
88
+ //# sourceMappingURL=casing-BE2R1RXg.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"casing-BE2R1RXg.cjs","names":[],"sources":["../../../internals/utils/src/casing.ts"],"sourcesContent":["type Options = {\n /**\n * Text prepended before casing is applied.\n */\n prefix?: string\n /**\n * Text appended before casing is applied.\n */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n return text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n .split(/[\\s\\-_./\\\\:]+/)\n .filter(Boolean)\n .map((word, i) => {\n if (word.length > 1 && word === word.toUpperCase()) return word\n const head = i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()\n return head + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Converts `text` to camelCase.\n *\n * @example Word boundaries\n * `camelCase('hello-world') // 'helloWorld'`\n *\n * @example With a prefix\n * `camelCase('tag', { prefix: 'create' }) // 'createTag'`\n */\nexport function camelCase(text: string, { prefix = '', suffix = '' }: Options = {}): string {\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n *\n * @example Word boundaries\n * `pascalCase('hello-world') // 'HelloWorld'`\n *\n * @example With a suffix\n * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`\n */\nexport function pascalCase(text: string, { prefix = '', suffix = '' }: Options = {}): string {\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA,SAAS,gBAAgB,MAAc,QAAyB;CAC9D,OAAO,KACJ,KAAK,CAAC,CACN,QAAQ,qBAAqB,OAAO,CAAC,CACrC,QAAQ,yBAAyB,OAAO,CAAC,CACzC,QAAQ,gBAAgB,OAAO,CAAC,CAChC,MAAM,eAAe,CAAC,CACtB,OAAO,OAAO,CAAC,CACf,KAAK,MAAM,MAAM;EAChB,IAAI,KAAK,SAAS,KAAK,SAAS,KAAK,YAAY,GAAG,OAAO;EAE3D,QADa,MAAM,KAAK,CAAC,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,KAC9E,KAAK,MAAM,CAAC;CAC5B,CAAC,CAAC,CACD,KAAK,EAAE,CAAC,CACR,QAAQ,iBAAiB,EAAE;AAChC;;;;;;;;;;AAwBA,SAAgB,WAAW,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC3F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,IAAI;AAC5D"}
@@ -0,0 +1,17 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __name = (target, value) => __defProp(target, "name", {
4
+ value,
5
+ configurable: true
6
+ });
7
+ var __exportAll = (all, no_symbols) => {
8
+ let target = {};
9
+ for (var name in all) __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true
12
+ });
13
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
14
+ return target;
15
+ };
16
+ //#endregion
17
+ export { __name as n, __exportAll as t };
@@ -0,0 +1,82 @@
1
+ import { n as __name } from "./chunk-CNktS9qV.js";
2
+ import { Pt as CodeNode, _t as SchemaNode } from "./index-BzjwdK2M.js";
3
+
4
+ //#region src/transformers.d.ts
5
+ /**
6
+ * Replaces a discriminator property's schema with a string enum of allowed values.
7
+ *
8
+ * If `node` is not an object schema, or if the property does not exist, the input
9
+ * node is returned as-is.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const schema = createSchema({
14
+ * type: 'object',
15
+ * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
16
+ * })
17
+ * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
18
+ * ```
19
+ */
20
+ declare function setDiscriminatorEnum({
21
+ node,
22
+ propertyName,
23
+ values,
24
+ enumName
25
+ }: {
26
+ node: SchemaNode;
27
+ propertyName: string;
28
+ values: Array<string>;
29
+ enumName?: string;
30
+ }): SchemaNode;
31
+ /**
32
+ * Merges adjacent anonymous object members into a single anonymous object member.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const merged = mergeAdjacentObjects([
37
+ * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
38
+ * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
39
+ * ])
40
+ * ```
41
+ */
42
+ declare function mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined>;
43
+ /**
44
+ * Removes enum members that are covered by broader scalar primitives in the same union.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const simplified = simplifyUnion([
49
+ * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
50
+ * createSchema({ type: 'string' }),
51
+ * ])
52
+ * // keeps only string member
53
+ * ```
54
+ */
55
+ declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
56
+ declare function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode;
57
+ /**
58
+ * Merges a ref node with its resolved schema, giving usage-site fields precedence.
59
+ *
60
+ * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
61
+ * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * // Ref with description override
66
+ * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
67
+ * const merged = syncSchemaRef(ref) // merges with resolved Pet schema
68
+ * ```
69
+ */
70
+ declare function syncSchemaRef(node: SchemaNode): SchemaNode;
71
+ //#endregion
72
+ //#region src/utils/extractStringsFromNodes.d.ts
73
+ /**
74
+ * Extracts all string content from a `CodeNode` tree recursively.
75
+ *
76
+ * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
77
+ * and nested node content. Used to build the full source string for import filtering.
78
+ */
79
+ declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
80
+ //#endregion
81
+ export { simplifyUnion as a, setEnumName as i, mergeAdjacentObjectsLazy as n, syncSchemaRef as o, setDiscriminatorEnum as r, extractStringsFromNodes as t };
82
+ //# sourceMappingURL=extractStringsFromNodes-WMYJ8nQL.d.ts.map