@formspec/build 0.1.0-alpha.10 → 0.1.0-alpha.12
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 +51 -15
- package/dist/__tests__/chain-dsl-canonicalizer.test.d.ts +2 -0
- package/dist/__tests__/chain-dsl-canonicalizer.test.d.ts.map +1 -0
- package/dist/__tests__/constraint-validator.test.d.ts +2 -0
- package/dist/__tests__/constraint-validator.test.d.ts.map +1 -0
- package/dist/__tests__/extension-api.test.d.ts +2 -0
- package/dist/__tests__/extension-api.test.d.ts.map +1 -0
- package/dist/__tests__/fixtures/example-a-builtins.d.ts +18 -0
- package/dist/__tests__/fixtures/example-a-builtins.d.ts.map +1 -1
- package/dist/__tests__/ir-analyzer.test.d.ts +11 -0
- package/dist/__tests__/ir-analyzer.test.d.ts.map +1 -0
- package/dist/__tests__/ir-jsdoc-constraints.test.d.ts +12 -0
- package/dist/__tests__/ir-jsdoc-constraints.test.d.ts.map +1 -0
- package/dist/__tests__/ir-json-schema-generator.test.d.ts +11 -0
- package/dist/__tests__/ir-json-schema-generator.test.d.ts.map +1 -0
- package/dist/__tests__/ir-ui-schema-generator.test.d.ts +2 -0
- package/dist/__tests__/ir-ui-schema-generator.test.d.ts.map +1 -0
- package/dist/__tests__/jsdoc-constraints.test.d.ts +4 -4
- package/dist/__tests__/parity/fixtures/address/chain-dsl.d.ts +9 -0
- package/dist/__tests__/parity/fixtures/address/chain-dsl.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/address/expected-ir.d.ts +9 -0
- package/dist/__tests__/parity/fixtures/address/expected-ir.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/address/tsdoc.d.ts +19 -0
- package/dist/__tests__/parity/fixtures/address/tsdoc.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/product-config/chain-dsl.d.ts +13 -0
- package/dist/__tests__/parity/fixtures/product-config/chain-dsl.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/product-config/expected-ir.d.ts +9 -0
- package/dist/__tests__/parity/fixtures/product-config/expected-ir.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/product-config/tsdoc.d.ts +28 -0
- package/dist/__tests__/parity/fixtures/product-config/tsdoc.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/user-registration/chain-dsl.d.ts +12 -0
- package/dist/__tests__/parity/fixtures/user-registration/chain-dsl.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/user-registration/expected-ir.d.ts +9 -0
- package/dist/__tests__/parity/fixtures/user-registration/expected-ir.d.ts.map +1 -0
- package/dist/__tests__/parity/fixtures/user-registration/tsdoc.d.ts +19 -0
- package/dist/__tests__/parity/fixtures/user-registration/tsdoc.d.ts.map +1 -0
- package/dist/__tests__/parity/parity.test.d.ts +14 -0
- package/dist/__tests__/parity/parity.test.d.ts.map +1 -0
- package/dist/__tests__/parity/utils.d.ts +139 -0
- package/dist/__tests__/parity/utils.d.ts.map +1 -0
- package/dist/analyzer/class-analyzer.d.ts +54 -99
- package/dist/analyzer/class-analyzer.d.ts.map +1 -1
- package/dist/analyzer/jsdoc-constraints.d.ts +78 -30
- package/dist/analyzer/jsdoc-constraints.d.ts.map +1 -1
- package/dist/analyzer/tsdoc-parser.d.ts +61 -0
- package/dist/analyzer/tsdoc-parser.d.ts.map +1 -0
- package/dist/browser.cjs +1200 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.ts +12 -6
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +1147 -44
- package/dist/browser.js.map +1 -1
- package/dist/build.d.ts +385 -160
- package/dist/canonicalize/chain-dsl-canonicalizer.d.ts +18 -0
- package/dist/canonicalize/chain-dsl-canonicalizer.d.ts.map +1 -0
- package/dist/canonicalize/index.d.ts +8 -0
- package/dist/canonicalize/index.d.ts.map +1 -0
- package/dist/canonicalize/tsdoc-canonicalizer.d.ts +34 -0
- package/dist/canonicalize/tsdoc-canonicalizer.d.ts.map +1 -0
- package/dist/cli.cjs +2028 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.js +1978 -101
- package/dist/cli.js.map +1 -1
- package/dist/extensions/index.d.ts +8 -0
- package/dist/extensions/index.d.ts.map +1 -0
- package/dist/extensions/registry.d.ts +55 -0
- package/dist/extensions/registry.d.ts.map +1 -0
- package/dist/generators/class-schema.d.ts +28 -47
- package/dist/generators/class-schema.d.ts.map +1 -1
- package/dist/generators/method-schema.d.ts +6 -8
- package/dist/generators/method-schema.d.ts.map +1 -1
- package/dist/index.cjs +1832 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1779 -114
- package/dist/index.js.map +1 -1
- package/dist/internals.cjs +2125 -0
- package/dist/internals.cjs.map +1 -0
- package/dist/internals.d.ts +12 -2
- package/dist/internals.d.ts.map +1 -1
- package/dist/internals.js +2084 -21
- package/dist/internals.js.map +1 -1
- package/dist/json-schema/generator.d.ts +10 -5
- package/dist/json-schema/generator.d.ts.map +1 -1
- package/dist/json-schema/ir-generator.d.ts +84 -0
- package/dist/json-schema/ir-generator.d.ts.map +1 -0
- package/dist/json-schema/schema.d.ts +16 -0
- package/dist/json-schema/schema.d.ts.map +1 -0
- package/dist/json-schema/types.d.ts +5 -6
- package/dist/json-schema/types.d.ts.map +1 -1
- package/dist/ui-schema/generator.d.ts +5 -0
- package/dist/ui-schema/generator.d.ts.map +1 -1
- package/dist/ui-schema/ir-generator.d.ts +53 -0
- package/dist/ui-schema/ir-generator.d.ts.map +1 -0
- package/dist/ui-schema/schema.d.ts +357 -0
- package/dist/ui-schema/schema.d.ts.map +1 -0
- package/dist/ui-schema/types.d.ts +8 -73
- package/dist/ui-schema/types.d.ts.map +1 -1
- package/dist/validate/constraint-validator.d.ts +66 -0
- package/dist/validate/constraint-validator.d.ts.map +1 -0
- package/dist/validate/index.d.ts +9 -0
- package/dist/validate/index.d.ts.map +1 -0
- package/package.json +15 -9
- package/dist/__tests__/analyzer-edge-cases.test.d.ts +0 -13
- package/dist/__tests__/analyzer-edge-cases.test.d.ts.map +0 -1
- package/dist/__tests__/analyzer-edge-cases.test.js +0 -376
- package/dist/__tests__/analyzer-edge-cases.test.js.map +0 -1
- package/dist/__tests__/analyzer.test.d.ts +0 -5
- package/dist/__tests__/analyzer.test.d.ts.map +0 -1
- package/dist/__tests__/analyzer.test.js +0 -190
- package/dist/__tests__/analyzer.test.js.map +0 -1
- package/dist/__tests__/cli.test.js +0 -178
- package/dist/__tests__/cli.test.js.map +0 -1
- package/dist/__tests__/codegen.test.d.ts +0 -5
- package/dist/__tests__/codegen.test.d.ts.map +0 -1
- package/dist/__tests__/codegen.test.js +0 -506
- package/dist/__tests__/codegen.test.js.map +0 -1
- package/dist/__tests__/decorator-pipeline.test.d.ts +0 -11
- package/dist/__tests__/decorator-pipeline.test.d.ts.map +0 -1
- package/dist/__tests__/decorator-pipeline.test.js +0 -460
- package/dist/__tests__/decorator-pipeline.test.js.map +0 -1
- package/dist/__tests__/edge-cases.test.js +0 -215
- package/dist/__tests__/edge-cases.test.js.map +0 -1
- package/dist/__tests__/fixtures/edge-cases.js +0 -137
- package/dist/__tests__/fixtures/edge-cases.js.map +0 -1
- package/dist/__tests__/fixtures/example-a-builtins.js +0 -100
- package/dist/__tests__/fixtures/example-a-builtins.js.map +0 -1
- package/dist/__tests__/fixtures/example-b-decorators.d.ts +0 -5
- package/dist/__tests__/fixtures/example-b-decorators.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-b-decorators.js +0 -5
- package/dist/__tests__/fixtures/example-b-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-b-extended.d.ts +0 -5
- package/dist/__tests__/fixtures/example-b-extended.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-b-extended.js +0 -60
- package/dist/__tests__/fixtures/example-b-extended.js.map +0 -1
- package/dist/__tests__/fixtures/example-c-custom.d.ts +0 -5
- package/dist/__tests__/fixtures/example-c-custom.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-c-custom.js +0 -61
- package/dist/__tests__/fixtures/example-c-custom.js.map +0 -1
- package/dist/__tests__/fixtures/example-c-decorators.d.ts +0 -5
- package/dist/__tests__/fixtures/example-c-decorators.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-c-decorators.js +0 -4
- package/dist/__tests__/fixtures/example-c-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-d-mixed-decorators.d.ts +0 -6
- package/dist/__tests__/fixtures/example-d-mixed-decorators.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-d-mixed-decorators.js +0 -75
- package/dist/__tests__/fixtures/example-d-mixed-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-e-decorators.d.ts +0 -11
- package/dist/__tests__/fixtures/example-e-decorators.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-e-decorators.js +0 -10
- package/dist/__tests__/fixtures/example-e-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-e-no-namespace.d.ts +0 -5
- package/dist/__tests__/fixtures/example-e-no-namespace.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-e-no-namespace.js +0 -61
- package/dist/__tests__/fixtures/example-e-no-namespace.js.map +0 -1
- package/dist/__tests__/fixtures/example-interface-types.js +0 -8
- package/dist/__tests__/fixtures/example-interface-types.js.map +0 -1
- package/dist/__tests__/fixtures/example-jsdoc-constraints.d.ts +0 -16
- package/dist/__tests__/fixtures/example-jsdoc-constraints.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-jsdoc-constraints.js +0 -98
- package/dist/__tests__/fixtures/example-jsdoc-constraints.js.map +0 -1
- package/dist/__tests__/fixtures/example-nested-class.d.ts +0 -45
- package/dist/__tests__/fixtures/example-nested-class.d.ts.map +0 -1
- package/dist/__tests__/fixtures/example-nested-class.js +0 -248
- package/dist/__tests__/fixtures/example-nested-class.js.map +0 -1
- package/dist/__tests__/fixtures/sample-forms.js +0 -78
- package/dist/__tests__/fixtures/sample-forms.js.map +0 -1
- package/dist/__tests__/generator.test.js +0 -234
- package/dist/__tests__/generator.test.js.map +0 -1
- package/dist/__tests__/integration.test.js +0 -161
- package/dist/__tests__/integration.test.js.map +0 -1
- package/dist/__tests__/interface-types.test.d.ts +0 -11
- package/dist/__tests__/interface-types.test.d.ts.map +0 -1
- package/dist/__tests__/interface-types.test.js +0 -404
- package/dist/__tests__/interface-types.test.js.map +0 -1
- package/dist/__tests__/jsdoc-constraints.test.js +0 -465
- package/dist/__tests__/jsdoc-constraints.test.js.map +0 -1
- package/dist/__tests__/write-schemas.test.js +0 -198
- package/dist/__tests__/write-schemas.test.js.map +0 -1
- package/dist/analyzer/class-analyzer.js +0 -377
- package/dist/analyzer/class-analyzer.js.map +0 -1
- package/dist/analyzer/decorator-extractor.d.ts +0 -78
- package/dist/analyzer/decorator-extractor.d.ts.map +0 -1
- package/dist/analyzer/decorator-extractor.js +0 -336
- package/dist/analyzer/decorator-extractor.js.map +0 -1
- package/dist/analyzer/jsdoc-constraints.js +0 -153
- package/dist/analyzer/jsdoc-constraints.js.map +0 -1
- package/dist/analyzer/program.js +0 -114
- package/dist/analyzer/program.js.map +0 -1
- package/dist/analyzer/type-converter.d.ts +0 -75
- package/dist/analyzer/type-converter.d.ts.map +0 -1
- package/dist/analyzer/type-converter.js +0 -474
- package/dist/analyzer/type-converter.js.map +0 -1
- package/dist/codegen/index.d.ts +0 -75
- package/dist/codegen/index.d.ts.map +0 -1
- package/dist/codegen/index.js +0 -597
- package/dist/codegen/index.js.map +0 -1
- package/dist/generators/class-schema.js +0 -140
- package/dist/generators/class-schema.js.map +0 -1
- package/dist/generators/method-schema.js +0 -108
- package/dist/generators/method-schema.js.map +0 -1
- package/dist/json-schema/generator.js +0 -166
- package/dist/json-schema/generator.js.map +0 -1
- package/dist/json-schema/types.js +0 -33
- package/dist/json-schema/types.js.map +0 -1
- package/dist/ui-schema/generator.js +0 -148
- package/dist/ui-schema/generator.js.map +0 -1
- package/dist/ui-schema/types.js +0 -8
- package/dist/ui-schema/types.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1832 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
buildFormSchemas: () => buildFormSchemas,
|
|
34
|
+
categorizationSchema: () => categorizationSchema,
|
|
35
|
+
categorySchema: () => categorySchema,
|
|
36
|
+
controlSchema: () => controlSchema,
|
|
37
|
+
generateJsonSchema: () => generateJsonSchema,
|
|
38
|
+
generateSchemas: () => generateSchemas,
|
|
39
|
+
generateSchemasFromClass: () => generateSchemasFromClass,
|
|
40
|
+
generateUiSchema: () => generateUiSchema,
|
|
41
|
+
getSchemaExtension: () => getSchemaExtension,
|
|
42
|
+
groupLayoutSchema: () => groupLayoutSchema,
|
|
43
|
+
horizontalLayoutSchema: () => horizontalLayoutSchema,
|
|
44
|
+
jsonSchema7Schema: () => jsonSchema7Schema,
|
|
45
|
+
jsonSchemaTypeSchema: () => jsonSchemaTypeSchema,
|
|
46
|
+
labelElementSchema: () => labelElementSchema,
|
|
47
|
+
ruleConditionSchema: () => ruleConditionSchema,
|
|
48
|
+
ruleEffectSchema: () => ruleEffectSchema,
|
|
49
|
+
ruleSchema: () => ruleSchema,
|
|
50
|
+
schemaBasedConditionSchema: () => schemaBasedConditionSchema,
|
|
51
|
+
setSchemaExtension: () => setSchemaExtension,
|
|
52
|
+
uiSchemaElementSchema: () => uiSchemaElementSchema,
|
|
53
|
+
uiSchemaElementTypeSchema: () => uiSchemaElementTypeSchema,
|
|
54
|
+
uiSchemaSchema: () => uiSchema,
|
|
55
|
+
verticalLayoutSchema: () => verticalLayoutSchema,
|
|
56
|
+
writeSchemas: () => writeSchemas
|
|
57
|
+
});
|
|
58
|
+
module.exports = __toCommonJS(index_exports);
|
|
59
|
+
|
|
60
|
+
// src/canonicalize/chain-dsl-canonicalizer.ts
|
|
61
|
+
var import_core = require("@formspec/core");
|
|
62
|
+
var CHAIN_DSL_PROVENANCE = {
|
|
63
|
+
surface: "chain-dsl",
|
|
64
|
+
file: "",
|
|
65
|
+
line: 0,
|
|
66
|
+
column: 0
|
|
67
|
+
};
|
|
68
|
+
function isGroup(el) {
|
|
69
|
+
return el._type === "group";
|
|
70
|
+
}
|
|
71
|
+
function isConditional(el) {
|
|
72
|
+
return el._type === "conditional";
|
|
73
|
+
}
|
|
74
|
+
function isField(el) {
|
|
75
|
+
return el._type === "field";
|
|
76
|
+
}
|
|
77
|
+
function canonicalizeChainDSL(form) {
|
|
78
|
+
return {
|
|
79
|
+
kind: "form-ir",
|
|
80
|
+
irVersion: import_core.IR_VERSION,
|
|
81
|
+
elements: canonicalizeElements(form.elements),
|
|
82
|
+
typeRegistry: {},
|
|
83
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function canonicalizeElements(elements) {
|
|
87
|
+
return elements.map(canonicalizeElement);
|
|
88
|
+
}
|
|
89
|
+
function canonicalizeElement(element) {
|
|
90
|
+
if (isField(element)) {
|
|
91
|
+
return canonicalizeField(element);
|
|
92
|
+
}
|
|
93
|
+
if (isGroup(element)) {
|
|
94
|
+
return canonicalizeGroup(element);
|
|
95
|
+
}
|
|
96
|
+
if (isConditional(element)) {
|
|
97
|
+
return canonicalizeConditional(element);
|
|
98
|
+
}
|
|
99
|
+
const _exhaustive = element;
|
|
100
|
+
throw new Error(`Unknown element type: ${JSON.stringify(_exhaustive)}`);
|
|
101
|
+
}
|
|
102
|
+
function canonicalizeField(field) {
|
|
103
|
+
switch (field._field) {
|
|
104
|
+
case "text":
|
|
105
|
+
return canonicalizeTextField(field);
|
|
106
|
+
case "number":
|
|
107
|
+
return canonicalizeNumberField(field);
|
|
108
|
+
case "boolean":
|
|
109
|
+
return canonicalizeBooleanField(field);
|
|
110
|
+
case "enum":
|
|
111
|
+
return canonicalizeStaticEnumField(field);
|
|
112
|
+
case "dynamic_enum":
|
|
113
|
+
return canonicalizeDynamicEnumField(field);
|
|
114
|
+
case "dynamic_schema":
|
|
115
|
+
return canonicalizeDynamicSchemaField(field);
|
|
116
|
+
case "array":
|
|
117
|
+
return canonicalizeArrayField(field);
|
|
118
|
+
case "object":
|
|
119
|
+
return canonicalizeObjectField(field);
|
|
120
|
+
default: {
|
|
121
|
+
const _exhaustive = field;
|
|
122
|
+
throw new Error(`Unknown field type: ${JSON.stringify(_exhaustive)}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function canonicalizeTextField(field) {
|
|
127
|
+
const type = { kind: "primitive", primitiveKind: "string" };
|
|
128
|
+
return buildFieldNode(
|
|
129
|
+
field.name,
|
|
130
|
+
type,
|
|
131
|
+
field.required,
|
|
132
|
+
buildAnnotations(field.label, field.placeholder)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
function canonicalizeNumberField(field) {
|
|
136
|
+
const type = { kind: "primitive", primitiveKind: "number" };
|
|
137
|
+
const constraints = [];
|
|
138
|
+
if (field.min !== void 0) {
|
|
139
|
+
const c = {
|
|
140
|
+
kind: "constraint",
|
|
141
|
+
constraintKind: "minimum",
|
|
142
|
+
value: field.min,
|
|
143
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
144
|
+
};
|
|
145
|
+
constraints.push(c);
|
|
146
|
+
}
|
|
147
|
+
if (field.max !== void 0) {
|
|
148
|
+
const c = {
|
|
149
|
+
kind: "constraint",
|
|
150
|
+
constraintKind: "maximum",
|
|
151
|
+
value: field.max,
|
|
152
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
153
|
+
};
|
|
154
|
+
constraints.push(c);
|
|
155
|
+
}
|
|
156
|
+
return buildFieldNode(
|
|
157
|
+
field.name,
|
|
158
|
+
type,
|
|
159
|
+
field.required,
|
|
160
|
+
buildAnnotations(field.label),
|
|
161
|
+
constraints
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
function canonicalizeBooleanField(field) {
|
|
165
|
+
const type = { kind: "primitive", primitiveKind: "boolean" };
|
|
166
|
+
return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));
|
|
167
|
+
}
|
|
168
|
+
function canonicalizeStaticEnumField(field) {
|
|
169
|
+
const members = field.options.map((opt) => {
|
|
170
|
+
if (typeof opt === "string") {
|
|
171
|
+
return { value: opt };
|
|
172
|
+
}
|
|
173
|
+
return { value: opt.id, displayName: opt.label };
|
|
174
|
+
});
|
|
175
|
+
const type = { kind: "enum", members };
|
|
176
|
+
return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));
|
|
177
|
+
}
|
|
178
|
+
function canonicalizeDynamicEnumField(field) {
|
|
179
|
+
const type = {
|
|
180
|
+
kind: "dynamic",
|
|
181
|
+
dynamicKind: "enum",
|
|
182
|
+
sourceKey: field.source,
|
|
183
|
+
parameterFields: field.params ? [...field.params] : []
|
|
184
|
+
};
|
|
185
|
+
return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));
|
|
186
|
+
}
|
|
187
|
+
function canonicalizeDynamicSchemaField(field) {
|
|
188
|
+
const type = {
|
|
189
|
+
kind: "dynamic",
|
|
190
|
+
dynamicKind: "schema",
|
|
191
|
+
sourceKey: field.schemaSource,
|
|
192
|
+
parameterFields: []
|
|
193
|
+
};
|
|
194
|
+
return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));
|
|
195
|
+
}
|
|
196
|
+
function canonicalizeArrayField(field) {
|
|
197
|
+
const itemProperties = buildObjectProperties(field.items);
|
|
198
|
+
const itemsType = {
|
|
199
|
+
kind: "object",
|
|
200
|
+
properties: itemProperties,
|
|
201
|
+
additionalProperties: false
|
|
202
|
+
};
|
|
203
|
+
const type = { kind: "array", items: itemsType };
|
|
204
|
+
const constraints = [];
|
|
205
|
+
if (field.minItems !== void 0) {
|
|
206
|
+
const c = {
|
|
207
|
+
kind: "constraint",
|
|
208
|
+
constraintKind: "minItems",
|
|
209
|
+
value: field.minItems,
|
|
210
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
211
|
+
};
|
|
212
|
+
constraints.push(c);
|
|
213
|
+
}
|
|
214
|
+
if (field.maxItems !== void 0) {
|
|
215
|
+
const c = {
|
|
216
|
+
kind: "constraint",
|
|
217
|
+
constraintKind: "maxItems",
|
|
218
|
+
value: field.maxItems,
|
|
219
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
220
|
+
};
|
|
221
|
+
constraints.push(c);
|
|
222
|
+
}
|
|
223
|
+
return buildFieldNode(
|
|
224
|
+
field.name,
|
|
225
|
+
type,
|
|
226
|
+
field.required,
|
|
227
|
+
buildAnnotations(field.label),
|
|
228
|
+
constraints
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
function canonicalizeObjectField(field) {
|
|
232
|
+
const properties = buildObjectProperties(field.properties);
|
|
233
|
+
const type = {
|
|
234
|
+
kind: "object",
|
|
235
|
+
properties,
|
|
236
|
+
additionalProperties: false
|
|
237
|
+
};
|
|
238
|
+
return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));
|
|
239
|
+
}
|
|
240
|
+
function canonicalizeGroup(g) {
|
|
241
|
+
return {
|
|
242
|
+
kind: "group",
|
|
243
|
+
label: g.label,
|
|
244
|
+
elements: canonicalizeElements(g.elements),
|
|
245
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function canonicalizeConditional(c) {
|
|
249
|
+
return {
|
|
250
|
+
kind: "conditional",
|
|
251
|
+
fieldName: c.field,
|
|
252
|
+
// Conditional values from the chain DSL are JSON-serializable primitives
|
|
253
|
+
// (strings, numbers, booleans) produced by the `is()` predicate helper.
|
|
254
|
+
value: assertJsonValue(c.value),
|
|
255
|
+
elements: canonicalizeElements(c.elements),
|
|
256
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function assertJsonValue(v) {
|
|
260
|
+
if (v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
|
|
261
|
+
return v;
|
|
262
|
+
}
|
|
263
|
+
if (Array.isArray(v)) {
|
|
264
|
+
return v.map(assertJsonValue);
|
|
265
|
+
}
|
|
266
|
+
if (typeof v === "object") {
|
|
267
|
+
const result = {};
|
|
268
|
+
for (const [key, val] of Object.entries(v)) {
|
|
269
|
+
result[key] = assertJsonValue(val);
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
throw new TypeError(`Conditional value is not a valid JsonValue: ${typeof v}`);
|
|
274
|
+
}
|
|
275
|
+
function buildFieldNode(name, type, required, annotations, constraints = []) {
|
|
276
|
+
return {
|
|
277
|
+
kind: "field",
|
|
278
|
+
name,
|
|
279
|
+
type,
|
|
280
|
+
required: required === true,
|
|
281
|
+
constraints,
|
|
282
|
+
annotations,
|
|
283
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function buildAnnotations(label, placeholder) {
|
|
287
|
+
const annotations = [];
|
|
288
|
+
if (label !== void 0) {
|
|
289
|
+
const a = {
|
|
290
|
+
kind: "annotation",
|
|
291
|
+
annotationKind: "displayName",
|
|
292
|
+
value: label,
|
|
293
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
294
|
+
};
|
|
295
|
+
annotations.push(a);
|
|
296
|
+
}
|
|
297
|
+
if (placeholder !== void 0) {
|
|
298
|
+
const a = {
|
|
299
|
+
kind: "annotation",
|
|
300
|
+
annotationKind: "placeholder",
|
|
301
|
+
value: placeholder,
|
|
302
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
303
|
+
};
|
|
304
|
+
annotations.push(a);
|
|
305
|
+
}
|
|
306
|
+
return annotations;
|
|
307
|
+
}
|
|
308
|
+
function buildObjectProperties(elements, insideConditional = false) {
|
|
309
|
+
const properties = [];
|
|
310
|
+
for (const el of elements) {
|
|
311
|
+
if (isField(el)) {
|
|
312
|
+
const fieldNode = canonicalizeField(el);
|
|
313
|
+
properties.push({
|
|
314
|
+
name: fieldNode.name,
|
|
315
|
+
type: fieldNode.type,
|
|
316
|
+
// Fields inside a conditional branch are always optional in the
|
|
317
|
+
// data schema, regardless of their `required` flag — the condition
|
|
318
|
+
// may not be met, so the field may be absent.
|
|
319
|
+
optional: insideConditional || !fieldNode.required,
|
|
320
|
+
constraints: fieldNode.constraints,
|
|
321
|
+
annotations: fieldNode.annotations,
|
|
322
|
+
provenance: CHAIN_DSL_PROVENANCE
|
|
323
|
+
});
|
|
324
|
+
} else if (isGroup(el)) {
|
|
325
|
+
properties.push(...buildObjectProperties(el.elements, insideConditional));
|
|
326
|
+
} else if (isConditional(el)) {
|
|
327
|
+
properties.push(...buildObjectProperties(el.elements, true));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return properties;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/canonicalize/tsdoc-canonicalizer.ts
|
|
334
|
+
var import_core2 = require("@formspec/core");
|
|
335
|
+
function canonicalizeTSDoc(analysis, source) {
|
|
336
|
+
const file = source?.file ?? "";
|
|
337
|
+
const provenance = {
|
|
338
|
+
surface: "tsdoc",
|
|
339
|
+
file,
|
|
340
|
+
line: 1,
|
|
341
|
+
column: 0
|
|
342
|
+
};
|
|
343
|
+
const elements = assembleElements(analysis.fields, analysis.fieldLayouts, provenance);
|
|
344
|
+
return {
|
|
345
|
+
kind: "form-ir",
|
|
346
|
+
irVersion: import_core2.IR_VERSION,
|
|
347
|
+
elements,
|
|
348
|
+
typeRegistry: analysis.typeRegistry,
|
|
349
|
+
provenance
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function assembleElements(fields, layouts, provenance) {
|
|
353
|
+
const elements = [];
|
|
354
|
+
const groupMap = /* @__PURE__ */ new Map();
|
|
355
|
+
const topLevelOrder = [];
|
|
356
|
+
for (let i = 0; i < fields.length; i++) {
|
|
357
|
+
const field = fields[i];
|
|
358
|
+
const layout = layouts[i];
|
|
359
|
+
if (!field || !layout) continue;
|
|
360
|
+
const element = wrapInConditional(field, layout, provenance);
|
|
361
|
+
if (layout.groupLabel !== void 0) {
|
|
362
|
+
const label = layout.groupLabel;
|
|
363
|
+
let groupElements = groupMap.get(label);
|
|
364
|
+
if (!groupElements) {
|
|
365
|
+
groupElements = [];
|
|
366
|
+
groupMap.set(label, groupElements);
|
|
367
|
+
topLevelOrder.push({ type: "group", label });
|
|
368
|
+
}
|
|
369
|
+
groupElements.push(element);
|
|
370
|
+
} else {
|
|
371
|
+
topLevelOrder.push({ type: "element", element });
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
for (const entry of topLevelOrder) {
|
|
375
|
+
if (entry.type === "group") {
|
|
376
|
+
const groupElements = groupMap.get(entry.label);
|
|
377
|
+
if (groupElements) {
|
|
378
|
+
const groupNode = {
|
|
379
|
+
kind: "group",
|
|
380
|
+
label: entry.label,
|
|
381
|
+
elements: groupElements,
|
|
382
|
+
provenance
|
|
383
|
+
};
|
|
384
|
+
elements.push(groupNode);
|
|
385
|
+
groupMap.delete(entry.label);
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
elements.push(entry.element);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return elements;
|
|
392
|
+
}
|
|
393
|
+
function wrapInConditional(field, layout, provenance) {
|
|
394
|
+
if (layout.showWhen === void 0) {
|
|
395
|
+
return field;
|
|
396
|
+
}
|
|
397
|
+
const conditional = {
|
|
398
|
+
kind: "conditional",
|
|
399
|
+
fieldName: layout.showWhen.field,
|
|
400
|
+
value: layout.showWhen.value,
|
|
401
|
+
elements: [field],
|
|
402
|
+
provenance
|
|
403
|
+
};
|
|
404
|
+
return conditional;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// src/json-schema/ir-generator.ts
|
|
408
|
+
function makeContext() {
|
|
409
|
+
return { defs: {} };
|
|
410
|
+
}
|
|
411
|
+
function generateJsonSchemaFromIR(ir) {
|
|
412
|
+
const ctx = makeContext();
|
|
413
|
+
for (const [name, typeDef] of Object.entries(ir.typeRegistry)) {
|
|
414
|
+
ctx.defs[name] = generateTypeNode(typeDef.type, ctx);
|
|
415
|
+
}
|
|
416
|
+
const properties = {};
|
|
417
|
+
const required = [];
|
|
418
|
+
collectFields(ir.elements, properties, required, ctx);
|
|
419
|
+
const uniqueRequired = [...new Set(required)];
|
|
420
|
+
const result = {
|
|
421
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
422
|
+
type: "object",
|
|
423
|
+
properties,
|
|
424
|
+
...uniqueRequired.length > 0 && { required: uniqueRequired }
|
|
425
|
+
};
|
|
426
|
+
if (Object.keys(ctx.defs).length > 0) {
|
|
427
|
+
result.$defs = ctx.defs;
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
430
|
+
}
|
|
431
|
+
function collectFields(elements, properties, required, ctx) {
|
|
432
|
+
for (const element of elements) {
|
|
433
|
+
switch (element.kind) {
|
|
434
|
+
case "field":
|
|
435
|
+
properties[element.name] = generateFieldSchema(element, ctx);
|
|
436
|
+
if (element.required) {
|
|
437
|
+
required.push(element.name);
|
|
438
|
+
}
|
|
439
|
+
break;
|
|
440
|
+
case "group":
|
|
441
|
+
collectFields(element.elements, properties, required, ctx);
|
|
442
|
+
break;
|
|
443
|
+
case "conditional":
|
|
444
|
+
collectFields(element.elements, properties, required, ctx);
|
|
445
|
+
break;
|
|
446
|
+
default: {
|
|
447
|
+
const _exhaustive = element;
|
|
448
|
+
void _exhaustive;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function generateFieldSchema(field, ctx) {
|
|
454
|
+
const schema = generateTypeNode(field.type, ctx);
|
|
455
|
+
applyConstraints(schema, field.constraints);
|
|
456
|
+
applyAnnotations(schema, field.annotations);
|
|
457
|
+
return schema;
|
|
458
|
+
}
|
|
459
|
+
function generateTypeNode(type, ctx) {
|
|
460
|
+
switch (type.kind) {
|
|
461
|
+
case "primitive":
|
|
462
|
+
return generatePrimitiveType(type);
|
|
463
|
+
case "enum":
|
|
464
|
+
return generateEnumType(type);
|
|
465
|
+
case "array":
|
|
466
|
+
return generateArrayType(type, ctx);
|
|
467
|
+
case "object":
|
|
468
|
+
return generateObjectType(type, ctx);
|
|
469
|
+
case "union":
|
|
470
|
+
return generateUnionType(type, ctx);
|
|
471
|
+
case "reference":
|
|
472
|
+
return generateReferenceType(type);
|
|
473
|
+
case "dynamic":
|
|
474
|
+
return generateDynamicType(type);
|
|
475
|
+
case "custom":
|
|
476
|
+
return generateCustomType(type);
|
|
477
|
+
default: {
|
|
478
|
+
const _exhaustive = type;
|
|
479
|
+
return _exhaustive;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
function generatePrimitiveType(type) {
|
|
484
|
+
return { type: type.primitiveKind };
|
|
485
|
+
}
|
|
486
|
+
function generateEnumType(type) {
|
|
487
|
+
const hasDisplayNames = type.members.some((m) => m.displayName !== void 0);
|
|
488
|
+
if (hasDisplayNames) {
|
|
489
|
+
return {
|
|
490
|
+
oneOf: type.members.map((m) => {
|
|
491
|
+
const entry = { const: m.value };
|
|
492
|
+
if (m.displayName !== void 0) {
|
|
493
|
+
entry.title = m.displayName;
|
|
494
|
+
}
|
|
495
|
+
return entry;
|
|
496
|
+
})
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
return { enum: type.members.map((m) => m.value) };
|
|
500
|
+
}
|
|
501
|
+
function generateArrayType(type, ctx) {
|
|
502
|
+
return {
|
|
503
|
+
type: "array",
|
|
504
|
+
items: generateTypeNode(type.items, ctx)
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function generateObjectType(type, ctx) {
|
|
508
|
+
const properties = {};
|
|
509
|
+
const required = [];
|
|
510
|
+
for (const prop of type.properties) {
|
|
511
|
+
properties[prop.name] = generatePropertySchema(prop, ctx);
|
|
512
|
+
if (!prop.optional) {
|
|
513
|
+
required.push(prop.name);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
const schema = { type: "object", properties };
|
|
517
|
+
if (required.length > 0) {
|
|
518
|
+
schema.required = required;
|
|
519
|
+
}
|
|
520
|
+
if (!type.additionalProperties) {
|
|
521
|
+
schema.additionalProperties = false;
|
|
522
|
+
}
|
|
523
|
+
return schema;
|
|
524
|
+
}
|
|
525
|
+
function generatePropertySchema(prop, ctx) {
|
|
526
|
+
const schema = generateTypeNode(prop.type, ctx);
|
|
527
|
+
applyConstraints(schema, prop.constraints);
|
|
528
|
+
applyAnnotations(schema, prop.annotations);
|
|
529
|
+
return schema;
|
|
530
|
+
}
|
|
531
|
+
function generateUnionType(type, ctx) {
|
|
532
|
+
if (isBooleanUnion(type)) {
|
|
533
|
+
return { type: "boolean" };
|
|
534
|
+
}
|
|
535
|
+
return {
|
|
536
|
+
anyOf: type.members.map((m) => generateTypeNode(m, ctx))
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
function isBooleanUnion(type) {
|
|
540
|
+
if (type.members.length !== 2) return false;
|
|
541
|
+
const kinds = type.members.map((m) => m.kind);
|
|
542
|
+
return kinds.every((k) => k === "primitive") && type.members.every((m) => m.kind === "primitive" && m.primitiveKind === "boolean");
|
|
543
|
+
}
|
|
544
|
+
function generateReferenceType(type) {
|
|
545
|
+
return { $ref: `#/$defs/${type.name}` };
|
|
546
|
+
}
|
|
547
|
+
function generateDynamicType(type) {
|
|
548
|
+
if (type.dynamicKind === "enum") {
|
|
549
|
+
const schema = {
|
|
550
|
+
type: "string",
|
|
551
|
+
"x-formspec-source": type.sourceKey
|
|
552
|
+
};
|
|
553
|
+
if (type.parameterFields.length > 0) {
|
|
554
|
+
schema["x-formspec-params"] = [...type.parameterFields];
|
|
555
|
+
}
|
|
556
|
+
return schema;
|
|
557
|
+
}
|
|
558
|
+
return {
|
|
559
|
+
type: "object",
|
|
560
|
+
additionalProperties: true,
|
|
561
|
+
"x-formspec-schemaSource": type.sourceKey
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function generateCustomType(_type) {
|
|
565
|
+
return { type: "object" };
|
|
566
|
+
}
|
|
567
|
+
function applyConstraints(schema, constraints) {
|
|
568
|
+
for (const constraint of constraints) {
|
|
569
|
+
switch (constraint.constraintKind) {
|
|
570
|
+
case "minimum":
|
|
571
|
+
schema.minimum = constraint.value;
|
|
572
|
+
break;
|
|
573
|
+
case "maximum":
|
|
574
|
+
schema.maximum = constraint.value;
|
|
575
|
+
break;
|
|
576
|
+
case "exclusiveMinimum":
|
|
577
|
+
schema.exclusiveMinimum = constraint.value;
|
|
578
|
+
break;
|
|
579
|
+
case "exclusiveMaximum":
|
|
580
|
+
schema.exclusiveMaximum = constraint.value;
|
|
581
|
+
break;
|
|
582
|
+
case "multipleOf": {
|
|
583
|
+
const { value } = constraint;
|
|
584
|
+
if (value === 1 && schema.type === "number") {
|
|
585
|
+
schema.type = "integer";
|
|
586
|
+
} else {
|
|
587
|
+
schema.multipleOf = value;
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
case "minLength":
|
|
592
|
+
schema.minLength = constraint.value;
|
|
593
|
+
break;
|
|
594
|
+
case "maxLength":
|
|
595
|
+
schema.maxLength = constraint.value;
|
|
596
|
+
break;
|
|
597
|
+
case "minItems":
|
|
598
|
+
schema.minItems = constraint.value;
|
|
599
|
+
break;
|
|
600
|
+
case "maxItems":
|
|
601
|
+
schema.maxItems = constraint.value;
|
|
602
|
+
break;
|
|
603
|
+
case "pattern":
|
|
604
|
+
schema.pattern = constraint.pattern;
|
|
605
|
+
break;
|
|
606
|
+
case "uniqueItems":
|
|
607
|
+
schema.uniqueItems = constraint.value;
|
|
608
|
+
break;
|
|
609
|
+
case "allowedMembers":
|
|
610
|
+
break;
|
|
611
|
+
case "custom":
|
|
612
|
+
break;
|
|
613
|
+
default: {
|
|
614
|
+
const _exhaustive = constraint;
|
|
615
|
+
void _exhaustive;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function applyAnnotations(schema, annotations) {
|
|
621
|
+
for (const annotation of annotations) {
|
|
622
|
+
switch (annotation.annotationKind) {
|
|
623
|
+
case "displayName":
|
|
624
|
+
schema.title = annotation.value;
|
|
625
|
+
break;
|
|
626
|
+
case "description":
|
|
627
|
+
schema.description = annotation.value;
|
|
628
|
+
break;
|
|
629
|
+
case "defaultValue":
|
|
630
|
+
schema.default = annotation.value;
|
|
631
|
+
break;
|
|
632
|
+
case "deprecated":
|
|
633
|
+
schema.deprecated = true;
|
|
634
|
+
break;
|
|
635
|
+
case "placeholder":
|
|
636
|
+
break;
|
|
637
|
+
case "formatHint":
|
|
638
|
+
break;
|
|
639
|
+
case "custom":
|
|
640
|
+
break;
|
|
641
|
+
default: {
|
|
642
|
+
const _exhaustive = annotation;
|
|
643
|
+
void _exhaustive;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// src/json-schema/generator.ts
|
|
650
|
+
function generateJsonSchema(form) {
|
|
651
|
+
const ir = canonicalizeChainDSL(form);
|
|
652
|
+
return generateJsonSchemaFromIR(ir);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// src/ui-schema/schema.ts
|
|
656
|
+
var import_zod = require("zod");
|
|
657
|
+
var jsonPointerSchema = import_zod.z.string();
|
|
658
|
+
var ruleEffectSchema = import_zod.z.enum(["SHOW", "HIDE", "ENABLE", "DISABLE"]);
|
|
659
|
+
var uiSchemaElementTypeSchema = import_zod.z.enum([
|
|
660
|
+
"Control",
|
|
661
|
+
"VerticalLayout",
|
|
662
|
+
"HorizontalLayout",
|
|
663
|
+
"Group",
|
|
664
|
+
"Categorization",
|
|
665
|
+
"Category",
|
|
666
|
+
"Label"
|
|
667
|
+
]);
|
|
668
|
+
var ruleConditionSchema = import_zod.z.lazy(
|
|
669
|
+
() => import_zod.z.object({
|
|
670
|
+
const: import_zod.z.unknown().optional(),
|
|
671
|
+
enum: import_zod.z.array(import_zod.z.unknown()).readonly().optional(),
|
|
672
|
+
type: import_zod.z.string().optional(),
|
|
673
|
+
not: ruleConditionSchema.optional(),
|
|
674
|
+
minimum: import_zod.z.number().optional(),
|
|
675
|
+
maximum: import_zod.z.number().optional(),
|
|
676
|
+
exclusiveMinimum: import_zod.z.number().optional(),
|
|
677
|
+
exclusiveMaximum: import_zod.z.number().optional(),
|
|
678
|
+
minLength: import_zod.z.number().optional(),
|
|
679
|
+
properties: import_zod.z.record(import_zod.z.string(), ruleConditionSchema).optional(),
|
|
680
|
+
required: import_zod.z.array(import_zod.z.string()).optional(),
|
|
681
|
+
allOf: import_zod.z.array(ruleConditionSchema).optional()
|
|
682
|
+
}).strict()
|
|
683
|
+
);
|
|
684
|
+
var schemaBasedConditionSchema = import_zod.z.object({
|
|
685
|
+
scope: jsonPointerSchema,
|
|
686
|
+
schema: ruleConditionSchema
|
|
687
|
+
}).strict();
|
|
688
|
+
var ruleSchema = import_zod.z.object({
|
|
689
|
+
effect: ruleEffectSchema,
|
|
690
|
+
condition: schemaBasedConditionSchema
|
|
691
|
+
}).strict();
|
|
692
|
+
var uiSchemaElementSchema = import_zod.z.lazy(
|
|
693
|
+
() => import_zod.z.union([
|
|
694
|
+
controlSchema,
|
|
695
|
+
verticalLayoutSchema,
|
|
696
|
+
horizontalLayoutSchema,
|
|
697
|
+
groupLayoutSchema,
|
|
698
|
+
categorizationSchema,
|
|
699
|
+
categorySchema,
|
|
700
|
+
labelElementSchema
|
|
701
|
+
])
|
|
702
|
+
);
|
|
703
|
+
var controlSchema = import_zod.z.object({
|
|
704
|
+
type: import_zod.z.literal("Control"),
|
|
705
|
+
scope: jsonPointerSchema,
|
|
706
|
+
label: import_zod.z.union([import_zod.z.string(), import_zod.z.literal(false)]).optional(),
|
|
707
|
+
rule: ruleSchema.optional(),
|
|
708
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
709
|
+
}).passthrough();
|
|
710
|
+
var verticalLayoutSchema = import_zod.z.lazy(
|
|
711
|
+
() => import_zod.z.object({
|
|
712
|
+
type: import_zod.z.literal("VerticalLayout"),
|
|
713
|
+
elements: import_zod.z.array(uiSchemaElementSchema),
|
|
714
|
+
rule: ruleSchema.optional(),
|
|
715
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
716
|
+
}).passthrough()
|
|
717
|
+
);
|
|
718
|
+
var horizontalLayoutSchema = import_zod.z.lazy(
|
|
719
|
+
() => import_zod.z.object({
|
|
720
|
+
type: import_zod.z.literal("HorizontalLayout"),
|
|
721
|
+
elements: import_zod.z.array(uiSchemaElementSchema),
|
|
722
|
+
rule: ruleSchema.optional(),
|
|
723
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
724
|
+
}).passthrough()
|
|
725
|
+
);
|
|
726
|
+
var groupLayoutSchema = import_zod.z.lazy(
|
|
727
|
+
() => import_zod.z.object({
|
|
728
|
+
type: import_zod.z.literal("Group"),
|
|
729
|
+
label: import_zod.z.string(),
|
|
730
|
+
elements: import_zod.z.array(uiSchemaElementSchema),
|
|
731
|
+
rule: ruleSchema.optional(),
|
|
732
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
733
|
+
}).passthrough()
|
|
734
|
+
);
|
|
735
|
+
var categorySchema = import_zod.z.lazy(
|
|
736
|
+
() => import_zod.z.object({
|
|
737
|
+
type: import_zod.z.literal("Category"),
|
|
738
|
+
label: import_zod.z.string(),
|
|
739
|
+
elements: import_zod.z.array(uiSchemaElementSchema),
|
|
740
|
+
rule: ruleSchema.optional(),
|
|
741
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
742
|
+
}).passthrough()
|
|
743
|
+
);
|
|
744
|
+
var categorizationSchema = import_zod.z.lazy(
|
|
745
|
+
() => import_zod.z.object({
|
|
746
|
+
type: import_zod.z.literal("Categorization"),
|
|
747
|
+
elements: import_zod.z.array(categorySchema),
|
|
748
|
+
label: import_zod.z.string().optional(),
|
|
749
|
+
rule: ruleSchema.optional(),
|
|
750
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
751
|
+
}).passthrough()
|
|
752
|
+
);
|
|
753
|
+
var labelElementSchema = import_zod.z.object({
|
|
754
|
+
type: import_zod.z.literal("Label"),
|
|
755
|
+
text: import_zod.z.string(),
|
|
756
|
+
rule: ruleSchema.optional(),
|
|
757
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
758
|
+
}).passthrough();
|
|
759
|
+
var uiSchema = import_zod.z.lazy(
|
|
760
|
+
() => import_zod.z.union([verticalLayoutSchema, horizontalLayoutSchema, groupLayoutSchema, categorizationSchema])
|
|
761
|
+
);
|
|
762
|
+
|
|
763
|
+
// src/ui-schema/ir-generator.ts
|
|
764
|
+
var import_zod2 = require("zod");
|
|
765
|
+
function parseOrThrow(schema, value, label) {
|
|
766
|
+
try {
|
|
767
|
+
return schema.parse(value);
|
|
768
|
+
} catch (error) {
|
|
769
|
+
if (error instanceof import_zod2.z.ZodError) {
|
|
770
|
+
throw new Error(
|
|
771
|
+
`Generated ${label} failed validation:
|
|
772
|
+
${error.issues.map((i) => ` ${i.path.join(".")}: ${i.message}`).join("\n")}`
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
throw error;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
function fieldToScope(fieldName) {
|
|
779
|
+
return `#/properties/${fieldName}`;
|
|
780
|
+
}
|
|
781
|
+
function createShowRule(fieldName, value) {
|
|
782
|
+
return {
|
|
783
|
+
effect: "SHOW",
|
|
784
|
+
condition: {
|
|
785
|
+
scope: fieldToScope(fieldName),
|
|
786
|
+
schema: { const: value }
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
function combineRules(parentRule, childRule) {
|
|
791
|
+
const parentCondition = parentRule.condition;
|
|
792
|
+
const childCondition = childRule.condition;
|
|
793
|
+
return {
|
|
794
|
+
effect: "SHOW",
|
|
795
|
+
condition: {
|
|
796
|
+
scope: "#",
|
|
797
|
+
schema: {
|
|
798
|
+
allOf: [
|
|
799
|
+
{
|
|
800
|
+
properties: {
|
|
801
|
+
[parentCondition.scope.replace("#/properties/", "")]: parentCondition.schema
|
|
802
|
+
}
|
|
803
|
+
},
|
|
804
|
+
{
|
|
805
|
+
properties: {
|
|
806
|
+
[childCondition.scope.replace("#/properties/", "")]: childCondition.schema
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
]
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
function fieldNodeToControl(field, parentRule) {
|
|
815
|
+
const displayNameAnnotation = field.annotations.find((a) => a.annotationKind === "displayName");
|
|
816
|
+
const control = {
|
|
817
|
+
type: "Control",
|
|
818
|
+
scope: fieldToScope(field.name),
|
|
819
|
+
...displayNameAnnotation !== void 0 && { label: displayNameAnnotation.value },
|
|
820
|
+
...parentRule !== void 0 && { rule: parentRule }
|
|
821
|
+
};
|
|
822
|
+
return control;
|
|
823
|
+
}
|
|
824
|
+
function groupNodeToLayout(group, parentRule) {
|
|
825
|
+
return {
|
|
826
|
+
type: "Group",
|
|
827
|
+
label: group.label,
|
|
828
|
+
elements: irElementsToUiSchema(group.elements, parentRule),
|
|
829
|
+
...parentRule !== void 0 && { rule: parentRule }
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
function irElementsToUiSchema(elements, parentRule) {
|
|
833
|
+
const result = [];
|
|
834
|
+
for (const element of elements) {
|
|
835
|
+
switch (element.kind) {
|
|
836
|
+
case "field": {
|
|
837
|
+
result.push(fieldNodeToControl(element, parentRule));
|
|
838
|
+
break;
|
|
839
|
+
}
|
|
840
|
+
case "group": {
|
|
841
|
+
result.push(groupNodeToLayout(element, parentRule));
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
case "conditional": {
|
|
845
|
+
const newRule = createShowRule(element.fieldName, element.value);
|
|
846
|
+
const combinedRule = parentRule !== void 0 ? combineRules(parentRule, newRule) : newRule;
|
|
847
|
+
const childElements = irElementsToUiSchema(element.elements, combinedRule);
|
|
848
|
+
result.push(...childElements);
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
default: {
|
|
852
|
+
const _exhaustive = element;
|
|
853
|
+
void _exhaustive;
|
|
854
|
+
throw new Error("Unhandled IR element kind");
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
return result;
|
|
859
|
+
}
|
|
860
|
+
function generateUiSchemaFromIR(ir) {
|
|
861
|
+
const result = {
|
|
862
|
+
type: "VerticalLayout",
|
|
863
|
+
elements: irElementsToUiSchema(ir.elements)
|
|
864
|
+
};
|
|
865
|
+
return parseOrThrow(uiSchema, result, "UI Schema");
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// src/ui-schema/generator.ts
|
|
869
|
+
function generateUiSchema(form) {
|
|
870
|
+
const ir = canonicalizeChainDSL(form);
|
|
871
|
+
return generateUiSchemaFromIR(ir);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// src/index.ts
|
|
875
|
+
var fs = __toESM(require("fs"), 1);
|
|
876
|
+
var path2 = __toESM(require("path"), 1);
|
|
877
|
+
|
|
878
|
+
// src/json-schema/types.ts
|
|
879
|
+
function setSchemaExtension(schema, key, value) {
|
|
880
|
+
schema[key] = value;
|
|
881
|
+
}
|
|
882
|
+
function getSchemaExtension(schema, key) {
|
|
883
|
+
return schema[key];
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// src/json-schema/schema.ts
|
|
887
|
+
var import_zod3 = require("zod");
|
|
888
|
+
var jsonSchemaTypeSchema = import_zod3.z.enum([
|
|
889
|
+
"string",
|
|
890
|
+
"number",
|
|
891
|
+
"integer",
|
|
892
|
+
"boolean",
|
|
893
|
+
"object",
|
|
894
|
+
"array",
|
|
895
|
+
"null"
|
|
896
|
+
]);
|
|
897
|
+
var jsonSchema7Schema = import_zod3.z.lazy(
|
|
898
|
+
() => import_zod3.z.object({
|
|
899
|
+
$schema: import_zod3.z.string().optional(),
|
|
900
|
+
$id: import_zod3.z.string().optional(),
|
|
901
|
+
$ref: import_zod3.z.string().optional(),
|
|
902
|
+
// Metadata
|
|
903
|
+
title: import_zod3.z.string().optional(),
|
|
904
|
+
description: import_zod3.z.string().optional(),
|
|
905
|
+
deprecated: import_zod3.z.boolean().optional(),
|
|
906
|
+
// Type
|
|
907
|
+
type: import_zod3.z.union([jsonSchemaTypeSchema, import_zod3.z.array(jsonSchemaTypeSchema)]).optional(),
|
|
908
|
+
// String validation
|
|
909
|
+
minLength: import_zod3.z.number().optional(),
|
|
910
|
+
maxLength: import_zod3.z.number().optional(),
|
|
911
|
+
pattern: import_zod3.z.string().optional(),
|
|
912
|
+
// Number validation
|
|
913
|
+
minimum: import_zod3.z.number().optional(),
|
|
914
|
+
maximum: import_zod3.z.number().optional(),
|
|
915
|
+
exclusiveMinimum: import_zod3.z.number().optional(),
|
|
916
|
+
exclusiveMaximum: import_zod3.z.number().optional(),
|
|
917
|
+
// Enum
|
|
918
|
+
enum: import_zod3.z.array(import_zod3.z.union([import_zod3.z.string(), import_zod3.z.number(), import_zod3.z.boolean(), import_zod3.z.null()])).readonly().optional(),
|
|
919
|
+
const: import_zod3.z.union([import_zod3.z.string(), import_zod3.z.number(), import_zod3.z.boolean(), import_zod3.z.null()]).optional(),
|
|
920
|
+
// Object
|
|
921
|
+
properties: import_zod3.z.record(import_zod3.z.string(), jsonSchema7Schema).optional(),
|
|
922
|
+
required: import_zod3.z.array(import_zod3.z.string()).optional(),
|
|
923
|
+
additionalProperties: import_zod3.z.union([import_zod3.z.boolean(), jsonSchema7Schema]).optional(),
|
|
924
|
+
// Array
|
|
925
|
+
items: import_zod3.z.union([jsonSchema7Schema, import_zod3.z.array(jsonSchema7Schema)]).optional(),
|
|
926
|
+
minItems: import_zod3.z.number().optional(),
|
|
927
|
+
maxItems: import_zod3.z.number().optional(),
|
|
928
|
+
// Composition
|
|
929
|
+
allOf: import_zod3.z.array(jsonSchema7Schema).optional(),
|
|
930
|
+
anyOf: import_zod3.z.array(jsonSchema7Schema).optional(),
|
|
931
|
+
oneOf: import_zod3.z.array(jsonSchema7Schema).optional(),
|
|
932
|
+
not: jsonSchema7Schema.optional(),
|
|
933
|
+
// Conditional
|
|
934
|
+
if: jsonSchema7Schema.optional(),
|
|
935
|
+
then: jsonSchema7Schema.optional(),
|
|
936
|
+
else: jsonSchema7Schema.optional(),
|
|
937
|
+
// Format
|
|
938
|
+
format: import_zod3.z.string().optional(),
|
|
939
|
+
// Default
|
|
940
|
+
default: import_zod3.z.unknown().optional(),
|
|
941
|
+
// FormSpec extensions
|
|
942
|
+
"x-formspec-source": import_zod3.z.string().optional(),
|
|
943
|
+
"x-formspec-params": import_zod3.z.array(import_zod3.z.string()).readonly().optional(),
|
|
944
|
+
"x-formspec-schemaSource": import_zod3.z.string().optional()
|
|
945
|
+
}).passthrough()
|
|
946
|
+
);
|
|
947
|
+
|
|
948
|
+
// src/analyzer/program.ts
|
|
949
|
+
var ts = __toESM(require("typescript"), 1);
|
|
950
|
+
var path = __toESM(require("path"), 1);
|
|
951
|
+
function createProgramContext(filePath) {
|
|
952
|
+
const absolutePath = path.resolve(filePath);
|
|
953
|
+
const fileDir = path.dirname(absolutePath);
|
|
954
|
+
const configPath = ts.findConfigFile(fileDir, ts.sys.fileExists.bind(ts.sys), "tsconfig.json");
|
|
955
|
+
let compilerOptions;
|
|
956
|
+
let fileNames;
|
|
957
|
+
if (configPath) {
|
|
958
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile.bind(ts.sys));
|
|
959
|
+
if (configFile.error) {
|
|
960
|
+
throw new Error(
|
|
961
|
+
`Error reading tsconfig.json: ${ts.flattenDiagnosticMessageText(configFile.error.messageText, "\n")}`
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
965
|
+
configFile.config,
|
|
966
|
+
ts.sys,
|
|
967
|
+
path.dirname(configPath)
|
|
968
|
+
);
|
|
969
|
+
if (parsed.errors.length > 0) {
|
|
970
|
+
const errorMessages = parsed.errors.map((e) => ts.flattenDiagnosticMessageText(e.messageText, "\n")).join("\n");
|
|
971
|
+
throw new Error(`Error parsing tsconfig.json: ${errorMessages}`);
|
|
972
|
+
}
|
|
973
|
+
compilerOptions = parsed.options;
|
|
974
|
+
fileNames = parsed.fileNames.includes(absolutePath) ? parsed.fileNames : [...parsed.fileNames, absolutePath];
|
|
975
|
+
} else {
|
|
976
|
+
compilerOptions = {
|
|
977
|
+
target: ts.ScriptTarget.ES2022,
|
|
978
|
+
module: ts.ModuleKind.NodeNext,
|
|
979
|
+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
980
|
+
strict: true,
|
|
981
|
+
skipLibCheck: true,
|
|
982
|
+
declaration: true
|
|
983
|
+
};
|
|
984
|
+
fileNames = [absolutePath];
|
|
985
|
+
}
|
|
986
|
+
const program = ts.createProgram(fileNames, compilerOptions);
|
|
987
|
+
const sourceFile = program.getSourceFile(absolutePath);
|
|
988
|
+
if (!sourceFile) {
|
|
989
|
+
throw new Error(`Could not find source file: ${absolutePath}`);
|
|
990
|
+
}
|
|
991
|
+
return {
|
|
992
|
+
program,
|
|
993
|
+
checker: program.getTypeChecker(),
|
|
994
|
+
sourceFile
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
function findNodeByName(sourceFile, name, predicate, getName) {
|
|
998
|
+
let result = null;
|
|
999
|
+
function visit(node) {
|
|
1000
|
+
if (result) return;
|
|
1001
|
+
if (predicate(node) && getName(node) === name) {
|
|
1002
|
+
result = node;
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
ts.forEachChild(node, visit);
|
|
1006
|
+
}
|
|
1007
|
+
visit(sourceFile);
|
|
1008
|
+
return result;
|
|
1009
|
+
}
|
|
1010
|
+
function findClassByName(sourceFile, className) {
|
|
1011
|
+
return findNodeByName(sourceFile, className, ts.isClassDeclaration, (n) => n.name?.text);
|
|
1012
|
+
}
|
|
1013
|
+
function findInterfaceByName(sourceFile, interfaceName) {
|
|
1014
|
+
return findNodeByName(sourceFile, interfaceName, ts.isInterfaceDeclaration, (n) => n.name.text);
|
|
1015
|
+
}
|
|
1016
|
+
function findTypeAliasByName(sourceFile, aliasName) {
|
|
1017
|
+
return findNodeByName(sourceFile, aliasName, ts.isTypeAliasDeclaration, (n) => n.name.text);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// src/analyzer/class-analyzer.ts
|
|
1021
|
+
var ts4 = __toESM(require("typescript"), 1);
|
|
1022
|
+
|
|
1023
|
+
// src/analyzer/jsdoc-constraints.ts
|
|
1024
|
+
var ts3 = __toESM(require("typescript"), 1);
|
|
1025
|
+
var import_core4 = require("@formspec/core");
|
|
1026
|
+
|
|
1027
|
+
// src/analyzer/tsdoc-parser.ts
|
|
1028
|
+
var ts2 = __toESM(require("typescript"), 1);
|
|
1029
|
+
var import_tsdoc = require("@microsoft/tsdoc");
|
|
1030
|
+
var import_core3 = require("@formspec/core");
|
|
1031
|
+
var NUMERIC_CONSTRAINT_MAP = {
|
|
1032
|
+
Minimum: "minimum",
|
|
1033
|
+
Maximum: "maximum",
|
|
1034
|
+
ExclusiveMinimum: "exclusiveMinimum",
|
|
1035
|
+
ExclusiveMaximum: "exclusiveMaximum"
|
|
1036
|
+
};
|
|
1037
|
+
var LENGTH_CONSTRAINT_MAP = {
|
|
1038
|
+
MinLength: "minLength",
|
|
1039
|
+
MaxLength: "maxLength"
|
|
1040
|
+
};
|
|
1041
|
+
var TAGS_REQUIRING_RAW_TEXT = /* @__PURE__ */ new Set(["Pattern", "EnumOptions"]);
|
|
1042
|
+
function isBuiltinConstraintName(tagName) {
|
|
1043
|
+
return tagName in import_core3.BUILTIN_CONSTRAINT_DEFINITIONS;
|
|
1044
|
+
}
|
|
1045
|
+
function createFormSpecTSDocConfig() {
|
|
1046
|
+
const config = new import_tsdoc.TSDocConfiguration();
|
|
1047
|
+
for (const tagName of Object.keys(import_core3.BUILTIN_CONSTRAINT_DEFINITIONS)) {
|
|
1048
|
+
config.addTagDefinition(
|
|
1049
|
+
new import_tsdoc.TSDocTagDefinition({
|
|
1050
|
+
tagName: "@" + tagName,
|
|
1051
|
+
syntaxKind: import_tsdoc.TSDocTagSyntaxKind.BlockTag,
|
|
1052
|
+
allowMultiple: true
|
|
1053
|
+
})
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
return config;
|
|
1057
|
+
}
|
|
1058
|
+
var sharedParser;
|
|
1059
|
+
function getParser() {
|
|
1060
|
+
sharedParser ??= new import_tsdoc.TSDocParser(createFormSpecTSDocConfig());
|
|
1061
|
+
return sharedParser;
|
|
1062
|
+
}
|
|
1063
|
+
function parseTSDocTags(node, file = "") {
|
|
1064
|
+
const constraints = [];
|
|
1065
|
+
const annotations = [];
|
|
1066
|
+
const sourceFile = node.getSourceFile();
|
|
1067
|
+
const sourceText = sourceFile.getFullText();
|
|
1068
|
+
const commentRanges = ts2.getLeadingCommentRanges(sourceText, node.getFullStart());
|
|
1069
|
+
if (commentRanges) {
|
|
1070
|
+
for (const range of commentRanges) {
|
|
1071
|
+
if (range.kind !== ts2.SyntaxKind.MultiLineCommentTrivia) {
|
|
1072
|
+
continue;
|
|
1073
|
+
}
|
|
1074
|
+
const commentText = sourceText.substring(range.pos, range.end);
|
|
1075
|
+
if (!commentText.startsWith("/**")) {
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
const parser = getParser();
|
|
1079
|
+
const parserContext = parser.parseRange(
|
|
1080
|
+
import_tsdoc.TextRange.fromStringRange(sourceText, range.pos, range.end)
|
|
1081
|
+
);
|
|
1082
|
+
const docComment = parserContext.docComment;
|
|
1083
|
+
for (const block of docComment.customBlocks) {
|
|
1084
|
+
const tagName = block.blockTag.tagName.substring(1);
|
|
1085
|
+
if (TAGS_REQUIRING_RAW_TEXT.has(tagName)) continue;
|
|
1086
|
+
const text = extractBlockText(block).trim();
|
|
1087
|
+
if (text === "") continue;
|
|
1088
|
+
const provenance = provenanceForComment(range, sourceFile, file, tagName);
|
|
1089
|
+
const constraintNode = parseConstraintValue(tagName, text, provenance);
|
|
1090
|
+
if (constraintNode) {
|
|
1091
|
+
constraints.push(constraintNode);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
if (docComment.deprecatedBlock !== void 0) {
|
|
1095
|
+
annotations.push({
|
|
1096
|
+
kind: "annotation",
|
|
1097
|
+
annotationKind: "deprecated",
|
|
1098
|
+
provenance: provenanceForComment(range, sourceFile, file, "deprecated")
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
const jsDocTagsAll = ts2.getJSDocTags(node);
|
|
1104
|
+
for (const tag of jsDocTagsAll) {
|
|
1105
|
+
const tagName = tag.tagName.text;
|
|
1106
|
+
if (!TAGS_REQUIRING_RAW_TEXT.has(tagName)) continue;
|
|
1107
|
+
const commentText = getTagCommentText(tag);
|
|
1108
|
+
if (commentText === void 0 || commentText.trim() === "") continue;
|
|
1109
|
+
const text = commentText.trim();
|
|
1110
|
+
const provenance = provenanceForJSDocTag(tag, file);
|
|
1111
|
+
const constraintNode = parseConstraintValue(tagName, text, provenance);
|
|
1112
|
+
if (constraintNode) {
|
|
1113
|
+
constraints.push(constraintNode);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
let displayName;
|
|
1117
|
+
let description;
|
|
1118
|
+
let displayNameTag;
|
|
1119
|
+
let descriptionTag;
|
|
1120
|
+
for (const tag of jsDocTagsAll) {
|
|
1121
|
+
const tagName = tag.tagName.text;
|
|
1122
|
+
const commentText = getTagCommentText(tag);
|
|
1123
|
+
if (commentText === void 0 || commentText.trim() === "") {
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
const trimmed = commentText.trim();
|
|
1127
|
+
if (tagName === "Field_displayName") {
|
|
1128
|
+
displayName = trimmed;
|
|
1129
|
+
displayNameTag = tag;
|
|
1130
|
+
} else if (tagName === "Field_description") {
|
|
1131
|
+
description = trimmed;
|
|
1132
|
+
descriptionTag = tag;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
if (displayName !== void 0 && displayNameTag) {
|
|
1136
|
+
annotations.push({
|
|
1137
|
+
kind: "annotation",
|
|
1138
|
+
annotationKind: "displayName",
|
|
1139
|
+
value: displayName,
|
|
1140
|
+
provenance: provenanceForJSDocTag(displayNameTag, file)
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
if (description !== void 0 && descriptionTag) {
|
|
1144
|
+
annotations.push({
|
|
1145
|
+
kind: "annotation",
|
|
1146
|
+
annotationKind: "description",
|
|
1147
|
+
value: description,
|
|
1148
|
+
provenance: provenanceForJSDocTag(descriptionTag, file)
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
return { constraints, annotations };
|
|
1152
|
+
}
|
|
1153
|
+
function extractBlockText(block) {
|
|
1154
|
+
return extractPlainText(block.content);
|
|
1155
|
+
}
|
|
1156
|
+
function extractPlainText(node) {
|
|
1157
|
+
let result = "";
|
|
1158
|
+
if (node instanceof import_tsdoc.DocPlainText) {
|
|
1159
|
+
return node.text;
|
|
1160
|
+
}
|
|
1161
|
+
if (node instanceof import_tsdoc.DocSoftBreak) {
|
|
1162
|
+
return " ";
|
|
1163
|
+
}
|
|
1164
|
+
if (typeof node.getChildNodes === "function") {
|
|
1165
|
+
for (const child of node.getChildNodes()) {
|
|
1166
|
+
result += extractPlainText(child);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
return result;
|
|
1170
|
+
}
|
|
1171
|
+
function parseConstraintValue(tagName, text, provenance) {
|
|
1172
|
+
if (!isBuiltinConstraintName(tagName)) {
|
|
1173
|
+
return null;
|
|
1174
|
+
}
|
|
1175
|
+
const expectedType = import_core3.BUILTIN_CONSTRAINT_DEFINITIONS[tagName];
|
|
1176
|
+
if (expectedType === "number") {
|
|
1177
|
+
const value = Number(text);
|
|
1178
|
+
if (Number.isNaN(value)) {
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
const numericKind = NUMERIC_CONSTRAINT_MAP[tagName];
|
|
1182
|
+
if (numericKind) {
|
|
1183
|
+
return {
|
|
1184
|
+
kind: "constraint",
|
|
1185
|
+
constraintKind: numericKind,
|
|
1186
|
+
value,
|
|
1187
|
+
provenance
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
const lengthKind = LENGTH_CONSTRAINT_MAP[tagName];
|
|
1191
|
+
if (lengthKind) {
|
|
1192
|
+
return {
|
|
1193
|
+
kind: "constraint",
|
|
1194
|
+
constraintKind: lengthKind,
|
|
1195
|
+
value,
|
|
1196
|
+
provenance
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
return null;
|
|
1200
|
+
}
|
|
1201
|
+
if (expectedType === "json") {
|
|
1202
|
+
try {
|
|
1203
|
+
const parsed = JSON.parse(text);
|
|
1204
|
+
if (!Array.isArray(parsed)) {
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
const members = [];
|
|
1208
|
+
for (const item of parsed) {
|
|
1209
|
+
if (typeof item === "string" || typeof item === "number") {
|
|
1210
|
+
members.push(item);
|
|
1211
|
+
} else if (typeof item === "object" && item !== null && "id" in item) {
|
|
1212
|
+
const id = item["id"];
|
|
1213
|
+
if (typeof id === "string" || typeof id === "number") {
|
|
1214
|
+
members.push(id);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
return {
|
|
1219
|
+
kind: "constraint",
|
|
1220
|
+
constraintKind: "allowedMembers",
|
|
1221
|
+
members,
|
|
1222
|
+
provenance
|
|
1223
|
+
};
|
|
1224
|
+
} catch {
|
|
1225
|
+
return null;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
return {
|
|
1229
|
+
kind: "constraint",
|
|
1230
|
+
constraintKind: "pattern",
|
|
1231
|
+
pattern: text,
|
|
1232
|
+
provenance
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
function provenanceForComment(range, sourceFile, file, tagName) {
|
|
1236
|
+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(range.pos);
|
|
1237
|
+
return {
|
|
1238
|
+
surface: "tsdoc",
|
|
1239
|
+
file,
|
|
1240
|
+
line: line + 1,
|
|
1241
|
+
column: character,
|
|
1242
|
+
tagName: "@" + tagName
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
function provenanceForJSDocTag(tag, file) {
|
|
1246
|
+
const sourceFile = tag.getSourceFile();
|
|
1247
|
+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(tag.getStart());
|
|
1248
|
+
return {
|
|
1249
|
+
surface: "tsdoc",
|
|
1250
|
+
file,
|
|
1251
|
+
line: line + 1,
|
|
1252
|
+
column: character,
|
|
1253
|
+
tagName: "@" + tag.tagName.text
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
function getTagCommentText(tag) {
|
|
1257
|
+
if (tag.comment === void 0) {
|
|
1258
|
+
return void 0;
|
|
1259
|
+
}
|
|
1260
|
+
if (typeof tag.comment === "string") {
|
|
1261
|
+
return tag.comment;
|
|
1262
|
+
}
|
|
1263
|
+
return ts2.getTextOfJSDocComment(tag.comment);
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
// src/analyzer/jsdoc-constraints.ts
|
|
1267
|
+
function extractJSDocConstraintNodes(node, file = "") {
|
|
1268
|
+
const result = parseTSDocTags(node, file);
|
|
1269
|
+
return [...result.constraints];
|
|
1270
|
+
}
|
|
1271
|
+
function extractJSDocAnnotationNodes(node, file = "") {
|
|
1272
|
+
const result = parseTSDocTags(node, file);
|
|
1273
|
+
return [...result.annotations];
|
|
1274
|
+
}
|
|
1275
|
+
function extractDefaultValueAnnotation(initializer, file = "") {
|
|
1276
|
+
if (!initializer) return null;
|
|
1277
|
+
let value;
|
|
1278
|
+
if (ts3.isStringLiteral(initializer)) {
|
|
1279
|
+
value = initializer.text;
|
|
1280
|
+
} else if (ts3.isNumericLiteral(initializer)) {
|
|
1281
|
+
value = Number(initializer.text);
|
|
1282
|
+
} else if (initializer.kind === ts3.SyntaxKind.TrueKeyword) {
|
|
1283
|
+
value = true;
|
|
1284
|
+
} else if (initializer.kind === ts3.SyntaxKind.FalseKeyword) {
|
|
1285
|
+
value = false;
|
|
1286
|
+
} else if (initializer.kind === ts3.SyntaxKind.NullKeyword) {
|
|
1287
|
+
value = null;
|
|
1288
|
+
} else if (ts3.isPrefixUnaryExpression(initializer)) {
|
|
1289
|
+
if (initializer.operator === ts3.SyntaxKind.MinusToken && ts3.isNumericLiteral(initializer.operand)) {
|
|
1290
|
+
value = -Number(initializer.operand.text);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
if (value === void 0) return null;
|
|
1294
|
+
const sourceFile = initializer.getSourceFile();
|
|
1295
|
+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(initializer.getStart());
|
|
1296
|
+
return {
|
|
1297
|
+
kind: "annotation",
|
|
1298
|
+
annotationKind: "defaultValue",
|
|
1299
|
+
value,
|
|
1300
|
+
provenance: {
|
|
1301
|
+
surface: "tsdoc",
|
|
1302
|
+
file,
|
|
1303
|
+
line: line + 1,
|
|
1304
|
+
column: character
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
// src/analyzer/class-analyzer.ts
|
|
1310
|
+
function isObjectType(type) {
|
|
1311
|
+
return !!(type.flags & ts4.TypeFlags.Object);
|
|
1312
|
+
}
|
|
1313
|
+
function isTypeReference(type) {
|
|
1314
|
+
return !!(type.flags & ts4.TypeFlags.Object) && !!(type.objectFlags & ts4.ObjectFlags.Reference);
|
|
1315
|
+
}
|
|
1316
|
+
function analyzeClassToIR(classDecl, checker, file = "") {
|
|
1317
|
+
const name = classDecl.name?.text ?? "AnonymousClass";
|
|
1318
|
+
const fields = [];
|
|
1319
|
+
const fieldLayouts = [];
|
|
1320
|
+
const typeRegistry = {};
|
|
1321
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
1322
|
+
const instanceMethods = [];
|
|
1323
|
+
const staticMethods = [];
|
|
1324
|
+
for (const member of classDecl.members) {
|
|
1325
|
+
if (ts4.isPropertyDeclaration(member)) {
|
|
1326
|
+
const fieldNode = analyzeFieldToIR(member, checker, file, typeRegistry, visiting);
|
|
1327
|
+
if (fieldNode) {
|
|
1328
|
+
fields.push(fieldNode);
|
|
1329
|
+
fieldLayouts.push({});
|
|
1330
|
+
}
|
|
1331
|
+
} else if (ts4.isMethodDeclaration(member)) {
|
|
1332
|
+
const methodInfo = analyzeMethod(member, checker);
|
|
1333
|
+
if (methodInfo) {
|
|
1334
|
+
const isStatic = member.modifiers?.some((m) => m.kind === ts4.SyntaxKind.StaticKeyword);
|
|
1335
|
+
if (isStatic) {
|
|
1336
|
+
staticMethods.push(methodInfo);
|
|
1337
|
+
} else {
|
|
1338
|
+
instanceMethods.push(methodInfo);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
return { name, fields, fieldLayouts, typeRegistry, instanceMethods, staticMethods };
|
|
1344
|
+
}
|
|
1345
|
+
function analyzeInterfaceToIR(interfaceDecl, checker, file = "") {
|
|
1346
|
+
const name = interfaceDecl.name.text;
|
|
1347
|
+
const fields = [];
|
|
1348
|
+
const typeRegistry = {};
|
|
1349
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
1350
|
+
for (const member of interfaceDecl.members) {
|
|
1351
|
+
if (ts4.isPropertySignature(member)) {
|
|
1352
|
+
const fieldNode = analyzeInterfacePropertyToIR(member, checker, file, typeRegistry, visiting);
|
|
1353
|
+
if (fieldNode) {
|
|
1354
|
+
fields.push(fieldNode);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
const fieldLayouts = fields.map(() => ({}));
|
|
1359
|
+
return { name, fields, fieldLayouts, typeRegistry, instanceMethods: [], staticMethods: [] };
|
|
1360
|
+
}
|
|
1361
|
+
function analyzeTypeAliasToIR(typeAlias, checker, file = "") {
|
|
1362
|
+
if (!ts4.isTypeLiteralNode(typeAlias.type)) {
|
|
1363
|
+
const sourceFile = typeAlias.getSourceFile();
|
|
1364
|
+
const { line } = sourceFile.getLineAndCharacterOfPosition(typeAlias.getStart());
|
|
1365
|
+
const kindDesc = ts4.SyntaxKind[typeAlias.type.kind] ?? "unknown";
|
|
1366
|
+
return {
|
|
1367
|
+
ok: false,
|
|
1368
|
+
error: `Type alias "${typeAlias.name.text}" at line ${String(line + 1)} is not an object type literal (found ${kindDesc})`
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
const name = typeAlias.name.text;
|
|
1372
|
+
const fields = [];
|
|
1373
|
+
const typeRegistry = {};
|
|
1374
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
1375
|
+
for (const member of typeAlias.type.members) {
|
|
1376
|
+
if (ts4.isPropertySignature(member)) {
|
|
1377
|
+
const fieldNode = analyzeInterfacePropertyToIR(member, checker, file, typeRegistry, visiting);
|
|
1378
|
+
if (fieldNode) {
|
|
1379
|
+
fields.push(fieldNode);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
return {
|
|
1384
|
+
ok: true,
|
|
1385
|
+
analysis: {
|
|
1386
|
+
name,
|
|
1387
|
+
fields,
|
|
1388
|
+
fieldLayouts: fields.map(() => ({})),
|
|
1389
|
+
typeRegistry,
|
|
1390
|
+
instanceMethods: [],
|
|
1391
|
+
staticMethods: []
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
function analyzeFieldToIR(prop, checker, file, typeRegistry, visiting) {
|
|
1396
|
+
if (!ts4.isIdentifier(prop.name)) {
|
|
1397
|
+
return null;
|
|
1398
|
+
}
|
|
1399
|
+
const name = prop.name.text;
|
|
1400
|
+
const tsType = checker.getTypeAtLocation(prop);
|
|
1401
|
+
const optional = prop.questionToken !== void 0;
|
|
1402
|
+
const provenance = provenanceForNode(prop, file);
|
|
1403
|
+
const type = resolveTypeNode(tsType, checker, file, typeRegistry, visiting);
|
|
1404
|
+
const constraints = [];
|
|
1405
|
+
if (prop.type) {
|
|
1406
|
+
constraints.push(...extractTypeAliasConstraintNodes(prop.type, checker, file));
|
|
1407
|
+
}
|
|
1408
|
+
constraints.push(...extractJSDocConstraintNodes(prop, file));
|
|
1409
|
+
const annotations = [];
|
|
1410
|
+
annotations.push(...extractJSDocAnnotationNodes(prop, file));
|
|
1411
|
+
const defaultAnnotation = extractDefaultValueAnnotation(prop.initializer, file);
|
|
1412
|
+
if (defaultAnnotation) {
|
|
1413
|
+
annotations.push(defaultAnnotation);
|
|
1414
|
+
}
|
|
1415
|
+
return {
|
|
1416
|
+
kind: "field",
|
|
1417
|
+
name,
|
|
1418
|
+
type,
|
|
1419
|
+
required: !optional,
|
|
1420
|
+
constraints,
|
|
1421
|
+
annotations,
|
|
1422
|
+
provenance
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
function analyzeInterfacePropertyToIR(prop, checker, file, typeRegistry, visiting) {
|
|
1426
|
+
if (!ts4.isIdentifier(prop.name)) {
|
|
1427
|
+
return null;
|
|
1428
|
+
}
|
|
1429
|
+
const name = prop.name.text;
|
|
1430
|
+
const tsType = checker.getTypeAtLocation(prop);
|
|
1431
|
+
const optional = prop.questionToken !== void 0;
|
|
1432
|
+
const provenance = provenanceForNode(prop, file);
|
|
1433
|
+
const type = resolveTypeNode(tsType, checker, file, typeRegistry, visiting);
|
|
1434
|
+
const constraints = [];
|
|
1435
|
+
if (prop.type) {
|
|
1436
|
+
constraints.push(...extractTypeAliasConstraintNodes(prop.type, checker, file));
|
|
1437
|
+
}
|
|
1438
|
+
constraints.push(...extractJSDocConstraintNodes(prop, file));
|
|
1439
|
+
const annotations = [];
|
|
1440
|
+
annotations.push(...extractJSDocAnnotationNodes(prop, file));
|
|
1441
|
+
return {
|
|
1442
|
+
kind: "field",
|
|
1443
|
+
name,
|
|
1444
|
+
type,
|
|
1445
|
+
required: !optional,
|
|
1446
|
+
constraints,
|
|
1447
|
+
annotations,
|
|
1448
|
+
provenance
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
function resolveTypeNode(type, checker, file, typeRegistry, visiting) {
|
|
1452
|
+
if (type.flags & ts4.TypeFlags.String) {
|
|
1453
|
+
return { kind: "primitive", primitiveKind: "string" };
|
|
1454
|
+
}
|
|
1455
|
+
if (type.flags & ts4.TypeFlags.Number) {
|
|
1456
|
+
return { kind: "primitive", primitiveKind: "number" };
|
|
1457
|
+
}
|
|
1458
|
+
if (type.flags & ts4.TypeFlags.Boolean) {
|
|
1459
|
+
return { kind: "primitive", primitiveKind: "boolean" };
|
|
1460
|
+
}
|
|
1461
|
+
if (type.flags & ts4.TypeFlags.Null) {
|
|
1462
|
+
return { kind: "primitive", primitiveKind: "null" };
|
|
1463
|
+
}
|
|
1464
|
+
if (type.flags & ts4.TypeFlags.Undefined) {
|
|
1465
|
+
return { kind: "primitive", primitiveKind: "null" };
|
|
1466
|
+
}
|
|
1467
|
+
if (type.isStringLiteral()) {
|
|
1468
|
+
return {
|
|
1469
|
+
kind: "enum",
|
|
1470
|
+
members: [{ value: type.value }]
|
|
1471
|
+
};
|
|
1472
|
+
}
|
|
1473
|
+
if (type.isNumberLiteral()) {
|
|
1474
|
+
return {
|
|
1475
|
+
kind: "enum",
|
|
1476
|
+
members: [{ value: type.value }]
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1479
|
+
if (type.isUnion()) {
|
|
1480
|
+
return resolveUnionType(type, checker, file, typeRegistry, visiting);
|
|
1481
|
+
}
|
|
1482
|
+
if (checker.isArrayType(type)) {
|
|
1483
|
+
return resolveArrayType(type, checker, file, typeRegistry, visiting);
|
|
1484
|
+
}
|
|
1485
|
+
if (isObjectType(type)) {
|
|
1486
|
+
return resolveObjectType(type, checker, file, typeRegistry, visiting);
|
|
1487
|
+
}
|
|
1488
|
+
return { kind: "primitive", primitiveKind: "string" };
|
|
1489
|
+
}
|
|
1490
|
+
function resolveUnionType(type, checker, file, typeRegistry, visiting) {
|
|
1491
|
+
const allTypes = type.types;
|
|
1492
|
+
const nonNullTypes = allTypes.filter(
|
|
1493
|
+
(t) => !(t.flags & (ts4.TypeFlags.Null | ts4.TypeFlags.Undefined))
|
|
1494
|
+
);
|
|
1495
|
+
const hasNull = allTypes.some((t) => t.flags & ts4.TypeFlags.Null);
|
|
1496
|
+
const isBooleanUnion2 = nonNullTypes.length === 2 && nonNullTypes.every((t) => t.flags & ts4.TypeFlags.BooleanLiteral);
|
|
1497
|
+
if (isBooleanUnion2) {
|
|
1498
|
+
const boolNode = { kind: "primitive", primitiveKind: "boolean" };
|
|
1499
|
+
if (hasNull) {
|
|
1500
|
+
return {
|
|
1501
|
+
kind: "union",
|
|
1502
|
+
members: [boolNode, { kind: "primitive", primitiveKind: "null" }]
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
return boolNode;
|
|
1506
|
+
}
|
|
1507
|
+
const allStringLiterals = nonNullTypes.every((t) => t.isStringLiteral());
|
|
1508
|
+
if (allStringLiterals && nonNullTypes.length > 0) {
|
|
1509
|
+
const stringTypes = nonNullTypes.filter((t) => t.isStringLiteral());
|
|
1510
|
+
const enumNode = {
|
|
1511
|
+
kind: "enum",
|
|
1512
|
+
members: stringTypes.map((t) => ({ value: t.value }))
|
|
1513
|
+
};
|
|
1514
|
+
if (hasNull) {
|
|
1515
|
+
return {
|
|
1516
|
+
kind: "union",
|
|
1517
|
+
members: [enumNode, { kind: "primitive", primitiveKind: "null" }]
|
|
1518
|
+
};
|
|
1519
|
+
}
|
|
1520
|
+
return enumNode;
|
|
1521
|
+
}
|
|
1522
|
+
const allNumberLiterals = nonNullTypes.every((t) => t.isNumberLiteral());
|
|
1523
|
+
if (allNumberLiterals && nonNullTypes.length > 0) {
|
|
1524
|
+
const numberTypes = nonNullTypes.filter((t) => t.isNumberLiteral());
|
|
1525
|
+
const enumNode = {
|
|
1526
|
+
kind: "enum",
|
|
1527
|
+
members: numberTypes.map((t) => ({ value: t.value }))
|
|
1528
|
+
};
|
|
1529
|
+
if (hasNull) {
|
|
1530
|
+
return {
|
|
1531
|
+
kind: "union",
|
|
1532
|
+
members: [enumNode, { kind: "primitive", primitiveKind: "null" }]
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
return enumNode;
|
|
1536
|
+
}
|
|
1537
|
+
if (nonNullTypes.length === 1 && nonNullTypes[0]) {
|
|
1538
|
+
const inner = resolveTypeNode(nonNullTypes[0], checker, file, typeRegistry, visiting);
|
|
1539
|
+
if (hasNull) {
|
|
1540
|
+
return {
|
|
1541
|
+
kind: "union",
|
|
1542
|
+
members: [inner, { kind: "primitive", primitiveKind: "null" }]
|
|
1543
|
+
};
|
|
1544
|
+
}
|
|
1545
|
+
return inner;
|
|
1546
|
+
}
|
|
1547
|
+
const members = nonNullTypes.map(
|
|
1548
|
+
(t) => resolveTypeNode(t, checker, file, typeRegistry, visiting)
|
|
1549
|
+
);
|
|
1550
|
+
if (hasNull) {
|
|
1551
|
+
members.push({ kind: "primitive", primitiveKind: "null" });
|
|
1552
|
+
}
|
|
1553
|
+
return { kind: "union", members };
|
|
1554
|
+
}
|
|
1555
|
+
function resolveArrayType(type, checker, file, typeRegistry, visiting) {
|
|
1556
|
+
const typeArgs = isTypeReference(type) ? type.typeArguments : void 0;
|
|
1557
|
+
const elementType = typeArgs?.[0];
|
|
1558
|
+
const items = elementType ? resolveTypeNode(elementType, checker, file, typeRegistry, visiting) : { kind: "primitive", primitiveKind: "string" };
|
|
1559
|
+
return { kind: "array", items };
|
|
1560
|
+
}
|
|
1561
|
+
function resolveObjectType(type, checker, file, typeRegistry, visiting) {
|
|
1562
|
+
if (visiting.has(type)) {
|
|
1563
|
+
return { kind: "object", properties: [], additionalProperties: false };
|
|
1564
|
+
}
|
|
1565
|
+
visiting.add(type);
|
|
1566
|
+
const typeName = getNamedTypeName(type);
|
|
1567
|
+
if (typeName && typeName in typeRegistry) {
|
|
1568
|
+
visiting.delete(type);
|
|
1569
|
+
return { kind: "reference", name: typeName, typeArguments: [] };
|
|
1570
|
+
}
|
|
1571
|
+
const properties = [];
|
|
1572
|
+
const fieldInfoMap = getNamedTypeFieldNodeInfoMap(type, checker, file, typeRegistry, visiting);
|
|
1573
|
+
for (const prop of type.getProperties()) {
|
|
1574
|
+
const declaration = prop.valueDeclaration ?? prop.declarations?.[0];
|
|
1575
|
+
if (!declaration) continue;
|
|
1576
|
+
const propType = checker.getTypeOfSymbolAtLocation(prop, declaration);
|
|
1577
|
+
const optional = !!(prop.flags & ts4.SymbolFlags.Optional);
|
|
1578
|
+
const propTypeNode = resolveTypeNode(propType, checker, file, typeRegistry, visiting);
|
|
1579
|
+
const fieldNodeInfo = fieldInfoMap?.get(prop.name);
|
|
1580
|
+
properties.push({
|
|
1581
|
+
name: prop.name,
|
|
1582
|
+
type: propTypeNode,
|
|
1583
|
+
optional,
|
|
1584
|
+
constraints: fieldNodeInfo?.constraints ?? [],
|
|
1585
|
+
annotations: fieldNodeInfo?.annotations ?? [],
|
|
1586
|
+
provenance: fieldNodeInfo?.provenance ?? provenanceForFile(file)
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
visiting.delete(type);
|
|
1590
|
+
const objectNode = {
|
|
1591
|
+
kind: "object",
|
|
1592
|
+
properties,
|
|
1593
|
+
additionalProperties: false
|
|
1594
|
+
};
|
|
1595
|
+
if (typeName) {
|
|
1596
|
+
typeRegistry[typeName] = {
|
|
1597
|
+
name: typeName,
|
|
1598
|
+
type: objectNode,
|
|
1599
|
+
provenance: provenanceForFile(file)
|
|
1600
|
+
};
|
|
1601
|
+
return { kind: "reference", name: typeName, typeArguments: [] };
|
|
1602
|
+
}
|
|
1603
|
+
return objectNode;
|
|
1604
|
+
}
|
|
1605
|
+
function getNamedTypeFieldNodeInfoMap(type, checker, file, typeRegistry, visiting) {
|
|
1606
|
+
const symbols = [type.getSymbol(), type.aliasSymbol].filter(
|
|
1607
|
+
(s) => s?.declarations != null && s.declarations.length > 0
|
|
1608
|
+
);
|
|
1609
|
+
for (const symbol of symbols) {
|
|
1610
|
+
const declarations = symbol.declarations;
|
|
1611
|
+
if (!declarations) continue;
|
|
1612
|
+
const classDecl = declarations.find(ts4.isClassDeclaration);
|
|
1613
|
+
if (classDecl) {
|
|
1614
|
+
const map = /* @__PURE__ */ new Map();
|
|
1615
|
+
for (const member of classDecl.members) {
|
|
1616
|
+
if (ts4.isPropertyDeclaration(member) && ts4.isIdentifier(member.name)) {
|
|
1617
|
+
const fieldNode = analyzeFieldToIR(member, checker, file, typeRegistry, visiting);
|
|
1618
|
+
if (fieldNode) {
|
|
1619
|
+
map.set(fieldNode.name, {
|
|
1620
|
+
constraints: [...fieldNode.constraints],
|
|
1621
|
+
annotations: [...fieldNode.annotations],
|
|
1622
|
+
provenance: fieldNode.provenance
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
return map;
|
|
1628
|
+
}
|
|
1629
|
+
const interfaceDecl = declarations.find(ts4.isInterfaceDeclaration);
|
|
1630
|
+
if (interfaceDecl) {
|
|
1631
|
+
return buildFieldNodeInfoMap(interfaceDecl.members, checker, file, typeRegistry, visiting);
|
|
1632
|
+
}
|
|
1633
|
+
const typeAliasDecl = declarations.find(ts4.isTypeAliasDeclaration);
|
|
1634
|
+
if (typeAliasDecl && ts4.isTypeLiteralNode(typeAliasDecl.type)) {
|
|
1635
|
+
return buildFieldNodeInfoMap(
|
|
1636
|
+
typeAliasDecl.type.members,
|
|
1637
|
+
checker,
|
|
1638
|
+
file,
|
|
1639
|
+
typeRegistry,
|
|
1640
|
+
visiting
|
|
1641
|
+
);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
return null;
|
|
1645
|
+
}
|
|
1646
|
+
function buildFieldNodeInfoMap(members, checker, file, typeRegistry, visiting) {
|
|
1647
|
+
const map = /* @__PURE__ */ new Map();
|
|
1648
|
+
for (const member of members) {
|
|
1649
|
+
if (ts4.isPropertySignature(member)) {
|
|
1650
|
+
const fieldNode = analyzeInterfacePropertyToIR(member, checker, file, typeRegistry, visiting);
|
|
1651
|
+
if (fieldNode) {
|
|
1652
|
+
map.set(fieldNode.name, {
|
|
1653
|
+
constraints: [...fieldNode.constraints],
|
|
1654
|
+
annotations: [...fieldNode.annotations],
|
|
1655
|
+
provenance: fieldNode.provenance
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
return map;
|
|
1661
|
+
}
|
|
1662
|
+
function extractTypeAliasConstraintNodes(typeNode, checker, file) {
|
|
1663
|
+
if (!ts4.isTypeReferenceNode(typeNode)) return [];
|
|
1664
|
+
const symbol = checker.getSymbolAtLocation(typeNode.typeName);
|
|
1665
|
+
if (!symbol?.declarations) return [];
|
|
1666
|
+
const aliasDecl = symbol.declarations.find(ts4.isTypeAliasDeclaration);
|
|
1667
|
+
if (!aliasDecl) return [];
|
|
1668
|
+
if (ts4.isTypeLiteralNode(aliasDecl.type)) return [];
|
|
1669
|
+
return extractJSDocConstraintNodes(aliasDecl, file);
|
|
1670
|
+
}
|
|
1671
|
+
function provenanceForNode(node, file) {
|
|
1672
|
+
const sourceFile = node.getSourceFile();
|
|
1673
|
+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
1674
|
+
return {
|
|
1675
|
+
surface: "tsdoc",
|
|
1676
|
+
file,
|
|
1677
|
+
line: line + 1,
|
|
1678
|
+
column: character
|
|
1679
|
+
};
|
|
1680
|
+
}
|
|
1681
|
+
function provenanceForFile(file) {
|
|
1682
|
+
return { surface: "tsdoc", file, line: 0, column: 0 };
|
|
1683
|
+
}
|
|
1684
|
+
function getNamedTypeName(type) {
|
|
1685
|
+
const symbol = type.getSymbol();
|
|
1686
|
+
if (symbol?.declarations) {
|
|
1687
|
+
const decl = symbol.declarations[0];
|
|
1688
|
+
if (decl && (ts4.isClassDeclaration(decl) || ts4.isInterfaceDeclaration(decl) || ts4.isTypeAliasDeclaration(decl))) {
|
|
1689
|
+
const name = ts4.isClassDeclaration(decl) ? decl.name?.text : decl.name.text;
|
|
1690
|
+
if (name) return name;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
const aliasSymbol = type.aliasSymbol;
|
|
1694
|
+
if (aliasSymbol?.declarations) {
|
|
1695
|
+
const aliasDecl = aliasSymbol.declarations.find(ts4.isTypeAliasDeclaration);
|
|
1696
|
+
if (aliasDecl) {
|
|
1697
|
+
return aliasDecl.name.text;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
return null;
|
|
1701
|
+
}
|
|
1702
|
+
function analyzeMethod(method, checker) {
|
|
1703
|
+
if (!ts4.isIdentifier(method.name)) {
|
|
1704
|
+
return null;
|
|
1705
|
+
}
|
|
1706
|
+
const name = method.name.text;
|
|
1707
|
+
const parameters = [];
|
|
1708
|
+
for (const param of method.parameters) {
|
|
1709
|
+
if (ts4.isIdentifier(param.name)) {
|
|
1710
|
+
const paramInfo = analyzeParameter(param, checker);
|
|
1711
|
+
parameters.push(paramInfo);
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
const returnTypeNode = method.type;
|
|
1715
|
+
const signature = checker.getSignatureFromDeclaration(method);
|
|
1716
|
+
const returnType = signature ? checker.getReturnTypeOfSignature(signature) : checker.getTypeAtLocation(method);
|
|
1717
|
+
return { name, parameters, returnTypeNode, returnType };
|
|
1718
|
+
}
|
|
1719
|
+
function analyzeParameter(param, checker) {
|
|
1720
|
+
const name = ts4.isIdentifier(param.name) ? param.name.text : "param";
|
|
1721
|
+
const typeNode = param.type;
|
|
1722
|
+
const type = checker.getTypeAtLocation(param);
|
|
1723
|
+
const formSpecExportName = detectFormSpecReference(typeNode);
|
|
1724
|
+
const optional = param.questionToken !== void 0 || param.initializer !== void 0;
|
|
1725
|
+
return { name, typeNode, type, formSpecExportName, optional };
|
|
1726
|
+
}
|
|
1727
|
+
function detectFormSpecReference(typeNode) {
|
|
1728
|
+
if (!typeNode) return null;
|
|
1729
|
+
if (!ts4.isTypeReferenceNode(typeNode)) return null;
|
|
1730
|
+
const typeName = ts4.isIdentifier(typeNode.typeName) ? typeNode.typeName.text : ts4.isQualifiedName(typeNode.typeName) ? typeNode.typeName.right.text : null;
|
|
1731
|
+
if (typeName !== "InferSchema" && typeName !== "InferFormSchema") return null;
|
|
1732
|
+
const typeArg = typeNode.typeArguments?.[0];
|
|
1733
|
+
if (!typeArg || !ts4.isTypeQueryNode(typeArg)) return null;
|
|
1734
|
+
if (ts4.isIdentifier(typeArg.exprName)) {
|
|
1735
|
+
return typeArg.exprName.text;
|
|
1736
|
+
}
|
|
1737
|
+
if (ts4.isQualifiedName(typeArg.exprName)) {
|
|
1738
|
+
return typeArg.exprName.right.text;
|
|
1739
|
+
}
|
|
1740
|
+
return null;
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
// src/generators/class-schema.ts
|
|
1744
|
+
function generateClassSchemas(analysis, source) {
|
|
1745
|
+
const ir = canonicalizeTSDoc(analysis, source);
|
|
1746
|
+
return {
|
|
1747
|
+
jsonSchema: generateJsonSchemaFromIR(ir),
|
|
1748
|
+
uiSchema: generateUiSchemaFromIR(ir)
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
function generateSchemasFromClass(options) {
|
|
1752
|
+
const ctx = createProgramContext(options.filePath);
|
|
1753
|
+
const classDecl = findClassByName(ctx.sourceFile, options.className);
|
|
1754
|
+
if (!classDecl) {
|
|
1755
|
+
throw new Error(`Class "${options.className}" not found in ${options.filePath}`);
|
|
1756
|
+
}
|
|
1757
|
+
const analysis = analyzeClassToIR(classDecl, ctx.checker, options.filePath);
|
|
1758
|
+
return generateClassSchemas(analysis, { file: options.filePath });
|
|
1759
|
+
}
|
|
1760
|
+
function generateSchemas(options) {
|
|
1761
|
+
const ctx = createProgramContext(options.filePath);
|
|
1762
|
+
const source = { file: options.filePath };
|
|
1763
|
+
const classDecl = findClassByName(ctx.sourceFile, options.typeName);
|
|
1764
|
+
if (classDecl) {
|
|
1765
|
+
const analysis = analyzeClassToIR(classDecl, ctx.checker, options.filePath);
|
|
1766
|
+
return generateClassSchemas(analysis, source);
|
|
1767
|
+
}
|
|
1768
|
+
const interfaceDecl = findInterfaceByName(ctx.sourceFile, options.typeName);
|
|
1769
|
+
if (interfaceDecl) {
|
|
1770
|
+
const analysis = analyzeInterfaceToIR(interfaceDecl, ctx.checker, options.filePath);
|
|
1771
|
+
return generateClassSchemas(analysis, source);
|
|
1772
|
+
}
|
|
1773
|
+
const typeAlias = findTypeAliasByName(ctx.sourceFile, options.typeName);
|
|
1774
|
+
if (typeAlias) {
|
|
1775
|
+
const result = analyzeTypeAliasToIR(typeAlias, ctx.checker, options.filePath);
|
|
1776
|
+
if (result.ok) {
|
|
1777
|
+
return generateClassSchemas(result.analysis, source);
|
|
1778
|
+
}
|
|
1779
|
+
throw new Error(result.error);
|
|
1780
|
+
}
|
|
1781
|
+
throw new Error(
|
|
1782
|
+
`Type "${options.typeName}" not found as a class, interface, or type alias in ${options.filePath}`
|
|
1783
|
+
);
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
// src/index.ts
|
|
1787
|
+
function buildFormSchemas(form) {
|
|
1788
|
+
return {
|
|
1789
|
+
jsonSchema: generateJsonSchema(form),
|
|
1790
|
+
uiSchema: generateUiSchema(form)
|
|
1791
|
+
};
|
|
1792
|
+
}
|
|
1793
|
+
function writeSchemas(form, options) {
|
|
1794
|
+
const { outDir, name = "schema", indent = 2 } = options;
|
|
1795
|
+
const { jsonSchema, uiSchema: uiSchema2 } = buildFormSchemas(form);
|
|
1796
|
+
if (!fs.existsSync(outDir)) {
|
|
1797
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
1798
|
+
}
|
|
1799
|
+
const jsonSchemaPath = path2.join(outDir, `${name}-schema.json`);
|
|
1800
|
+
const uiSchemaPath = path2.join(outDir, `${name}-uischema.json`);
|
|
1801
|
+
fs.writeFileSync(jsonSchemaPath, JSON.stringify(jsonSchema, null, indent));
|
|
1802
|
+
fs.writeFileSync(uiSchemaPath, JSON.stringify(uiSchema2, null, indent));
|
|
1803
|
+
return { jsonSchemaPath, uiSchemaPath };
|
|
1804
|
+
}
|
|
1805
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1806
|
+
0 && (module.exports = {
|
|
1807
|
+
buildFormSchemas,
|
|
1808
|
+
categorizationSchema,
|
|
1809
|
+
categorySchema,
|
|
1810
|
+
controlSchema,
|
|
1811
|
+
generateJsonSchema,
|
|
1812
|
+
generateSchemas,
|
|
1813
|
+
generateSchemasFromClass,
|
|
1814
|
+
generateUiSchema,
|
|
1815
|
+
getSchemaExtension,
|
|
1816
|
+
groupLayoutSchema,
|
|
1817
|
+
horizontalLayoutSchema,
|
|
1818
|
+
jsonSchema7Schema,
|
|
1819
|
+
jsonSchemaTypeSchema,
|
|
1820
|
+
labelElementSchema,
|
|
1821
|
+
ruleConditionSchema,
|
|
1822
|
+
ruleEffectSchema,
|
|
1823
|
+
ruleSchema,
|
|
1824
|
+
schemaBasedConditionSchema,
|
|
1825
|
+
setSchemaExtension,
|
|
1826
|
+
uiSchemaElementSchema,
|
|
1827
|
+
uiSchemaElementTypeSchema,
|
|
1828
|
+
uiSchemaSchema,
|
|
1829
|
+
verticalLayoutSchema,
|
|
1830
|
+
writeSchemas
|
|
1831
|
+
});
|
|
1832
|
+
//# sourceMappingURL=index.cjs.map
|