ajsc 5.2.3 → 7.0.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/README.md +253 -145
- package/dist/converter/BaseConverter.d.ts +283 -0
- package/dist/converter/BaseConverter.js +131 -0
- package/dist/converter/BaseConverter.js.map +1 -0
- package/dist/converter/Emitter.d.ts +35 -0
- package/dist/converter/Emitter.js +50 -0
- package/dist/converter/Emitter.js.map +1 -0
- package/dist/converter/discriminatedUnions.d.ts +47 -0
- package/dist/converter/discriminatedUnions.js +168 -0
- package/dist/converter/discriminatedUnions.js.map +1 -0
- package/dist/converter/formatDefault.d.ts +20 -0
- package/dist/converter/formatDefault.js +31 -0
- package/dist/converter/formatDefault.js.map +1 -0
- package/dist/converter/index.d.ts +2 -0
- package/dist/converter/index.js +2 -0
- package/dist/converter/index.js.map +1 -0
- package/dist/converter/mergeUnions.d.ts +36 -0
- package/dist/converter/mergeUnions.js +189 -0
- package/dist/converter/mergeUnions.js.map +1 -0
- package/dist/converter/naming.d.ts +29 -0
- package/dist/converter/naming.js +130 -0
- package/dist/converter/naming.js.map +1 -0
- package/dist/converter/registry.d.ts +18 -0
- package/dist/converter/registry.js +50 -0
- package/dist/converter/registry.js.map +1 -0
- package/dist/converter/walk.d.ts +9 -0
- package/dist/converter/walk.js +40 -0
- package/dist/converter/walk.js.map +1 -0
- package/dist/index.d.ts +71 -3
- package/dist/index.js +63 -3
- package/dist/index.js.map +1 -1
- package/dist/{JSONSchemaConverter.d.ts → ir/JSONSchemaConverter.d.ts} +1 -1
- package/dist/{JSONSchemaConverter.js → ir/JSONSchemaConverter.js} +9 -3
- package/dist/ir/JSONSchemaConverter.js.map +1 -0
- package/dist/ir/index.d.ts +1 -0
- package/dist/ir/index.js +2 -0
- package/dist/ir/index.js.map +1 -0
- package/dist/kotlin/KotlinBaseConverter.d.ts +18 -0
- package/dist/kotlin/KotlinBaseConverter.js +36 -0
- package/dist/kotlin/KotlinBaseConverter.js.map +1 -0
- package/dist/kotlin/KotlinConverter.d.ts +67 -0
- package/dist/kotlin/KotlinConverter.js +140 -0
- package/dist/kotlin/KotlinConverter.js.map +1 -0
- package/dist/kotlin/annotations.d.ts +26 -0
- package/dist/kotlin/annotations.js +35 -0
- package/dist/kotlin/annotations.js.map +1 -0
- package/dist/kotlin/enums.d.ts +15 -0
- package/dist/kotlin/enums.js +58 -0
- package/dist/kotlin/enums.js.map +1 -0
- package/dist/kotlin/index.d.ts +2 -0
- package/dist/kotlin/index.js +3 -0
- package/dist/kotlin/index.js.map +1 -0
- package/dist/kotlin/objectEmitter.d.ts +12 -0
- package/dist/kotlin/objectEmitter.js +74 -0
- package/dist/kotlin/objectEmitter.js.map +1 -0
- package/dist/kotlin/sealedUnion.d.ts +17 -0
- package/dist/kotlin/sealedUnion.js +74 -0
- package/dist/kotlin/sealedUnion.js.map +1 -0
- package/dist/kotlin/typeMapper.d.ts +17 -0
- package/dist/kotlin/typeMapper.js +107 -0
- package/dist/kotlin/typeMapper.js.map +1 -0
- package/dist/kotlin/unsupported.d.ts +13 -0
- package/dist/kotlin/unsupported.js +53 -0
- package/dist/kotlin/unsupported.js.map +1 -0
- package/dist/swift/SwiftBaseConverter.d.ts +18 -0
- package/dist/swift/SwiftBaseConverter.js +38 -0
- package/dist/swift/SwiftBaseConverter.js.map +1 -0
- package/dist/swift/SwiftConverter.d.ts +60 -0
- package/dist/swift/SwiftConverter.js +111 -0
- package/dist/swift/SwiftConverter.js.map +1 -0
- package/dist/swift/discriminatedEnum.d.ts +18 -0
- package/dist/swift/discriminatedEnum.js +99 -0
- package/dist/swift/discriminatedEnum.js.map +1 -0
- package/dist/swift/enums.d.ts +15 -0
- package/dist/swift/enums.js +62 -0
- package/dist/swift/enums.js.map +1 -0
- package/dist/swift/index.d.ts +2 -0
- package/dist/swift/index.js +3 -0
- package/dist/swift/index.js.map +1 -0
- package/dist/swift/structEmitter.d.ts +12 -0
- package/dist/swift/structEmitter.js +70 -0
- package/dist/swift/structEmitter.js.map +1 -0
- package/dist/swift/typeMapper.d.ts +18 -0
- package/dist/swift/typeMapper.js +106 -0
- package/dist/swift/typeMapper.js.map +1 -0
- package/dist/swift/unsupported.d.ts +19 -0
- package/dist/swift/unsupported.js +88 -0
- package/dist/swift/unsupported.js.map +1 -0
- package/dist/typescript/TypescriptBaseConverter.d.ts +25 -0
- package/dist/typescript/TypescriptBaseConverter.js +178 -0
- package/dist/typescript/TypescriptBaseConverter.js.map +1 -0
- package/dist/typescript/TypescriptConverter.d.ts +74 -0
- package/dist/typescript/TypescriptConverter.js +252 -0
- package/dist/typescript/TypescriptConverter.js.map +1 -0
- package/dist/typescript/index.d.ts +2 -0
- package/dist/typescript/index.js +3 -0
- package/dist/typescript/index.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +37 -5
- package/dist/JSONSchemaConverter.js.map +0 -1
- package/dist/JSONSchemaConverter.test.d.ts +0 -1
- package/dist/JSONSchemaConverter.test.js +0 -585
- package/dist/JSONSchemaConverter.test.js.map +0 -1
- package/dist/Typebox.test.d.ts +0 -1
- package/dist/Typebox.test.js +0 -88
- package/dist/Typebox.test.js.map +0 -1
- package/dist/TypescriptBaseConverter.d.ts +0 -75
- package/dist/TypescriptBaseConverter.js +0 -321
- package/dist/TypescriptBaseConverter.js.map +0 -1
- package/dist/TypescriptConverter.additionalProperties.test.d.ts +0 -1
- package/dist/TypescriptConverter.additionalProperties.test.js +0 -110
- package/dist/TypescriptConverter.additionalProperties.test.js.map +0 -1
- package/dist/TypescriptConverter.arrays.test.d.ts +0 -1
- package/dist/TypescriptConverter.arrays.test.js +0 -130
- package/dist/TypescriptConverter.arrays.test.js.map +0 -1
- package/dist/TypescriptConverter.composites.advanced.test.d.ts +0 -1
- package/dist/TypescriptConverter.composites.advanced.test.js +0 -924
- package/dist/TypescriptConverter.composites.advanced.test.js.map +0 -1
- package/dist/TypescriptConverter.composites.test.d.ts +0 -1
- package/dist/TypescriptConverter.composites.test.js +0 -335
- package/dist/TypescriptConverter.composites.test.js.map +0 -1
- package/dist/TypescriptConverter.d.ts +0 -163
- package/dist/TypescriptConverter.js +0 -595
- package/dist/TypescriptConverter.js.map +0 -1
- package/dist/TypescriptConverter.jsdoc.test.d.ts +0 -1
- package/dist/TypescriptConverter.jsdoc.test.js +0 -194
- package/dist/TypescriptConverter.jsdoc.test.js.map +0 -1
- package/dist/TypescriptConverter.objects.test.d.ts +0 -1
- package/dist/TypescriptConverter.objects.test.js +0 -258
- package/dist/TypescriptConverter.objects.test.js.map +0 -1
- package/dist/TypescriptConverter.options.test.d.ts +0 -1
- package/dist/TypescriptConverter.options.test.js +0 -501
- package/dist/TypescriptConverter.options.test.js.map +0 -1
- package/dist/TypescriptConverter.primitives.test.d.ts +0 -1
- package/dist/TypescriptConverter.primitives.test.js +0 -26
- package/dist/TypescriptConverter.primitives.test.js.map +0 -1
- package/dist/utils/path-utils.test.d.ts +0 -1
- package/dist/utils/path-utils.test.js +0 -92
- package/dist/utils/path-utils.test.js.map +0 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { getOrCreateEnumDecl } from "./enums.js";
|
|
2
|
+
import { handleUnsupportedUnion } from "./unsupported.js";
|
|
3
|
+
export const PRIMITIVES = {
|
|
4
|
+
string: "String",
|
|
5
|
+
integer: "Int64",
|
|
6
|
+
number: "Double",
|
|
7
|
+
boolean: "Bool",
|
|
8
|
+
null: "Never",
|
|
9
|
+
};
|
|
10
|
+
export const FORMAT_MAP = {
|
|
11
|
+
"date-time": { type: "Date", needsFoundation: true, doc: "ISO-8601 — set JSONDecoder.dateDecodingStrategy = .iso8601" },
|
|
12
|
+
"uuid": { type: "UUID", needsFoundation: true },
|
|
13
|
+
"uri": { type: "URL", needsFoundation: true },
|
|
14
|
+
"date": { type: "String", needsFoundation: false, doc: "format: date" },
|
|
15
|
+
"time": { type: "String", needsFoundation: false, doc: "format: time" },
|
|
16
|
+
};
|
|
17
|
+
export const STRING_DOC_FORMATS = new Set(["email", "hostname", "ipv4", "ipv6"]);
|
|
18
|
+
/**
|
|
19
|
+
* Maps an IRNode to its Swift type expression. Handles primitives, formatted
|
|
20
|
+
* strings (with `Foundation` import and doc-comment tracking), arrays, tuples
|
|
21
|
+
* (homogeneous → array, heterogeneous → Swift tuple under `serializer: 'none'`),
|
|
22
|
+
* referenced object types, string enums, literals, and unions (T | null
|
|
23
|
+
* unwrapping, discriminated-enum dispatch, unsupported-union policy).
|
|
24
|
+
*/
|
|
25
|
+
export function generateSwiftType(c, ir, utils) {
|
|
26
|
+
switch (ir.type) {
|
|
27
|
+
case "string": {
|
|
28
|
+
const fmt = ir.constraints?.format;
|
|
29
|
+
if (fmt && FORMAT_MAP[fmt]) {
|
|
30
|
+
const m = FORMAT_MAP[fmt];
|
|
31
|
+
if (m.needsFoundation)
|
|
32
|
+
c.importsSet.add("Foundation");
|
|
33
|
+
if (m.doc)
|
|
34
|
+
c.docFormatNodes.set(ir, m.doc);
|
|
35
|
+
return m.type;
|
|
36
|
+
}
|
|
37
|
+
if (fmt && STRING_DOC_FORMATS.has(fmt)) {
|
|
38
|
+
c.docFormatNodes.set(ir, `format: ${fmt}`);
|
|
39
|
+
}
|
|
40
|
+
return "String";
|
|
41
|
+
}
|
|
42
|
+
case "integer": {
|
|
43
|
+
const fmt = ir.constraints?.format;
|
|
44
|
+
if (fmt === "int32")
|
|
45
|
+
return "Int32";
|
|
46
|
+
return PRIMITIVES.integer;
|
|
47
|
+
}
|
|
48
|
+
case "number":
|
|
49
|
+
case "boolean":
|
|
50
|
+
case "null":
|
|
51
|
+
return PRIMITIVES[ir.type];
|
|
52
|
+
case "array":
|
|
53
|
+
return `[${ir.items ? generateSwiftType(c, ir.items, utils) : "Any"}]`;
|
|
54
|
+
case "tuple": {
|
|
55
|
+
const items = ir.tupleItems ?? [];
|
|
56
|
+
const types = items.map((i) => generateSwiftType(c, i, utils));
|
|
57
|
+
if (types.length === 0)
|
|
58
|
+
return "[Any]";
|
|
59
|
+
const homogeneous = types.every((t) => t === types[0]);
|
|
60
|
+
if (homogeneous) {
|
|
61
|
+
return `[${types[0]}]`;
|
|
62
|
+
}
|
|
63
|
+
if (c.isCodable) {
|
|
64
|
+
throw new Error(`Unsupported: heterogeneous tuple at "${ir.path}" — Codable does not auto-derive heterogeneous tuples. ` +
|
|
65
|
+
`Use serializer: 'none' to emit a Swift native tuple, or restructure the schema.`);
|
|
66
|
+
}
|
|
67
|
+
return `(${types.join(", ")})`;
|
|
68
|
+
}
|
|
69
|
+
case "enum": {
|
|
70
|
+
if (ir.values?.every((v) => typeof v === "string")) {
|
|
71
|
+
return getOrCreateEnumDecl(c, ir.values, ir.path);
|
|
72
|
+
}
|
|
73
|
+
return "Any";
|
|
74
|
+
}
|
|
75
|
+
case "literal": {
|
|
76
|
+
const v = ir.constraints?.value;
|
|
77
|
+
if (typeof v === "string")
|
|
78
|
+
return "String";
|
|
79
|
+
if (typeof v === "number")
|
|
80
|
+
return Number.isInteger(v) ? "Int64" : "Double";
|
|
81
|
+
if (typeof v === "boolean")
|
|
82
|
+
return "Bool";
|
|
83
|
+
return "Any";
|
|
84
|
+
}
|
|
85
|
+
case "object": {
|
|
86
|
+
const ref = utils.getReferencedType(ir);
|
|
87
|
+
if (ref)
|
|
88
|
+
return ref;
|
|
89
|
+
return "Any";
|
|
90
|
+
}
|
|
91
|
+
case "union": {
|
|
92
|
+
const non = ir.options?.filter((o) => o.type !== "null") ?? [];
|
|
93
|
+
if (non.length === 1)
|
|
94
|
+
return generateSwiftType(c, non[0], utils);
|
|
95
|
+
// Discriminated union: variantNames populated
|
|
96
|
+
if (ir.options?.length &&
|
|
97
|
+
ir.options.every((o) => c.variantNames.has(o))) {
|
|
98
|
+
return c.emitSwiftDiscriminatedEnum(ir, utils);
|
|
99
|
+
}
|
|
100
|
+
return handleUnsupportedUnion(c, ir);
|
|
101
|
+
}
|
|
102
|
+
default:
|
|
103
|
+
return "Any";
|
|
104
|
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YAC3E,IAAI,OAAO,CAAC,KAAK,SAAS;gBAAE,OAAO,MAAM,CAAC;YAC1C,OAAO,KAAK,CAAC;QACf,CAAC;QACD,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,MAAM,GAAG,GAAG,KAAK,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;YACxC,IAAI,GAAG;gBAAE,OAAO,GAAG,CAAC;YACpB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,KAAK,OAAO,CAAC,CAAC,CAAC;YACb,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,EAAE,CAAC;YAC/D,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,iBAAiB,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;YAEjE,8CAA8C;YAC9C,IACE,EAAE,CAAC,OAAO,EAAE,MAAM;gBAClB,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAC9C,CAAC;gBACD,OAAO,CAAC,CAAC,0BAA0B,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;YACjD,CAAC;YAED,OAAO,sBAAsB,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACvC,CAAC;QACD;YACE,OAAO,KAAK,CAAC;IACjB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IRNode } from "../types.js";
|
|
2
|
+
import type { SwiftConverterContext } from "./SwiftConverter.js";
|
|
3
|
+
/**
|
|
4
|
+
* Applies the `unsupportedUnions` policy for an untagged union that survived
|
|
5
|
+
* merging and has no usable discriminator.
|
|
6
|
+
*/
|
|
7
|
+
export declare function handleUnsupportedUnion(c: SwiftConverterContext, ir: IRNode): string;
|
|
8
|
+
/**
|
|
9
|
+
* Pre-emission guard pass that throws on JSON Schema features Swift codegen
|
|
10
|
+
* does not support: `not` and `patternProperties`. These are NOT silenced by
|
|
11
|
+
* `unsupportedUnions: 'fallback'`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function guardUnsupportedKeywords(_c: SwiftConverterContext, root: IRNode): void;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the `AnyCodable` helper struct that wraps arbitrary JSON values for
|
|
16
|
+
* the unsupported-union fallback under `serializer: 'codable'`. Emitted only
|
|
17
|
+
* when at least one fallback path requested it via `c.needsAnyCodable`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function anyCodableHelper(c: SwiftConverterContext): string;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies the `unsupportedUnions` policy for an untagged union that survived
|
|
3
|
+
* merging and has no usable discriminator.
|
|
4
|
+
*/
|
|
5
|
+
export function handleUnsupportedUnion(c, ir) {
|
|
6
|
+
const policy = c.baseOpts?.unsupportedUnions ?? "throw";
|
|
7
|
+
if (policy === "throw") {
|
|
8
|
+
const opts = ir.options?.map((o) => o.type).join(", ") ?? "?";
|
|
9
|
+
throw new Error(`Unsupported: untagged union at "${ir.path}"\n` +
|
|
10
|
+
` options: [${opts}]\n` +
|
|
11
|
+
` Add a discriminator property with a const per variant, or set unsupportedUnions:'fallback' to emit AnyCodable.`);
|
|
12
|
+
}
|
|
13
|
+
// fallback
|
|
14
|
+
if (c.isCodable) {
|
|
15
|
+
c.needsAnyCodable = true;
|
|
16
|
+
return "AnyCodable";
|
|
17
|
+
}
|
|
18
|
+
return "Any";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Pre-emission guard pass that throws on JSON Schema features Swift codegen
|
|
22
|
+
* does not support: `not` and `patternProperties`. These are NOT silenced by
|
|
23
|
+
* `unsupportedUnions: 'fallback'`.
|
|
24
|
+
*/
|
|
25
|
+
export function guardUnsupportedKeywords(_c, root) {
|
|
26
|
+
const walk = (n) => {
|
|
27
|
+
if (n.not) {
|
|
28
|
+
throw new Error(`Unsupported: \`not\` keyword at "${n.path}".`);
|
|
29
|
+
}
|
|
30
|
+
if (n.patternProperties &&
|
|
31
|
+
Object.keys(n.patternProperties).length > 0) {
|
|
32
|
+
throw new Error(`Unsupported: patternProperties at "${n.path}".`);
|
|
33
|
+
}
|
|
34
|
+
if (n.properties) {
|
|
35
|
+
for (const k of Object.keys(n.properties))
|
|
36
|
+
walk(n.properties[k]);
|
|
37
|
+
}
|
|
38
|
+
if (n.items)
|
|
39
|
+
walk(n.items);
|
|
40
|
+
if (n.tupleItems)
|
|
41
|
+
for (const t of n.tupleItems)
|
|
42
|
+
walk(t);
|
|
43
|
+
if (n.options)
|
|
44
|
+
for (const o of n.options)
|
|
45
|
+
walk(o);
|
|
46
|
+
if (n.additionalProperties &&
|
|
47
|
+
typeof n.additionalProperties !== "boolean") {
|
|
48
|
+
walk(n.additionalProperties);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
walk(root);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns the `AnyCodable` helper struct that wraps arbitrary JSON values for
|
|
55
|
+
* the unsupported-union fallback under `serializer: 'codable'`. Emitted only
|
|
56
|
+
* when at least one fallback path requested it via `c.needsAnyCodable`.
|
|
57
|
+
*/
|
|
58
|
+
export function anyCodableHelper(c) {
|
|
59
|
+
const al = c.accessLevel;
|
|
60
|
+
return `${al} struct AnyCodable: Codable {
|
|
61
|
+
${al} let value: Any
|
|
62
|
+
${al} init(_ value: Any) { self.value = value }
|
|
63
|
+
${al} init(from decoder: Decoder) throws {
|
|
64
|
+
let container = try decoder.singleValueContainer()
|
|
65
|
+
if container.decodeNil() { self.value = (); return }
|
|
66
|
+
if let v = try? container.decode(Bool.self) { self.value = v; return }
|
|
67
|
+
if let v = try? container.decode(Int64.self) { self.value = v; return }
|
|
68
|
+
if let v = try? container.decode(Double.self) { self.value = v; return }
|
|
69
|
+
if let v = try? container.decode(String.self) { self.value = v; return }
|
|
70
|
+
if let v = try? container.decode([AnyCodable].self) { self.value = v; return }
|
|
71
|
+
if let v = try? container.decode([String: AnyCodable].self) { self.value = v; return }
|
|
72
|
+
throw DecodingError.typeMismatch(AnyCodable.self, .init(codingPath: decoder.codingPath, debugDescription: "Unsupported type"))
|
|
73
|
+
}
|
|
74
|
+
${al} func encode(to encoder: Encoder) throws {
|
|
75
|
+
var container = encoder.singleValueContainer()
|
|
76
|
+
switch self.value {
|
|
77
|
+
case let v as Bool: try container.encode(v)
|
|
78
|
+
case let v as Int64: try container.encode(v)
|
|
79
|
+
case let v as Double: try container.encode(v)
|
|
80
|
+
case let v as String: try container.encode(v)
|
|
81
|
+
case let v as [AnyCodable]: try container.encode(v)
|
|
82
|
+
case let v as [String: AnyCodable]: try container.encode(v)
|
|
83
|
+
default: try container.encodeNil()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}`;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=unsupported.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unsupported.js","sourceRoot":"","sources":["../../src/swift/unsupported.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,CAAwB,EAAE,EAAU;IACzE,MAAM,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,iBAAiB,IAAI,OAAO,CAAC;IACxD,IAAI,MAAM,KAAK,OAAO,EAAE,CAAC;QACvB,MAAM,IAAI,GAAG,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC;QAC9D,MAAM,IAAI,KAAK,CACb,mCAAmC,EAAE,CAAC,IAAI,KAAK;YAC7C,eAAe,IAAI,KAAK;YACxB,kHAAkH,CACrH,CAAC;IAC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I,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC,CAAC;IACF,IAAI,CAAC,IAAI,CAAC,CAAC;AACb,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,CAAwB;IACvD,MAAM,EAAE,GAAG,CAAC,CAAC,WAAW,CAAC;IACzB,OAAO,GAAG,EAAE;MACR,EAAE;MACF,EAAE;MACF,EAAE;;;;;;;;;;;MAWF,EAAE;;;;;;;;;;;;EAYN,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { IRNode } from "../types.js";
|
|
2
|
+
import { BaseConverter, GenerateTypeUtils } from "../converter/BaseConverter.js";
|
|
3
|
+
export type { RefTypeNamingConfig, GenerateTypeUtils } from "../converter/BaseConverter.js";
|
|
4
|
+
export type { RefTypeName, RefTypes, RefTypeEntry } from "../converter/BaseConverter.js";
|
|
5
|
+
export declare abstract class TypescriptBaseConverter extends BaseConverter {
|
|
6
|
+
protected emitJSDoc: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Recursively generates a TypeScript type string for the given IR node.
|
|
9
|
+
*/
|
|
10
|
+
protected generateType(ir: IRNode, utils: GenerateTypeUtils): string;
|
|
11
|
+
protected generateArrayType(ir: IRNode, utils: GenerateTypeUtils): string;
|
|
12
|
+
protected generateTupleType(ir: IRNode, utils: GenerateTypeUtils): string;
|
|
13
|
+
protected generateUnionType(ir: IRNode, utils: GenerateTypeUtils): string;
|
|
14
|
+
protected generateIntersectionType(ir: IRNode, utils: GenerateTypeUtils): string;
|
|
15
|
+
protected generateEnumType(ir: IRNode): string;
|
|
16
|
+
protected generateLiteralType(ir: IRNode): string;
|
|
17
|
+
protected getPrimitiveTypeMap(): Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Generates a TypeScript object type (as an inline type literal) from the given IR node.
|
|
20
|
+
* @internal Public for `BaseConverterContext`; treat as protected for subclasses.
|
|
21
|
+
*/
|
|
22
|
+
generateObjectType(ir: IRNode, utils: GenerateTypeUtils): string;
|
|
23
|
+
protected isValidIdentifier(name: string): boolean;
|
|
24
|
+
protected formatJSDoc(description: string, title?: string): string;
|
|
25
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { BaseConverter } from "../converter/BaseConverter.js";
|
|
2
|
+
export class TypescriptBaseConverter extends BaseConverter {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
this.emitJSDoc = false;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Recursively generates a TypeScript type string for the given IR node.
|
|
9
|
+
*/
|
|
10
|
+
generateType(ir, utils) {
|
|
11
|
+
switch (ir.type) {
|
|
12
|
+
case "object": {
|
|
13
|
+
const referencedType = utils.getReferencedType(ir);
|
|
14
|
+
if (referencedType)
|
|
15
|
+
return referencedType;
|
|
16
|
+
return this.generateObjectType(ir, utils);
|
|
17
|
+
}
|
|
18
|
+
case "array":
|
|
19
|
+
return this.generateArrayType(ir, utils);
|
|
20
|
+
case "tuple":
|
|
21
|
+
return this.generateTupleType(ir, utils);
|
|
22
|
+
case "union":
|
|
23
|
+
return this.generateUnionType(ir, utils);
|
|
24
|
+
case "intersection":
|
|
25
|
+
return this.generateIntersectionType(ir, utils);
|
|
26
|
+
case "enum":
|
|
27
|
+
return this.generateEnumType(ir);
|
|
28
|
+
case "literal":
|
|
29
|
+
return this.generateLiteralType(ir);
|
|
30
|
+
case "any":
|
|
31
|
+
return "any";
|
|
32
|
+
case "unknown":
|
|
33
|
+
return "unknown";
|
|
34
|
+
case "never":
|
|
35
|
+
return "never";
|
|
36
|
+
default: {
|
|
37
|
+
const primitiveMap = this.getPrimitiveTypeMap();
|
|
38
|
+
if (ir.type in primitiveMap) {
|
|
39
|
+
return primitiveMap[ir.type];
|
|
40
|
+
}
|
|
41
|
+
return "any";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
generateArrayType(ir, utils) {
|
|
46
|
+
if (ir.items) {
|
|
47
|
+
return `Array<${this.generateType(ir.items, utils)}>`;
|
|
48
|
+
}
|
|
49
|
+
return "any[]";
|
|
50
|
+
}
|
|
51
|
+
generateTupleType(ir, utils) {
|
|
52
|
+
if (ir.tupleItems && ir.tupleItems.length > 0) {
|
|
53
|
+
const elements = ir.tupleItems.map((item) => this.generateType(item, utils));
|
|
54
|
+
return `[${elements.join(", ")}]`;
|
|
55
|
+
}
|
|
56
|
+
return "[]";
|
|
57
|
+
}
|
|
58
|
+
generateUnionType(ir, utils) {
|
|
59
|
+
if (ir.options && ir.options.length > 0) {
|
|
60
|
+
return ir.options
|
|
61
|
+
.map((opt) => this.generateType(opt, utils))
|
|
62
|
+
.join(" | ");
|
|
63
|
+
}
|
|
64
|
+
return "never";
|
|
65
|
+
}
|
|
66
|
+
generateIntersectionType(ir, utils) {
|
|
67
|
+
if (ir.options && ir.options.length > 0) {
|
|
68
|
+
return ir.options
|
|
69
|
+
.map((opt) => this.generateType(opt, utils))
|
|
70
|
+
.join(" & ");
|
|
71
|
+
}
|
|
72
|
+
return "never";
|
|
73
|
+
}
|
|
74
|
+
generateEnumType(ir) {
|
|
75
|
+
if (ir.values && ir.values.length > 0) {
|
|
76
|
+
return ir.values.map((v) => JSON.stringify(v)).join(" | ");
|
|
77
|
+
}
|
|
78
|
+
return "never";
|
|
79
|
+
}
|
|
80
|
+
generateLiteralType(ir) {
|
|
81
|
+
return typeof ir.constraints?.value === "string"
|
|
82
|
+
? JSON.stringify(ir.constraints.value)
|
|
83
|
+
: String(ir.constraints?.value);
|
|
84
|
+
}
|
|
85
|
+
getPrimitiveTypeMap() {
|
|
86
|
+
return {
|
|
87
|
+
string: "string",
|
|
88
|
+
number: "number",
|
|
89
|
+
integer: "number",
|
|
90
|
+
boolean: "boolean",
|
|
91
|
+
null: "null",
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Generates a TypeScript object type (as an inline type literal) from the given IR node.
|
|
96
|
+
* @internal Public for `BaseConverterContext`; treat as protected for subclasses.
|
|
97
|
+
*/
|
|
98
|
+
generateObjectType(ir, utils) {
|
|
99
|
+
let generated = ``;
|
|
100
|
+
const generatedAdditionalValues = new Set();
|
|
101
|
+
if (ir.additionalProperties === true) {
|
|
102
|
+
generatedAdditionalValues.add("Record<string, any>");
|
|
103
|
+
}
|
|
104
|
+
else if (ir.additionalProperties) {
|
|
105
|
+
generatedAdditionalValues.add(`Record<string, ${this.generateType(ir.additionalProperties, utils)}>`);
|
|
106
|
+
}
|
|
107
|
+
if (ir.properties) {
|
|
108
|
+
const hasAnnotations = this.emitJSDoc &&
|
|
109
|
+
Object.values(ir.properties).some((p) => p.description || p.title);
|
|
110
|
+
const props = Object.keys(ir.properties).map((key) => {
|
|
111
|
+
const prop = ir.properties[key];
|
|
112
|
+
// Append '?' if the property is not required.
|
|
113
|
+
const optional = prop.required ? "" : "?";
|
|
114
|
+
const propKey = this.isValidIdentifier(key) ? key : `"${key}"`;
|
|
115
|
+
const propLine = `${propKey}${optional}: ${this.generateType(prop, utils)};`;
|
|
116
|
+
if (hasAnnotations && (prop.description || prop.title)) {
|
|
117
|
+
const jsdoc = prop.description
|
|
118
|
+
? this.formatJSDoc(prop.description, prop.title)
|
|
119
|
+
: this.formatJSDoc(prop.title);
|
|
120
|
+
return `${jsdoc}\n${propLine}`;
|
|
121
|
+
}
|
|
122
|
+
return propLine;
|
|
123
|
+
});
|
|
124
|
+
if (!props.length && generatedAdditionalValues.size === 0) {
|
|
125
|
+
generatedAdditionalValues.add("{ [key:string]: unknown }");
|
|
126
|
+
}
|
|
127
|
+
else if (props.length) {
|
|
128
|
+
if (hasAnnotations) {
|
|
129
|
+
const indented = props
|
|
130
|
+
.map((block) => block
|
|
131
|
+
.split("\n")
|
|
132
|
+
.map((line) => ` ${line}`)
|
|
133
|
+
.join("\n"))
|
|
134
|
+
.join("\n");
|
|
135
|
+
generated = `{\n${indented}\n}`;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
generated = `{ ${props.join(" ")} }`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let returnValue = ``;
|
|
143
|
+
if (generatedAdditionalValues.size > 0) {
|
|
144
|
+
if (generated) {
|
|
145
|
+
returnValue = `(${generated} & (${Array.from(generatedAdditionalValues).join(" & ")}))`;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
returnValue = Array.from(generatedAdditionalValues).join(" & ");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
returnValue = generated || "{ [key:string]: unknown }";
|
|
153
|
+
}
|
|
154
|
+
return returnValue;
|
|
155
|
+
}
|
|
156
|
+
isValidIdentifier(name) {
|
|
157
|
+
return /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(name);
|
|
158
|
+
}
|
|
159
|
+
formatJSDoc(description, title) {
|
|
160
|
+
const safe = description.replace(/\*\//g, "*\\/");
|
|
161
|
+
const parts = [];
|
|
162
|
+
if (title) {
|
|
163
|
+
parts.push(title.replace(/\*\//g, "*\\/"));
|
|
164
|
+
parts.push("");
|
|
165
|
+
parts.push(safe);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
parts.push(safe);
|
|
169
|
+
}
|
|
170
|
+
const text = parts.join("\n");
|
|
171
|
+
const lines = text.split("\n");
|
|
172
|
+
if (lines.length === 1) {
|
|
173
|
+
return `/** ${text} */`;
|
|
174
|
+
}
|
|
175
|
+
return `/**\n${lines.map((l) => ` * ${l}`).join("\n")}\n */`;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=TypescriptBaseConverter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TypescriptBaseConverter.js","sourceRoot":"","sources":["../../src/typescript/TypescriptBaseConverter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAqB,MAAM,+BAA+B,CAAC;AAMjF,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AES,iBAAiB,CAAC,IAAY;QACtC,OAAO,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjD,CAAC;IAES,WAAW,CAAC,WAAmB,EAAE,KAAc;QACvD,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAClD,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,IAAI,KAAK,EAAE,CAAC;YACV,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;YAC3C,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACf,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;QACD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO,OAAO,IAAI,KAAK,CAAC;QAC1B,CAAC;QACD,OAAO,QAAQ,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAC/D,CAAC;CAEF"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ILanguageConverter, IRNode } from "../types.js";
|
|
2
|
+
import { TypescriptBaseConverter } from "./TypescriptBaseConverter.js";
|
|
3
|
+
import { JSONSchema7Definition } from "json-schema";
|
|
4
|
+
import { BaseConverterOpts, LanguageProfile } from "../converter/BaseConverter.js";
|
|
5
|
+
export interface TypescriptConverterOpts extends BaseConverterOpts {
|
|
6
|
+
/**
|
|
7
|
+
* If true, referenced types will not be created for objects and instead
|
|
8
|
+
* the object type will be inlined.
|
|
9
|
+
*/
|
|
10
|
+
inlineTypes?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Controls how enum values are emitted.
|
|
13
|
+
* - `"union"` (default) → `"a" | "b" | "c"`
|
|
14
|
+
* - `"enum"` → `export enum Status { A = "a", B = "b", C = "c" }`
|
|
15
|
+
* Only applies when `inlineTypes` is false and all values are strings.
|
|
16
|
+
*/
|
|
17
|
+
enumStyle?: "union" | "enum";
|
|
18
|
+
/**
|
|
19
|
+
* If true, emit JSDoc comments from JSON Schema `description` annotations.
|
|
20
|
+
* Only applies when `inlineTypes` is false (the default).
|
|
21
|
+
*/
|
|
22
|
+
jsdoc?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A TypeScript language converter plugin.
|
|
26
|
+
*/
|
|
27
|
+
export declare class TypescriptConverter extends TypescriptBaseConverter implements ILanguageConverter {
|
|
28
|
+
/** @internal Public for `BaseConverterContext`; treat as protected for subclasses. */
|
|
29
|
+
readonly languageProfile: LanguageProfile;
|
|
30
|
+
private schemaConverter;
|
|
31
|
+
private opts;
|
|
32
|
+
private inlineTypes;
|
|
33
|
+
private enumStyle;
|
|
34
|
+
/**
|
|
35
|
+
* Stores enum declarations keyed by canonical signature (sorted JSON values)
|
|
36
|
+
* for deduplication across the schema.
|
|
37
|
+
*/
|
|
38
|
+
private enumDeclarations;
|
|
39
|
+
readonly code: string;
|
|
40
|
+
readonly rootTypeName: string;
|
|
41
|
+
readonly extractedTypeNames: string[];
|
|
42
|
+
readonly imports: string[];
|
|
43
|
+
constructor(schema: JSONSchema7Definition, opts?: TypescriptConverterOpts);
|
|
44
|
+
private computeRefTypeNamingConfigOverrides;
|
|
45
|
+
/**
|
|
46
|
+
* Pre-scans the IR tree to discover and register all enum declarations
|
|
47
|
+
* before type generation begins. Traversal order mirrors generateType's
|
|
48
|
+
* DFS order so that canonical-key deduplication picks the same "first"
|
|
49
|
+
* path that runtime generation would encounter.
|
|
50
|
+
*/
|
|
51
|
+
private preRegisterEnumNames;
|
|
52
|
+
protected generateEnumType(ir: IRNode): string;
|
|
53
|
+
protected generateLiteralType(ir: IRNode): string;
|
|
54
|
+
/**
|
|
55
|
+
* Returns (or creates) a deduplicated enum declaration for the given string values.
|
|
56
|
+
* Same set of values at different paths reuses the same enum.
|
|
57
|
+
*
|
|
58
|
+
* Name derivation uses PathUtils to strip array indices, then progressively
|
|
59
|
+
* includes parent path segments for collision resolution (e.g. "Type" →
|
|
60
|
+
* "ArgsType" → "DataArgsType") before falling back to a numeric suffix.
|
|
61
|
+
*/
|
|
62
|
+
private getOrCreateEnumDeclaration;
|
|
63
|
+
/**
|
|
64
|
+
* Derives a unique, valid TypeScript enum name from a dot-notation path.
|
|
65
|
+
* Strips array indices via PathUtils, then tries the last segment ("Type"),
|
|
66
|
+
* escalates to include parent segments on collision ("ArgsType", "DataArgsType"),
|
|
67
|
+
* and falls back to a numeric suffix as a last resort.
|
|
68
|
+
*/
|
|
69
|
+
private deriveEnumName;
|
|
70
|
+
/**
|
|
71
|
+
* Converts a string value to a valid PascalCase enum member name.
|
|
72
|
+
*/
|
|
73
|
+
private toEnumMemberName;
|
|
74
|
+
}
|