@kubb/ast 5.0.0-alpha.32 → 5.0.0-alpha.33
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.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { A as
|
|
2
|
+
import { A as createInput, B as createSource, C as createConst, D as createFunctionParameter, E as createFunction, F as createParameterGroup, H as createType, Ht as CodeNode, I as createParamsType, K as ParserOptions, L as createProperty, M as createOperation, Mt as FunctionParameterNode, N as createOutput, Nt as FunctionParametersNode, O as createFunctionParameters, P as createParameter, Pt as ParameterGroupNode, Q as OperationNode, Qt as ScalarPrimitive, R as createResponse, S as createBreak, St as SchemaNodeByType, T as createFile, U as syncOptionality, V as createText, X as OutputNode, Y as InputNode, _ as PrinterPartial, d as transform, en as httpMethods, f as walk, g as PrinterFactoryOptions, h as Printer, in as schemaTypes, it as ParameterNode, j as createJsx, k as createImport, l as collect, m as extractRefName, nn as mediaTypes, rn as nodeKinds, tn as isScalarPrimitive, u as composeTransformers, v as createPrinterFactory, w as createExport, x as createArrowFunction, xt as SchemaNode, y as definePrinter, z as createSchema } from "./visitor-CJMIoAE3.js";
|
|
3
3
|
|
|
4
4
|
//#region src/guards.d.ts
|
|
5
5
|
/**
|
|
@@ -328,22 +328,13 @@ type CreateOperationParamsOptions = {
|
|
|
328
328
|
*/
|
|
329
329
|
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
|
|
330
330
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Deduplicates and merges an array of `ExportNode` objects.
|
|
338
|
-
* Exports with the same path and `isTypeOnly` flag have their names merged.
|
|
339
|
-
*/
|
|
340
|
-
declare function combineExports(exports: Array<ExportNode>): Array<ExportNode>;
|
|
341
|
-
/**
|
|
342
|
-
* Deduplicates and merges an array of `ImportNode` objects.
|
|
343
|
-
* Filters out unused imports (names not referenced in `source` or re-exported).
|
|
344
|
-
* Imports with the same path and `isTypeOnly` flag have their names merged.
|
|
331
|
+
* Recursively extracts all string content embedded in a {@link CodeNode} tree.
|
|
332
|
+
*
|
|
333
|
+
* Includes text node values, and string attribute fields (`params`, `generics`,
|
|
334
|
+
* `returnType`, `type`) that may reference identifiers needing imports.
|
|
335
|
+
* Used by `createFile` to build the full source string for import filtering.
|
|
345
336
|
*/
|
|
346
|
-
declare function
|
|
337
|
+
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
|
|
347
338
|
//#endregion
|
|
348
|
-
export { type OperationParamsResolver, type ParserOptions, type Printer, type PrinterFactoryOptions, type PrinterPartial, type ScalarPrimitive, caseParams, childName, collect, collectImports,
|
|
339
|
+
export { type OperationParamsResolver, type ParserOptions, type Printer, type PrinterFactoryOptions, type PrinterPartial, type ScalarPrimitive, caseParams, childName, collect, collectImports, composeTransformers, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
349
340
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,9 @@ const nodeKinds = {
|
|
|
21
21
|
file: "File",
|
|
22
22
|
import: "Import",
|
|
23
23
|
export: "Export",
|
|
24
|
-
source: "Source"
|
|
24
|
+
source: "Source",
|
|
25
|
+
text: "Text",
|
|
26
|
+
break: "Break"
|
|
25
27
|
};
|
|
26
28
|
/**
|
|
27
29
|
* Canonical schema type strings used by AST schema nodes.
|
|
@@ -593,7 +595,7 @@ function toStructType({ node, params, resolver }) {
|
|
|
593
595
|
});
|
|
594
596
|
}
|
|
595
597
|
function sourceKey(source) {
|
|
596
|
-
return `${source.name ?? source.
|
|
598
|
+
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
597
599
|
}
|
|
598
600
|
function pathTypeKey(path, isTypeOnly) {
|
|
599
601
|
return `${path}:${isTypeOnly ?? false}`;
|
|
@@ -618,7 +620,7 @@ function sortKey(node) {
|
|
|
618
620
|
/**
|
|
619
621
|
* Deduplicates an array of `SourceNode` objects.
|
|
620
622
|
* Named sources are deduplicated by `name + isExportable + isTypeOnly`.
|
|
621
|
-
* Unnamed sources are deduplicated by
|
|
623
|
+
* Unnamed sources are deduplicated by object reference.
|
|
622
624
|
*/
|
|
623
625
|
function combineSources(sources) {
|
|
624
626
|
const seen = /* @__PURE__ */ new Map();
|
|
@@ -709,6 +711,30 @@ function combineImports(imports, exports, source) {
|
|
|
709
711
|
}
|
|
710
712
|
return result;
|
|
711
713
|
}
|
|
714
|
+
/**
|
|
715
|
+
* Recursively extracts all string content embedded in a {@link CodeNode} tree.
|
|
716
|
+
*
|
|
717
|
+
* Includes text node values, and string attribute fields (`params`, `generics`,
|
|
718
|
+
* `returnType`, `type`) that may reference identifiers needing imports.
|
|
719
|
+
* Used by `createFile` to build the full source string for import filtering.
|
|
720
|
+
*/
|
|
721
|
+
function extractStringsFromNodes(nodes) {
|
|
722
|
+
if (!nodes?.length) return "";
|
|
723
|
+
return nodes.map((node) => {
|
|
724
|
+
if (typeof node === "string") return node;
|
|
725
|
+
if (node.kind === "Text") return node.value;
|
|
726
|
+
if (node.kind === "Break") return "";
|
|
727
|
+
if (node.kind === "Jsx") return node.value;
|
|
728
|
+
const parts = [];
|
|
729
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
730
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
731
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
732
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
733
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
734
|
+
if (nested) parts.push(nested);
|
|
735
|
+
return parts.join("\n");
|
|
736
|
+
}).filter(Boolean).join("\n");
|
|
737
|
+
}
|
|
712
738
|
//#endregion
|
|
713
739
|
//#region src/factory.ts
|
|
714
740
|
/**
|
|
@@ -1090,7 +1116,7 @@ function createExport(props) {
|
|
|
1090
1116
|
*
|
|
1091
1117
|
* @example
|
|
1092
1118
|
* ```ts
|
|
1093
|
-
* createSource({ name: 'Pet',
|
|
1119
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1094
1120
|
* ```
|
|
1095
1121
|
*/
|
|
1096
1122
|
function createSource(props) {
|
|
@@ -1119,7 +1145,7 @@ function createSource(props) {
|
|
|
1119
1145
|
* const file = createFile({
|
|
1120
1146
|
* baseName: 'petStore.ts',
|
|
1121
1147
|
* path: 'src/models/petStore.ts',
|
|
1122
|
-
* sources: [createSource({ name: 'Pet',
|
|
1148
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
1123
1149
|
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
1124
1150
|
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1125
1151
|
* })
|
|
@@ -1131,9 +1157,9 @@ function createSource(props) {
|
|
|
1131
1157
|
function createFile(input) {
|
|
1132
1158
|
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
1133
1159
|
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
1134
|
-
const source = (input.sources ?? []).
|
|
1160
|
+
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
|
|
1135
1161
|
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
1136
|
-
const resolvedImports = input.imports?.length
|
|
1162
|
+
const resolvedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
|
|
1137
1163
|
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
1138
1164
|
return {
|
|
1139
1165
|
kind: "File",
|
|
@@ -1150,7 +1176,7 @@ function createFile(input) {
|
|
|
1150
1176
|
/**
|
|
1151
1177
|
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
1152
1178
|
*
|
|
1153
|
-
* Mirrors the `Const` component from `@kubb/
|
|
1179
|
+
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
1154
1180
|
* The component's `children` are represented as `nodes`.
|
|
1155
1181
|
*
|
|
1156
1182
|
* @example Simple constant
|
|
@@ -1184,7 +1210,7 @@ function createConst(props) {
|
|
|
1184
1210
|
/**
|
|
1185
1211
|
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
1186
1212
|
*
|
|
1187
|
-
* Mirrors the `Type` component from `@kubb/
|
|
1213
|
+
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
1188
1214
|
* The component's `children` are represented as `nodes`.
|
|
1189
1215
|
*
|
|
1190
1216
|
* @example Simple type alias
|
|
@@ -1212,7 +1238,7 @@ function createType(props) {
|
|
|
1212
1238
|
/**
|
|
1213
1239
|
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
1214
1240
|
*
|
|
1215
|
-
* Mirrors the `Function` component from `@kubb/
|
|
1241
|
+
* Mirrors the `Function` component from `@kubb/renderer-jsx`.
|
|
1216
1242
|
* The component's `children` are represented as `nodes`.
|
|
1217
1243
|
*
|
|
1218
1244
|
* @example Simple function
|
|
@@ -1248,7 +1274,7 @@ function createFunction(props) {
|
|
|
1248
1274
|
/**
|
|
1249
1275
|
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
1250
1276
|
*
|
|
1251
|
-
* Mirrors the `Function.Arrow` component from `@kubb/
|
|
1277
|
+
* Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
1252
1278
|
* The component's `children` are represented as `nodes`.
|
|
1253
1279
|
*
|
|
1254
1280
|
* @example Simple arrow function
|
|
@@ -1282,6 +1308,56 @@ function createArrowFunction(props) {
|
|
|
1282
1308
|
kind: "ArrowFunction"
|
|
1283
1309
|
};
|
|
1284
1310
|
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
1313
|
+
*
|
|
1314
|
+
* Use this instead of bare strings when building `nodes` arrays so that every
|
|
1315
|
+
* entry in the array is a typed {@link CodeNode}.
|
|
1316
|
+
*
|
|
1317
|
+
* @example
|
|
1318
|
+
* ```ts
|
|
1319
|
+
* createText('return fetch(id)')
|
|
1320
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
1321
|
+
* ```
|
|
1322
|
+
*/
|
|
1323
|
+
function createText(value) {
|
|
1324
|
+
return {
|
|
1325
|
+
value,
|
|
1326
|
+
kind: "Text"
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
1331
|
+
*
|
|
1332
|
+
* Corresponds to `<br/>` in JSX components. Prints as an empty string which,
|
|
1333
|
+
* when joined with `\n` by `printNodes`, produces a blank line.
|
|
1334
|
+
*
|
|
1335
|
+
* @example
|
|
1336
|
+
* ```ts
|
|
1337
|
+
* createBreak()
|
|
1338
|
+
* // { kind: 'Break' }
|
|
1339
|
+
* ```
|
|
1340
|
+
*/
|
|
1341
|
+
function createBreak() {
|
|
1342
|
+
return { kind: "Break" };
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
1346
|
+
*
|
|
1347
|
+
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
1348
|
+
*
|
|
1349
|
+
* @example
|
|
1350
|
+
* ```ts
|
|
1351
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
1352
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
1353
|
+
* ```
|
|
1354
|
+
*/
|
|
1355
|
+
function createJsx(value) {
|
|
1356
|
+
return {
|
|
1357
|
+
value,
|
|
1358
|
+
kind: "Jsx"
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1285
1361
|
//#endregion
|
|
1286
1362
|
//#region src/printer.ts
|
|
1287
1363
|
/**
|
|
@@ -1839,6 +1915,6 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
1839
1915
|
return propNode;
|
|
1840
1916
|
}
|
|
1841
1917
|
//#endregion
|
|
1842
|
-
export { caseParams, childName, collect, collectImports,
|
|
1918
|
+
export { caseParams, childName, collect, collectImports, composeTransformers, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
1843
1919
|
|
|
1844
1920
|
//# sourceMappingURL=index.js.map
|