@kubb/ast 5.0.0-beta.80 → 5.0.0-beta.81
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 +2280 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +420 -18
- package/dist/index.js +2133 -15
- package/dist/index.js.map +1 -1
- package/dist/{index-Cu2zmNxv.d.ts → types-XcYJovdT.d.ts} +717 -2
- package/dist/types.d.ts +1 -3
- package/package.json +1 -9
- package/dist/defineMacro-CqX4m8Dq.js +0 -98
- package/dist/defineMacro-CqX4m8Dq.js.map +0 -1
- package/dist/defineMacro-DmGR7vfu.cjs +0 -114
- package/dist/defineMacro-DmGR7vfu.cjs.map +0 -1
- package/dist/defineMacro-DzsACbFo.d.ts +0 -466
- package/dist/macros.cjs +0 -130
- package/dist/macros.cjs.map +0 -1
- package/dist/macros.d.ts +0 -61
- package/dist/macros.js +0 -128
- package/dist/macros.js.map +0 -1
- package/dist/refs-Bg3rmujP.cjs +0 -135
- package/dist/refs-Bg3rmujP.cjs.map +0 -1
- package/dist/refs-BjBHwunY.js +0 -101
- package/dist/refs-BjBHwunY.js.map +0 -1
- package/dist/types-CWF7DV0f.d.ts +0 -259
- package/dist/utils.cjs +0 -613
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.ts +0 -353
- package/dist/utils.js +0 -589
- package/dist/utils.js.map +0 -1
- package/dist/visitor-Dk0DNLfC.js +0 -1203
- package/dist/visitor-Dk0DNLfC.js.map +0 -1
- package/dist/visitor-h6dEM3vR.cjs +0 -1567
- package/dist/visitor-h6dEM3vR.cjs.map +0 -1
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as Dialect, n as PrinterFactoryOptions, o as SchemaDialect, r as PrinterPartial, t as Printer } from "./types-CWF7DV0f.js";
|
|
3
|
-
import { $ as ScalarSchemaType, A as SourceNode, B as ContentNode, C as ParameterNode, Ct as TypeNode, D as ExportNode, G as DatetimeSchemaNode, Ht as NodeKind, J as NumberSchemaNode, K as EnumSchemaNode, O as FileNode, Q as ScalarSchemaNode, Rt as DistributiveOmit, S as ParameterLocation, St as TextNode, U as ArraySchemaNode, W as DateSchemaNode, X as PrimitiveSchemaType, Y as ObjectSchemaNode, Z as RefSchemaNode, _t as CodeNode, a as InputMeta, at as UnionSchemaNode, bt as JSDocNode, d as HttpOperationNode, et as SchemaNode, f as OperationNode, g as StatusCode, gt as BreakNode, h as ResponseNode, ht as ArrowFunctionNode, it as TimeSchemaNode, j as UserFileNode, k as ImportNode, l as GenericOperationNode, lt as PropertyNode, mt as ParserOptions, n as OutputNode, nt as SchemaType, o as InputNode, ot as UrlSchemaNode, pt as InferSchemaNode, q as IntersectionSchemaNode, rt as StringSchemaNode, t as Node, tt as SchemaNodeByType, u as HttpMethod, vt as ConstNode, w as ParameterStyle, xt as JsxNode, y as RequestBodyNode, yt as FunctionNode, zt as NodeDef } from "./index-Cu2zmNxv.js";
|
|
1
|
+
import { $t as DistributiveOmit, A as ResponseNode, At as ArrowFunctionNode, D as OperationNode, E as HttpOperationNode, Ft as JSDocNode, G as SourceNode, H as ExportNode, It as JsxNode, K as UserFileNode, L as ParameterLocation, Lt as TextNode, Mt as CodeNode, Nt as ConstNode, Ot as InferSchemaNode, P as RequestBodyNode, Pt as FunctionNode, R as ParameterNode, Rt as TypeNode, T as HttpMethod, U as FileNode, W as ImportNode, _ as OutputNode, _t as SchemaType, a as Enforce, an as SchemaDialect, at as DateSchemaNode, b as InputMeta, bt as UnionSchemaNode, ct as IntersectionSchemaNode, d as Visitor, dt as PrimitiveSchemaType, en as NodeDef, f as VisitorContext, ft as RefSchemaNode, g as Node, gt as SchemaNodeByType, ht as SchemaNode, in as Dialect, it as ArraySchemaNode, j as StatusCode, jt as BreakNode, kt as ParserOptions, lt as NumberSchemaNode, mt as ScalarSchemaType, n as PrinterFactoryOptions, o as Macro, ot as DatetimeSchemaNode, pt as ScalarSchemaNode, r as PrinterPartial, rn as NodeKind, st as EnumSchemaNode, t as Printer, tt as ContentNode, u as ParentOf, ut as ObjectSchemaNode, vt as StringSchemaNode, w as GenericOperationNode, wt as PropertyNode, x as InputNode, xt as UrlSchemaNode, yt as TimeSchemaNode, z as ParameterStyle } from "./types-XcYJovdT.js";
|
|
4
2
|
export type { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, Dialect, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeDef, NodeKind, NumberSchemaNode, ObjectSchemaNode, OperationNode, OutputNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/ast",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.81",
|
|
4
4
|
"description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ast",
|
|
@@ -31,18 +31,10 @@
|
|
|
31
31
|
"import": "./dist/index.js",
|
|
32
32
|
"require": "./dist/index.cjs"
|
|
33
33
|
},
|
|
34
|
-
"./macros": {
|
|
35
|
-
"import": "./dist/macros.js",
|
|
36
|
-
"require": "./dist/macros.cjs"
|
|
37
|
-
},
|
|
38
34
|
"./types": {
|
|
39
35
|
"import": "./dist/types.js",
|
|
40
36
|
"require": "./dist/types.cjs"
|
|
41
37
|
},
|
|
42
|
-
"./utils": {
|
|
43
|
-
"import": "./dist/utils.js",
|
|
44
|
-
"require": "./dist/utils.cjs"
|
|
45
|
-
},
|
|
46
38
|
"./package.json": "./package.json"
|
|
47
39
|
},
|
|
48
40
|
"publishConfig": {
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import { X as visitorKeys, r as transform } from "./visitor-Dk0DNLfC.js";
|
|
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 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 transform(root, {
|
|
91
|
-
...composeMacros(macros),
|
|
92
|
-
...options
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
//#endregion
|
|
96
|
-
export { composeMacros as n, defineMacro as r, applyMacros as t };
|
|
97
|
-
|
|
98
|
-
//# sourceMappingURL=defineMacro-CqX4m8Dq.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"defineMacro-CqX4m8Dq.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 used to tell macros apart, for example `'simplify-union'`.\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"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
const require_visitor = require("./visitor-h6dEM3vR.cjs");
|
|
2
|
-
//#region src/defineMacro.ts
|
|
3
|
-
/**
|
|
4
|
-
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
5
|
-
* list keeps its authored order.
|
|
6
|
-
*/
|
|
7
|
-
function enforceWeight(enforce) {
|
|
8
|
-
if (enforce === "pre") return 0;
|
|
9
|
-
if (enforce === "post") return 2;
|
|
10
|
-
return 1;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
14
|
-
* Adds no runtime behavior.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const macroUntagged = defineMacro({
|
|
19
|
-
* name: 'untagged',
|
|
20
|
-
* operation(node) {
|
|
21
|
-
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
22
|
-
* },
|
|
23
|
-
* })
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
function defineMacro(macro) {
|
|
27
|
-
return macro;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
|
|
31
|
-
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
|
|
32
|
-
* original reference (structural sharing).
|
|
33
|
-
*/
|
|
34
|
-
function chain({ macros, key, node, context }) {
|
|
35
|
-
let current = node;
|
|
36
|
-
for (const macro of macros) {
|
|
37
|
-
const callback = macro[key];
|
|
38
|
-
if (!callback) continue;
|
|
39
|
-
if (macro.when && !macro.when(current)) continue;
|
|
40
|
-
const next = callback(current, context);
|
|
41
|
-
if (next != null) current = next;
|
|
42
|
-
}
|
|
43
|
-
return current === node ? void 0 : current;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
47
|
-
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
48
|
-
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
49
|
-
* has no names, ordering, or composition.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```ts
|
|
53
|
-
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
54
|
-
* const next = transform(root, visitor)
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
function composeMacros(macros) {
|
|
58
|
-
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
|
|
59
|
-
const visitor = {};
|
|
60
|
-
for (const key of require_visitor.visitorKeys) {
|
|
61
|
-
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
|
|
62
|
-
const callback = (node, context) => chain({
|
|
63
|
-
macros: ordered,
|
|
64
|
-
key,
|
|
65
|
-
node,
|
|
66
|
-
context
|
|
67
|
-
});
|
|
68
|
-
visitor[key] = callback;
|
|
69
|
-
}
|
|
70
|
-
return visitor;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
74
|
-
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
75
|
-
* `depth: 'shallow'` to rewrite the root node only.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```ts
|
|
79
|
-
* const next = applyMacros(root, [macroIntegerToString])
|
|
80
|
-
* ```
|
|
81
|
-
*
|
|
82
|
-
* @example Apply to the root node only
|
|
83
|
-
* ```ts
|
|
84
|
-
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
function applyMacros(root, macros, options) {
|
|
88
|
-
if (macros.length === 0) return root;
|
|
89
|
-
return require_visitor.transform(root, {
|
|
90
|
-
...composeMacros(macros),
|
|
91
|
-
...options
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
//#endregion
|
|
95
|
-
Object.defineProperty(exports, "applyMacros", {
|
|
96
|
-
enumerable: true,
|
|
97
|
-
get: function() {
|
|
98
|
-
return applyMacros;
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
Object.defineProperty(exports, "composeMacros", {
|
|
102
|
-
enumerable: true,
|
|
103
|
-
get: function() {
|
|
104
|
-
return composeMacros;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
Object.defineProperty(exports, "defineMacro", {
|
|
108
|
-
enumerable: true,
|
|
109
|
-
get: function() {
|
|
110
|
-
return defineMacro;
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
//# sourceMappingURL=defineMacro-DmGR7vfu.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"defineMacro-DmGR7vfu.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 used to tell macros apart, for example `'simplify-union'`.\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,gBAAAA,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"}
|