Skip to content

Commit 3b1fceb

Browse files
committed
ref: drop isWithMetaField; inline isResourceIdentifier(base)
In FHIR only resources carry `meta`, so the hierarchy walk in isWithMetaField is equivalent to `isResourceIdentifier(profile.base)`. Inlining the check removes a redundant index helper and simplifies a couple of writer conditions. - Remove isWithMetaField from TypeSchemaIndex (interface, implementation, return-object key). - Inline isResourceIdentifier(flatProfile.base) at both call sites. - Simplify canEmitIs from `(hasMeta && isResourceIdentifier(base)) || base.name === "Extension"` to `hasMeta || base.name === "Extension"`, and the nested branch from `hasMeta && isResourceIdentifier(base)` to just `hasMeta`. - Drop now-unused tsIndex params from generateProfileHelpersImport and generateFactoryMethods. - Remove stale entry from docs/guides/typeschema-index.md.
1 parent de66f19 commit 3b1fceb

3 files changed

Lines changed: 7 additions & 27 deletions

File tree

docs/guides/typeschema-index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ Work with FHIR profiles and their constraints:
115115
```typescript
116116
flatProfile(schema: ProfileTypeSchema): ProfileTypeSchema
117117
Flattens a profile by resolving all differential constraints into a complete snapshot
118-
119-
isWithMetaField(profile: ProfileTypeSchema): boolean
120-
Checks if a profile includes the meta field
121118
```
122119

123120
---
@@ -172,7 +169,6 @@ const specializedId = tsIndex.findLastSpecializationByIdentifier(patientIdentifi
172169

173170
```typescript
174171
const flatProfile = tsIndex.flatProfile(useCorePatientProfile);
175-
const hasMeta = tsIndex.isWithMetaField(profile);
176172
```
177173

178174
### Debug Utilities

src/api/writer-generator/typescript/profile.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,12 @@ export const generateProfileIndexFile = (
226226

227227
const generateProfileHelpersImport = (
228228
w: TypeScript,
229-
tsIndex: TypeSchemaIndex,
230229
flatProfile: ProfileTypeSchema,
231230
sliceDefs: SliceDef[],
232231
factoryInfo: ProfileFactoryInfo,
233232
) => {
234233
const extensions = flatProfile.extensions ?? [];
235-
const hasMeta = tsIndex.isWithMetaField(flatProfile);
234+
const hasMeta = isResourceIdentifier(flatProfile.base);
236235
const canonicalUrl = flatProfile.identifier.url;
237236

238237
const imports: string[] = [];
@@ -356,15 +355,10 @@ const generateStaticSliceFields = (w: TypeScript, sliceDefs: SliceDef[]) => {
356355
if (sliceDefs.length > 0) w.line();
357356
};
358357

359-
const generateFactoryMethods = (
360-
w: TypeScript,
361-
tsIndex: TypeSchemaIndex,
362-
flatProfile: ProfileTypeSchema,
363-
factoryInfo: ProfileFactoryInfo,
364-
) => {
358+
const generateFactoryMethods = (w: TypeScript, flatProfile: ProfileTypeSchema, factoryInfo: ProfileFactoryInfo) => {
365359
const profileClassName = tsProfileClassName(flatProfile);
366360
const tsBaseResourceName = tsTypeFromIdentifier(flatProfile.base);
367-
const hasMeta = tsIndex.isWithMetaField(flatProfile);
361+
const hasMeta = isResourceIdentifier(flatProfile.base);
368362
const hasParams = factoryInfo.params.length > 0 || factoryInfo.sliceAutoFields.length > 0;
369363
const createArgsTypeName = `${profileClassName}Raw`;
370364
const paramSignature = hasParams ? `args: ${createArgsTypeName}` : "";
@@ -391,11 +385,11 @@ const generateFactoryMethods = (
391385
w.lineSM("return profile");
392386
});
393387
w.line();
394-
const canEmitIs = (hasMeta && isResourceIdentifier(flatProfile.base)) || flatProfile.base.name === "Extension";
388+
const canEmitIs = hasMeta || flatProfile.base.name === "Extension";
395389
if (canEmitIs) {
396390
w.curlyBlock(["static", "is", "(resource: unknown)", `: resource is ${tsBaseResourceName}`], () => {
397391
w.line(`if (typeof resource !== "object" || resource === null) return false;`);
398-
if (hasMeta && isResourceIdentifier(flatProfile.base)) {
392+
if (hasMeta) {
399393
w.line(`const r = resource as { resourceType?: string; meta?: { profile?: string[] } };`);
400394
w.line(`if (r.resourceType !== ${JSON.stringify(flatProfile.base.name)}) return false;`);
401395
w.lineSM(`return (r.meta?.profile ?? []).includes(${profileClassName}.canonicalUrl)`);
@@ -754,7 +748,7 @@ export const generateProfileClass = (w: TypeScript, tsIndex: TypeSchemaIndex, fl
754748
generateInlineExtensionInputTypes(w, tsIndex, flatProfile);
755749
generateSliceInputTypes(w, flatProfile, sliceDefs);
756750

757-
generateProfileHelpersImport(w, tsIndex, flatProfile, sliceDefs, factoryInfo);
751+
generateProfileHelpersImport(w, flatProfile, sliceDefs, factoryInfo);
758752

759753
generateRawType(w, flatProfile, factoryInfo);
760754
generateFlatInputType(w, flatProfile);
@@ -768,7 +762,7 @@ export const generateProfileClass = (w: TypeScript, tsIndex: TypeSchemaIndex, fl
768762
generateStaticSliceFields(w, sliceDefs);
769763
w.lineSM(`private resource: ${tsBaseResourceName}`);
770764
w.line();
771-
generateFactoryMethods(w, tsIndex, flatProfile, factoryInfo);
765+
generateFactoryMethods(w, flatProfile, factoryInfo);
772766
generateFieldAccessors(w, factoryInfo);
773767

774768
w.line("// Extensions");

src/typeschema/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ export type TypeSchemaIndex = {
314314
baseTypeId: TypeIdentifier,
315315
sliceElements: string[],
316316
) => ConstrainedChoiceInfo | undefined;
317-
isWithMetaField: (profile: ProfileTypeSchema) => boolean;
318317
entityTree: () => EntityTree;
319318
exportTree: (filename: string) => Promise<void>;
320319
irReport: () => IrReport;
@@ -573,14 +572,6 @@ export const mkTypeSchemaIndex = (
573572
return undefined;
574573
};
575574

576-
const isWithMetaField = (profile: ProfileTypeSchema): boolean => {
577-
const genealogy = tryHierarchy(profile);
578-
if (!genealogy) return false;
579-
return genealogy.filter(isSpecializationTypeSchema).some((schema) => {
580-
return schema.fields?.meta !== undefined;
581-
});
582-
};
583-
584575
const entityTree = () => {
585576
const tree: EntityTree = {};
586577
for (const [pkgId, shemas] of Object.entries(groupByPackages(schemas))) {
@@ -626,7 +617,6 @@ export const mkTypeSchemaIndex = (
626617
findLastSpecializationByIdentifier,
627618
flatProfile,
628619
constrainedChoice,
629-
isWithMetaField,
630620
entityTree,
631621
exportTree,
632622
irReport: () => irReport,

0 commit comments

Comments
 (0)