@kubb/ast 5.0.0-alpha.32 → 5.0.0-alpha.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +91 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -17
- package/dist/index.js +88 -12
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/{visitor-DysNCWvh.d.ts → visitor-CJMIoAE3.d.ts} +132 -33
- package/package.json +1 -1
- package/src/constants.ts +2 -0
- package/src/factory.ts +63 -9
- package/src/index.ts +4 -3
- package/src/nodes/base.ts +3 -0
- package/src/nodes/code.ts +81 -14
- package/src/nodes/file.ts +5 -10
- package/src/nodes/index.ts +1 -1
- package/src/types.ts +3 -0
- package/src/utils.ts +33 -2
package/dist/index.cjs
CHANGED
|
@@ -44,7 +44,9 @@ const nodeKinds = {
|
|
|
44
44
|
file: "File",
|
|
45
45
|
import: "Import",
|
|
46
46
|
export: "Export",
|
|
47
|
-
source: "Source"
|
|
47
|
+
source: "Source",
|
|
48
|
+
text: "Text",
|
|
49
|
+
break: "Break"
|
|
48
50
|
};
|
|
49
51
|
/**
|
|
50
52
|
* Canonical schema type strings used by AST schema nodes.
|
|
@@ -616,7 +618,7 @@ function toStructType({ node, params, resolver }) {
|
|
|
616
618
|
});
|
|
617
619
|
}
|
|
618
620
|
function sourceKey(source) {
|
|
619
|
-
return `${source.name ?? source.
|
|
621
|
+
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
620
622
|
}
|
|
621
623
|
function pathTypeKey(path, isTypeOnly) {
|
|
622
624
|
return `${path}:${isTypeOnly ?? false}`;
|
|
@@ -641,7 +643,7 @@ function sortKey(node) {
|
|
|
641
643
|
/**
|
|
642
644
|
* Deduplicates an array of `SourceNode` objects.
|
|
643
645
|
* Named sources are deduplicated by `name + isExportable + isTypeOnly`.
|
|
644
|
-
* Unnamed sources are deduplicated by
|
|
646
|
+
* Unnamed sources are deduplicated by object reference.
|
|
645
647
|
*/
|
|
646
648
|
function combineSources(sources) {
|
|
647
649
|
const seen = /* @__PURE__ */ new Map();
|
|
@@ -732,6 +734,30 @@ function combineImports(imports, exports, source) {
|
|
|
732
734
|
}
|
|
733
735
|
return result;
|
|
734
736
|
}
|
|
737
|
+
/**
|
|
738
|
+
* Recursively extracts all string content embedded in a {@link CodeNode} tree.
|
|
739
|
+
*
|
|
740
|
+
* Includes text node values, and string attribute fields (`params`, `generics`,
|
|
741
|
+
* `returnType`, `type`) that may reference identifiers needing imports.
|
|
742
|
+
* Used by `createFile` to build the full source string for import filtering.
|
|
743
|
+
*/
|
|
744
|
+
function extractStringsFromNodes(nodes) {
|
|
745
|
+
if (!nodes?.length) return "";
|
|
746
|
+
return nodes.map((node) => {
|
|
747
|
+
if (typeof node === "string") return node;
|
|
748
|
+
if (node.kind === "Text") return node.value;
|
|
749
|
+
if (node.kind === "Break") return "";
|
|
750
|
+
if (node.kind === "Jsx") return node.value;
|
|
751
|
+
const parts = [];
|
|
752
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
753
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
754
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
755
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
756
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
757
|
+
if (nested) parts.push(nested);
|
|
758
|
+
return parts.join("\n");
|
|
759
|
+
}).filter(Boolean).join("\n");
|
|
760
|
+
}
|
|
735
761
|
//#endregion
|
|
736
762
|
//#region src/factory.ts
|
|
737
763
|
/**
|
|
@@ -1113,7 +1139,7 @@ function createExport(props) {
|
|
|
1113
1139
|
*
|
|
1114
1140
|
* @example
|
|
1115
1141
|
* ```ts
|
|
1116
|
-
* createSource({ name: 'Pet',
|
|
1142
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1117
1143
|
* ```
|
|
1118
1144
|
*/
|
|
1119
1145
|
function createSource(props) {
|
|
@@ -1142,7 +1168,7 @@ function createSource(props) {
|
|
|
1142
1168
|
* const file = createFile({
|
|
1143
1169
|
* baseName: 'petStore.ts',
|
|
1144
1170
|
* path: 'src/models/petStore.ts',
|
|
1145
|
-
* sources: [createSource({ name: 'Pet',
|
|
1171
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
1146
1172
|
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
1147
1173
|
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1148
1174
|
* })
|
|
@@ -1154,9 +1180,9 @@ function createSource(props) {
|
|
|
1154
1180
|
function createFile(input) {
|
|
1155
1181
|
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
1156
1182
|
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
1157
|
-
const source = (input.sources ?? []).
|
|
1183
|
+
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
|
|
1158
1184
|
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
1159
|
-
const resolvedImports = input.imports?.length
|
|
1185
|
+
const resolvedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
|
|
1160
1186
|
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
1161
1187
|
return {
|
|
1162
1188
|
kind: "File",
|
|
@@ -1173,7 +1199,7 @@ function createFile(input) {
|
|
|
1173
1199
|
/**
|
|
1174
1200
|
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
1175
1201
|
*
|
|
1176
|
-
* Mirrors the `Const` component from `@kubb/
|
|
1202
|
+
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
1177
1203
|
* The component's `children` are represented as `nodes`.
|
|
1178
1204
|
*
|
|
1179
1205
|
* @example Simple constant
|
|
@@ -1207,7 +1233,7 @@ function createConst(props) {
|
|
|
1207
1233
|
/**
|
|
1208
1234
|
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
1209
1235
|
*
|
|
1210
|
-
* Mirrors the `Type` component from `@kubb/
|
|
1236
|
+
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
1211
1237
|
* The component's `children` are represented as `nodes`.
|
|
1212
1238
|
*
|
|
1213
1239
|
* @example Simple type alias
|
|
@@ -1235,7 +1261,7 @@ function createType(props) {
|
|
|
1235
1261
|
/**
|
|
1236
1262
|
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
1237
1263
|
*
|
|
1238
|
-
* Mirrors the `Function` component from `@kubb/
|
|
1264
|
+
* Mirrors the `Function` component from `@kubb/renderer-jsx`.
|
|
1239
1265
|
* The component's `children` are represented as `nodes`.
|
|
1240
1266
|
*
|
|
1241
1267
|
* @example Simple function
|
|
@@ -1271,7 +1297,7 @@ function createFunction(props) {
|
|
|
1271
1297
|
/**
|
|
1272
1298
|
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
1273
1299
|
*
|
|
1274
|
-
* Mirrors the `Function.Arrow` component from `@kubb/
|
|
1300
|
+
* Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
1275
1301
|
* The component's `children` are represented as `nodes`.
|
|
1276
1302
|
*
|
|
1277
1303
|
* @example Simple arrow function
|
|
@@ -1305,6 +1331,56 @@ function createArrowFunction(props) {
|
|
|
1305
1331
|
kind: "ArrowFunction"
|
|
1306
1332
|
};
|
|
1307
1333
|
}
|
|
1334
|
+
/**
|
|
1335
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
1336
|
+
*
|
|
1337
|
+
* Use this instead of bare strings when building `nodes` arrays so that every
|
|
1338
|
+
* entry in the array is a typed {@link CodeNode}.
|
|
1339
|
+
*
|
|
1340
|
+
* @example
|
|
1341
|
+
* ```ts
|
|
1342
|
+
* createText('return fetch(id)')
|
|
1343
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
1344
|
+
* ```
|
|
1345
|
+
*/
|
|
1346
|
+
function createText(value) {
|
|
1347
|
+
return {
|
|
1348
|
+
value,
|
|
1349
|
+
kind: "Text"
|
|
1350
|
+
};
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
1354
|
+
*
|
|
1355
|
+
* Corresponds to `<br/>` in JSX components. Prints as an empty string which,
|
|
1356
|
+
* when joined with `\n` by `printNodes`, produces a blank line.
|
|
1357
|
+
*
|
|
1358
|
+
* @example
|
|
1359
|
+
* ```ts
|
|
1360
|
+
* createBreak()
|
|
1361
|
+
* // { kind: 'Break' }
|
|
1362
|
+
* ```
|
|
1363
|
+
*/
|
|
1364
|
+
function createBreak() {
|
|
1365
|
+
return { kind: "Break" };
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
1369
|
+
*
|
|
1370
|
+
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
1371
|
+
*
|
|
1372
|
+
* @example
|
|
1373
|
+
* ```ts
|
|
1374
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
1375
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
1376
|
+
* ```
|
|
1377
|
+
*/
|
|
1378
|
+
function createJsx(value) {
|
|
1379
|
+
return {
|
|
1380
|
+
value,
|
|
1381
|
+
kind: "Jsx"
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1308
1384
|
//#endregion
|
|
1309
1385
|
//#region src/printer.ts
|
|
1310
1386
|
/**
|
|
@@ -1866,11 +1942,9 @@ exports.caseParams = caseParams;
|
|
|
1866
1942
|
exports.childName = childName;
|
|
1867
1943
|
exports.collect = collect;
|
|
1868
1944
|
exports.collectImports = collectImports;
|
|
1869
|
-
exports.combineExports = combineExports;
|
|
1870
|
-
exports.combineImports = combineImports;
|
|
1871
|
-
exports.combineSources = combineSources;
|
|
1872
1945
|
exports.composeTransformers = composeTransformers;
|
|
1873
1946
|
exports.createArrowFunction = createArrowFunction;
|
|
1947
|
+
exports.createBreak = createBreak;
|
|
1874
1948
|
exports.createConst = createConst;
|
|
1875
1949
|
exports.createDiscriminantNode = createDiscriminantNode;
|
|
1876
1950
|
exports.createExport = createExport;
|
|
@@ -1880,6 +1954,7 @@ exports.createFunctionParameter = createFunctionParameter;
|
|
|
1880
1954
|
exports.createFunctionParameters = createFunctionParameters;
|
|
1881
1955
|
exports.createImport = createImport;
|
|
1882
1956
|
exports.createInput = createInput;
|
|
1957
|
+
exports.createJsx = createJsx;
|
|
1883
1958
|
exports.createOperation = createOperation;
|
|
1884
1959
|
exports.createOperationParams = createOperationParams;
|
|
1885
1960
|
exports.createOutput = createOutput;
|
|
@@ -1891,10 +1966,12 @@ exports.createProperty = createProperty;
|
|
|
1891
1966
|
exports.createResponse = createResponse;
|
|
1892
1967
|
exports.createSchema = createSchema;
|
|
1893
1968
|
exports.createSource = createSource;
|
|
1969
|
+
exports.createText = createText;
|
|
1894
1970
|
exports.createType = createType;
|
|
1895
1971
|
exports.definePrinter = definePrinter;
|
|
1896
1972
|
exports.enumPropName = enumPropName;
|
|
1897
1973
|
exports.extractRefName = extractRefName;
|
|
1974
|
+
exports.extractStringsFromNodes = extractStringsFromNodes;
|
|
1898
1975
|
exports.findDiscriminator = findDiscriminator;
|
|
1899
1976
|
exports.httpMethods = httpMethods;
|
|
1900
1977
|
exports.isInputNode = isInputNode;
|