Skip to content

Commit a91de7e

Browse files
dmealingclaude
andcommitted
refactor(codegen-ts-react): extract enumValues helper (dedupe @values retrieval)
Code-simplifier finding: the @values retrieval was duplicated verbatim in the dropdown and radio branches of fieldControlFor. Extract a module-scope enumValues(field) helper. Output-neutral (value expression, not template content) — generated form output byte-identical; codegen-ts-react 19/19 green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NGQ7oSuNcjhsMHWwZzhBwr
1 parent 817e535 commit a91de7e

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

  • server/typescript/packages/codegen-ts-react/src/templates

server/typescript/packages/codegen-ts-react/src/templates/form-file.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ function viewKindFor(field: MetaField): string | null {
277277
return null;
278278
}
279279

280+
/** The field's declared @values (enum member symbols), or [] when absent. */
281+
function enumValues(field: MetaField): string[] {
282+
return (field.attr(FIELD_ATTR_VALUES) as string[] | undefined) ?? [];
283+
}
284+
280285
export function renderFormFile(entity: MetaObject, ctx: RenderContext): string {
281286
const entityName = entity.name;
282287
// Import the entity's own file. Same target → relative "./Entity"; cross
@@ -342,7 +347,7 @@ ${control}
342347
// Enum member symbols are validated to /^[A-Za-z_][A-Za-z0-9_]*$/, so raw
343348
// interpolation into JSX attribute/text positions is safe (no escaping).
344349
if (kind === VIEW_SUBTYPE_DROPDOWN) {
345-
const values = (field.attr(FIELD_ATTR_VALUES) as string[] | undefined) ?? [];
350+
const values = enumValues(field);
346351
const required = field.attr(FIELD_ATTR_REQUIRED) === true;
347352
const empty = required ? "" : ` <option value="">Select…</option>\n`;
348353
const options = values.map((v) => ` <option value="${v}">${v}</option>`).join("\n");
@@ -365,7 +370,7 @@ ${control}
365370
);
366371
}
367372
if (kind === VIEW_SUBTYPE_RADIO) {
368-
const values = (field.attr(FIELD_ATTR_VALUES) as string[] | undefined) ?? [];
373+
const values = enumValues(field);
369374
const radios = values
370375
.map(
371376
(v) =>

0 commit comments

Comments
 (0)