Compare commits

...

2 Commits

Author SHA1 Message Date
hypercross 89be2783d1 fix(csv-loader): use commas in generated type declarations
Update `type-gen.ts` to replace semicolons with commas in the
generated TypeScript type definitions to ensure valid syntax.
2026-04-22 19:23:49 +08:00
hypercross eef3dbfac8 feat(csv-loader): resolve @table references in type declarations
Update type generation to transform @table references into capitalized
type names when using custom schema strings. This ensures that
generated TypeScript types correctly reference other CSV modules.
2026-04-22 18:15:55 +08:00
2 changed files with 44 additions and 6 deletions

View File

@ -247,7 +247,7 @@ describe("parseCsv - type declarations", () => {
expect(result.typeDefinition).toContain("export type IntentEffects ="); expect(result.typeDefinition).toContain("export type IntentEffects =");
// IntentEffect should reference IntentEffectTarget, not expand it // IntentEffect should reference IntentEffectTarget, not expand it
expect(result.typeDefinition).toContain( expect(result.typeDefinition).toContain(
"[IntentEffectTarget; string; int]", "[IntentEffectTarget, string, int]",
); );
// IntentEffects should reference IntentEffect, not expand it // IntentEffects should reference IntentEffect, not expand it
expect(result.typeDefinition).toContain("IntentEffect[]"); expect(result.typeDefinition).toContain("IntentEffect[]");
@ -281,9 +281,32 @@ describe("parseCsv - type declarations", () => {
}); });
expect(typeResult.typeDefinition).toContain("export type Type ="); expect(typeResult.typeDefinition).toContain("export type Type =");
expect(typeResult.typeDefinition).toContain("[Type; int][]"); expect(typeResult.typeDefinition).toContain("[Type, int][]");
expect(typeResult.typeDefinition).toContain( expect(typeResult.typeDefinition).toContain(
"readonly items: [Type; int][];", "readonly items: [Type, int][];",
);
});
it("should resolve @table references inside declared types to capitalized type names", () => {
const csv = [
"# type Entry = [@user]",
"id,entry",
"string,Entry",
"1,1",
].join("\n");
const result = parseCsv(csv, {
emitTypes: true,
resourceName: "entries",
currentFilePath: path.join(fixturesDir, "entries.csv"),
resolveReferences: false,
});
expect(result.typeDefinition).toContain("export type Entry = [User]");
expect(result.typeDefinition).toContain("readonly entry: Entry;");
// It should also import the User type
expect(result.typeDefinition).toContain(
"import type { User } from './user.csv'",
); );
}); });
}); });

View File

@ -2,6 +2,20 @@ import * as path from "path";
import { schemaToTypeString } from "../index.js"; import { schemaToTypeString } from "../index.js";
import type { PropertyConfig, TypeDeclaration } from "./types.js"; import type { PropertyConfig, TypeDeclaration } from "./types.js";
function resolveReferencesInSchemaString(
schemaString: string,
resourceNames: Map<string, string>,
): string {
return schemaString
.replace(/@([a-zA-Z0-9\-_]+)(\[\])?/g, (_match, tableName, arraySuffix) => {
const typeName =
resourceNames.get(tableName) ||
tableName.charAt(0).toUpperCase() + tableName.slice(1);
return arraySuffix ? `${typeName}[]` : typeName;
})
.replace(/; ?/g, ", ");
}
/** /**
* Generate TypeScript interface for the CSV data * Generate TypeScript interface for the CSV data
*/ */
@ -51,7 +65,7 @@ export function generateTypeDefinition(
? typeDeclarations ? typeDeclarations
.map( .map(
(decl) => (decl) =>
`export type ${decl.name} = ${decl.schemaString ?? schemaToTypeString(decl.schema, resourceNames)};`, `export type ${decl.name} = ${decl.schemaString ? resolveReferencesInSchemaString(decl.schemaString, resourceNames) : schemaToTypeString(decl.schema, resourceNames)};`,
) )
.join("\n") + "\n\n" .join("\n") + "\n\n"
: ""; : "";
@ -60,8 +74,9 @@ export function generateTypeDefinition(
.map((config) => { .map((config) => {
const typeStr = config.declaredTypeName const typeStr = config.declaredTypeName
? config.declaredTypeName ? config.declaredTypeName
: (config.schemaString ?? : config.schemaString
schemaToTypeString(config.schema, resourceNames)); ? resolveReferencesInSchemaString(config.schemaString, resourceNames)
: schemaToTypeString(config.schema, resourceNames);
return ` readonly ${config.name}: ${typeStr};`; return ` readonly ${config.name}: ${typeStr};`;
}) })
.join("\n"); .join("\n");