@kubb/ast 5.21.2 → 5.23.0
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/base-IpYuh3ZP.d.cts +173 -0
- package/dist/base-IpYuh3ZP.d.ts +173 -0
- package/dist/constants-Ckeonm-D.js +141 -0
- package/dist/constants-Ckeonm-D.js.map +1 -0
- package/dist/constants-CquR083U.cjs +152 -0
- package/dist/constants-CquR083U.cjs.map +1 -0
- package/dist/factory.cjs +23 -0
- package/dist/factory.d.cts +4 -0
- package/dist/factory.d.ts +4 -0
- package/dist/factory.js +4 -0
- package/dist/file-BVqSiSvV.d.cts +624 -0
- package/dist/file-Cg0T6cgz.cjs +319 -0
- package/dist/file-Cg0T6cgz.cjs.map +1 -0
- package/dist/file-CnuL8oUj.js +283 -0
- package/dist/file-CnuL8oUj.js.map +1 -0
- package/dist/file-CvlMAp_d.js +131 -0
- package/dist/file-CvlMAp_d.js.map +1 -0
- package/dist/file-Dd50jPkH.cjs +142 -0
- package/dist/file-Dd50jPkH.cjs.map +1 -0
- package/dist/file-xJnBQPGq.d.ts +624 -0
- package/dist/file.cjs +3 -0
- package/dist/file.d.cts +2 -0
- package/dist/file.d.ts +2 -0
- package/dist/file.js +1 -0
- package/dist/graph-BcwFCvei.js +144 -0
- package/dist/graph-BcwFCvei.js.map +1 -0
- package/dist/graph-DcIY6tQb.cjs +173 -0
- package/dist/graph-DcIY6tQb.cjs.map +1 -0
- package/dist/graph.cjs +6 -0
- package/dist/graph.d.cts +2 -0
- package/dist/graph.d.ts +2 -0
- package/dist/graph.js +1 -0
- package/dist/guards-B8qDgSaT.d.cts +23 -0
- package/dist/guards-COVJE6TU.cjs +41 -0
- package/dist/guards-COVJE6TU.cjs.map +1 -0
- package/dist/guards-D-h9R_sV.d.ts +23 -0
- package/dist/guards-DlJkKsT1.js +30 -0
- package/dist/guards-DlJkKsT1.js.map +1 -0
- package/dist/http-Bm5lhVK4.cjs +12 -0
- package/dist/http-Bm5lhVK4.cjs.map +1 -0
- package/dist/http-DwboM6pK.js +7 -0
- package/dist/http-DwboM6pK.js.map +1 -0
- package/dist/http.cjs +4 -0
- package/dist/http.d.cts +3 -0
- package/dist/http.d.ts +3 -0
- package/dist/http.js +2 -0
- package/dist/index-BWd95onW.d.cts +5 -0
- package/dist/index-Bb1kqq8H.d.cts +20 -0
- package/dist/index-BngGJux5.d.cts +12 -0
- package/dist/index-BwnMIoLl.d.ts +24 -0
- package/dist/index-C-vhkzwR.d.cts +79 -0
- package/dist/index-CAGuKv6t.d.ts +74 -0
- package/dist/index-CFH4ZC7q.d.ts +44 -0
- package/dist/index-CIqwy4EE.d.cts +74 -0
- package/dist/index-Cc2K3OzU.d.cts +11 -0
- package/dist/index-Cc2K3OzU.d.ts +11 -0
- package/dist/index-Csf8xsCc.d.cts +46 -0
- package/dist/index-D3M2spDR.d.ts +12 -0
- package/dist/index-DNiIem0x.d.ts +79 -0
- package/dist/index-DSVef1pp.d.cts +24 -0
- package/dist/index-DU_jYpBt.d.cts +44 -0
- package/dist/index-DYDha-3I.d.cts +67 -0
- package/dist/index-DaHsMJ_S.d.ts +5 -0
- package/dist/index-Dp50LM3a.d.ts +67 -0
- package/dist/index-cpjUxIik.d.ts +46 -0
- package/dist/index-eGkM4FE9.d.ts +20 -0
- package/dist/index.cjs +87 -2131
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +25 -2745
- package/dist/index.d.ts +25 -2745
- package/dist/index.js +40 -2052
- package/dist/index.js.map +1 -1
- package/dist/json-DH7_lcWm.js +74 -0
- package/dist/json-DH7_lcWm.js.map +1 -0
- package/dist/json-DURdtz9A.cjs +91 -0
- package/dist/json-DURdtz9A.cjs.map +1 -0
- package/dist/json.cjs +4 -0
- package/dist/json.d.cts +2 -0
- package/dist/json.d.ts +2 -0
- package/dist/json.js +1 -0
- package/dist/macro-D0KfNtff.js +104 -0
- package/dist/macro-D0KfNtff.js.map +1 -0
- package/dist/macro-DrTBT7fh.cjs +127 -0
- package/dist/macro-DrTBT7fh.cjs.map +1 -0
- package/dist/macro.cjs +4 -0
- package/dist/macro.d.cts +2 -0
- package/dist/macro.d.ts +2 -0
- package/dist/macro.js +1 -0
- package/dist/mediaType-CRjggmoG.js +19 -0
- package/dist/mediaType-CRjggmoG.js.map +1 -0
- package/dist/mediaType-D8uIO8sG.cjs +35 -0
- package/dist/mediaType-D8uIO8sG.cjs.map +1 -0
- package/dist/mediaType.cjs +4 -0
- package/dist/mediaType.d.cts +2 -0
- package/dist/mediaType.d.ts +2 -0
- package/dist/mediaType.js +1 -0
- package/dist/node-DjcETnc8.cjs +19 -0
- package/dist/node-DjcETnc8.cjs.map +1 -0
- package/dist/node-Djh-mZB2.js +14 -0
- package/dist/node-Djh-mZB2.js.map +1 -0
- package/dist/node.cjs +9 -0
- package/dist/node.d.cts +4 -0
- package/dist/node.d.ts +4 -0
- package/dist/node.js +5 -0
- package/dist/nodeText-DRb_rVd1.cjs +58 -0
- package/dist/nodeText-DRb_rVd1.cjs.map +1 -0
- package/dist/nodeText-DVVDZQjF.js +47 -0
- package/dist/nodeText-DVVDZQjF.js.map +1 -0
- package/dist/nodes-BB60Bq2H.cjs +320 -0
- package/dist/nodes-BB60Bq2H.cjs.map +1 -0
- package/dist/nodes-CWMa9xhW.js +231 -0
- package/dist/nodes-CWMa9xhW.js.map +1 -0
- package/dist/operation-BgRyVS7z.d.ts +1272 -0
- package/dist/operation-X7chCdL2.d.cts +1272 -0
- package/dist/optionality-D-zqzVMH.cjs +22 -0
- package/dist/optionality-D-zqzVMH.cjs.map +1 -0
- package/dist/optionality-DISn8ytP.js +17 -0
- package/dist/optionality-DISn8ytP.js.map +1 -0
- package/dist/promise-BOdlgT0N.cjs +50 -0
- package/dist/promise-BOdlgT0N.cjs.map +1 -0
- package/dist/promise-RpnEev9D.js +45 -0
- package/dist/promise-RpnEev9D.js.map +1 -0
- package/dist/ref-Bu7PoHar.js +22 -0
- package/dist/ref-Bu7PoHar.js.map +1 -0
- package/dist/ref-P0ZLT1Bg.cjs +39 -0
- package/dist/ref-P0ZLT1Bg.cjs.map +1 -0
- package/dist/ref.cjs +7 -0
- package/dist/ref.d.cts +2 -0
- package/dist/ref.d.ts +2 -0
- package/dist/ref.js +3 -0
- package/dist/refs-CYHpRlQo.cjs +131 -0
- package/dist/refs-CYHpRlQo.cjs.map +1 -0
- package/dist/refs-YCV3CznS.js +102 -0
- package/dist/refs-YCV3CznS.js.map +1 -0
- package/dist/rolldown-runtime-BV8xUvPy.cjs +52 -0
- package/dist/rolldown-runtime-BYyVTPww.js +16 -0
- package/dist/schema-2ZAQk7qE.js +44 -0
- package/dist/schema-2ZAQk7qE.js.map +1 -0
- package/dist/schema-BIErWqgc.cjs +214 -0
- package/dist/schema-BIErWqgc.cjs.map +1 -0
- package/dist/schema-DloctnMm.js +179 -0
- package/dist/schema-DloctnMm.js.map +1 -0
- package/dist/schema-hxMlgCrV.cjs +49 -0
- package/dist/schema-hxMlgCrV.cjs.map +1 -0
- package/dist/schema.cjs +16 -0
- package/dist/schema.d.cts +5 -0
- package/dist/schema.d.ts +5 -0
- package/dist/schema.js +6 -0
- package/dist/visitor-BZozlXBF.d.ts +179 -0
- package/dist/visitor-Bw_opzjF.d.cts +179 -0
- package/dist/visitor-DTrzvA3R.cjs +382 -0
- package/dist/visitor-DTrzvA3R.cjs.map +1 -0
- package/dist/visitor-NoQSUQTu.js +347 -0
- package/dist/visitor-NoQSUQTu.js.map +1 -0
- package/package.json +101 -1
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
const require_rolldown_runtime = require("./rolldown-runtime-BV8xUvPy.cjs");
|
|
2
|
+
const require_constants = require("./constants-CquR083U.cjs");
|
|
3
|
+
const require_promise = require("./promise-BOdlgT0N.cjs");
|
|
4
|
+
const require_nodeText = require("./nodeText-DRb_rVd1.cjs");
|
|
5
|
+
let node_path = require("node:path");
|
|
6
|
+
node_path = require_rolldown_runtime.__toESM(node_path, 1);
|
|
7
|
+
let node_crypto = require("node:crypto");
|
|
8
|
+
// src/utils/combineFileMembers.ts
|
|
9
|
+
/**
|
|
10
|
+
* File-member merging. `combineImports` and `combineExports` deduplicate and sort the import and
|
|
11
|
+
* export entries of one file, while `combineSources` deduplicates source entries in original order.
|
|
12
|
+
* `combineImports` also drops imports nothing references. This works on a file's members, not on
|
|
13
|
+
* schema content.
|
|
14
|
+
*/
|
|
15
|
+
const IDENTIFIER_RUN = /[\w$]+/g;
|
|
16
|
+
/**
|
|
17
|
+
* How many imports a file needs before indexing the source beats scanning it once per name.
|
|
18
|
+
*/
|
|
19
|
+
const INDEX_ABOVE_IMPORTS = 128;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the local binding an import name item introduces: the alias when there is one, else the
|
|
22
|
+
* imported name.
|
|
23
|
+
*/
|
|
24
|
+
function importLocalName(item) {
|
|
25
|
+
return typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns the specifier a member is written with: POSIX and relative to the importing file when it
|
|
29
|
+
* has a `root`, like `getRelativePath` in parser-ts. Sorting on it keeps the order the same on every OS.
|
|
30
|
+
*/
|
|
31
|
+
function specifier(node) {
|
|
32
|
+
if (!node.root) return node.path;
|
|
33
|
+
const relative = node_path.default.relative(node_path.default.dirname(node.root), node.path).replaceAll("\\", "/");
|
|
34
|
+
return relative.startsWith("../") ? relative : `./${relative}`;
|
|
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 specifier. 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}:${specifier(node)}:${hasName}:${name}`;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sorts members by {@link sortKey}. Keys are computed once per node, not per comparison.
|
|
49
|
+
*/
|
|
50
|
+
function sortMembers(nodes) {
|
|
51
|
+
return nodes.map((node) => ({
|
|
52
|
+
node,
|
|
53
|
+
key: sortKey(node)
|
|
54
|
+
})).sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0).map(({ node }) => node);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns a collector for array-named members. A member whose path and `isTypeOnly` were already
|
|
58
|
+
* seen folds its names into that entry, otherwise it joins `result` as a new entry.
|
|
59
|
+
*/
|
|
60
|
+
function mergeByPath(result) {
|
|
61
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
62
|
+
return ({ node, names }) => {
|
|
63
|
+
const key = `${node.path}:${node.isTypeOnly ?? false}`;
|
|
64
|
+
const existing = byPath.get(key);
|
|
65
|
+
if (existing) {
|
|
66
|
+
for (const name of names) {
|
|
67
|
+
if (existing.seen.has(name)) continue;
|
|
68
|
+
existing.seen.add(name);
|
|
69
|
+
existing.names.push(name);
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const item = {
|
|
74
|
+
...node,
|
|
75
|
+
name: [...names]
|
|
76
|
+
};
|
|
77
|
+
result.push(item);
|
|
78
|
+
byPath.set(key, {
|
|
79
|
+
names: item.name,
|
|
80
|
+
seen: new Set(names)
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Deduplicates `SourceNode` objects by `name + isExportable + isTypeOnly`, keeping the first of each
|
|
86
|
+
* key. An unnamed source only drops when the same node appears twice, so markdown can repeat a
|
|
87
|
+
* paragraph or a table. Returns the deduplicated array in original order.
|
|
88
|
+
*/
|
|
89
|
+
function combineSources(sources) {
|
|
90
|
+
const seen = /* @__PURE__ */ new Set();
|
|
91
|
+
return sources.filter((source) => {
|
|
92
|
+
const key = source.name != null ? `${source.name}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}` : source;
|
|
93
|
+
if (seen.has(key)) return false;
|
|
94
|
+
seen.add(key);
|
|
95
|
+
return true;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
100
|
+
*
|
|
101
|
+
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
102
|
+
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
|
|
103
|
+
*/
|
|
104
|
+
function combineExports(exports) {
|
|
105
|
+
const result = [];
|
|
106
|
+
const merge = mergeByPath(result);
|
|
107
|
+
const seen = /* @__PURE__ */ new Set();
|
|
108
|
+
for (const curr of sortMembers(exports)) {
|
|
109
|
+
const { name, path, isTypeOnly, asAlias } = curr;
|
|
110
|
+
if (Array.isArray(name)) {
|
|
111
|
+
if (name.length) merge({
|
|
112
|
+
node: curr,
|
|
113
|
+
names: [...new Set(name)]
|
|
114
|
+
});
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const key = `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
118
|
+
if (!seen.has(key)) {
|
|
119
|
+
result.push(curr);
|
|
120
|
+
seen.add(key);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
|
|
127
|
+
*
|
|
128
|
+
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
129
|
+
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
130
|
+
*/
|
|
131
|
+
function combineImports(imports, exports, source) {
|
|
132
|
+
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
133
|
+
const identifiers = source && imports.length > INDEX_ABOVE_IMPORTS ? new Set(source.match(IDENTIFIER_RUN)) : null;
|
|
134
|
+
const isUsed = (importName) => !source || identifiers?.has(importName) || source.includes(importName) || exportedNames.has(importName);
|
|
135
|
+
const importNameMemo = require_promise.memo.create();
|
|
136
|
+
const canonicalizeName = (n) => {
|
|
137
|
+
if (typeof n === "string") return n;
|
|
138
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
139
|
+
return require_promise.memo(importNameMemo, key, () => n);
|
|
140
|
+
};
|
|
141
|
+
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
142
|
+
for (const node of imports) if (Array.isArray(node.name) && node.name.some((item) => isUsed(importLocalName(item)))) pathsWithUsedNamedImport.add(node.path);
|
|
143
|
+
const result = [];
|
|
144
|
+
const merge = mergeByPath(result);
|
|
145
|
+
const seen = /* @__PURE__ */ new Set();
|
|
146
|
+
for (const curr of sortMembers(imports)) {
|
|
147
|
+
if (curr.path === curr.root) continue;
|
|
148
|
+
const { name, path, isTypeOnly } = curr;
|
|
149
|
+
if (Array.isArray(name)) {
|
|
150
|
+
const names = [...new Set(name.map(canonicalizeName))].filter((item) => isUsed(importLocalName(item)));
|
|
151
|
+
if (names.length) merge({
|
|
152
|
+
node: curr,
|
|
153
|
+
names
|
|
154
|
+
});
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (!isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
158
|
+
const key = `${path}:${name}:${isTypeOnly ?? false}`;
|
|
159
|
+
if (!seen.has(key)) {
|
|
160
|
+
result.push(curr);
|
|
161
|
+
seen.add(key);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
// src/nodes/base.ts
|
|
167
|
+
/** Builds a `createX` factory for a node whose only computed field is `kind`. */
|
|
168
|
+
function nodeFactory(kind) {
|
|
169
|
+
return (input) => {
|
|
170
|
+
return {
|
|
171
|
+
...input,
|
|
172
|
+
kind
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
// src/nodes/file.ts
|
|
177
|
+
/**
|
|
178
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
179
|
+
*
|
|
180
|
+
* @example Named import
|
|
181
|
+
* ```ts
|
|
182
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
183
|
+
* // import { useState } from 'react'
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
const createImport = nodeFactory(require_constants.nodeKinds.import);
|
|
187
|
+
/**
|
|
188
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
189
|
+
*
|
|
190
|
+
* @example Named export
|
|
191
|
+
* ```ts
|
|
192
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
193
|
+
* // export { Pet } from './Pet'
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
const createExport = nodeFactory(require_constants.nodeKinds.export);
|
|
197
|
+
/**
|
|
198
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
const createSource = nodeFactory(require_constants.nodeKinds.source);
|
|
206
|
+
/**
|
|
207
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
208
|
+
*
|
|
209
|
+
* Computes:
|
|
210
|
+
* - `id` SHA256 hash of the file path
|
|
211
|
+
* - `name` `baseName` without extension
|
|
212
|
+
* - `extname` extension extracted from `baseName`
|
|
213
|
+
*
|
|
214
|
+
* Deduplicates:
|
|
215
|
+
* - `sources` via `combineSources`
|
|
216
|
+
* - `exports` via `combineExports`
|
|
217
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
218
|
+
*
|
|
219
|
+
* @throws {Error} when `baseName` has no extension.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* const file = createFile({
|
|
224
|
+
* baseName: 'petStore.ts',
|
|
225
|
+
* path: 'src/models/petStore.ts',
|
|
226
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
227
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
228
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
229
|
+
* })
|
|
230
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
231
|
+
* // file.name = 'petStore'
|
|
232
|
+
* // file.extname = '.ts'
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* @example Copy a real file into the output
|
|
236
|
+
* ```ts
|
|
237
|
+
* const file = createFile({
|
|
238
|
+
* baseName: 'client.ts',
|
|
239
|
+
* path: 'src/gen/client.ts',
|
|
240
|
+
* copy: '/abs/path/to/templates/client.ts',
|
|
241
|
+
* })
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
function createFile(input) {
|
|
245
|
+
const extname = node_path.default.extname(input.baseName);
|
|
246
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
247
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
248
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
249
|
+
const resolvedImports = (() => {
|
|
250
|
+
if (!input.imports?.length) return [];
|
|
251
|
+
const sourceParts = [];
|
|
252
|
+
const localNames = /* @__PURE__ */ new Set();
|
|
253
|
+
for (const item of resolvedSources) {
|
|
254
|
+
const extracted = item.nodes && require_nodeText.getNodeText(item.nodes);
|
|
255
|
+
if (extracted) sourceParts.push(extracted);
|
|
256
|
+
if (item.name) localNames.add(item.name);
|
|
257
|
+
}
|
|
258
|
+
const source = sourceParts.join("\n") || void 0;
|
|
259
|
+
return combineImports(input.imports, resolvedExports, source).flatMap((imp) => {
|
|
260
|
+
if (imp.path === input.path) return [];
|
|
261
|
+
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
262
|
+
const kept = imp.name.filter((item) => !localNames.has(importLocalName(item)));
|
|
263
|
+
if (!kept.length) return [];
|
|
264
|
+
return [kept.length === imp.name.length ? imp : {
|
|
265
|
+
...imp,
|
|
266
|
+
name: kept
|
|
267
|
+
}];
|
|
268
|
+
});
|
|
269
|
+
})();
|
|
270
|
+
return {
|
|
271
|
+
...input,
|
|
272
|
+
kind: require_constants.nodeKinds.file,
|
|
273
|
+
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
|
|
274
|
+
name: node_path.default.basename(input.baseName, extname),
|
|
275
|
+
extname,
|
|
276
|
+
imports: resolvedImports,
|
|
277
|
+
exports: resolvedExports,
|
|
278
|
+
sources: resolvedSources,
|
|
279
|
+
meta: input.meta ?? {}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
Object.defineProperty(exports, "createExport", {
|
|
283
|
+
enumerable: true,
|
|
284
|
+
get: function() {
|
|
285
|
+
return createExport;
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
Object.defineProperty(exports, "createFile", {
|
|
289
|
+
enumerable: true,
|
|
290
|
+
get: function() {
|
|
291
|
+
return createFile;
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
Object.defineProperty(exports, "createImport", {
|
|
295
|
+
enumerable: true,
|
|
296
|
+
get: function() {
|
|
297
|
+
return createImport;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
Object.defineProperty(exports, "createSource", {
|
|
301
|
+
enumerable: true,
|
|
302
|
+
get: function() {
|
|
303
|
+
return createSource;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
Object.defineProperty(exports, "importLocalName", {
|
|
307
|
+
enumerable: true,
|
|
308
|
+
get: function() {
|
|
309
|
+
return importLocalName;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
Object.defineProperty(exports, "nodeFactory", {
|
|
313
|
+
enumerable: true,
|
|
314
|
+
get: function() {
|
|
315
|
+
return nodeFactory;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
//# sourceMappingURL=file-Cg0T6cgz.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-Cg0T6cgz.cjs","names":["path","memo","nodeKinds","path","getNodeText","hash"],"sources":["../src/utils/combineFileMembers.ts","../src/nodes/base.ts","../src/nodes/file.ts"],"sourcesContent":["/**\n * File-member merging. `combineImports` and `combineExports` deduplicate and sort the import and\n * export entries of one file, while `combineSources` deduplicates source entries in original order.\n * `combineImports` also drops imports nothing references. This works on a file's members, not on\n * schema content.\n */\nimport path from 'node:path'\nimport { memo } from '@internals/utils'\nimport type { ExportNode, ImportNode, SourceNode } from '../nodes/file.ts'\n\nconst IDENTIFIER_RUN = /[\\w$]+/g\n\n/**\n * How many imports a file needs before indexing the source beats scanning it once per name.\n */\nconst INDEX_ABOVE_IMPORTS = 128\n\ntype ImportNameItem = string | { propertyName: string; name?: string }\n\ntype FileMember = { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string; root?: string | null }\n\n/**\n * Returns the local binding an import name item introduces: the alias when there is one, else the\n * imported name.\n */\nexport function importLocalName(item: ImportNameItem): string {\n return typeof item === 'string' ? item : (item.name ?? item.propertyName)\n}\n\n/**\n * Returns the specifier a member is written with: POSIX and relative to the importing file when it\n * has a `root`, like `getRelativePath` in parser-ts. Sorting on it keeps the order the same on every OS.\n */\nfunction specifier(node: FileMember): string {\n if (!node.root) return node.path\n const relative = path.relative(path.dirname(node.root), node.path).replaceAll('\\\\', '/')\n return relative.startsWith('../') ? relative : `./${relative}`\n}\n\n/**\n * Computes a multi-level sort key for exports and imports:\n * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical specifier. Unnamed before named.\n */\nfunction sortKey(node: FileMember): string {\n const isArray = Array.isArray(node.name) ? '1' : '0'\n const typeOnly = node.isTypeOnly ? '0' : '1'\n const hasName = node.name != null ? '1' : '0'\n const name = Array.isArray(node.name) ? node.name.toSorted().join('\\0') : (node.name ?? '')\n return `${isArray}:${typeOnly}:${specifier(node)}:${hasName}:${name}`\n}\n\n/**\n * Sorts members by {@link sortKey}. Keys are computed once per node, not per comparison.\n */\nfunction sortMembers<TNode extends FileMember>(nodes: Array<TNode>): Array<TNode> {\n return nodes\n .map((node) => ({ node, key: sortKey(node) }))\n .sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n .map(({ node }) => node)\n}\n\n/**\n * Returns a collector for array-named members. A member whose path and `isTypeOnly` were already\n * seen folds its names into that entry, otherwise it joins `result` as a new entry.\n */\nfunction mergeByPath<TNode extends FileMember>(result: Array<TNode>): (member: { node: TNode; names: Array<unknown> }) => void {\n // Append new names instead of rebuilding the list, which went quadratic on a shared path.\n const byPath = new Map<string, { names: Array<unknown>; seen: Set<unknown> }>()\n\n return ({ node, names }) => {\n const key = `${node.path}:${node.isTypeOnly ?? false}`\n const existing = byPath.get(key)\n\n if (existing) {\n for (const name of names) {\n if (existing.seen.has(name)) continue\n existing.seen.add(name)\n existing.names.push(name)\n }\n return\n }\n\n const item = { ...node, name: [...names] }\n result.push(item)\n byPath.set(key, { names: item.name, seen: new Set(names) })\n }\n}\n\n/**\n * Deduplicates `SourceNode` objects by `name + isExportable + isTypeOnly`, keeping the first of each\n * key. An unnamed source only drops when the same node appears twice, so markdown can repeat a\n * paragraph or a table. Returns the deduplicated array in original order.\n */\nexport function combineSources(sources: Array<SourceNode>): Array<SourceNode> {\n const seen = new Set<SourceNode | string>()\n return sources.filter((source) => {\n const key = source.name != null ? `${source.name}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}` : source\n if (seen.has(key)) return false\n seen.add(key)\n return true\n })\n}\n\n/**\n * Deduplicates and merges `ExportNode` objects by path and type.\n *\n * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.\n * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.\n */\nexport function combineExports(exports: Array<ExportNode>): Array<ExportNode> {\n const result: Array<ExportNode> = []\n const merge = mergeByPath(result)\n // Deduplicates non-array exports by their exact identity\n const seen = new Set<string>()\n\n for (const curr of sortMembers(exports)) {\n const { name, path, isTypeOnly, asAlias } = curr\n\n if (Array.isArray(name)) {\n if (name.length) merge({ node: curr, names: [...new Set(name)] })\n continue\n }\n\n const key = `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n\n return result\n}\n\n/**\n * Deduplicates and merges `ImportNode` objects, filtering out unused imports.\n *\n * Retains imports that are referenced in `source` or re-exported. Imports with the same path and\n * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.\n */\nexport function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {\n // Build a lookup of all exported names to retain imports that are re-exported\n const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))\n // `createFile` re-runs on every merge into a file, so scanning the source once per import name\n // costs a heavily merged file that scan again for every fragment it already holds. Indexing pays\n // for itself there, and costs more than it saves on the small files the default output produces.\n const identifiers = source && imports.length > INDEX_ABOVE_IMPORTS ? new Set(source.match(IDENTIFIER_RUN)) : null\n\n // A name in the index is used. One that is not still might be, inside a longer identifier, so the\n // substring test stays behind it.\n const isUsed = (importName: string): boolean => !source || identifiers?.has(importName) || source.includes(importName) || exportedNames.has(importName)\n\n // Memoize object import names so the same logical (propertyName, name) pair always\n // reuses the same object reference. Set-based deduplication then works correctly.\n const importNameMemo = memo.create<string, ImportNameItem>()\n const canonicalizeName = (n: ImportNameItem): ImportNameItem => {\n if (typeof n === 'string') return n\n const key = `${n.propertyName}:${n.name ?? ''}`\n return memo(importNameMemo, key, () => n)\n }\n\n // Paths that keep at least one used named import. A default import from such a path is retained\n // even when its binding can't be found in `source` e.g. a generated `client` default import\n // alongside `import type { Client } from <same path>`, where merged grouped output omits the body.\n const pathsWithUsedNamedImport = new Set<string>()\n for (const node of imports) {\n if (Array.isArray(node.name) && node.name.some((item) => isUsed(importLocalName(item)))) {\n pathsWithUsedNamedImport.add(node.path)\n }\n }\n\n const result: Array<ImportNode> = []\n const merge = mergeByPath(result)\n // Deduplicates non-array imports by their exact identity\n const seen = new Set<string>()\n\n for (const curr of sortMembers(imports)) {\n if (curr.path === curr.root) continue\n\n const { name, path, isTypeOnly } = curr\n\n if (Array.isArray(name)) {\n const names = [...new Set(name.map(canonicalizeName))].filter((item) => isUsed(importLocalName(item)))\n if (names.length) merge({ node: curr, names })\n continue\n }\n\n if (!isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue\n\n const key = `${path}:${name}:${isTypeOnly ?? false}`\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n\n return result\n}\n","import type { ValueOf } from '@internals/utils'\nimport type { nodeKinds } from '../constants.ts'\n\n/**\n * `kind` values used by AST nodes.\n *\n * @example\n * ```ts\n * const kind: NodeKind = 'Schema'\n * ```\n */\nexport type NodeKind = ValueOf<typeof nodeKinds>\n\n/**\n * Base shape shared by all AST nodes.\n *\n * @example\n * ```ts\n * const base: BaseNode = { kind: 'Input' }\n * ```\n */\nexport type BaseNode = {\n /**\n * Node discriminator.\n */\n kind: NodeKind\n /**\n * Where the node comes from in the input, in the form the adapter's `locate` reads. Document\n * adapters such as OpenAPI and AsyncAPI use an RFC 6901 JSON pointer, for example\n * `#/components/schemas/Pet`. Plugins can copy it to the code nodes they emit.\n */\n pointer?: string\n}\n\n/** `Omit` applied to each member of a union, so the union survives. */\nexport type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never\n\n/** Builds a `createX` factory for a node whose only computed field is `kind`. */\nexport function nodeFactory<T extends BaseNode>(kind: T['kind']): (input: Omit<T, 'kind'>) => T {\n return (input) => {\n const node = { ...input, kind }\n return node as T\n }\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { nodeKinds } from '../constants.ts'\nimport { combineExports, combineImports, combineSources, importLocalName } from '../utils/combineFileMembers.ts'\nimport { getNodeText } from '../utils/nodeText.ts'\nimport { type BaseNode, nodeFactory } from './base.ts'\nimport type { CodeNode } from './code.ts'\n\n/**\n * Supported file extensions.\n */\ntype Extname = '.ts' | '.js' | '.tsx' | '.json' | `.${string}`\n\ntype ImportName = string | Array<string | { propertyName: string; name?: string }>\n\n/**\n * Represents a language-agnostic import/dependency declaration.\n *\n * @example Named import (TypeScript: `import { useState } from 'react'`)\n * ```ts\n * createImport({ name: ['useState'], path: 'react' })\n * ```\n *\n * @example Default import (TypeScript: `import React from 'react'`)\n * ```ts\n * createImport({ name: 'React', path: 'react' })\n * ```\n *\n * @example Type-only import (TypeScript: `import type { FC } from 'react'`)\n * ```ts\n * createImport({ name: ['FC'], path: 'react', isTypeOnly: true })\n * ```\n *\n * @example Namespace import (TypeScript: `import * as React from 'react'`)\n * ```ts\n * createImport({ name: 'React', path: 'react', isNameSpace: true })\n * ```\n */\nexport type ImportNode = BaseNode & {\n kind: 'Import'\n /**\n * Import name(s) to be used.\n *\n * @example Named imports\n * `['useState']`\n *\n * @example Default import\n * `'React'`\n */\n name: ImportName\n /**\n * Path for the import.\n *\n * @example\n * `'@kubb/core'`\n */\n path: string\n /**\n * Add a type-only import prefix.\n * - `true` generates `import type { Type } from './path'`\n * - `false` generates `import { Type } from './path'`\n */\n isTypeOnly?: boolean | null\n /**\n * Import the entire module as a namespace.\n * - `true` generates `import * as Name from './path'`\n * - `false` generates a standard import\n */\n isNameSpace?: boolean | null\n /**\n * When set, the import path is resolved relative to this root.\n */\n root?: string | null\n}\n\n/**\n * Represents a language-agnostic export/public API declaration.\n *\n * @example Named export (TypeScript: `export { Pets } from './Pets'`)\n * ```ts\n * createExport({ name: ['Pets'], path: './Pets' })\n * ```\n *\n * @example Type-only export (TypeScript: `export type { Pet } from './Pet'`)\n * ```ts\n * createExport({ name: ['Pet'], path: './Pet', isTypeOnly: true })\n * ```\n *\n * @example Wildcard export (TypeScript: `export * from './utils'`)\n * ```ts\n * createExport({ path: './utils' })\n * ```\n *\n * @example Namespace alias (TypeScript: `export * as utils from './utils'`)\n * ```ts\n * createExport({ name: 'utils', path: './utils', asAlias: true })\n * ```\n */\nexport type ExportNode = BaseNode & {\n kind: 'Export'\n /**\n * Export name(s) to be used. When omitted, generates a wildcard export.\n *\n * @example Named exports\n * `['useState']`\n *\n * @example Single export\n * `'React'`\n */\n name?: string | Array<string> | null\n /**\n * Path for the export.\n *\n * @example\n * `'@kubb/core'`\n */\n path: string\n /**\n * Add a type-only export prefix.\n * - `true` generates `export type { Type } from './path'`\n * - `false` generates `export { Type } from './path'`\n */\n isTypeOnly?: boolean | null\n /**\n * Export as an aliased namespace.\n * - `true` generates `export * as aliasName from './path'`\n * - `false` generates a standard export\n */\n asAlias?: boolean | null\n}\n\n/**\n * Represents a fragment of source code within a file.\n *\n * @example Named exportable source\n * ```ts\n * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true, isIndexable: true })\n * ```\n *\n * @example Inline unnamed code block\n * ```ts\n * createSource({ nodes: [createText('const x = 1')] })\n * ```\n */\nexport type SourceNode = BaseNode & {\n kind: 'Source'\n /**\n * Optional name identifying this source (used for deduplication and barrel generation).\n */\n name?: string | null\n /**\n * Mark this source as a type-only export.\n */\n isTypeOnly?: boolean | null\n /**\n * Mark this source as an exported declaration. The source text carries its own `export` keyword.\n */\n isExportable?: boolean | null\n /**\n * Include this source in barrel/index file generation.\n */\n isIndexable?: boolean | null\n /**\n * Marks the source as a definition that importing files absorb instead of import. `expression` is\n * the declaration's right-hand side, such as `{ body: Pet }`.\n */\n inline?: { expression: string } | null\n /**\n * Child nodes that make up this source fragment, in DOM order.\n * Use a {@link TextNode} for raw string content.\n */\n nodes?: Array<CodeNode>\n}\n\n/**\n * Represents a fully resolved file in the AST.\n *\n * Created via `createFile()`, which computes the `id`, `name`, and `extname` from the input\n * and deduplicates `imports`, `exports`, and `sources`.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of the path\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport type FileNode<TMeta extends object = object> = BaseNode & {\n kind: 'File'\n /**\n * Unique identifier derived from a SHA256 hash of the file path. `createFile`\n * computes it, so callers do not need to provide it.\n */\n id: string\n /**\n * File name without extension, derived from `baseName`.\n *\n * @see https://nodejs.org/api/path.html#pathformatpathobject\n */\n name: string\n /**\n * File base name, including extension, shaped like `${name}${extname}`.\n *\n * @see https://nodejs.org/api/path.html#pathbasenamepath-suffix\n */\n baseName: `${string}.${string}`\n /**\n * Full qualified path to the file.\n */\n path: string\n /**\n * File extension extracted from `baseName`.\n */\n extname: Extname\n /**\n * Deduplicated list of source code fragments.\n */\n sources: Array<SourceNode>\n /**\n * Deduplicated list of import declarations.\n */\n imports: Array<ImportNode>\n /**\n * Deduplicated list of export declarations.\n */\n exports: Array<ExportNode>\n /**\n * Optional metadata attached to this file, read by plugins during barrel generation.\n */\n meta?: TMeta\n /**\n * Optional banner prepended to the generated file content.\n * Accepts `null` so `resolver.default.banner()` results can be passed directly.\n */\n banner?: string | null\n /**\n * Optional footer appended to the generated file content.\n * Accepts `null` so `resolver.default.footer()` results can be passed directly.\n */\n footer?: string | null\n /**\n * Absolute on-disk path to copy into the output, through the parser's `copy` hook when it has one.\n *\n * Use to emit a real source file shipped inside a package (a template) into the generated\n * folder. When set, `copy` provides the file content and any `sources` nodes are ignored for\n * output; `sources` may still carry `name`/`isExportable`/`isIndexable` so barrel generation\n * treats the file the same as a rendered one.\n */\n copy?: string | null\n}\n\n/**\n * Creates an `ImportNode` representing a language-agnostic import/dependency declaration.\n *\n * @example Named import\n * ```ts\n * createImport({ name: ['useState'], path: 'react' })\n * // import { useState } from 'react'\n * ```\n */\nexport const createImport = nodeFactory<ImportNode>(nodeKinds.import)\n\n/**\n * Creates an `ExportNode` representing a language-agnostic export/public API declaration.\n *\n * @example Named export\n * ```ts\n * createExport({ name: ['Pet'], path: './Pet' })\n * // export { Pet } from './Pet'\n * ```\n */\nexport const createExport = nodeFactory<ExportNode>(nodeKinds.export)\n\n/**\n * Creates a `SourceNode` representing a fragment of source code within a file.\n *\n * @example\n * ```ts\n * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })\n * ```\n */\nexport const createSource = nodeFactory<SourceNode>(nodeKinds.source)\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n *\n * @example Copy a real file into the output\n * ```ts\n * const file = createFile({\n * baseName: 'client.ts',\n * path: 'src/gen/client.ts',\n * copy: '/abs/path/to/templates/client.ts',\n * })\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const extname = path.extname(input.baseName) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n // Skip building the source text and local-name set when there are no imports to resolve against them.\n const resolvedImports: Array<ImportNode> = (() => {\n if (!input.imports?.length) return []\n\n const sourceParts: Array<string> = []\n const localNames = new Set<string>()\n // Only the sources the file keeps count: an import used just by a dropped duplicate is unused.\n for (const item of resolvedSources) {\n const extracted = item.nodes && getNodeText(item.nodes)\n if (extracted) sourceParts.push(extracted)\n if (item.name) localNames.add(item.name)\n }\n const source = sourceParts.join('\\n') || undefined\n const combinedImports = combineImports(input.imports, resolvedExports, source)\n // Consolidating output (`mode: 'file'`) can put a symbol's definition and an import of it in the\n // same file: by path when the import still targets this file, by name when consolidation moved it.\n return combinedImports.flatMap((imp) => {\n if (imp.path === input.path) return []\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(importLocalName(item)))\n\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n })()\n\n return {\n ...input,\n kind: nodeKinds.file,\n id: hash('sha256', input.path, 'hex'),\n name: path.basename(input.baseName, extname),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: (input.meta ?? {}) as TMeta,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAUA,MAAM,iBAAiB;;;;AAKvB,MAAM,sBAAsB;;;;;AAU5B,SAAgB,gBAAgB,MAA8B;CAC5D,OAAO,OAAO,SAAS,WAAW,OAAQ,KAAK,QAAQ,KAAK;AAC9D;;;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AAW,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG;GAE7E,MAAM,OAAO,IAAI,KAAK,QAAQ,SAAS,CAAC,WAAW,IAAI,gBAAgB,IAAI,CAAC,CAAC;GAE7E,IAAI,CAAC,KAAK,QAAQ,OAAO,CAAC;GAC1B,OAAO,CAAC,KAAK,WAAW,IAAI,KAAK,SAAS,MAAM;IAAE,GAAG;IAAK,MAAM;GAAK,CAAC;EACxE,CAAC;CACH,EAAA,CAAG;CAEH,OAAO;EACL,GAAG;EACH,MAAMF,kBAAAA,UAAU;EAChB,KAAA,GAAIG,YAAAA,KAAAA,CAAK,UAAU,MAAM,MAAM,KAAK;EACpC,MAAMF,UAAAA,QAAK,SAAS,MAAM,UAAU,OAAO;EAC3C;EACA,SAAS;EACT,SAAS;EACT,SAAS;EACT,MAAO,MAAM,QAAQ,CAAC;CACxB;AACF"}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import "./rolldown-runtime-BYyVTPww.js";
|
|
2
|
+
import { t as nodeKinds } from "./constants-Ckeonm-D.js";
|
|
3
|
+
import { t as memo } from "./promise-RpnEev9D.js";
|
|
4
|
+
import { n as getNodeText } from "./nodeText-DVVDZQjF.js";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { hash } from "node:crypto";
|
|
7
|
+
// src/utils/combineFileMembers.ts
|
|
8
|
+
/**
|
|
9
|
+
* File-member merging. `combineImports` and `combineExports` deduplicate and sort the import and
|
|
10
|
+
* export entries of one file, while `combineSources` deduplicates source entries in original order.
|
|
11
|
+
* `combineImports` also drops imports nothing references. This works on a file's members, not on
|
|
12
|
+
* schema content.
|
|
13
|
+
*/
|
|
14
|
+
const IDENTIFIER_RUN = /[\w$]+/g;
|
|
15
|
+
/**
|
|
16
|
+
* How many imports a file needs before indexing the source beats scanning it once per name.
|
|
17
|
+
*/
|
|
18
|
+
const INDEX_ABOVE_IMPORTS = 128;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the local binding an import name item introduces: the alias when there is one, else the
|
|
21
|
+
* imported name.
|
|
22
|
+
*/
|
|
23
|
+
function importLocalName(item) {
|
|
24
|
+
return typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns the specifier a member is written with: POSIX and relative to the importing file when it
|
|
28
|
+
* has a `root`, like `getRelativePath` in parser-ts. Sorting on it keeps the order the same on every OS.
|
|
29
|
+
*/
|
|
30
|
+
function specifier(node) {
|
|
31
|
+
if (!node.root) return node.path;
|
|
32
|
+
const relative = path.relative(path.dirname(node.root), node.path).replaceAll("\\", "/");
|
|
33
|
+
return relative.startsWith("../") ? relative : `./${relative}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Computes a multi-level sort key for exports and imports:
|
|
37
|
+
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical specifier. Unnamed before named.
|
|
38
|
+
*/
|
|
39
|
+
function sortKey(node) {
|
|
40
|
+
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
41
|
+
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
42
|
+
const hasName = node.name != null ? "1" : "0";
|
|
43
|
+
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
44
|
+
return `${isArray}:${typeOnly}:${specifier(node)}:${hasName}:${name}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Sorts members by {@link sortKey}. Keys are computed once per node, not per comparison.
|
|
48
|
+
*/
|
|
49
|
+
function sortMembers(nodes) {
|
|
50
|
+
return nodes.map((node) => ({
|
|
51
|
+
node,
|
|
52
|
+
key: sortKey(node)
|
|
53
|
+
})).sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0).map(({ node }) => node);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns a collector for array-named members. A member whose path and `isTypeOnly` were already
|
|
57
|
+
* seen folds its names into that entry, otherwise it joins `result` as a new entry.
|
|
58
|
+
*/
|
|
59
|
+
function mergeByPath(result) {
|
|
60
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
61
|
+
return ({ node, names }) => {
|
|
62
|
+
const key = `${node.path}:${node.isTypeOnly ?? false}`;
|
|
63
|
+
const existing = byPath.get(key);
|
|
64
|
+
if (existing) {
|
|
65
|
+
for (const name of names) {
|
|
66
|
+
if (existing.seen.has(name)) continue;
|
|
67
|
+
existing.seen.add(name);
|
|
68
|
+
existing.names.push(name);
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const item = {
|
|
73
|
+
...node,
|
|
74
|
+
name: [...names]
|
|
75
|
+
};
|
|
76
|
+
result.push(item);
|
|
77
|
+
byPath.set(key, {
|
|
78
|
+
names: item.name,
|
|
79
|
+
seen: new Set(names)
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Deduplicates `SourceNode` objects by `name + isExportable + isTypeOnly`, keeping the first of each
|
|
85
|
+
* key. An unnamed source only drops when the same node appears twice, so markdown can repeat a
|
|
86
|
+
* paragraph or a table. Returns the deduplicated array in original order.
|
|
87
|
+
*/
|
|
88
|
+
function combineSources(sources) {
|
|
89
|
+
const seen = /* @__PURE__ */ new Set();
|
|
90
|
+
return sources.filter((source) => {
|
|
91
|
+
const key = source.name != null ? `${source.name}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}` : source;
|
|
92
|
+
if (seen.has(key)) return false;
|
|
93
|
+
seen.add(key);
|
|
94
|
+
return true;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
99
|
+
*
|
|
100
|
+
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
101
|
+
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
|
|
102
|
+
*/
|
|
103
|
+
function combineExports(exports) {
|
|
104
|
+
const result = [];
|
|
105
|
+
const merge = mergeByPath(result);
|
|
106
|
+
const seen = /* @__PURE__ */ new Set();
|
|
107
|
+
for (const curr of sortMembers(exports)) {
|
|
108
|
+
const { name, path, isTypeOnly, asAlias } = curr;
|
|
109
|
+
if (Array.isArray(name)) {
|
|
110
|
+
if (name.length) merge({
|
|
111
|
+
node: curr,
|
|
112
|
+
names: [...new Set(name)]
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const key = `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
117
|
+
if (!seen.has(key)) {
|
|
118
|
+
result.push(curr);
|
|
119
|
+
seen.add(key);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
|
|
126
|
+
*
|
|
127
|
+
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
128
|
+
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
129
|
+
*/
|
|
130
|
+
function combineImports(imports, exports, source) {
|
|
131
|
+
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
132
|
+
const identifiers = source && imports.length > INDEX_ABOVE_IMPORTS ? new Set(source.match(IDENTIFIER_RUN)) : null;
|
|
133
|
+
const isUsed = (importName) => !source || identifiers?.has(importName) || source.includes(importName) || exportedNames.has(importName);
|
|
134
|
+
const importNameMemo = memo.create();
|
|
135
|
+
const canonicalizeName = (n) => {
|
|
136
|
+
if (typeof n === "string") return n;
|
|
137
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
138
|
+
return memo(importNameMemo, key, () => n);
|
|
139
|
+
};
|
|
140
|
+
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
141
|
+
for (const node of imports) if (Array.isArray(node.name) && node.name.some((item) => isUsed(importLocalName(item)))) pathsWithUsedNamedImport.add(node.path);
|
|
142
|
+
const result = [];
|
|
143
|
+
const merge = mergeByPath(result);
|
|
144
|
+
const seen = /* @__PURE__ */ new Set();
|
|
145
|
+
for (const curr of sortMembers(imports)) {
|
|
146
|
+
if (curr.path === curr.root) continue;
|
|
147
|
+
const { name, path, isTypeOnly } = curr;
|
|
148
|
+
if (Array.isArray(name)) {
|
|
149
|
+
const names = [...new Set(name.map(canonicalizeName))].filter((item) => isUsed(importLocalName(item)));
|
|
150
|
+
if (names.length) merge({
|
|
151
|
+
node: curr,
|
|
152
|
+
names
|
|
153
|
+
});
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (!isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
157
|
+
const key = `${path}:${name}:${isTypeOnly ?? false}`;
|
|
158
|
+
if (!seen.has(key)) {
|
|
159
|
+
result.push(curr);
|
|
160
|
+
seen.add(key);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
// src/nodes/base.ts
|
|
166
|
+
/** Builds a `createX` factory for a node whose only computed field is `kind`. */
|
|
167
|
+
function nodeFactory(kind) {
|
|
168
|
+
return (input) => {
|
|
169
|
+
return {
|
|
170
|
+
...input,
|
|
171
|
+
kind
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
// src/nodes/file.ts
|
|
176
|
+
/**
|
|
177
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
178
|
+
*
|
|
179
|
+
* @example Named import
|
|
180
|
+
* ```ts
|
|
181
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
182
|
+
* // import { useState } from 'react'
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
const createImport = nodeFactory(nodeKinds.import);
|
|
186
|
+
/**
|
|
187
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
188
|
+
*
|
|
189
|
+
* @example Named export
|
|
190
|
+
* ```ts
|
|
191
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
192
|
+
* // export { Pet } from './Pet'
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
const createExport = nodeFactory(nodeKinds.export);
|
|
196
|
+
/**
|
|
197
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
const createSource = nodeFactory(nodeKinds.source);
|
|
205
|
+
/**
|
|
206
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
207
|
+
*
|
|
208
|
+
* Computes:
|
|
209
|
+
* - `id` SHA256 hash of the file path
|
|
210
|
+
* - `name` `baseName` without extension
|
|
211
|
+
* - `extname` extension extracted from `baseName`
|
|
212
|
+
*
|
|
213
|
+
* Deduplicates:
|
|
214
|
+
* - `sources` via `combineSources`
|
|
215
|
+
* - `exports` via `combineExports`
|
|
216
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
217
|
+
*
|
|
218
|
+
* @throws {Error} when `baseName` has no extension.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* const file = createFile({
|
|
223
|
+
* baseName: 'petStore.ts',
|
|
224
|
+
* path: 'src/models/petStore.ts',
|
|
225
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
226
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
227
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
228
|
+
* })
|
|
229
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
230
|
+
* // file.name = 'petStore'
|
|
231
|
+
* // file.extname = '.ts'
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @example Copy a real file into the output
|
|
235
|
+
* ```ts
|
|
236
|
+
* const file = createFile({
|
|
237
|
+
* baseName: 'client.ts',
|
|
238
|
+
* path: 'src/gen/client.ts',
|
|
239
|
+
* copy: '/abs/path/to/templates/client.ts',
|
|
240
|
+
* })
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
function createFile(input) {
|
|
244
|
+
const extname = path.extname(input.baseName);
|
|
245
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
246
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
247
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
248
|
+
const resolvedImports = (() => {
|
|
249
|
+
if (!input.imports?.length) return [];
|
|
250
|
+
const sourceParts = [];
|
|
251
|
+
const localNames = /* @__PURE__ */ new Set();
|
|
252
|
+
for (const item of resolvedSources) {
|
|
253
|
+
const extracted = item.nodes && getNodeText(item.nodes);
|
|
254
|
+
if (extracted) sourceParts.push(extracted);
|
|
255
|
+
if (item.name) localNames.add(item.name);
|
|
256
|
+
}
|
|
257
|
+
const source = sourceParts.join("\n") || void 0;
|
|
258
|
+
return combineImports(input.imports, resolvedExports, source).flatMap((imp) => {
|
|
259
|
+
if (imp.path === input.path) return [];
|
|
260
|
+
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
261
|
+
const kept = imp.name.filter((item) => !localNames.has(importLocalName(item)));
|
|
262
|
+
if (!kept.length) return [];
|
|
263
|
+
return [kept.length === imp.name.length ? imp : {
|
|
264
|
+
...imp,
|
|
265
|
+
name: kept
|
|
266
|
+
}];
|
|
267
|
+
});
|
|
268
|
+
})();
|
|
269
|
+
return {
|
|
270
|
+
...input,
|
|
271
|
+
kind: nodeKinds.file,
|
|
272
|
+
id: hash("sha256", input.path, "hex"),
|
|
273
|
+
name: path.basename(input.baseName, extname),
|
|
274
|
+
extname,
|
|
275
|
+
imports: resolvedImports,
|
|
276
|
+
exports: resolvedExports,
|
|
277
|
+
sources: resolvedSources,
|
|
278
|
+
meta: input.meta ?? {}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
export { nodeFactory as a, createSource as i, createFile as n, importLocalName as o, createImport as r, createExport as t };
|
|
282
|
+
|
|
283
|
+
//# sourceMappingURL=file-CnuL8oUj.js.map
|