fix: all tables generate accessors

This commit is contained in:
hypercross 2026-04-15 14:52:41 +08:00
parent 852a108c53
commit 1f3a812728
2 changed files with 26 additions and 50 deletions

View File

@ -745,7 +745,7 @@ describe('parseCsv - resolveReferences: false', () => {
});
describe('csvToModule - accessor-based output', () => {
it('should emit static JSON for tables without references', () => {
it('should emit accessor function for tables without references', () => {
const csv = [
'name,age',
'string,number',
@ -754,9 +754,9 @@ describe('csvToModule - accessor-based output', () => {
const result = csvToModule(csv, { emitTypes: false });
expect(result.js).toContain('export default');
expect(result.js).toContain('export default function getData()');
expect(result.js).not.toContain('import ');
expect(result.js).not.toContain('getData');
expect(result.js).not.toContain('Lookup');
});
it('should emit accessor function for tables with references', () => {
@ -817,7 +817,7 @@ describe('csvToModule - accessor-based output', () => {
expect(result.dts).not.toContain('declare const data');
});
it('should generate const type in dts for tables without references', () => {
it('should generate function type in dts for tables without references', () => {
const csv = [
'name,age',
'string,number',
@ -826,9 +826,9 @@ describe('csvToModule - accessor-based output', () => {
const result = csvToModule(csv, { emitTypes: true, resourceName: 'people' });
expect(result.dts).toContain('declare const data: peopleTable');
expect(result.dts).toContain('export default data');
expect(result.dts).not.toContain('declare function getData');
expect(result.dts).toContain('declare function getData(): peopleTable');
expect(result.dts).toContain('export default getData');
expect(result.dts).not.toContain('declare const data');
});
it('should handle nested references in tuples', () => {

View File

@ -534,8 +534,7 @@ function generateTypeDefinition(
resourceName: string,
propertyConfigs: PropertyConfig[],
references: Set<string>,
currentFilePath?: string,
hasRefs?: boolean
currentFilePath?: string
): string {
const typeName = resourceName ? `${resourceName}Table` : 'Table';
const currentTableName = currentFilePath
@ -580,7 +579,6 @@ function generateTypeDefinition(
exportAlias = `\nexport type ${singularType} = ${typeName}[number];`;
}
if (hasRefs) {
return `${importSection}type ${typeName} = readonly {
${properties}
}[];
@ -591,16 +589,6 @@ export default getData;
`;
}
return `${importSection}type ${typeName} = readonly {
${properties}
}[];
${exportAlias}
declare const data: ${typeName};
export default data;
`;
}
/**
* Parse CSV content string into structured data with schema validation.
* This is a standalone function that doesn't depend on webpack/rspack LoaderContext.
@ -756,8 +744,7 @@ export function parseCsv(
options.resourceName || '',
propertyConfigs,
references,
options.currentFilePath,
referenceFields.length > 0
options.currentFilePath
);
}
@ -832,14 +819,6 @@ export function csvToModule(
const hasRefs = result.referenceFields.length > 0;
const defaultPrimaryKey = options.defaultPrimaryKey ?? 'id';
if (!hasRefs) {
const json = JSON.stringify(result.data, null, 2);
return {
js: `export default ${json};`,
dts: result.typeDefinition,
};
}
const imports: string[] = [];
const lookupInits: string[] = [];
const lookupVarMap = new Map<string, string>();
@ -883,9 +862,7 @@ export function csvToModule(
const rawJson = JSON.stringify(result.data, null, 2);
let js: string;
if (rowResolvers.length > 0) {
js = [
const js = [
...imports,
'',
`const _raw = ${rawJson};`,
@ -896,16 +873,15 @@ export function csvToModule(
' if (_resolved) return _resolved;',
' _resolved = _raw;',
...lookupInits.map(l => ` ${l}`),
...rowResolvers.length > 0 ? [
' _resolved = _raw.map(row => ({',
' ...row,',
...rowResolvers,
' }));',
] : [],
' return _resolved;',
'}',
].join('\n');
} else {
js = `export default ${rawJson};`;
}
return {
js,