@kubb/ast 5.0.0-beta.6 → 5.0.0-beta.61
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/LICENSE +17 -10
- package/README.md +51 -27
- package/dist/chunk-CNktS9qV.js +17 -0
- package/dist/defineMacro-BLIR6k-j.d.ts +475 -0
- package/dist/defineMacro-BTXvS8nI.js +106 -0
- package/dist/defineMacro-BTXvS8nI.js.map +1 -0
- package/dist/defineMacro-Bv9R_9a2.cjs +123 -0
- package/dist/defineMacro-Bv9R_9a2.cjs.map +1 -0
- package/dist/extractStringsFromNodes-Cja-xxx5.js +29 -0
- package/dist/extractStringsFromNodes-Cja-xxx5.js.map +1 -0
- package/dist/extractStringsFromNodes-DKgDjFO0.cjs +34 -0
- package/dist/extractStringsFromNodes-DKgDjFO0.cjs.map +1 -0
- package/dist/extractStringsFromNodes-p4mX1TQD.d.ts +14 -0
- package/dist/factory-CZNOGI-N.js +283 -0
- package/dist/factory-CZNOGI-N.js.map +1 -0
- package/dist/factory-DG1CVkEb.cjs +300 -0
- package/dist/factory-DG1CVkEb.cjs.map +1 -0
- package/dist/factory.cjs +29 -0
- package/dist/factory.d.ts +62 -0
- package/dist/factory.js +3 -0
- package/dist/index-BzjwdK2M.d.ts +2433 -0
- package/dist/index.cjs +444 -2180
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +94 -3408
- package/dist/index.js +395 -2101
- package/dist/index.js.map +1 -1
- package/dist/macros.cjs +117 -0
- package/dist/macros.cjs.map +1 -0
- package/dist/macros.d.ts +59 -0
- package/dist/macros.js +115 -0
- package/dist/macros.js.map +1 -0
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-KUdWiDWw.js +658 -0
- package/dist/response-KUdWiDWw.js.map +1 -0
- package/dist/response-hnSw2NKE.cjs +1027 -0
- package/dist/response-hnSw2NKE.cjs.map +1 -0
- package/dist/types-DyDzizSf.d.ts +364 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/dist/utils-BLJwyza-.cjs +912 -0
- package/dist/utils-BLJwyza-.cjs.map +1 -0
- package/dist/utils-CF_-Pn_c.js +770 -0
- package/dist/utils-CF_-Pn_c.js.map +1 -0
- package/dist/utils.cjs +36 -0
- package/dist/utils.d.ts +358 -0
- package/dist/utils.js +4 -0
- package/dist/visitor-DJ6ZEJvq.js +548 -0
- package/dist/visitor-DJ6ZEJvq.js.map +1 -0
- package/dist/visitor-DpKZ9Tk0.cjs +654 -0
- package/dist/visitor-DpKZ9Tk0.cjs.map +1 -0
- package/package.json +21 -6
- package/src/constants.ts +19 -64
- package/src/dedupe.ts +239 -0
- package/src/defineMacro.ts +132 -0
- package/src/dialect.ts +53 -0
- package/src/factory.ts +67 -678
- package/src/guards.ts +10 -92
- package/src/index.ts +13 -44
- package/src/infer.ts +16 -14
- package/src/macros/index.ts +3 -0
- package/src/macros/macroDiscriminatorEnum.ts +44 -0
- package/src/macros/macroEnumName.ts +25 -0
- package/src/macros/macroSimplifyUnion.ts +50 -0
- package/src/mocks.ts +7 -127
- package/src/node.ts +128 -0
- package/src/nodes/base.ts +5 -12
- package/src/nodes/code.ts +165 -74
- package/src/nodes/content.ts +56 -0
- package/src/nodes/file.ts +97 -36
- package/src/nodes/function.ts +216 -156
- package/src/nodes/http.ts +1 -35
- package/src/nodes/index.ts +23 -15
- package/src/nodes/input.ts +140 -0
- package/src/nodes/operation.ts +122 -68
- package/src/nodes/output.ts +23 -0
- package/src/nodes/parameter.ts +33 -3
- package/src/nodes/property.ts +36 -3
- package/src/nodes/requestBody.ts +61 -0
- package/src/nodes/response.ts +58 -13
- package/src/nodes/schema.ts +93 -17
- package/src/printer.ts +48 -42
- package/src/registry.ts +75 -0
- package/src/signature.ts +207 -0
- package/src/types.ts +8 -68
- package/src/utils/codegen.ts +104 -0
- package/src/utils/extractStringsFromNodes.ts +34 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +138 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/schemaMerge.ts +34 -0
- package/src/utils/schemaTraversal.ts +86 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +227 -289
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/nodes/root.ts +0 -64
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/utils.ts +0 -915
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import "./chunk-CNktS9qV.js";
|
|
2
|
+
import { r as transform } from "./visitor-DJ6ZEJvq.js";
|
|
3
|
+
//#region src/defineMacro.ts
|
|
4
|
+
/**
|
|
5
|
+
* Visitor callback names a macro can implement, one per traversable node kind.
|
|
6
|
+
* Mirrors the keys of {@link Visitor}.
|
|
7
|
+
*/
|
|
8
|
+
const macroKeys = [
|
|
9
|
+
"input",
|
|
10
|
+
"output",
|
|
11
|
+
"operation",
|
|
12
|
+
"schema",
|
|
13
|
+
"property",
|
|
14
|
+
"parameter",
|
|
15
|
+
"response"
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
19
|
+
* list keeps its authored order.
|
|
20
|
+
*/
|
|
21
|
+
function enforceWeight(enforce) {
|
|
22
|
+
if (enforce === "pre") return 0;
|
|
23
|
+
if (enforce === "post") return 2;
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
28
|
+
* Adds no runtime behavior.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const macroUntagged = defineMacro({
|
|
33
|
+
* name: 'untagged',
|
|
34
|
+
* operation(node) {
|
|
35
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
36
|
+
* },
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function defineMacro(macro) {
|
|
41
|
+
return macro;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
|
|
45
|
+
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
|
|
46
|
+
* original reference (structural sharing).
|
|
47
|
+
*/
|
|
48
|
+
function chain(macros, key, node, context) {
|
|
49
|
+
let current = node;
|
|
50
|
+
for (const macro of macros) {
|
|
51
|
+
const callback = macro[key];
|
|
52
|
+
if (!callback) continue;
|
|
53
|
+
if (macro.when && !macro.when(current)) continue;
|
|
54
|
+
const next = callback(current, context);
|
|
55
|
+
if (next != null) current = next;
|
|
56
|
+
}
|
|
57
|
+
return current === node ? void 0 : current;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
61
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
62
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
63
|
+
* has no names, ordering, or composition.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
68
|
+
* const next = transform(root, visitor)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
function composeMacros(macros) {
|
|
72
|
+
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
|
|
73
|
+
const visitor = {};
|
|
74
|
+
for (const key of macroKeys) {
|
|
75
|
+
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
|
|
76
|
+
const callback = (node, context) => chain(ordered, key, node, context);
|
|
77
|
+
visitor[key] = callback;
|
|
78
|
+
}
|
|
79
|
+
return visitor;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
83
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
84
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example Apply to the root node only
|
|
92
|
+
* ```ts
|
|
93
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
function applyMacros(root, macros, options) {
|
|
97
|
+
if (macros.length === 0) return root;
|
|
98
|
+
return transform(root, {
|
|
99
|
+
...composeMacros(macros),
|
|
100
|
+
depth: options?.depth
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
export { composeMacros as n, defineMacro as r, applyMacros as t };
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=defineMacro-BTXvS8nI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineMacro-BTXvS8nI.js","names":[],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Visitor callback names a macro can implement, one per traversable node kind.\n * Mirrors the keys of {@link Visitor}.\n */\nconst macroKeys = ['input', 'output', 'operation', 'schema', 'property', 'parameter', 'response'] as const\n\ntype MacroKey = (typeof macroKeys)[number]\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?: 'pre' | 'post'): 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?: 'pre' | 'post'\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\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: ReadonlyArray<Macro>, key: MacroKey, node: Node, context: VisitorContext): 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 macroKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain(ordered, key, node, context)\n ;(visitor as Record<MacroKey, 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), depth: options?.depth }) as TNode\n}\n"],"mappings":";;;;;;;AASA,MAAM,YAAY;CAAC;CAAS;CAAU;CAAa;CAAU;CAAY;CAAa;AAAU;;;;;AAQhG,SAAS,cAAc,SAAkC;CACvD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AASA,SAAS,MAAM,QAA8B,KAAe,MAAY,SAA2C;CACjH,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,WAAW;EAC3B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM,SAAS,KAAK,MAAM,OAAO;EAC1F,QAA6C,OAAO;CACvD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAO,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,OAAO,SAAS;CAAM,CAAC;AAC5E"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
require("./response-hnSw2NKE.cjs");
|
|
2
|
+
const require_visitor = require("./visitor-DpKZ9Tk0.cjs");
|
|
3
|
+
//#region src/defineMacro.ts
|
|
4
|
+
/**
|
|
5
|
+
* Visitor callback names a macro can implement, one per traversable node kind.
|
|
6
|
+
* Mirrors the keys of {@link Visitor}.
|
|
7
|
+
*/
|
|
8
|
+
const macroKeys = [
|
|
9
|
+
"input",
|
|
10
|
+
"output",
|
|
11
|
+
"operation",
|
|
12
|
+
"schema",
|
|
13
|
+
"property",
|
|
14
|
+
"parameter",
|
|
15
|
+
"response"
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
19
|
+
* list keeps its authored order.
|
|
20
|
+
*/
|
|
21
|
+
function enforceWeight(enforce) {
|
|
22
|
+
if (enforce === "pre") return 0;
|
|
23
|
+
if (enforce === "post") return 2;
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
28
|
+
* Adds no runtime behavior.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const macroUntagged = defineMacro({
|
|
33
|
+
* name: 'untagged',
|
|
34
|
+
* operation(node) {
|
|
35
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
36
|
+
* },
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function defineMacro(macro) {
|
|
41
|
+
return macro;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
|
|
45
|
+
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
|
|
46
|
+
* original reference (structural sharing).
|
|
47
|
+
*/
|
|
48
|
+
function chain(macros, key, node, context) {
|
|
49
|
+
let current = node;
|
|
50
|
+
for (const macro of macros) {
|
|
51
|
+
const callback = macro[key];
|
|
52
|
+
if (!callback) continue;
|
|
53
|
+
if (macro.when && !macro.when(current)) continue;
|
|
54
|
+
const next = callback(current, context);
|
|
55
|
+
if (next != null) current = next;
|
|
56
|
+
}
|
|
57
|
+
return current === node ? void 0 : current;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
61
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
62
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
63
|
+
* has no names, ordering, or composition.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
68
|
+
* const next = transform(root, visitor)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
function composeMacros(macros) {
|
|
72
|
+
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
|
|
73
|
+
const visitor = {};
|
|
74
|
+
for (const key of macroKeys) {
|
|
75
|
+
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
|
|
76
|
+
const callback = (node, context) => chain(ordered, key, node, context);
|
|
77
|
+
visitor[key] = callback;
|
|
78
|
+
}
|
|
79
|
+
return visitor;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
83
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
84
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example Apply to the root node only
|
|
92
|
+
* ```ts
|
|
93
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
function applyMacros(root, macros, options) {
|
|
97
|
+
if (macros.length === 0) return root;
|
|
98
|
+
return require_visitor.transform(root, {
|
|
99
|
+
...composeMacros(macros),
|
|
100
|
+
depth: options?.depth
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
Object.defineProperty(exports, "applyMacros", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
get: function() {
|
|
107
|
+
return applyMacros;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(exports, "composeMacros", {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
get: function() {
|
|
113
|
+
return composeMacros;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(exports, "defineMacro", {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
get: function() {
|
|
119
|
+
return defineMacro;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
//# sourceMappingURL=defineMacro-Bv9R_9a2.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineMacro-Bv9R_9a2.cjs","names":["transform"],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Visitor callback names a macro can implement, one per traversable node kind.\n * Mirrors the keys of {@link Visitor}.\n */\nconst macroKeys = ['input', 'output', 'operation', 'schema', 'property', 'parameter', 'response'] as const\n\ntype MacroKey = (typeof macroKeys)[number]\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?: 'pre' | 'post'): 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?: 'pre' | 'post'\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\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: ReadonlyArray<Macro>, key: MacroKey, node: Node, context: VisitorContext): 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 macroKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain(ordered, key, node, context)\n ;(visitor as Record<MacroKey, 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), depth: options?.depth }) as TNode\n}\n"],"mappings":";;;;;;;AASA,MAAM,YAAY;CAAC;CAAS;CAAU;CAAa;CAAU;CAAY;CAAa;AAAU;;;;;AAQhG,SAAS,cAAc,SAAkC;CACvD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AASA,SAAS,MAAM,QAA8B,KAAe,MAAY,SAA2C;CACjH,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,WAAW;EAC3B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM,SAAS,KAAK,MAAM,OAAO;EAC1F,QAA6C,OAAO;CACvD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAOA,gBAAAA,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,OAAO,SAAS;CAAM,CAAC;AAC5E"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import "./chunk-CNktS9qV.js";
|
|
2
|
+
//#region src/utils/extractStringsFromNodes.ts
|
|
3
|
+
/**
|
|
4
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
5
|
+
*
|
|
6
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
7
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
8
|
+
*/
|
|
9
|
+
function extractStringsFromNodes(nodes) {
|
|
10
|
+
if (!nodes?.length) return "";
|
|
11
|
+
return nodes.map((node) => {
|
|
12
|
+
if (typeof node === "string") return node;
|
|
13
|
+
if (node.kind === "Text") return node.value;
|
|
14
|
+
if (node.kind === "Break") return "";
|
|
15
|
+
if (node.kind === "Jsx") return node.value;
|
|
16
|
+
const parts = [];
|
|
17
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
18
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
19
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
20
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
21
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
22
|
+
if (nested) parts.push(nested);
|
|
23
|
+
return parts.join("\n");
|
|
24
|
+
}).filter(Boolean).join("\n");
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { extractStringsFromNodes as t };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=extractStringsFromNodes-Cja-xxx5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractStringsFromNodes-Cja-xxx5.js","names":[],"sources":["../src/utils/extractStringsFromNodes.ts"],"sourcesContent":["import type { CodeNode } from '../nodes/code.ts'\n\n/**\n * Extracts all string content from a `CodeNode` tree recursively.\n *\n * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),\n * and nested node content. Used to build the full source string for import filtering.\n */\nexport function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes?.length) return ''\n return nodes\n .map((node) => {\n // Backward-compat: compiled plugins may still pass bare strings at runtime\n if (typeof node === 'string') return node as string\n if (node.kind === 'Text') return node.value\n if (node.kind === 'Break') return ''\n if (node.kind === 'Jsx') return node.value\n\n const parts: Array<string> = []\n\n if ('params' in node && node.params) parts.push(node.params)\n if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)\n if ('returnType' in node && node.returnType) parts.push(node.returnType)\n if ('type' in node && typeof node.type === 'string') parts.push(node.type)\n\n const nested = extractStringsFromNodes(node.nodes)\n\n if (nested) parts.push(nested)\n\n return parts.join('\\n')\n })\n .filter(Boolean)\n .join('\\n')\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,wBAAwB,OAA4C;CAClF,IAAI,CAAC,OAAO,QAAQ,OAAO;CAC3B,OAAO,MACJ,KAAK,SAAS;EAEb,IAAI,OAAO,SAAS,UAAU,OAAO;EACrC,IAAI,KAAK,SAAS,QAAQ,OAAO,KAAK;EACtC,IAAI,KAAK,SAAS,SAAS,OAAO;EAClC,IAAI,KAAK,SAAS,OAAO,OAAO,KAAK;EAErC,MAAM,QAAuB,CAAC;EAE9B,IAAI,YAAY,QAAQ,KAAK,QAAQ,MAAM,KAAK,KAAK,MAAM;EAC3D,IAAI,cAAc,QAAQ,KAAK,UAAU,MAAM,KAAK,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAK,SAAS,KAAK,IAAI,IAAI,KAAK,QAAQ;EAC3H,IAAI,gBAAgB,QAAQ,KAAK,YAAY,MAAM,KAAK,KAAK,UAAU;EACvE,IAAI,UAAU,QAAQ,OAAO,KAAK,SAAS,UAAU,MAAM,KAAK,KAAK,IAAI;EAEzE,MAAM,SAAS,wBAAwB,KAAK,KAAK;EAEjD,IAAI,QAAQ,MAAM,KAAK,MAAM;EAE7B,OAAO,MAAM,KAAK,IAAI;CACxB,CAAC,CAAC,CACD,OAAO,OAAO,CAAC,CACf,KAAK,IAAI;AACd"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require("./response-hnSw2NKE.cjs");
|
|
2
|
+
//#region src/utils/extractStringsFromNodes.ts
|
|
3
|
+
/**
|
|
4
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
5
|
+
*
|
|
6
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
7
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
8
|
+
*/
|
|
9
|
+
function extractStringsFromNodes(nodes) {
|
|
10
|
+
if (!nodes?.length) return "";
|
|
11
|
+
return nodes.map((node) => {
|
|
12
|
+
if (typeof node === "string") return node;
|
|
13
|
+
if (node.kind === "Text") return node.value;
|
|
14
|
+
if (node.kind === "Break") return "";
|
|
15
|
+
if (node.kind === "Jsx") return node.value;
|
|
16
|
+
const parts = [];
|
|
17
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
18
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
19
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
20
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
21
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
22
|
+
if (nested) parts.push(nested);
|
|
23
|
+
return parts.join("\n");
|
|
24
|
+
}).filter(Boolean).join("\n");
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
Object.defineProperty(exports, "extractStringsFromNodes", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function() {
|
|
30
|
+
return extractStringsFromNodes;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=extractStringsFromNodes-DKgDjFO0.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractStringsFromNodes-DKgDjFO0.cjs","names":[],"sources":["../src/utils/extractStringsFromNodes.ts"],"sourcesContent":["import type { CodeNode } from '../nodes/code.ts'\n\n/**\n * Extracts all string content from a `CodeNode` tree recursively.\n *\n * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),\n * and nested node content. Used to build the full source string for import filtering.\n */\nexport function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes?.length) return ''\n return nodes\n .map((node) => {\n // Backward-compat: compiled plugins may still pass bare strings at runtime\n if (typeof node === 'string') return node as string\n if (node.kind === 'Text') return node.value\n if (node.kind === 'Break') return ''\n if (node.kind === 'Jsx') return node.value\n\n const parts: Array<string> = []\n\n if ('params' in node && node.params) parts.push(node.params)\n if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)\n if ('returnType' in node && node.returnType) parts.push(node.returnType)\n if ('type' in node && typeof node.type === 'string') parts.push(node.type)\n\n const nested = extractStringsFromNodes(node.nodes)\n\n if (nested) parts.push(nested)\n\n return parts.join('\\n')\n })\n .filter(Boolean)\n .join('\\n')\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,wBAAwB,OAA4C;CAClF,IAAI,CAAC,OAAO,QAAQ,OAAO;CAC3B,OAAO,MACJ,KAAK,SAAS;EAEb,IAAI,OAAO,SAAS,UAAU,OAAO;EACrC,IAAI,KAAK,SAAS,QAAQ,OAAO,KAAK;EACtC,IAAI,KAAK,SAAS,SAAS,OAAO;EAClC,IAAI,KAAK,SAAS,OAAO,OAAO,KAAK;EAErC,MAAM,QAAuB,CAAC;EAE9B,IAAI,YAAY,QAAQ,KAAK,QAAQ,MAAM,KAAK,KAAK,MAAM;EAC3D,IAAI,cAAc,QAAQ,KAAK,UAAU,MAAM,KAAK,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAK,SAAS,KAAK,IAAI,IAAI,KAAK,QAAQ;EAC3H,IAAI,gBAAgB,QAAQ,KAAK,YAAY,MAAM,KAAK,KAAK,UAAU;EACvE,IAAI,UAAU,QAAQ,OAAO,KAAK,SAAS,UAAU,MAAM,KAAK,KAAK,IAAI;EAEzE,MAAM,SAAS,wBAAwB,KAAK,KAAK;EAEjD,IAAI,QAAQ,MAAM,KAAK,MAAM;EAE7B,OAAO,MAAM,KAAK,IAAI;CACxB,CAAC,CAAC,CACD,OAAO,OAAO,CAAC,CACf,KAAK,IAAI;AACd"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { n as __name } from "./chunk-CNktS9qV.js";
|
|
2
|
+
import { Pt as CodeNode } from "./index-BzjwdK2M.js";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/extractStringsFromNodes.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
7
|
+
*
|
|
8
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
9
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
10
|
+
*/
|
|
11
|
+
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { extractStringsFromNodes as t };
|
|
14
|
+
//# sourceMappingURL=extractStringsFromNodes-p4mX1TQD.d.ts.map
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-CNktS9qV.js";
|
|
2
|
+
import { B as createFunction, D as createSource, E as createImport, H as createText, L as createArrowFunction, N as createContent, R as createBreak, T as createExport, U as createType, V as createJsx, X as createSchema, _ as createIndexedAccessType, a as createParameter, d as createRequestBody, g as createFunctionParameters, h as createFunctionParameter, l as createOperation, p as createInput, r as createProperty, s as createOutput, t as createResponse, v as createObjectBindingPattern, y as createTypeLiteral, z as createConst } from "./response-KUdWiDWw.js";
|
|
3
|
+
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-Cja-xxx5.js";
|
|
4
|
+
import { hash } from "node:crypto";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
//#region ../../internals/utils/src/fs.ts
|
|
7
|
+
/**
|
|
8
|
+
* Strips the file extension from a path or file name.
|
|
9
|
+
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* trimExtName('petStore.ts') // 'petStore'
|
|
13
|
+
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
14
|
+
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
15
|
+
* trimExtName('noExtension') // 'noExtension'
|
|
16
|
+
*/
|
|
17
|
+
function trimExtName(text) {
|
|
18
|
+
const dotIndex = text.lastIndexOf(".");
|
|
19
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
20
|
+
return text;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/utils/fileMerge.ts
|
|
24
|
+
function sourceKey(source) {
|
|
25
|
+
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
26
|
+
}
|
|
27
|
+
function pathTypeKey(path, isTypeOnly) {
|
|
28
|
+
return `${path}:${isTypeOnly ?? false}`;
|
|
29
|
+
}
|
|
30
|
+
function exportKey(path, name, isTypeOnly, asAlias) {
|
|
31
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
32
|
+
}
|
|
33
|
+
function importKey(path, name, isTypeOnly) {
|
|
34
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Computes a multi-level sort key for exports and imports:
|
|
38
|
+
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
|
|
39
|
+
*/
|
|
40
|
+
function sortKey(node) {
|
|
41
|
+
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
42
|
+
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
43
|
+
const hasName = node.name != null ? "1" : "0";
|
|
44
|
+
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
45
|
+
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
|
|
49
|
+
*
|
|
50
|
+
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
|
|
51
|
+
*/
|
|
52
|
+
function combineSources(sources) {
|
|
53
|
+
const seen = /* @__PURE__ */ new Map();
|
|
54
|
+
for (const source of sources) {
|
|
55
|
+
const key = sourceKey(source);
|
|
56
|
+
if (!seen.has(key)) seen.set(key, source);
|
|
57
|
+
}
|
|
58
|
+
return [...seen.values()];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
|
|
62
|
+
*
|
|
63
|
+
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
|
|
64
|
+
*/
|
|
65
|
+
function mergeNameArrays(existing, incoming) {
|
|
66
|
+
const merged = new Set(existing);
|
|
67
|
+
for (const name of incoming) merged.add(name);
|
|
68
|
+
return [...merged];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
72
|
+
*
|
|
73
|
+
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
74
|
+
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
|
|
75
|
+
*/
|
|
76
|
+
function combineExports(exports) {
|
|
77
|
+
const result = [];
|
|
78
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
79
|
+
const seen = /* @__PURE__ */ new Set();
|
|
80
|
+
const keyed = exports.map((node) => ({
|
|
81
|
+
node,
|
|
82
|
+
key: sortKey(node)
|
|
83
|
+
}));
|
|
84
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
85
|
+
for (const { node: curr } of keyed) {
|
|
86
|
+
const { name, path, isTypeOnly, asAlias } = curr;
|
|
87
|
+
if (Array.isArray(name)) {
|
|
88
|
+
if (!name.length) continue;
|
|
89
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
90
|
+
const existing = namedByPath.get(key);
|
|
91
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
92
|
+
else {
|
|
93
|
+
const newItem = {
|
|
94
|
+
...curr,
|
|
95
|
+
name: [...new Set(name)]
|
|
96
|
+
};
|
|
97
|
+
result.push(newItem);
|
|
98
|
+
namedByPath.set(key, newItem);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
const key = exportKey(path, name, isTypeOnly, asAlias);
|
|
102
|
+
if (!seen.has(key)) {
|
|
103
|
+
result.push(curr);
|
|
104
|
+
seen.add(key);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
|
|
112
|
+
*
|
|
113
|
+
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
114
|
+
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
115
|
+
*/
|
|
116
|
+
function combineImports(imports, exports, source) {
|
|
117
|
+
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
118
|
+
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
119
|
+
const importNameMemo = /* @__PURE__ */ new Map();
|
|
120
|
+
const canonicalizeName = (n) => {
|
|
121
|
+
if (typeof n === "string") return n;
|
|
122
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
123
|
+
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
124
|
+
return importNameMemo.get(key);
|
|
125
|
+
};
|
|
126
|
+
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
127
|
+
for (const node of imports) {
|
|
128
|
+
if (!Array.isArray(node.name)) continue;
|
|
129
|
+
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
|
|
130
|
+
}
|
|
131
|
+
const result = [];
|
|
132
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
133
|
+
const seen = /* @__PURE__ */ new Set();
|
|
134
|
+
const keyed = imports.map((node) => ({
|
|
135
|
+
node,
|
|
136
|
+
key: sortKey(node)
|
|
137
|
+
}));
|
|
138
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
139
|
+
for (const { node: curr } of keyed) {
|
|
140
|
+
if (curr.path === curr.root) continue;
|
|
141
|
+
const { path, isTypeOnly } = curr;
|
|
142
|
+
let { name } = curr;
|
|
143
|
+
if (Array.isArray(name)) {
|
|
144
|
+
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
145
|
+
if (!name.length) continue;
|
|
146
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
147
|
+
const existing = namedByPath.get(key);
|
|
148
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
149
|
+
else {
|
|
150
|
+
const newItem = {
|
|
151
|
+
...curr,
|
|
152
|
+
name
|
|
153
|
+
};
|
|
154
|
+
result.push(newItem);
|
|
155
|
+
namedByPath.set(key, newItem);
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
159
|
+
const key = importKey(path, name, isTypeOnly);
|
|
160
|
+
if (!seen.has(key)) {
|
|
161
|
+
result.push(curr);
|
|
162
|
+
seen.add(key);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/factory.ts
|
|
170
|
+
var factory_exports = /* @__PURE__ */ __exportAll({
|
|
171
|
+
createArrowFunction: () => createArrowFunction,
|
|
172
|
+
createBreak: () => createBreak,
|
|
173
|
+
createConst: () => createConst,
|
|
174
|
+
createContent: () => createContent,
|
|
175
|
+
createExport: () => createExport,
|
|
176
|
+
createFile: () => createFile,
|
|
177
|
+
createFunction: () => createFunction,
|
|
178
|
+
createFunctionParameter: () => createFunctionParameter,
|
|
179
|
+
createFunctionParameters: () => createFunctionParameters,
|
|
180
|
+
createImport: () => createImport,
|
|
181
|
+
createIndexedAccessType: () => createIndexedAccessType,
|
|
182
|
+
createInput: () => createInput,
|
|
183
|
+
createJsx: () => createJsx,
|
|
184
|
+
createObjectBindingPattern: () => createObjectBindingPattern,
|
|
185
|
+
createOperation: () => createOperation,
|
|
186
|
+
createOutput: () => createOutput,
|
|
187
|
+
createParameter: () => createParameter,
|
|
188
|
+
createProperty: () => createProperty,
|
|
189
|
+
createRequestBody: () => createRequestBody,
|
|
190
|
+
createResponse: () => createResponse,
|
|
191
|
+
createSchema: () => createSchema,
|
|
192
|
+
createSource: () => createSource,
|
|
193
|
+
createText: () => createText,
|
|
194
|
+
createType: () => createType,
|
|
195
|
+
createTypeLiteral: () => createTypeLiteral,
|
|
196
|
+
update: () => update
|
|
197
|
+
});
|
|
198
|
+
/**
|
|
199
|
+
* Identity-preserving node update: returns `node` unchanged when every field in
|
|
200
|
+
* `changes` already equals (by reference) the current value, otherwise a new node
|
|
201
|
+
* with the changes applied.
|
|
202
|
+
*
|
|
203
|
+
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
|
|
204
|
+
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
|
|
205
|
+
* downstream passes can detect "nothing changed" by identity. Comparison is
|
|
206
|
+
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* update(node, { name: node.name }) // -> same `node` reference
|
|
211
|
+
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
function update(node, changes) {
|
|
215
|
+
for (const key in changes) if (changes[key] !== node[key]) return {
|
|
216
|
+
...node,
|
|
217
|
+
...changes
|
|
218
|
+
};
|
|
219
|
+
return node;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
223
|
+
*
|
|
224
|
+
* Computes:
|
|
225
|
+
* - `id` SHA256 hash of the file path
|
|
226
|
+
* - `name` `baseName` without extension
|
|
227
|
+
* - `extname` extension extracted from `baseName`
|
|
228
|
+
*
|
|
229
|
+
* Deduplicates:
|
|
230
|
+
* - `sources` via `combineSources`
|
|
231
|
+
* - `exports` via `combineExports`
|
|
232
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
233
|
+
*
|
|
234
|
+
* @throws {Error} when `baseName` has no extension.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* const file = createFile({
|
|
239
|
+
* baseName: 'petStore.ts',
|
|
240
|
+
* path: 'src/models/petStore.ts',
|
|
241
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
242
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
243
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
244
|
+
* })
|
|
245
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
246
|
+
* // file.name = 'petStore'
|
|
247
|
+
* // file.extname = '.ts'
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
function createFile(input) {
|
|
251
|
+
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
252
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
253
|
+
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
|
|
254
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
255
|
+
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
|
|
256
|
+
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
|
|
257
|
+
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
258
|
+
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
|
|
259
|
+
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
260
|
+
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
|
|
261
|
+
if (!kept.length) return [];
|
|
262
|
+
return [kept.length === imp.name.length ? imp : {
|
|
263
|
+
...imp,
|
|
264
|
+
name: kept
|
|
265
|
+
}];
|
|
266
|
+
});
|
|
267
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
268
|
+
return {
|
|
269
|
+
kind: "File",
|
|
270
|
+
...input,
|
|
271
|
+
id: hash("sha256", input.path, "hex"),
|
|
272
|
+
name: trimExtName(input.baseName),
|
|
273
|
+
extname,
|
|
274
|
+
imports: resolvedImports,
|
|
275
|
+
exports: resolvedExports,
|
|
276
|
+
sources: resolvedSources,
|
|
277
|
+
meta: input.meta ?? {}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
//#endregion
|
|
281
|
+
export { factory_exports as n, update as r, createFile as t };
|
|
282
|
+
|
|
283
|
+
//# sourceMappingURL=factory-CZNOGI-N.js.map
|