vovk-cli 0.0.1-draft.275 → 0.0.1-draft.277
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/utils/compileTs.mjs +27 -14
- package/package.json +1 -1
package/dist/utils/compileTs.mjs
CHANGED
|
@@ -8,7 +8,9 @@ export function compileTs(options) {
|
|
|
8
8
|
};
|
|
9
9
|
// Collect all definitions from the schema
|
|
10
10
|
collectDefinitions(options.schema, context.refs);
|
|
11
|
-
|
|
11
|
+
// Ensure the main type name is valid
|
|
12
|
+
const mainTypeName = sanitizeTypeName(options.name);
|
|
13
|
+
const mainType = compileSchema(options.schema, mainTypeName, context);
|
|
12
14
|
// Compile all referenced types, unless dontCreateRefTypes is set
|
|
13
15
|
const compiledRefs = options.dontCreateRefTypes
|
|
14
16
|
? ''
|
|
@@ -16,8 +18,8 @@ export function compileTs(options) {
|
|
|
16
18
|
.map(([, typeDecl]) => typeDecl)
|
|
17
19
|
.join('\n\n');
|
|
18
20
|
return compiledRefs
|
|
19
|
-
? `${compiledRefs}\n\n${options.schema.description ? `/** ${options.schema.description} */\n` : ''}export type ${
|
|
20
|
-
: `${options.schema.description ? `/** ${options.schema.description} */\n` : ''}export type ${
|
|
21
|
+
? `${compiledRefs}\n\n${options.schema.description ? `/** ${escapeJSDocComment(options.schema.description)} */\n` : ''}export type ${mainTypeName} = ${mainType};`
|
|
22
|
+
: `${options.schema.description ? `/** ${escapeJSDocComment(options.schema.description)} */\n` : ''}export type ${mainTypeName} = ${mainType};`;
|
|
21
23
|
}
|
|
22
24
|
function collectDefinitions(schema, refs) {
|
|
23
25
|
// Collect from $defs
|
|
@@ -151,14 +153,16 @@ function handleRef(ref, context) {
|
|
|
151
153
|
context.refsInProgress.add(ref);
|
|
152
154
|
// Compile the referenced schema
|
|
153
155
|
const compiledType = compileSchema(referencedSchema, typeName, context);
|
|
154
|
-
const description = referencedSchema.description
|
|
156
|
+
const description = referencedSchema.description
|
|
157
|
+
? `/** ${escapeJSDocComment(referencedSchema.description)} */\n`
|
|
158
|
+
: '';
|
|
155
159
|
context.compiledRefs.set(ref, `${description}export type ${typeName} = ${compiledType};`);
|
|
156
160
|
// Mark as completed
|
|
157
161
|
context.refsInProgress.delete(ref);
|
|
158
162
|
return typeName;
|
|
159
163
|
}
|
|
160
164
|
function handleAllOf(schemas, name, context) {
|
|
161
|
-
const types = schemas.map((s, i) => compileSchema(s, `${name}
|
|
165
|
+
const types = schemas.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-all-of-${i}`), context));
|
|
162
166
|
// For allOf, we need to intersect types
|
|
163
167
|
// If they're all objects, we can merge them properly
|
|
164
168
|
const objectTypes = types.filter((t) => t.startsWith('{') && t.endsWith('}'));
|
|
@@ -170,12 +174,12 @@ function handleAllOf(schemas, name, context) {
|
|
|
170
174
|
return types.join(' & ');
|
|
171
175
|
}
|
|
172
176
|
function handleAnyOf(schemas, name, context) {
|
|
173
|
-
const types = schemas.map((s, i) => compileSchema(s, `${name}
|
|
177
|
+
const types = schemas.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-any-of-${i}`), context));
|
|
174
178
|
return types.join(' | ');
|
|
175
179
|
}
|
|
176
180
|
function handleOneOf(schemas, name, context) {
|
|
177
181
|
// For TypeScript, oneOf behaves like anyOf
|
|
178
|
-
const types = schemas.map((s, i) => compileSchema(s, `${name}
|
|
182
|
+
const types = schemas.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-one-of-${i}`), context));
|
|
179
183
|
return types.join(' | ');
|
|
180
184
|
}
|
|
181
185
|
function handleEnum(enumValues) {
|
|
@@ -205,10 +209,12 @@ function handleObject(schema, name, context) {
|
|
|
205
209
|
if (!isSchema(propSchema))
|
|
206
210
|
continue;
|
|
207
211
|
const isRequired = required.has(propName);
|
|
208
|
-
|
|
212
|
+
// Ensure the generated type name for nested properties is valid
|
|
213
|
+
const nestedTypeName = sanitizeTypeName(`${name}-${propName}`);
|
|
214
|
+
const propType = compileSchema(propSchema, nestedTypeName, context);
|
|
209
215
|
const safePropName = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propName) ? propName : `"${propName}"`;
|
|
210
216
|
// Add JSDoc comment if description is present
|
|
211
|
-
const comment = propSchema.description ? `\n/** ${propSchema.description} */\n` : '';
|
|
217
|
+
const comment = propSchema.description ? `\n/** ${escapeJSDocComment(propSchema.description)} */\n` : '';
|
|
212
218
|
props.push(`${comment}${safePropName}${isRequired ? '' : '?'}: ${propType}`);
|
|
213
219
|
}
|
|
214
220
|
}
|
|
@@ -217,7 +223,8 @@ function handleObject(schema, name, context) {
|
|
|
217
223
|
props.push('[key: string]: any');
|
|
218
224
|
}
|
|
219
225
|
else if (schema.additionalProperties && isSchema(schema.additionalProperties)) {
|
|
220
|
-
const
|
|
226
|
+
const additionalTypeName = sanitizeTypeName(`${name}-additional`);
|
|
227
|
+
const additionalType = compileSchema(schema.additionalProperties, additionalTypeName, context);
|
|
221
228
|
props.push(`[key: string]: ${additionalType}`);
|
|
222
229
|
}
|
|
223
230
|
// Handle pattern properties
|
|
@@ -225,7 +232,7 @@ function handleObject(schema, name, context) {
|
|
|
225
232
|
// For simplicity, treat pattern properties as string index signature
|
|
226
233
|
const patternTypes = Object.values(schema.patternProperties)
|
|
227
234
|
.filter(isSchema)
|
|
228
|
-
.map((s, i) => compileSchema(s, `${name}
|
|
235
|
+
.map((s, i) => compileSchema(s, sanitizeTypeName(`${name}-pattern-${i}`), context));
|
|
229
236
|
if (patternTypes.length > 0) {
|
|
230
237
|
props.push(`[key: string]: ${patternTypes.join(' | ')}`);
|
|
231
238
|
}
|
|
@@ -239,10 +246,16 @@ function refToTypeName(ref) {
|
|
|
239
246
|
// Convert kebab-case to PascalCase
|
|
240
247
|
return upperFirst(camelCase(name));
|
|
241
248
|
}
|
|
242
|
-
function capitalize(str) {
|
|
243
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
244
|
-
}
|
|
245
249
|
function wrapUnionType(type) {
|
|
246
250
|
// Wrap union types in parentheses for array types
|
|
247
251
|
return type.includes('|') ? `(${type})` : type;
|
|
248
252
|
}
|
|
253
|
+
function sanitizeTypeName(name) {
|
|
254
|
+
return upperFirst(camelCase(name));
|
|
255
|
+
}
|
|
256
|
+
// Utility function to escape JSDoc comment terminators in descriptions
|
|
257
|
+
function escapeJSDocComment(description) {
|
|
258
|
+
if (!description)
|
|
259
|
+
return '';
|
|
260
|
+
return description.replace(/\*\//g, '*\\/');
|
|
261
|
+
}
|