@mirascript/typed 0.1.78 → 0.1.80
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.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/json/impl.d.ts +8 -0
- package/dist/json/impl.d.ts.map +1 -0
- package/dist/json/impl.js +51 -0
- package/dist/json/impl.js.map +1 -0
- package/dist/{json.d.ts → json/index.d.ts} +2 -2
- package/dist/json/index.d.ts.map +1 -0
- package/dist/json/index.js +17 -0
- package/dist/json/index.js.map +1 -0
- package/dist/json/literal.d.ts +9 -0
- package/dist/json/literal.d.ts.map +1 -0
- package/dist/json/literal.js +16 -0
- package/dist/json/literal.js.map +1 -0
- package/dist/json/record.d.ts +6 -0
- package/dist/json/record.d.ts.map +1 -0
- package/dist/json/record.js +66 -0
- package/dist/json/record.js.map +1 -0
- package/dist/json/string.d.ts +5 -0
- package/dist/json/string.d.ts.map +1 -0
- package/dist/json/string.js +28 -0
- package/dist/json/string.js.map +1 -0
- package/dist/json/template.d.ts +8 -0
- package/dist/json/template.d.ts.map +1 -0
- package/dist/json/template.js +72 -0
- package/dist/json/template.js.map +1 -0
- package/dist/json/tuple.d.ts +8 -0
- package/dist/json/tuple.d.ts.map +1 -0
- package/dist/json/tuple.js +52 -0
- package/dist/json/tuple.js.map +1 -0
- package/dist/json/union-intersection.d.ts +8 -0
- package/dist/json/union-intersection.d.ts.map +1 -0
- package/dist/json/union-intersection.js +40 -0
- package/dist/json/union-intersection.js.map +1 -0
- package/dist/simplifier/dedup.d.ts +4 -0
- package/dist/simplifier/dedup.d.ts.map +1 -0
- package/dist/simplifier/dedup.js +61 -0
- package/dist/simplifier/dedup.js.map +1 -0
- package/dist/simplifier/impl.d.ts +7 -0
- package/dist/simplifier/impl.d.ts.map +1 -0
- package/dist/simplifier/impl.js +51 -0
- package/dist/simplifier/impl.js.map +1 -0
- package/dist/{simplifier.d.ts → simplifier/index.d.ts} +3 -5
- package/dist/simplifier/index.d.ts.map +1 -0
- package/dist/simplifier/index.js +22 -0
- package/dist/simplifier/index.js.map +1 -0
- package/dist/simplifier/intersection.d.ts +5 -0
- package/dist/simplifier/intersection.d.ts.map +1 -0
- package/dist/simplifier/intersection.js +126 -0
- package/dist/simplifier/intersection.js.map +1 -0
- package/dist/simplifier/record.d.ts +5 -0
- package/dist/simplifier/record.d.ts.map +1 -0
- package/dist/simplifier/record.js +24 -0
- package/dist/simplifier/record.js.map +1 -0
- package/dist/simplifier/top-type.d.ts +5 -0
- package/dist/simplifier/top-type.d.ts.map +1 -0
- package/dist/simplifier/top-type.js +9 -0
- package/dist/simplifier/top-type.js.map +1 -0
- package/dist/simplifier/tuple.d.ts +5 -0
- package/dist/simplifier/tuple.d.ts.map +1 -0
- package/dist/simplifier/tuple.js +17 -0
- package/dist/simplifier/tuple.js.map +1 -0
- package/dist/simplifier/union.d.ts +5 -0
- package/dist/simplifier/union.d.ts.map +1 -0
- package/dist/simplifier/union.js +47 -0
- package/dist/simplifier/union.js.map +1 -0
- package/dist/simplifier/utils.d.ts +12 -0
- package/dist/simplifier/utils.d.ts.map +1 -0
- package/dist/simplifier/utils.js +17 -0
- package/dist/simplifier/utils.js.map +1 -0
- package/package.json +5 -5
- package/src/index.ts +2 -2
- package/src/json/impl.ts +57 -0
- package/src/json/index.ts +28 -0
- package/src/json/literal.ts +20 -0
- package/src/json/record.ts +70 -0
- package/src/json/string.ts +30 -0
- package/src/json/template.ts +71 -0
- package/src/json/tuple.ts +52 -0
- package/src/json/union-intersection.ts +43 -0
- package/src/simplifier/dedup.ts +61 -0
- package/src/simplifier/impl.ts +64 -0
- package/src/simplifier/index.ts +67 -0
- package/src/simplifier/intersection.ts +129 -0
- package/src/simplifier/record.ts +24 -0
- package/src/simplifier/top-type.ts +9 -0
- package/src/simplifier/tuple.ts +20 -0
- package/src/simplifier/union.ts +45 -0
- package/src/simplifier/utils.ts +21 -0
- package/dist/json.d.ts.map +0 -1
- package/dist/json.js +0 -314
- package/dist/json.js.map +0 -1
- package/dist/simplifier.d.ts.map +0 -1
- package/dist/simplifier.js +0 -332
- package/dist/simplifier.js.map +0 -1
- package/src/json.ts +0 -323
- package/src/simplifier.ts +0 -404
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { simplifyIntersection } from './intersection.js';
|
|
2
|
+
import { simplifyUnion } from './union.js';
|
|
3
|
+
import { simplifyRecord } from './record.js';
|
|
4
|
+
import { simplifyTuple } from './tuple.js';
|
|
5
|
+
/** Simplifies a Type AST in place, optionally disabling individual normalization passes. */
|
|
6
|
+
export function simplifyImpl(type, options) {
|
|
7
|
+
if (typeof type == 'symbol' || typeof type == 'string') {
|
|
8
|
+
return type;
|
|
9
|
+
}
|
|
10
|
+
if (type.kind === 'array') {
|
|
11
|
+
type.element = simplifyImpl(type.element, options);
|
|
12
|
+
if (options.normalizeGenericArray && (type.element === 'any' || type.element === 'unknown')) {
|
|
13
|
+
return 'array';
|
|
14
|
+
}
|
|
15
|
+
return type;
|
|
16
|
+
}
|
|
17
|
+
if (type.kind === 'function') {
|
|
18
|
+
for (const param of type.params) {
|
|
19
|
+
param.type = simplifyImpl(param.type, options);
|
|
20
|
+
}
|
|
21
|
+
if (type.returns != null)
|
|
22
|
+
type.returns = simplifyImpl(type.returns, options);
|
|
23
|
+
return type;
|
|
24
|
+
}
|
|
25
|
+
if (type.kind === 'literal') {
|
|
26
|
+
return type;
|
|
27
|
+
}
|
|
28
|
+
if (type.kind === 'template') {
|
|
29
|
+
type.parts = type.parts.map((part) => simplifyImpl(part, options));
|
|
30
|
+
return type;
|
|
31
|
+
}
|
|
32
|
+
if (type.kind === 'tuple') {
|
|
33
|
+
return simplifyTuple(type, options);
|
|
34
|
+
}
|
|
35
|
+
if (type.kind === 'reflection') {
|
|
36
|
+
return type;
|
|
37
|
+
}
|
|
38
|
+
if (type.kind === 'record') {
|
|
39
|
+
return simplifyRecord(type, options);
|
|
40
|
+
}
|
|
41
|
+
if (type.kind === 'union') {
|
|
42
|
+
return simplifyUnion(type, options);
|
|
43
|
+
}
|
|
44
|
+
if (type.kind === 'intersection') {
|
|
45
|
+
return simplifyIntersection(type, options);
|
|
46
|
+
}
|
|
47
|
+
/* c8 ignore next 3 */
|
|
48
|
+
(type);
|
|
49
|
+
return type;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"impl.js","sourceRoot":"","sources":["../../src/simplifier/impl.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3C,4FAA4F;AAC5F,MAAM,UAAU,YAAY,CAAC,IAAU,EAAE,OAA4B;IACjE,IAAI,OAAO,IAAI,IAAI,QAAQ,IAAI,OAAO,IAAI,IAAI,QAAQ,EAAE,CAAC;QACrD,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACnD,IAAI,OAAO,CAAC,qBAAqB,IAAI,CAAC,IAAI,CAAC,OAAO,KAAK,KAAK,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS,CAAC,EAAE,CAAC;YAC1F,OAAO,OAAO,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QAC3B,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAC9B,KAAK,CAAC,IAAI,GAA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@@
|
|
|
1
|
-
import type { Type } from '
|
|
2
|
-
|
|
3
|
-
type TopType = 'unknown' | 'never' | 'any';
|
|
1
|
+
import type { Type } from '../parser.js';
|
|
2
|
+
import type { TopType } from './top-type.js';
|
|
4
3
|
/** Controls which type simplifications are applied. */
|
|
5
4
|
export interface SimplifyOptions {
|
|
6
5
|
/** Flatten nested union nodes. */
|
|
@@ -44,5 +43,4 @@ export interface SimplifyOptions {
|
|
|
44
43
|
}
|
|
45
44
|
/** Simplifies a Type AST in place, optionally disabling individual normalization passes. */
|
|
46
45
|
export declare function simplify(type: Type, options?: SimplifyOptions): Type;
|
|
47
|
-
|
|
48
|
-
//# sourceMappingURL=simplifier.d.ts.map
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/simplifier/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C,uDAAuD;AACvD,MAAM,WAAW,eAAe;IAC5B,kCAAkC;IAClC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,yCAAyC;IACzC,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,mDAAmD;IACnD,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,0DAA0D;IAC1D,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,wCAAwC;IACxC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,+CAA+C;IAC/C,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,4CAA4C;IAC5C,iCAAiC,CAAC,EAAE,OAAO,CAAC;IAC5C,qDAAqD;IACrD,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,sDAAsD;IACtD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;;;;OAMG;IACH,wBAAwB,CAAC,EAAE,OAAO,GAAG,OAAO,EAAE,CAAC;IAC/C;;;;;;OAMG;IACH,+BAA+B,CAAC,EAAE,OAAO,GAAG,OAAO,EAAE,CAAC;IACtD,qEAAqE;IACrE,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,gEAAgE;IAChE,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACnC;AAkBD,4FAA4F;AAC5F,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,IAAI,CAGpE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { simplifyImpl } from './impl.js';
|
|
2
|
+
const DEFAULT_OPTIONS = {
|
|
3
|
+
flattenUnions: true,
|
|
4
|
+
flattenIntersections: true,
|
|
5
|
+
deduplicateUnions: true,
|
|
6
|
+
deduplicateIntersections: true,
|
|
7
|
+
unwrapSingleUnion: true,
|
|
8
|
+
unwrapSingleIntersection: true,
|
|
9
|
+
distributeIntersectionsOverUnions: true,
|
|
10
|
+
mergeRecordIntersections: true,
|
|
11
|
+
expandTupleSpreads: true,
|
|
12
|
+
simplifyTopTypesInUnions: true,
|
|
13
|
+
simplifyTopTypesInIntersections: true,
|
|
14
|
+
normalizeGenericRecord: true,
|
|
15
|
+
normalizeGenericArray: true,
|
|
16
|
+
};
|
|
17
|
+
/** Simplifies a Type AST in place, optionally disabling individual normalization passes. */
|
|
18
|
+
export function simplify(type, options) {
|
|
19
|
+
const config = { ...DEFAULT_OPTIONS, ...options };
|
|
20
|
+
return simplifyImpl(type, config);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/simplifier/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AA6CzC,MAAM,eAAe,GAA8B;IAC/C,aAAa,EAAE,IAAI;IACnB,oBAAoB,EAAE,IAAI;IAC1B,iBAAiB,EAAE,IAAI;IACvB,wBAAwB,EAAE,IAAI;IAC9B,iBAAiB,EAAE,IAAI;IACvB,wBAAwB,EAAE,IAAI;IAC9B,iCAAiC,EAAE,IAAI;IACvC,wBAAwB,EAAE,IAAI;IAC9B,kBAAkB,EAAE,IAAI;IACxB,wBAAwB,EAAE,IAAI;IAC9B,+BAA+B,EAAE,IAAI;IACrC,sBAAsB,EAAE,IAAI;IAC5B,qBAAqB,EAAE,IAAI;CAC9B,CAAC;AAEF,4FAA4F;AAC5F,MAAM,UAAU,QAAQ,CAAC,IAAU,EAAE,OAAyB;IAC1D,MAAM,MAAM,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;IAClD,OAAO,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { IntersectionType, Type } from '../parser.js';
|
|
2
|
+
import { type SimplifyImplOptions } from './impl.js';
|
|
3
|
+
/** Simplifies an intersection type */
|
|
4
|
+
export declare function simplifyIntersection(type: IntersectionType, options: SimplifyImplOptions): Type;
|
|
5
|
+
//# sourceMappingURL=intersection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersection.d.ts","sourceRoot":"","sources":["../../src/simplifier/intersection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAA2B,MAAM,cAAc,CAAC;AAEpF,OAAO,EAAE,KAAK,mBAAmB,EAAgB,MAAM,WAAW,CAAC;AAwEnE,sCAAsC;AACtC,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,mBAAmB,GAAG,IAAI,CAqD/F"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { deduplicateTypeMembers } from './dedup.js';
|
|
2
|
+
import { simplifyImpl } from './impl.js';
|
|
3
|
+
import { resolveTopTypes } from './top-type.js';
|
|
4
|
+
import { isFieldRecordType, isUnionType, isIntersectionType } from './utils.js';
|
|
5
|
+
/** Merges explicit record fields across an intersection. */
|
|
6
|
+
function mergeRecordFieldIntersections(types) {
|
|
7
|
+
const merged = new Map();
|
|
8
|
+
for (const record of types) {
|
|
9
|
+
for (const field of record.fields) {
|
|
10
|
+
const prev = merged.get(field.name);
|
|
11
|
+
if (prev == null) {
|
|
12
|
+
merged.set(field.name, {
|
|
13
|
+
optional: field.optional ?? false,
|
|
14
|
+
type: field.type,
|
|
15
|
+
});
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
merged.set(field.name, {
|
|
19
|
+
optional: (prev.optional ?? false) && (field.optional ?? false),
|
|
20
|
+
type: {
|
|
21
|
+
kind: 'intersection',
|
|
22
|
+
types: [prev.type, field.type],
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
kind: 'record',
|
|
29
|
+
fields: Array.from(merged.entries()).map(([name, field]) => ({
|
|
30
|
+
name,
|
|
31
|
+
optional: field.optional,
|
|
32
|
+
type: field.type,
|
|
33
|
+
})),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Flattens nested intersection nodes when the corresponding option is enabled. */
|
|
37
|
+
function flattenIntersectionTypes(type, options) {
|
|
38
|
+
const types = type.types.map((item) => simplifyImpl(item, options));
|
|
39
|
+
if (!options.flattenIntersections)
|
|
40
|
+
return types;
|
|
41
|
+
const result = [];
|
|
42
|
+
for (const type of types) {
|
|
43
|
+
if (isIntersectionType(type)) {
|
|
44
|
+
result.push(...flattenIntersectionTypes(type, options));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
result.push(type);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
/** Distributes intersections over unions using a cartesian product. */
|
|
53
|
+
function distributeIntersectionsOverUnions(types, options) {
|
|
54
|
+
let combinations = [[]];
|
|
55
|
+
for (const type of types) {
|
|
56
|
+
const choices = isUnionType(type) ? type.types : [type];
|
|
57
|
+
const next = [];
|
|
58
|
+
for (const combo of combinations) {
|
|
59
|
+
for (const choice of choices) {
|
|
60
|
+
next.push([...combo, choice]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
combinations = next;
|
|
64
|
+
}
|
|
65
|
+
const opt = { ...options, distributeIntersectionsOverUnions: false };
|
|
66
|
+
const branches = combinations.map((combination) => simplifyImpl({ kind: 'intersection', types: combination }, opt));
|
|
67
|
+
if (branches.length === 1)
|
|
68
|
+
return branches[0];
|
|
69
|
+
return { kind: 'union', types: branches };
|
|
70
|
+
}
|
|
71
|
+
/** Simplifies an intersection type */
|
|
72
|
+
export function simplifyIntersection(type, options) {
|
|
73
|
+
let simplifiedTypes = flattenIntersectionTypes(type, options);
|
|
74
|
+
// Top-type elimination: never & T → never, T & unknown → T, T & any → T
|
|
75
|
+
const topTypes = resolveTopTypes(options.simplifyTopTypesInIntersections);
|
|
76
|
+
if (topTypes.length > 0) {
|
|
77
|
+
const typeNames = new Set(simplifiedTypes.filter((t) => typeof t === 'string'));
|
|
78
|
+
if (topTypes.includes('never') && typeNames.has('never'))
|
|
79
|
+
return 'never';
|
|
80
|
+
let filtered = false;
|
|
81
|
+
if (topTypes.includes('unknown') && typeNames.has('unknown')) {
|
|
82
|
+
simplifiedTypes = simplifiedTypes.filter((t) => t !== 'unknown');
|
|
83
|
+
filtered = true;
|
|
84
|
+
}
|
|
85
|
+
if (topTypes.includes('any') && typeNames.has('any')) {
|
|
86
|
+
simplifiedTypes = simplifiedTypes.filter((t) => t !== 'any');
|
|
87
|
+
filtered = true;
|
|
88
|
+
}
|
|
89
|
+
if (filtered) {
|
|
90
|
+
if (simplifiedTypes.length === 1)
|
|
91
|
+
return simplifiedTypes[0];
|
|
92
|
+
if (simplifiedTypes.length === 0) {
|
|
93
|
+
// All members were the same eliminated top type
|
|
94
|
+
return typeNames.has('any') ? 'any' : 'unknown';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (options.deduplicateIntersections) {
|
|
99
|
+
simplifiedTypes = deduplicateTypeMembers(simplifiedTypes);
|
|
100
|
+
}
|
|
101
|
+
if (options.distributeIntersectionsOverUnions && simplifiedTypes.some(isUnionType)) {
|
|
102
|
+
return distributeIntersectionsOverUnions(simplifiedTypes, options);
|
|
103
|
+
}
|
|
104
|
+
if (options.mergeRecordIntersections) {
|
|
105
|
+
const recordTypes = simplifiedTypes.filter(isFieldRecordType);
|
|
106
|
+
if (recordTypes.length >= 2) {
|
|
107
|
+
const nonRecordTypes = simplifiedTypes.filter((item) => !isFieldRecordType(item));
|
|
108
|
+
const mergedRecord = simplifyImpl(mergeRecordFieldIntersections(recordTypes), options);
|
|
109
|
+
let mergedTypes = [mergedRecord, ...nonRecordTypes];
|
|
110
|
+
if (options.deduplicateIntersections) {
|
|
111
|
+
mergedTypes = deduplicateTypeMembers(mergedTypes);
|
|
112
|
+
}
|
|
113
|
+
if (options.unwrapSingleIntersection && mergedTypes.length === 1) {
|
|
114
|
+
return mergedTypes[0];
|
|
115
|
+
}
|
|
116
|
+
type.types = mergedTypes;
|
|
117
|
+
return type;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (options.unwrapSingleIntersection && simplifiedTypes.length === 1) {
|
|
121
|
+
return simplifiedTypes[0];
|
|
122
|
+
}
|
|
123
|
+
type.types = simplifiedTypes;
|
|
124
|
+
return type;
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=intersection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../src/simplifier/intersection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAA4B,YAAY,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,eAAe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C,YAAY,EAAE,GAAG,cAAc,CAAC,CAAC;YACpD,IAAI,OAAO,CAAC,wBAAwB,EAAE,CAAC;gBACnC,WAAW,GAAG,sBAAsB,CAAC,WAAW,CAAC,CAAC;YACtD,CAAC;YACD,IAAI,OAAO,CAAC,wBAAwB,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC/D,OAAO,WAAW,CAAC,CAAC,CAAE,CAAC;YAC3B,CAAC;YACD,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC;YACzB,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC;IACD,IAAI,OAAO,CAAC,wBAAwB,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnE,OAAO,eAAe,CAAC,CAAC,CAAE,CAAC;IAC/B,CAAC;IACD,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC;IAC7B,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RecordType } from '../parser.js';
|
|
2
|
+
import { type SimplifyImplOptions } from './impl.js';
|
|
3
|
+
/** Simplifies a record recursively. */
|
|
4
|
+
export declare function simplifyRecord(record: RecordType, options: SimplifyImplOptions): RecordType;
|
|
5
|
+
//# sourceMappingURL=record.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../../src/simplifier/record.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,UAAU,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,KAAK,mBAAmB,EAAgB,MAAM,WAAW,CAAC;AAUnE,uCAAuC;AACvC,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,GAAG,UAAU,CAW3F"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { simplifyImpl } from './impl.js';
|
|
2
|
+
/** Simplifies a record field recursively. */
|
|
3
|
+
function simplifyRecordField(field, options) {
|
|
4
|
+
return {
|
|
5
|
+
...field,
|
|
6
|
+
type: simplifyImpl(field.type, options),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Simplifies a record recursively. */
|
|
10
|
+
export function simplifyRecord(record, options) {
|
|
11
|
+
if ('fields' in record) {
|
|
12
|
+
record.fields = record.fields.map((field) => simplifyRecordField(field, options));
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
if (record.key != null)
|
|
16
|
+
record.key = simplifyImpl(record.key, options);
|
|
17
|
+
record.value = simplifyImpl(record.value, options);
|
|
18
|
+
if (options.normalizeGenericRecord && record.key === 'string') {
|
|
19
|
+
delete record.key;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return record;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.js","sourceRoot":"","sources":["../../src/simplifier/record.ts"],"names":[],"mappings":"AACA,OAAO,EAA4B,YAAY,EAAE,MAAM,WAAW,CAAC;AAEnE,6CAA6C;AAC7C,SAAS,mBAAmB,CAAC,KAAkB,EAAE,OAA4B;IACzE,OAAO;QACH,GAAG,KAAK;QACR,IAAI,EAAE,YAAY,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC;KAC1C,CAAC;AACN,CAAC;AAED,uCAAuC;AACvC,MAAM,UAAU,cAAc,CAAC,MAAkB,EAAE,OAA4B;IAC3E,IAAI,QAAQ,IAAI,MAAM,EAAE,CAAC;QACrB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,mBAAmB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IACtF,CAAC;SAAM,CAAC;QACJ,IAAI,MAAM,CAAC,GAAG,IAAI,IAAI;YAAE,MAAM,CAAC,GAAG,GAAG,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACvE,MAAM,CAAC,KAAK,GAAG,YAAY,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnD,IAAI,OAAO,CAAC,sBAAsB,IAAI,MAAM,CAAC,GAAG,KAAK,QAAQ,EAAE,CAAC;YAC5D,OAAO,MAAM,CAAC,GAAG,CAAC;QACtB,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Top types that can be absorbed / eliminated in simplification. */
|
|
2
|
+
export type TopType = 'unknown' | 'never' | 'any';
|
|
3
|
+
/** Resolves the top types option into an array of top types. */
|
|
4
|
+
export declare function resolveTopTypes(value: boolean | TopType[] | undefined): TopType[];
|
|
5
|
+
//# sourceMappingURL=top-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"top-type.d.ts","sourceRoot":"","sources":["../../src/simplifier/top-type.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,MAAM,MAAM,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,KAAK,CAAC;AAElD,gEAAgE;AAChE,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,EAAE,GAAG,SAAS,GAAG,OAAO,EAAE,CAIjF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Resolves the top types option into an array of top types. */
|
|
2
|
+
export function resolveTopTypes(value) {
|
|
3
|
+
if (value === false || value == null)
|
|
4
|
+
return [];
|
|
5
|
+
if (value === true)
|
|
6
|
+
return ['unknown', 'never', 'any'];
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=top-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"top-type.js","sourceRoot":"","sources":["../../src/simplifier/top-type.ts"],"names":[],"mappings":"AAGA,gEAAgE;AAChE,MAAM,UAAU,eAAe,CAAC,KAAsC;IAClE,IAAI,KAAK,KAAK,KAAK,IAAI,KAAK,IAAI,IAAI;QAAE,OAAO,EAAE,CAAC;IAChD,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,CAAC,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACvD,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../../src/simplifier/tuple.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAgB,KAAK,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEnE,6BAA6B;AAC7B,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,mBAAmB,GAAG,SAAS,CAetF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { simplifyImpl } from './impl.js';
|
|
2
|
+
/** Simplifies tuple types */
|
|
3
|
+
export function simplifyTuple(type, options) {
|
|
4
|
+
type.elements = type.elements.flatMap((element) => {
|
|
5
|
+
const simplifiedType = simplifyImpl(element.type, options);
|
|
6
|
+
if (options.expandTupleSpreads &&
|
|
7
|
+
element.spread &&
|
|
8
|
+
typeof simplifiedType == 'object' &&
|
|
9
|
+
simplifiedType.kind === 'tuple') {
|
|
10
|
+
// Inline tuple spread: ..[A, B] → A, B
|
|
11
|
+
return simplifiedType.elements;
|
|
12
|
+
}
|
|
13
|
+
return [{ ...element, type: simplifiedType }];
|
|
14
|
+
});
|
|
15
|
+
return type;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=tuple.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tuple.js","sourceRoot":"","sources":["../../src/simplifier/tuple.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAA4B,MAAM,WAAW,CAAC;AAEnE,6BAA6B;AAC7B,MAAM,UAAU,aAAa,CAAC,IAAe,EAAE,OAA4B;IACvE,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC9C,MAAM,cAAc,GAAG,YAAY,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC3D,IACI,OAAO,CAAC,kBAAkB;YAC1B,OAAO,CAAC,MAAM;YACd,OAAO,cAAc,IAAI,QAAQ;YACjC,cAAc,CAAC,IAAI,KAAK,OAAO,EACjC,CAAC;YACC,uCAAuC;YACvC,OAAO,cAAc,CAAC,QAAQ,CAAC;QACnC,CAAC;QACD,OAAO,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IACH,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Type, UnionType } from '../parser.js';
|
|
2
|
+
import { type SimplifyImplOptions } from './impl.js';
|
|
3
|
+
/** Simplifies a union recursively. */
|
|
4
|
+
export declare function simplifyUnion(type: UnionType, options: SimplifyImplOptions): Type;
|
|
5
|
+
//# sourceMappingURL=union.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/simplifier/union.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAgB,KAAK,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAmBnE,sCAAsC;AACtC,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,mBAAmB,GAAG,IAAI,CAsBjF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { deduplicateTypeMembers } from './dedup.js';
|
|
2
|
+
import { simplifyImpl } from './impl.js';
|
|
3
|
+
import { resolveTopTypes } from './top-type.js';
|
|
4
|
+
import { isTypeObject } from './utils.js';
|
|
5
|
+
/** Flattens nested union nodes when the corresponding option is enabled. */
|
|
6
|
+
function flattenUnionTypes(type, options) {
|
|
7
|
+
const types = type.types.map((item) => simplifyImpl(item, options));
|
|
8
|
+
if (!options.flattenUnions)
|
|
9
|
+
return types;
|
|
10
|
+
const result = [];
|
|
11
|
+
for (const type of types) {
|
|
12
|
+
if (isTypeObject(type) && type.kind === 'union') {
|
|
13
|
+
result.push(...flattenUnionTypes(type, options));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
result.push(type);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
/** Simplifies a union recursively. */
|
|
22
|
+
export function simplifyUnion(type, options) {
|
|
23
|
+
let simplifiedTypes = flattenUnionTypes(type, options);
|
|
24
|
+
// Top-type elimination: unknown | T → unknown, any | T → any, T | never → T
|
|
25
|
+
const topTypes = resolveTopTypes(options.simplifyTopTypesInUnions);
|
|
26
|
+
if (topTypes.length > 0) {
|
|
27
|
+
if (topTypes.includes('any') && simplifiedTypes.includes('any'))
|
|
28
|
+
return 'any';
|
|
29
|
+
if (topTypes.includes('unknown') && simplifiedTypes.includes('unknown'))
|
|
30
|
+
return 'unknown';
|
|
31
|
+
if (topTypes.includes('never')) {
|
|
32
|
+
simplifiedTypes = simplifiedTypes.filter((t) => t !== 'never');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (options.deduplicateUnions) {
|
|
36
|
+
simplifiedTypes = deduplicateTypeMembers(simplifiedTypes);
|
|
37
|
+
}
|
|
38
|
+
if (options.unwrapSingleUnion) {
|
|
39
|
+
if (simplifiedTypes.length === 1)
|
|
40
|
+
return simplifiedTypes[0];
|
|
41
|
+
if (simplifiedTypes.length === 0)
|
|
42
|
+
return 'never';
|
|
43
|
+
}
|
|
44
|
+
type.types = simplifiedTypes;
|
|
45
|
+
return type;
|
|
46
|
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AG,CAAC,EAAE,CAAC;QACtB,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC;QAC9E,IAAI,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,eAAe,CAAC,QAAQ,CAAC,SAAS,CAAC;YAAE,OAAO,SAAS,CAAC;QAC1F,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAC7B,eAAe,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC;QACnE,CAAC;IACL,CAAC;IAED,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;QAC5B,eAAe,GAAG,sBAAsB,CAAC,eAAe,CAAC,CAAC;IAC9D,CAAC;IACD,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;QAC5B,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,eAAe,CAAC,CAAC,CAAE,CAAC;QAC7D,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,OAAO,CAAC;IACrD,CAAC;IACD,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC;IAC7B,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Type, GenericType, RecordField, RecordType, UnionType, IntersectionType } from '../parser.js';
|
|
2
|
+
/** Checks whether a type is represented by an object node. */
|
|
3
|
+
export declare function isTypeObject(type: Type): type is Exclude<Type, GenericType | string>;
|
|
4
|
+
/** Checks whether a type is union. */
|
|
5
|
+
export declare function isUnionType(type: Type): type is UnionType;
|
|
6
|
+
/** Checks whether a type is intersection. */
|
|
7
|
+
export declare function isIntersectionType(type: Type): type is IntersectionType;
|
|
8
|
+
/** Checks whether a record type uses the explicit fields form. */
|
|
9
|
+
export declare function isFieldRecordType(type: Type): type is Extract<RecordType, {
|
|
10
|
+
fields: RecordField[];
|
|
11
|
+
}>;
|
|
12
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/simplifier/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAE5G,8DAA8D;AAC9D,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAAC,CAEpF;AAED,sCAAsC;AACtC,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,IAAI,SAAS,CAEzD;AAED,6CAA6C;AAC7C,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,IAAI,gBAAgB,CAEvE;AAED,kEAAkE;AAClE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE;IAAE,MAAM,EAAE,WAAW,EAAE,CAAA;CAAE,CAAC,CAEpG"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Checks whether a type is represented by an object node. */
|
|
2
|
+
export function isTypeObject(type) {
|
|
3
|
+
return typeof type === 'object';
|
|
4
|
+
}
|
|
5
|
+
/** Checks whether a type is union. */
|
|
6
|
+
export function isUnionType(type) {
|
|
7
|
+
return isTypeObject(type) && type.kind === 'union';
|
|
8
|
+
}
|
|
9
|
+
/** Checks whether a type is intersection. */
|
|
10
|
+
export function isIntersectionType(type) {
|
|
11
|
+
return isTypeObject(type) && type.kind === 'intersection';
|
|
12
|
+
}
|
|
13
|
+
/** Checks whether a record type uses the explicit fields form. */
|
|
14
|
+
export function isFieldRecordType(type) {
|
|
15
|
+
return isTypeObject(type) && type.kind === 'record' && 'fields' in type;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/simplifier/utils.ts"],"names":[],"mappings":"AAEA,8DAA8D;AAC9D,MAAM,UAAU,YAAY,CAAC,IAAU;IACnC,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC;AACpC,CAAC;AAED,sCAAsC;AACtC,MAAM,UAAU,WAAW,CAAC,IAAU;IAClC,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC;AACvD,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,kBAAkB,CAAC,IAAU;IACzC,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC;AAC9D,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,iBAAiB,CAAC,IAAU;IACxC,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,QAAQ,IAAI,IAAI,CAAC;AAC5E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mirascript/typed",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.80",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Parse MiraScript type definitions and generate TypeScript types / JSON schemas.",
|
|
@@ -23,16 +23,16 @@
|
|
|
23
23
|
"./package.json": "./package.json"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@types/node": "^26.1.
|
|
26
|
+
"@types/node": "^26.1.2",
|
|
27
27
|
"ava": "^8.0.1",
|
|
28
28
|
"c8": "^12.0.0",
|
|
29
|
-
"js-yaml": "^5.2.
|
|
29
|
+
"js-yaml": "^5.2.3",
|
|
30
30
|
"peggy": "^5.1.0",
|
|
31
|
-
"@mirascript/mirascript": "~0.1.
|
|
31
|
+
"@mirascript/mirascript": "~0.1.80"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"json-schema-typed": "^8.0.2",
|
|
35
|
-
"@mirascript/constants": "~0.1.
|
|
35
|
+
"@mirascript/constants": "~0.1.80"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "peggy src/type.peggy --multi-output ./dist --dts --format es --source-map && tsc",
|
package/src/index.ts
CHANGED
package/src/json/impl.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed';
|
|
2
|
+
import type { Type } from '../parser.js';
|
|
3
|
+
import { simplify } from '../simplifier/index.js';
|
|
4
|
+
import type { ToJSONSchemaOptions } from './index.js';
|
|
5
|
+
import { string } from './string.js';
|
|
6
|
+
import { template } from './template.js';
|
|
7
|
+
import { tuple } from './tuple.js';
|
|
8
|
+
import { literal } from './literal.js';
|
|
9
|
+
import { record } from './record.js';
|
|
10
|
+
import { intersection, union } from './union-intersection.js';
|
|
11
|
+
|
|
12
|
+
/** Options for toJSONSchemaImpl */
|
|
13
|
+
export type ToJSONSchemaOptionsImpl = Required<ToJSONSchemaOptions>;
|
|
14
|
+
|
|
15
|
+
/** Converts a Type object into JSON Schema */
|
|
16
|
+
export function toJSONSchemaImpl(type: Type, options: ToJSONSchemaOptionsImpl): JSONSchema {
|
|
17
|
+
const simplified = simplify(type);
|
|
18
|
+
if (typeof simplified == 'symbol') {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if (typeof simplified == 'string') {
|
|
22
|
+
return string(simplified);
|
|
23
|
+
}
|
|
24
|
+
if (simplified.kind === 'array') {
|
|
25
|
+
return {
|
|
26
|
+
type: 'array',
|
|
27
|
+
items: toJSONSchemaImpl(simplified.element, options),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (simplified.kind === 'union') {
|
|
31
|
+
return union(simplified, options);
|
|
32
|
+
}
|
|
33
|
+
if (simplified.kind === 'intersection') {
|
|
34
|
+
return intersection(simplified, options);
|
|
35
|
+
}
|
|
36
|
+
if (simplified.kind === 'record') {
|
|
37
|
+
return record(simplified, options);
|
|
38
|
+
}
|
|
39
|
+
if (simplified.kind === 'literal') {
|
|
40
|
+
return literal(simplified);
|
|
41
|
+
}
|
|
42
|
+
if (simplified.kind === 'function') {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (simplified.kind === 'template') {
|
|
46
|
+
return template(simplified);
|
|
47
|
+
}
|
|
48
|
+
if (simplified.kind === 'tuple') {
|
|
49
|
+
return tuple(simplified, options);
|
|
50
|
+
}
|
|
51
|
+
if (simplified.kind === 'reflection') {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
/* c8 ignore next 3 */
|
|
55
|
+
simplified satisfies never;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type JSONSchema, $schema } from 'json-schema-typed';
|
|
2
|
+
import type { Type } from '../parser.js';
|
|
3
|
+
import { toJSONSchemaImpl } from './impl.js';
|
|
4
|
+
|
|
5
|
+
export type { JSONSchema } from 'json-schema-typed';
|
|
6
|
+
|
|
7
|
+
/** Options for toJSONSchema */
|
|
8
|
+
export interface ToJSONSchemaOptions {
|
|
9
|
+
/** When true, object schemas allow arbitrary additional properties */
|
|
10
|
+
loose?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Converts a Type object into JSON Schema */
|
|
14
|
+
export function toJSONSchema(
|
|
15
|
+
type: Type,
|
|
16
|
+
options?: ToJSONSchemaOptions,
|
|
17
|
+
): JSONSchema.Interface & { $schema: typeof $schema } {
|
|
18
|
+
let schema = toJSONSchemaImpl(type, { loose: options?.loose ?? false });
|
|
19
|
+
if (schema === true) {
|
|
20
|
+
schema = {};
|
|
21
|
+
} else if (schema === false) {
|
|
22
|
+
schema = { not: true };
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
...schema,
|
|
26
|
+
$schema,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed';
|
|
2
|
+
import type { LiteralType, Type } from '../parser.js';
|
|
3
|
+
|
|
4
|
+
/** Converts a LiteralType into JSON Schema */
|
|
5
|
+
export function literal(type: LiteralType): JSONSchema {
|
|
6
|
+
return { const: type.value };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Converts a union of LiteralTypes into a single JSON Schema enum */
|
|
10
|
+
export function literalEnum(types: LiteralType[]): JSONSchema {
|
|
11
|
+
const schema: JSONSchema = {
|
|
12
|
+
enum: types.map((t) => t.value),
|
|
13
|
+
};
|
|
14
|
+
return schema;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Type guard for LiteralType */
|
|
18
|
+
export function isLiteralType(type: Type): type is LiteralType {
|
|
19
|
+
return typeof type == 'object' && type.kind === 'literal';
|
|
20
|
+
}
|