@kubb/ast 5.0.0-alpha.13 → 5.0.0-alpha.15
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 +126 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +125 -51
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/{visitor-D68E3x_d.d.ts → visitor-UlWOe-In.d.ts} +78 -37
- package/package.json +1 -1
- package/src/factory.ts +23 -2
- package/src/index.ts +2 -1
- package/src/types.ts +1 -1
- package/src/visitor.ts +157 -80
package/dist/index.js
CHANGED
|
@@ -110,23 +110,41 @@ function createSchema(props) {
|
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
|
+
* Derives `schema.optional` and `schema.nullish` from `required` and `schema.nullable`.
|
|
114
|
+
* This keeps `PropertyNode.required` as the single source of truth for optionality.
|
|
115
|
+
*/
|
|
116
|
+
function syncPropertySchema(required, schema) {
|
|
117
|
+
const nullable = schema.nullable ?? false;
|
|
118
|
+
return {
|
|
119
|
+
...schema,
|
|
120
|
+
optional: !required && !nullable ? true : void 0,
|
|
121
|
+
nullish: !required && nullable ? true : void 0
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
113
125
|
* Creates a `PropertyNode`. `required` defaults to `false`.
|
|
126
|
+
* `schema.optional` and `schema.nullish` are auto-derived from `required` and `schema.nullable`.
|
|
114
127
|
*/
|
|
115
128
|
function createProperty(props) {
|
|
129
|
+
const required = props.required ?? false;
|
|
116
130
|
return {
|
|
117
|
-
required: false,
|
|
118
131
|
...props,
|
|
119
|
-
kind: "Property"
|
|
132
|
+
kind: "Property",
|
|
133
|
+
required,
|
|
134
|
+
schema: syncPropertySchema(required, props.schema)
|
|
120
135
|
};
|
|
121
136
|
}
|
|
122
137
|
/**
|
|
123
138
|
* Creates a `ParameterNode`. `required` defaults to `false`.
|
|
139
|
+
* `schema.optional` is auto-derived from `required` and `schema.nullable`.
|
|
124
140
|
*/
|
|
125
141
|
function createParameter(props) {
|
|
142
|
+
const required = props.required ?? false;
|
|
126
143
|
return {
|
|
127
|
-
required: false,
|
|
128
144
|
...props,
|
|
129
|
-
kind: "Parameter"
|
|
145
|
+
kind: "Parameter",
|
|
146
|
+
required,
|
|
147
|
+
schema: syncPropertySchema(required, props.schema)
|
|
130
148
|
};
|
|
131
149
|
}
|
|
132
150
|
/**
|
|
@@ -852,103 +870,129 @@ function getChildren(node, recurse) {
|
|
|
852
870
|
* Depth-first traversal for side effects. Visitor return values are ignored.
|
|
853
871
|
* Sibling nodes at each level are visited concurrently up to `options.concurrency` (default: 30).
|
|
854
872
|
*/
|
|
855
|
-
async function walk(node,
|
|
856
|
-
return _walk(node,
|
|
873
|
+
async function walk(node, options) {
|
|
874
|
+
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
|
|
857
875
|
}
|
|
858
|
-
|
|
859
|
-
* Internal recursive walk implementation — calls visitor then recurses into children.
|
|
860
|
-
*/
|
|
861
|
-
async function _walk(node, visitor, recurse, limit) {
|
|
876
|
+
async function _walk(node, visitor, recurse, limit, parent) {
|
|
862
877
|
switch (node.kind) {
|
|
863
878
|
case "Root":
|
|
864
|
-
await limit(() => visitor.root?.(node));
|
|
879
|
+
await limit(() => visitor.root?.(node, { parent }));
|
|
865
880
|
break;
|
|
866
881
|
case "Operation":
|
|
867
|
-
await limit(() => visitor.operation?.(node));
|
|
882
|
+
await limit(() => visitor.operation?.(node, { parent }));
|
|
868
883
|
break;
|
|
869
884
|
case "Schema":
|
|
870
|
-
await limit(() => visitor.schema?.(node));
|
|
885
|
+
await limit(() => visitor.schema?.(node, { parent }));
|
|
871
886
|
break;
|
|
872
887
|
case "Property":
|
|
873
|
-
await limit(() => visitor.property?.(node));
|
|
888
|
+
await limit(() => visitor.property?.(node, { parent }));
|
|
874
889
|
break;
|
|
875
890
|
case "Parameter":
|
|
876
|
-
await limit(() => visitor.parameter?.(node));
|
|
891
|
+
await limit(() => visitor.parameter?.(node, { parent }));
|
|
877
892
|
break;
|
|
878
893
|
case "Response":
|
|
879
|
-
await limit(() => visitor.response?.(node));
|
|
894
|
+
await limit(() => visitor.response?.(node, { parent }));
|
|
880
895
|
break;
|
|
881
896
|
case "FunctionParameter":
|
|
882
897
|
case "ObjectBindingParameter":
|
|
883
898
|
case "FunctionParameters": break;
|
|
884
899
|
}
|
|
885
900
|
const children = getChildren(node, recurse);
|
|
886
|
-
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit)));
|
|
901
|
+
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
|
|
887
902
|
}
|
|
888
|
-
function transform(node,
|
|
889
|
-
const
|
|
903
|
+
function transform(node, options) {
|
|
904
|
+
const { depth, parent, ...visitor } = options;
|
|
905
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
890
906
|
switch (node.kind) {
|
|
891
907
|
case "Root": {
|
|
892
908
|
let root = node;
|
|
893
|
-
const replaced = visitor.root?.(root);
|
|
909
|
+
const replaced = visitor.root?.(root, { parent });
|
|
894
910
|
if (replaced) root = replaced;
|
|
895
911
|
return {
|
|
896
912
|
...root,
|
|
897
|
-
schemas: root.schemas.map((s) => transform(s,
|
|
898
|
-
|
|
913
|
+
schemas: root.schemas.map((s) => transform(s, {
|
|
914
|
+
...options,
|
|
915
|
+
parent: root
|
|
916
|
+
})),
|
|
917
|
+
operations: root.operations.map((op) => transform(op, {
|
|
918
|
+
...options,
|
|
919
|
+
parent: root
|
|
920
|
+
}))
|
|
899
921
|
};
|
|
900
922
|
}
|
|
901
923
|
case "Operation": {
|
|
902
924
|
let op = node;
|
|
903
|
-
const replaced = visitor.operation?.(op);
|
|
925
|
+
const replaced = visitor.operation?.(op, { parent });
|
|
904
926
|
if (replaced) op = replaced;
|
|
905
927
|
return {
|
|
906
928
|
...op,
|
|
907
|
-
parameters: op.parameters.map((p) => transform(p,
|
|
929
|
+
parameters: op.parameters.map((p) => transform(p, {
|
|
930
|
+
...options,
|
|
931
|
+
parent: op
|
|
932
|
+
})),
|
|
908
933
|
requestBody: op.requestBody ? {
|
|
909
934
|
...op.requestBody,
|
|
910
|
-
schema: op.requestBody.schema ? transform(op.requestBody.schema,
|
|
935
|
+
schema: op.requestBody.schema ? transform(op.requestBody.schema, {
|
|
936
|
+
...options,
|
|
937
|
+
parent: op
|
|
938
|
+
}) : void 0
|
|
911
939
|
} : void 0,
|
|
912
|
-
responses: op.responses.map((r) => transform(r,
|
|
940
|
+
responses: op.responses.map((r) => transform(r, {
|
|
941
|
+
...options,
|
|
942
|
+
parent: op
|
|
943
|
+
}))
|
|
913
944
|
};
|
|
914
945
|
}
|
|
915
946
|
case "Schema": {
|
|
916
947
|
let schema = node;
|
|
917
|
-
const replaced = visitor.schema?.(schema);
|
|
948
|
+
const replaced = visitor.schema?.(schema, { parent });
|
|
918
949
|
if (replaced) schema = replaced;
|
|
950
|
+
const childOptions = {
|
|
951
|
+
...options,
|
|
952
|
+
parent: schema
|
|
953
|
+
};
|
|
919
954
|
return {
|
|
920
955
|
...schema,
|
|
921
|
-
..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p,
|
|
922
|
-
..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i,
|
|
923
|
-
..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m,
|
|
924
|
-
..."additionalProperties" in schema && recurse && schema.additionalProperties && schema.additionalProperties !== true ? { additionalProperties: transform(schema.additionalProperties,
|
|
956
|
+
..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p, childOptions)) } : {},
|
|
957
|
+
..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i, childOptions)) } : {},
|
|
958
|
+
..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m, childOptions)) } : {},
|
|
959
|
+
..."additionalProperties" in schema && recurse && schema.additionalProperties && schema.additionalProperties !== true ? { additionalProperties: transform(schema.additionalProperties, childOptions) } : {}
|
|
925
960
|
};
|
|
926
961
|
}
|
|
927
962
|
case "Property": {
|
|
928
963
|
let prop = node;
|
|
929
|
-
const replaced = visitor.property?.(prop);
|
|
964
|
+
const replaced = visitor.property?.(prop, { parent });
|
|
930
965
|
if (replaced) prop = replaced;
|
|
931
|
-
return {
|
|
966
|
+
return createProperty({
|
|
932
967
|
...prop,
|
|
933
|
-
schema: transform(prop.schema,
|
|
934
|
-
|
|
968
|
+
schema: transform(prop.schema, {
|
|
969
|
+
...options,
|
|
970
|
+
parent: prop
|
|
971
|
+
})
|
|
972
|
+
});
|
|
935
973
|
}
|
|
936
974
|
case "Parameter": {
|
|
937
975
|
let param = node;
|
|
938
|
-
const replaced = visitor.parameter?.(param);
|
|
976
|
+
const replaced = visitor.parameter?.(param, { parent });
|
|
939
977
|
if (replaced) param = replaced;
|
|
940
|
-
return {
|
|
978
|
+
return createParameter({
|
|
941
979
|
...param,
|
|
942
|
-
schema: transform(param.schema,
|
|
943
|
-
|
|
980
|
+
schema: transform(param.schema, {
|
|
981
|
+
...options,
|
|
982
|
+
parent: param
|
|
983
|
+
})
|
|
984
|
+
});
|
|
944
985
|
}
|
|
945
986
|
case "Response": {
|
|
946
987
|
let response = node;
|
|
947
|
-
const replaced = visitor.response?.(response);
|
|
988
|
+
const replaced = visitor.response?.(response, { parent });
|
|
948
989
|
if (replaced) response = replaced;
|
|
949
990
|
return {
|
|
950
991
|
...response,
|
|
951
|
-
schema: transform(response.schema,
|
|
992
|
+
schema: transform(response.schema, {
|
|
993
|
+
...options,
|
|
994
|
+
parent: response
|
|
995
|
+
})
|
|
952
996
|
};
|
|
953
997
|
}
|
|
954
998
|
case "FunctionParameter":
|
|
@@ -957,40 +1001,70 @@ function transform(node, visitor, options = {}) {
|
|
|
957
1001
|
}
|
|
958
1002
|
}
|
|
959
1003
|
/**
|
|
1004
|
+
* Combines multiple visitors into a single visitor that applies them sequentially (left to right).
|
|
1005
|
+
* For each node kind, the output of one visitor becomes the input of the next.
|
|
1006
|
+
*/
|
|
1007
|
+
function composeTransformers(...visitors) {
|
|
1008
|
+
return {
|
|
1009
|
+
root(node, context) {
|
|
1010
|
+
return visitors.reduce((acc, v) => v.root?.(acc, context) ?? acc, node);
|
|
1011
|
+
},
|
|
1012
|
+
operation(node, context) {
|
|
1013
|
+
return visitors.reduce((acc, v) => v.operation?.(acc, context) ?? acc, node);
|
|
1014
|
+
},
|
|
1015
|
+
schema(node, context) {
|
|
1016
|
+
return visitors.reduce((acc, v) => v.schema?.(acc, context) ?? acc, node);
|
|
1017
|
+
},
|
|
1018
|
+
property(node, context) {
|
|
1019
|
+
return visitors.reduce((acc, v) => v.property?.(acc, context) ?? acc, node);
|
|
1020
|
+
},
|
|
1021
|
+
parameter(node, context) {
|
|
1022
|
+
return visitors.reduce((acc, v) => v.parameter?.(acc, context) ?? acc, node);
|
|
1023
|
+
},
|
|
1024
|
+
response(node, context) {
|
|
1025
|
+
return visitors.reduce((acc, v) => v.response?.(acc, context) ?? acc, node);
|
|
1026
|
+
}
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
960
1030
|
* Depth-first synchronous reduction. Collects non-`undefined` visitor return values into an array.
|
|
961
1031
|
*/
|
|
962
|
-
function collect(node,
|
|
963
|
-
const
|
|
1032
|
+
function collect(node, options) {
|
|
1033
|
+
const { depth, parent, ...visitor } = options;
|
|
1034
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
964
1035
|
const results = [];
|
|
965
1036
|
let v;
|
|
966
1037
|
switch (node.kind) {
|
|
967
1038
|
case "Root":
|
|
968
|
-
v = visitor.root?.(node);
|
|
1039
|
+
v = visitor.root?.(node, { parent });
|
|
969
1040
|
break;
|
|
970
1041
|
case "Operation":
|
|
971
|
-
v = visitor.operation?.(node);
|
|
1042
|
+
v = visitor.operation?.(node, { parent });
|
|
972
1043
|
break;
|
|
973
1044
|
case "Schema":
|
|
974
|
-
v = visitor.schema?.(node);
|
|
1045
|
+
v = visitor.schema?.(node, { parent });
|
|
975
1046
|
break;
|
|
976
1047
|
case "Property":
|
|
977
|
-
v = visitor.property?.(node);
|
|
1048
|
+
v = visitor.property?.(node, { parent });
|
|
978
1049
|
break;
|
|
979
1050
|
case "Parameter":
|
|
980
|
-
v = visitor.parameter?.(node);
|
|
1051
|
+
v = visitor.parameter?.(node, { parent });
|
|
981
1052
|
break;
|
|
982
1053
|
case "Response":
|
|
983
|
-
v = visitor.response?.(node);
|
|
1054
|
+
v = visitor.response?.(node, { parent });
|
|
984
1055
|
break;
|
|
985
1056
|
case "FunctionParameter":
|
|
986
1057
|
case "ObjectBindingParameter":
|
|
987
1058
|
case "FunctionParameters": break;
|
|
988
1059
|
}
|
|
989
1060
|
if (v !== void 0) results.push(v);
|
|
990
|
-
for (const child of getChildren(node, recurse)) for (const item of collect(child,
|
|
1061
|
+
for (const child of getChildren(node, recurse)) for (const item of collect(child, {
|
|
1062
|
+
...options,
|
|
1063
|
+
parent: node
|
|
1064
|
+
})) results.push(item);
|
|
991
1065
|
return results;
|
|
992
1066
|
}
|
|
993
1067
|
//#endregion
|
|
994
|
-
export { applyParamsCasing, buildRefMap, collect, createFunctionParameter, createFunctionParameters, createObjectBindingParameter, createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, definePrinter, functionPrinter, httpMethods, isFunctionParameterNode, isFunctionParametersNode, isObjectBindingParameterNode, isOperationNode, isParameterNode, isPlainStringType, isPropertyNode, isResponseNode, isRootNode, isSchemaNode, mediaTypes, narrowSchema, nodeKinds, refMapToObject, resolveRef, schemaTypes, transform, walk };
|
|
1068
|
+
export { applyParamsCasing, buildRefMap, collect, composeTransformers, createFunctionParameter, createFunctionParameters, createObjectBindingParameter, createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, definePrinter, functionPrinter, httpMethods, isFunctionParameterNode, isFunctionParametersNode, isObjectBindingParameterNode, isOperationNode, isParameterNode, isPlainStringType, isPropertyNode, isResponseNode, isRootNode, isSchemaNode, mediaTypes, narrowSchema, nodeKinds, refMapToObject, resolveRef, schemaTypes, syncPropertySchema, transform, walk };
|
|
995
1069
|
|
|
996
1070
|
//# sourceMappingURL=index.js.map
|