Compare commits
2 Commits
e0317946d5
...
89be2783d1
| Author | SHA1 | Date |
|---|---|---|
|
|
89be2783d1 | |
|
|
eef3dbfac8 |
|
|
@ -247,7 +247,7 @@ describe("parseCsv - type declarations", () => {
|
|||
expect(result.typeDefinition).toContain("export type IntentEffects =");
|
||||
// IntentEffect should reference IntentEffectTarget, not expand it
|
||||
expect(result.typeDefinition).toContain(
|
||||
"[IntentEffectTarget; string; int]",
|
||||
"[IntentEffectTarget, string, int]",
|
||||
);
|
||||
// IntentEffects should reference IntentEffect, not expand it
|
||||
expect(result.typeDefinition).toContain("IntentEffect[]");
|
||||
|
|
@ -281,9 +281,32 @@ describe("parseCsv - type declarations", () => {
|
|||
});
|
||||
|
||||
expect(typeResult.typeDefinition).toContain("export type Type =");
|
||||
expect(typeResult.typeDefinition).toContain("[Type; int][]");
|
||||
expect(typeResult.typeDefinition).toContain("[Type, int][]");
|
||||
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'",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,6 +2,20 @@ import * as path from "path";
|
|||
import { schemaToTypeString } from "../index.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
|
||||
*/
|
||||
|
|
@ -51,7 +65,7 @@ export function generateTypeDefinition(
|
|||
? typeDeclarations
|
||||
.map(
|
||||
(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"
|
||||
: "";
|
||||
|
|
@ -60,8 +74,9 @@ export function generateTypeDefinition(
|
|||
.map((config) => {
|
||||
const typeStr = config.declaredTypeName
|
||||
? config.declaredTypeName
|
||||
: (config.schemaString ??
|
||||
schemaToTypeString(config.schema, resourceNames));
|
||||
: config.schemaString
|
||||
? resolveReferencesInSchemaString(config.schemaString, resourceNames)
|
||||
: schemaToTypeString(config.schema, resourceNames);
|
||||
return ` readonly ${config.name}: ${typeStr};`;
|
||||
})
|
||||
.join("\n");
|
||||
|
|
|
|||
Loading…
Reference in New Issue