@kubb/ast 5.0.0-beta.60 → 5.0.0-beta.62

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 (87) hide show
  1. package/README.md +2 -1
  2. package/dist/defineMacro-BryDiuQl.cjs +115 -0
  3. package/dist/defineMacro-BryDiuQl.cjs.map +1 -0
  4. package/dist/defineMacro-CNkUpxia.js +99 -0
  5. package/dist/defineMacro-CNkUpxia.js.map +1 -0
  6. package/dist/defineMacro-Vw-xQvq7.d.ts +467 -0
  7. package/dist/factory.cjs +64 -4
  8. package/dist/factory.cjs.map +1 -0
  9. package/dist/factory.d.ts +2 -37
  10. package/dist/factory.js +58 -3
  11. package/dist/factory.js.map +1 -0
  12. package/dist/{index-BzjwdK2M.d.ts → index-CTaN_ee_.d.ts} +235 -198
  13. package/dist/index.cjs +33 -32
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.ts +36 -36
  16. package/dist/index.js +19 -14
  17. package/dist/index.js.map +1 -1
  18. package/dist/macros.cjs +130 -0
  19. package/dist/macros.cjs.map +1 -0
  20. package/dist/macros.d.ts +61 -0
  21. package/dist/macros.js +128 -0
  22. package/dist/macros.js.map +1 -0
  23. package/dist/{operationParams-BZ07xDm0.d.ts → operationParams-B44p6EQy.d.ts} +2 -2
  24. package/dist/{response-DS5S3IG4.cjs → response-BeG6hq2D.cjs} +389 -173
  25. package/dist/response-BeG6hq2D.cjs.map +1 -0
  26. package/dist/{response-DKxTr522.js → response-Rd1uisM1.js} +377 -162
  27. package/dist/response-Rd1uisM1.js.map +1 -0
  28. package/dist/types-DQTgVSwE.d.ts +314 -0
  29. package/dist/types.d.ts +5 -5
  30. package/dist/{utils-D83JA6Xx.cjs → utils-BDI3kXlb.cjs} +20 -753
  31. package/dist/utils-BDI3kXlb.cjs.map +1 -0
  32. package/dist/{utils-Dj_KoXMv.js → utils-DW9rnfsD.js} +11 -630
  33. package/dist/utils-DW9rnfsD.js.map +1 -0
  34. package/dist/utils.cjs +11 -9
  35. package/dist/utils.d.ts +38 -4
  36. package/dist/utils.js +4 -3
  37. package/dist/visitor-B7Zlnp4c.cjs +604 -0
  38. package/dist/visitor-B7Zlnp4c.cjs.map +1 -0
  39. package/dist/visitor-DepQEKyp.js +510 -0
  40. package/dist/visitor-DepQEKyp.js.map +1 -0
  41. package/package.json +5 -1
  42. package/src/constants.ts +0 -30
  43. package/src/dedupe.ts +1 -9
  44. package/src/defineMacro.ts +139 -0
  45. package/src/{node.ts → defineNode.ts} +19 -42
  46. package/src/dialect.ts +42 -19
  47. package/src/factory.ts +3 -90
  48. package/src/index.ts +9 -15
  49. package/src/macros/index.ts +3 -0
  50. package/src/macros/macroDiscriminatorEnum.ts +50 -0
  51. package/src/macros/macroEnumName.ts +33 -0
  52. package/src/macros/macroSimplifyUnion.ts +60 -0
  53. package/src/nodes/code.ts +32 -75
  54. package/src/nodes/content.ts +2 -7
  55. package/src/nodes/file.ts +102 -14
  56. package/src/nodes/function.ts +46 -46
  57. package/src/nodes/index.ts +2 -3
  58. package/src/nodes/input.ts +13 -9
  59. package/src/nodes/operation.ts +5 -5
  60. package/src/nodes/output.ts +1 -1
  61. package/src/nodes/parameter.ts +6 -6
  62. package/src/nodes/property.ts +6 -6
  63. package/src/nodes/requestBody.ts +6 -13
  64. package/src/nodes/response.ts +93 -8
  65. package/src/nodes/schema.ts +1 -1
  66. package/src/registry.ts +1 -1
  67. package/src/types.ts +4 -4
  68. package/src/utils/extractStringsFromNodes.ts +1 -0
  69. package/src/utils/index.ts +2 -2
  70. package/src/utils/operationParams.ts +1 -1
  71. package/src/utils/refs.ts +27 -1
  72. package/src/utils/schemaMerge.ts +34 -0
  73. package/src/visitor.ts +2 -13
  74. package/dist/extractStringsFromNodes-WMYJ8nQL.d.ts +0 -82
  75. package/dist/factory-Cl8Z7mcc.cjs +0 -299
  76. package/dist/factory-Cl8Z7mcc.cjs.map +0 -1
  77. package/dist/factory-Du7nEP4B.js +0 -282
  78. package/dist/factory-Du7nEP4B.js.map +0 -1
  79. package/dist/response-DKxTr522.js.map +0 -1
  80. package/dist/response-DS5S3IG4.cjs.map +0 -1
  81. package/dist/types-olVl9v5p.d.ts +0 -764
  82. package/dist/utils-D83JA6Xx.cjs.map +0 -1
  83. package/dist/utils-Dj_KoXMv.js.map +0 -1
  84. package/src/mocks.ts +0 -56
  85. package/src/nodes/http.ts +0 -85
  86. package/src/transformers.ts +0 -191
  87. /package/src/{printer.ts → definePrinter.ts} +0 -0
package/README.md CHANGED
@@ -30,8 +30,9 @@ Defines the node tree, visitor pattern, factory functions, and type guards used
30
30
 
31
31
  | Path | Contents |
32
32
  | ------------------- | ---------------------------------------------------------------------------------------- |
33
- | `@kubb/ast` | Runtime: node definitions, guards, visitor, transformers, constants |
33
+ | `@kubb/ast` | Runtime: node definitions, guards, visitor, macro engine, constants |
34
34
  | `@kubb/ast/factory` | Node constructors (`createSchema`, `createFile`, and friends), the `ts.factory` analogue |
35
+ | `@kubb/ast/macros` | Built-in macro presets: `macroDiscriminatorEnum`, `macroSimplifyUnion`, `macroEnumName` |
35
36
  | `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types |
36
37
  | `@kubb/ast/utils` | Spec-agnostic string and identifier helpers, ref helpers |
37
38
 
@@ -0,0 +1,115 @@
1
+ const require_response = require("./response-BeG6hq2D.cjs");
2
+ const require_visitor = require("./visitor-B7Zlnp4c.cjs");
3
+ //#region src/defineMacro.ts
4
+ /**
5
+ * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
6
+ * list keeps its authored order.
7
+ */
8
+ function enforceWeight(enforce) {
9
+ if (enforce === "pre") return 0;
10
+ if (enforce === "post") return 2;
11
+ return 1;
12
+ }
13
+ /**
14
+ * Types a macro for inference and a single construction site, mirroring `definePlugin`.
15
+ * Adds no runtime behavior.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const macroUntagged = defineMacro({
20
+ * name: 'untagged',
21
+ * operation(node) {
22
+ * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
23
+ * },
24
+ * })
25
+ * ```
26
+ */
27
+ function defineMacro(macro) {
28
+ return macro;
29
+ }
30
+ /**
31
+ * Runs every macro's callback for one node kind in order, chaining the result so each macro sees
32
+ * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
33
+ * original reference (structural sharing).
34
+ */
35
+ function chain({ macros, key, node, context }) {
36
+ let current = node;
37
+ for (const macro of macros) {
38
+ const callback = macro[key];
39
+ if (!callback) continue;
40
+ if (macro.when && !macro.when(current)) continue;
41
+ const next = callback(current, context);
42
+ if (next != null) current = next;
43
+ }
44
+ return current === node ? void 0 : current;
45
+ }
46
+ /**
47
+ * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
48
+ * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
49
+ * sequentially per node so later macros see earlier output. This differs from a plain visitor, which
50
+ * has no names, ordering, or composition.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
55
+ * const next = transform(root, visitor)
56
+ * ```
57
+ */
58
+ function composeMacros(macros) {
59
+ const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
60
+ const visitor = {};
61
+ for (const key of require_response.visitorKeys) {
62
+ if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
63
+ const callback = (node, context) => chain({
64
+ macros: ordered,
65
+ key,
66
+ node,
67
+ context
68
+ });
69
+ visitor[key] = callback;
70
+ }
71
+ return visitor;
72
+ }
73
+ /**
74
+ * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
75
+ * structural sharing, so an empty or no-op macro list returns the same reference. Pass
76
+ * `depth: 'shallow'` to rewrite the root node only.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * const next = applyMacros(root, [macroIntegerToString])
81
+ * ```
82
+ *
83
+ * @example Apply to the root node only
84
+ * ```ts
85
+ * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
86
+ * ```
87
+ */
88
+ function applyMacros(root, macros, options) {
89
+ if (macros.length === 0) return root;
90
+ return require_visitor.transform(root, {
91
+ ...composeMacros(macros),
92
+ ...options
93
+ });
94
+ }
95
+ //#endregion
96
+ Object.defineProperty(exports, "applyMacros", {
97
+ enumerable: true,
98
+ get: function() {
99
+ return applyMacros;
100
+ }
101
+ });
102
+ Object.defineProperty(exports, "composeMacros", {
103
+ enumerable: true,
104
+ get: function() {
105
+ return composeMacros;
106
+ }
107
+ });
108
+ Object.defineProperty(exports, "defineMacro", {
109
+ enumerable: true,
110
+ get: function() {
111
+ return defineMacro;
112
+ }
113
+ });
114
+
115
+ //# sourceMappingURL=defineMacro-BryDiuQl.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineMacro-BryDiuQl.cjs","names":["visitorKeys","transform"],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { VisitorKey } from './defineNode.ts'\nimport { visitorKeys } from './defineNode.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,\n * and `undefined` keeps declaration order.\n */\nexport type Enforce = 'pre' | 'post'\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: Enforce): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: Enforce\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\ntype ChainProps = {\n macros: ReadonlyArray<Macro>\n key: VisitorKey\n node: Node\n context: VisitorContext\n}\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain({ macros, key, node, context }: ChainProps): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of visitorKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain({ macros: ordered, key, node, context })\n ;(visitor as Record<VisitorKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...composeMacros(macros), ...options }) as TNode\n}\n"],"mappings":";;;;;;;AAiBA,SAAS,cAAc,SAA2B;CAChD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AAgBA,SAAS,MAAM,EAAE,QAAQ,KAAK,MAAM,WAAyC;CAC3E,IAAI,UAAU;CAEd,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,WAAW,MAAM;EACvB,IAAI,CAAC,UAAU;EACf,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,OAAO,GAAG;EAExC,MAAM,OAAO,SAAS,SAAS,OAAO;EACtC,IAAI,QAAQ,MAAM,UAAU;CAC9B;CAEA,OAAO,YAAY,OAAO,KAAA,IAAY;AACxC;;;;;;;;;;;;;AAcA,SAAgB,cAAc,QAAuC;CACnE,MAAM,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,cAAc,EAAE,OAAO,IAAI,cAAc,EAAE,OAAO,CAAC;CAE9F,MAAM,UAAmB,CAAC;CAC1B,KAAK,MAAM,OAAOA,iBAAAA,aAAa;EAC7B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM;GAAE,QAAQ;GAAS;GAAK;GAAM;EAAQ,CAAC;EACtG,QAA+C,OAAO;CACzD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAOC,gBAAAA,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,GAAG;CAAQ,CAAC;AACjE"}
@@ -0,0 +1,99 @@
1
+ import "./chunk-CNktS9qV.js";
2
+ import { r as transform } from "./visitor-DepQEKyp.js";
3
+ import { tt as visitorKeys } from "./response-Rd1uisM1.js";
4
+ //#region src/defineMacro.ts
5
+ /**
6
+ * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
7
+ * list keeps its authored order.
8
+ */
9
+ function enforceWeight(enforce) {
10
+ if (enforce === "pre") return 0;
11
+ if (enforce === "post") return 2;
12
+ return 1;
13
+ }
14
+ /**
15
+ * Types a macro for inference and a single construction site, mirroring `definePlugin`.
16
+ * Adds no runtime behavior.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const macroUntagged = defineMacro({
21
+ * name: 'untagged',
22
+ * operation(node) {
23
+ * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
24
+ * },
25
+ * })
26
+ * ```
27
+ */
28
+ function defineMacro(macro) {
29
+ return macro;
30
+ }
31
+ /**
32
+ * Runs every macro's callback for one node kind in order, chaining the result so each macro sees
33
+ * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
34
+ * original reference (structural sharing).
35
+ */
36
+ function chain({ macros, key, node, context }) {
37
+ let current = node;
38
+ for (const macro of macros) {
39
+ const callback = macro[key];
40
+ if (!callback) continue;
41
+ if (macro.when && !macro.when(current)) continue;
42
+ const next = callback(current, context);
43
+ if (next != null) current = next;
44
+ }
45
+ return current === node ? void 0 : current;
46
+ }
47
+ /**
48
+ * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
49
+ * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
50
+ * sequentially per node so later macros see earlier output. This differs from a plain visitor, which
51
+ * has no names, ordering, or composition.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
56
+ * const next = transform(root, visitor)
57
+ * ```
58
+ */
59
+ function composeMacros(macros) {
60
+ const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
61
+ const visitor = {};
62
+ for (const key of visitorKeys) {
63
+ if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
64
+ const callback = (node, context) => chain({
65
+ macros: ordered,
66
+ key,
67
+ node,
68
+ context
69
+ });
70
+ visitor[key] = callback;
71
+ }
72
+ return visitor;
73
+ }
74
+ /**
75
+ * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
76
+ * structural sharing, so an empty or no-op macro list returns the same reference. Pass
77
+ * `depth: 'shallow'` to rewrite the root node only.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const next = applyMacros(root, [macroIntegerToString])
82
+ * ```
83
+ *
84
+ * @example Apply to the root node only
85
+ * ```ts
86
+ * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
87
+ * ```
88
+ */
89
+ function applyMacros(root, macros, options) {
90
+ if (macros.length === 0) return root;
91
+ return transform(root, {
92
+ ...composeMacros(macros),
93
+ ...options
94
+ });
95
+ }
96
+ //#endregion
97
+ export { composeMacros as n, defineMacro as r, applyMacros as t };
98
+
99
+ //# sourceMappingURL=defineMacro-CNkUpxia.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineMacro-CNkUpxia.js","names":[],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { VisitorKey } from './defineNode.ts'\nimport { visitorKeys } from './defineNode.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,\n * and `undefined` keeps declaration order.\n */\nexport type Enforce = 'pre' | 'post'\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: Enforce): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: Enforce\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\ntype ChainProps = {\n macros: ReadonlyArray<Macro>\n key: VisitorKey\n node: Node\n context: VisitorContext\n}\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain({ macros, key, node, context }: ChainProps): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of visitorKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain({ macros: ordered, key, node, context })\n ;(visitor as Record<VisitorKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...composeMacros(macros), ...options }) as TNode\n}\n"],"mappings":";;;;;;;;AAiBA,SAAS,cAAc,SAA2B;CAChD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AAgBA,SAAS,MAAM,EAAE,QAAQ,KAAK,MAAM,WAAyC;CAC3E,IAAI,UAAU;CAEd,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,WAAW,MAAM;EACvB,IAAI,CAAC,UAAU;EACf,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,OAAO,GAAG;EAExC,MAAM,OAAO,SAAS,SAAS,OAAO;EACtC,IAAI,QAAQ,MAAM,UAAU;CAC9B;CAEA,OAAO,YAAY,OAAO,KAAA,IAAY;AACxC;;;;;;;;;;;;;AAcA,SAAgB,cAAc,QAAuC;CACnE,MAAM,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,cAAc,EAAE,OAAO,IAAI,cAAc,EAAE,OAAO,CAAC;CAE9F,MAAM,UAAmB,CAAC;CAC1B,KAAK,MAAM,OAAO,aAAa;EAC7B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM;GAAE,QAAQ;GAAS;GAAK;GAAM;EAAQ,CAAC;EACtG,QAA+C,OAAO;CACzD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAO,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,GAAG;CAAQ,CAAC;AACjE"}