fix: fix type generator for tuples
This commit is contained in:
parent
c8f0d6b0de
commit
740bb503b1
|
|
@ -493,7 +493,7 @@ class ReferenceValueParser {
|
||||||
/**
|
/**
|
||||||
* Convert a schema to TypeScript type string
|
* Convert a schema to TypeScript type string
|
||||||
*/
|
*/
|
||||||
function schemaToTypeString(schema: Schema, resourceNames?: Map<string, string>): string {
|
export function schemaToTypeString(schema: Schema, resourceNames?: Map<string, string>): string {
|
||||||
switch (schema.type) {
|
switch (schema.type) {
|
||||||
case 'string':
|
case 'string':
|
||||||
return 'string';
|
return 'string';
|
||||||
|
|
@ -519,7 +519,7 @@ function schemaToTypeString(schema: Schema, resourceNames?: Map<string, string>)
|
||||||
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
||||||
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
||||||
});
|
});
|
||||||
return `[${tupleElements.join(', ')}]`;
|
return `[${tupleElements.join(', ')}][]`;
|
||||||
}
|
}
|
||||||
// Wrap union types in parentheses to maintain correct precedence
|
// Wrap union types in parentheses to maintain correct precedence
|
||||||
const elementType = schemaToTypeString(schema.element, resourceNames);
|
const elementType = schemaToTypeString(schema.element, resourceNames);
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,5 @@ export function defineSchema(schemaString: string): ParsedSchema {
|
||||||
}
|
}
|
||||||
|
|
||||||
export { parseSchema, parseValue, createValidator, ParseError };
|
export { parseSchema, parseValue, createValidator, ParseError };
|
||||||
|
export { schemaToTypeString } from './csv-loader/loader.js';
|
||||||
export type { Schema, PrimitiveSchema, TupleSchema, ArraySchema, ReferenceSchema, StringLiteralSchema, UnionSchema, ParsedSchema };
|
export type { Schema, PrimitiveSchema, TupleSchema, ArraySchema, ReferenceSchema, StringLiteralSchema, UnionSchema, ParsedSchema };
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { defineSchema, parseSchema } from './index';
|
import { defineSchema, parseSchema, schemaToTypeString } from './index';
|
||||||
import type { Schema, StringLiteralSchema, UnionSchema } from './types';
|
import type { Schema, StringLiteralSchema, UnionSchema } from './types';
|
||||||
|
|
||||||
describe('Type generation for string literals and unions', () => {
|
describe('Type generation for string literals and unions', () => {
|
||||||
|
|
@ -136,64 +136,39 @@ describe('Type generation for string literals and unions', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Type string generation', () => {
|
describe('Type string generation', () => {
|
||||||
// Helper function to test type generation
|
|
||||||
function generateType(schema: Schema): string {
|
|
||||||
function toType(s: Schema): string {
|
|
||||||
switch (s.type) {
|
|
||||||
case 'string': return 'string';
|
|
||||||
case 'number':
|
|
||||||
case 'int':
|
|
||||||
case 'float': return 'number';
|
|
||||||
case 'boolean': return 'boolean';
|
|
||||||
case 'stringLiteral': return `"${s.value}"`;
|
|
||||||
case 'union': return s.members.map(m => toType(m)).join(' | ');
|
|
||||||
case 'array':
|
|
||||||
const elemType = toType(s.element);
|
|
||||||
if (s.element.type === 'union') {
|
|
||||||
return `(${elemType})[]`;
|
|
||||||
}
|
|
||||||
return `${elemType}[]`;
|
|
||||||
case 'tuple':
|
|
||||||
const elements = s.elements.map(el => {
|
|
||||||
const typeStr = toType(el.schema);
|
|
||||||
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
|
||||||
});
|
|
||||||
return `[${elements.join(', ')}]`;
|
|
||||||
default: return 'unknown';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return toType(schema);
|
|
||||||
}
|
|
||||||
|
|
||||||
it('should generate "on" for string literal', () => {
|
it('should generate "on" for string literal', () => {
|
||||||
const schema = parseSchema('"on"');
|
const schema = parseSchema('"on"');
|
||||||
expect(generateType(schema)).toBe('"on"');
|
expect(schemaToTypeString(schema)).toBe('"on"');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should generate "on" | "off" for union of string literals', () => {
|
it('should generate "on" | "off" for union of string literals', () => {
|
||||||
const schema = parseSchema('"on" | "off"');
|
const schema = parseSchema('"on" | "off"');
|
||||||
expect(generateType(schema)).toBe('"on" | "off"');
|
expect(schemaToTypeString(schema)).toBe('"on" | "off"');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should generate correct type for array of string literals', () => {
|
it('should generate correct type for array of string literals', () => {
|
||||||
const schema = parseSchema('"item"[]');
|
const schema = parseSchema('"item"[]');
|
||||||
expect(generateType(schema)).toBe('"item"[]');
|
expect(schemaToTypeString(schema)).toBe('"item"[]');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should generate correct type for array of unions', () => {
|
it('should generate correct type for array of unions', () => {
|
||||||
const schema = parseSchema('("pending" | "approved" | "rejected")[]');
|
const schema = parseSchema('("pending" | "approved" | "rejected")[]');
|
||||||
// Union types in arrays need parentheses for correct precedence
|
expect(schemaToTypeString(schema)).toBe('("pending" | "approved" | "rejected")[]');
|
||||||
expect(generateType(schema)).toBe('("pending" | "approved" | "rejected")[]');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should generate correct type for tuple with union field', () => {
|
it('should generate correct type for tuple with union field', () => {
|
||||||
const schema = parseSchema('[name: string; status: "active" | "inactive"]');
|
const schema = parseSchema('[name: string; status: "active" | "inactive"]');
|
||||||
expect(generateType(schema)).toBe('[name: string, status: "active" | "inactive"]');
|
expect(schemaToTypeString(schema)).toBe('[name: string, status: "active" | "inactive"]');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should generate correct type for complex union', () => {
|
it('should generate correct type for complex union', () => {
|
||||||
const schema = parseSchema('string | number | "special"');
|
const schema = parseSchema('string | number | "special"');
|
||||||
expect(generateType(schema)).toBe('string | number | "special"');
|
expect(schemaToTypeString(schema)).toBe('string | number | "special"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate correct type for nested tuple with references', () => {
|
||||||
|
const schema = parseSchema('[data: @enemy; hp: int; ([effect: @effect;stacks: int])[]][]');
|
||||||
|
expect(schemaToTypeString(schema)).toBe('[data: Enemy, hp: number, [effect: Effect, stacks: number][]][]');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue