@@ -53,6 +53,43 @@ function mapTypeOverrideToFieldBuilder(
5353 }
5454}
5555
56+ /**
57+ * Applies import aliases to a field builder expression
58+ * e.g., "textField()" with alias "textField" -> "tf" becomes "tf()"
59+ */
60+ function applyAliasToFieldBuilder (
61+ fieldBuilder : string ,
62+ importAliases : Map < string , string > | undefined ,
63+ ) : string {
64+ if ( ! importAliases || importAliases . size === 0 ) {
65+ return fieldBuilder ;
66+ }
67+
68+ // Map of field builder function names to their import names
69+ const fieldBuilderMap = new Map ( [
70+ [ "textField" , "textField" ] ,
71+ [ "numberField" , "numberField" ] ,
72+ [ "dateField" , "dateField" ] ,
73+ [ "timestampField" , "timestampField" ] ,
74+ [ "containerField" , "containerField" ] ,
75+ ] ) ;
76+
77+ // Try to find and replace each field builder with its alias
78+ let result = fieldBuilder ;
79+ for ( const [ baseName , importName ] of fieldBuilderMap ) {
80+ const alias = importAliases . get ( baseName ) ;
81+ if ( alias ) {
82+ // Replace "baseName(" with "alias("
83+ result = result . replace (
84+ new RegExp ( `\\b${ baseName } \\(` , "g" ) ,
85+ `${ alias } (` ,
86+ ) ;
87+ }
88+ }
89+
90+ return result ;
91+ }
92+
5693/**
5794 * Maps OData types to field builder functions from @proofkit/fmodata
5895 */
@@ -121,6 +158,7 @@ function generateTableOccurrence(
121158 tableOverride ?: NonNullable < FmodataConfig [ "tables" ] > [ number ] ,
122159 existingFields ?: ParsedTableOccurrence ,
123160 alwaysOverrideFieldNames ?: boolean ,
161+ importAliases ?: Map < string , string > , // Map base name -> alias (e.g., "textField" -> "tf")
124162) : GeneratedTO {
125163 const fmtId = entityType [ "@TableID" ] ;
126164 const keyFields = entityType . $Key || [ ] ;
@@ -250,7 +288,7 @@ function generateTableOccurrence(
250288 }
251289
252290 // Apply typeOverride if provided, otherwise use inferred type
253- const fieldBuilder = mapODataTypeToFieldBuilder (
291+ let fieldBuilder = mapODataTypeToFieldBuilder (
254292 metadata . $Type ,
255293 fieldOverride ?. typeOverride as
256294 | "text"
@@ -263,6 +301,9 @@ function generateTableOccurrence(
263301 | undefined ,
264302 ) ;
265303
304+ // Apply import aliases if present
305+ fieldBuilder = applyAliasToFieldBuilder ( fieldBuilder , importAliases ) ;
306+
266307 // Track which field builders are used
267308 if ( fieldBuilder . includes ( "textField()" ) ) {
268309 usedFieldBuilders . add ( "textField" ) ;
@@ -433,6 +474,7 @@ interface ParsedTableOccurrence {
433474 fields : Map < string , ParsedField > ; // keyed by field name
434475 fieldsByEntityId : Map < string , ParsedField > ; // keyed by entity ID
435476 existingImports : string [ ] ; // All existing import statements as strings
477+ importAliases : Map < string , string > ; // Map base name -> alias (e.g., "textField" -> "tf")
436478}
437479
438480/**
@@ -725,14 +767,26 @@ function parseExistingTableFile(
725767 }
726768 }
727769
728- // Extract existing imports
770+ // Extract existing imports and build alias map
729771 const existingImports : string [ ] = [ ] ;
772+ const importAliases = new Map < string , string > ( ) ; // base name -> alias
730773 const importDeclarations = sourceFile . getImportDeclarations ( ) ;
731774 for ( const importDecl of importDeclarations ) {
732775 const importText = importDecl . getFullText ( ) ;
733776 if ( importText . trim ( ) ) {
734777 existingImports . push ( importText . trim ( ) ) ;
735778 }
779+
780+ // Extract aliases from named imports
781+ const namedImports = importDecl . getNamedImports ( ) ;
782+ for ( const namedImport of namedImports ) {
783+ const name = namedImport . getName ( ) ; // The original name
784+ const aliasNode = namedImport . getAliasNode ( ) ;
785+ if ( aliasNode ) {
786+ const alias = aliasNode . getText ( ) ; // The alias
787+ importAliases . set ( name , alias ) ;
788+ }
789+ }
736790 }
737791
738792 // Parse each field
@@ -785,6 +839,7 @@ function parseExistingTableFile(
785839 fields,
786840 fieldsByEntityId,
787841 existingImports,
842+ importAliases,
788843 } ;
789844}
790845
@@ -937,6 +992,7 @@ export async function generateODataTypes(
937992 tableOverride ,
938993 undefined ,
939994 tableAlwaysOverrideFieldNames ,
995+ undefined ,
940996 ) ;
941997 generatedTOs . push ( {
942998 ...generated ,
@@ -995,6 +1051,7 @@ export async function generateODataTypes(
9951051 generated . tableOverride ,
9961052 existingFields ,
9971053 tableAlwaysOverrideFieldNames ,
1054+ existingFields . importAliases ,
9981055 )
9991056 : generated ;
10001057
@@ -1194,8 +1251,15 @@ export async function generateODataTypes(
11941251 } ) ;
11951252 }
11961253
1197- // Add missing required imports (without aliases)
1198- importSpecs . push ( ...missingImports ) ;
1254+ // Add missing required imports (apply aliases if they exist)
1255+ for ( const missingImport of missingImports ) {
1256+ const alias = existingFields . importAliases . get ( missingImport ) ;
1257+ if ( alias ) {
1258+ importSpecs . push ( `${ missingImport } as ${ alias } ` ) ;
1259+ } else {
1260+ importSpecs . push ( missingImport ) ;
1261+ }
1262+ }
11991263
12001264 // Sort imports (but preserve aliases)
12011265 importSpecs . sort ( ) ;
@@ -1259,7 +1323,17 @@ export async function generateODataTypes(
12591323 // Add any required imports that don't exist yet
12601324 for ( const [ module , namedImports ] of requiredImportsByModule . entries ( ) ) {
12611325 if ( module && ! handledModules . has ( module ) ) {
1262- const importsList = Array . from ( namedImports ) . sort ( ) . join ( ", " ) ;
1326+ // Apply aliases to new imports if they exist
1327+ const importSpecs : string [ ] = [ ] ;
1328+ for ( const importName of Array . from ( namedImports ) . sort ( ) ) {
1329+ const alias = existingFields . importAliases . get ( importName ) ;
1330+ if ( alias ) {
1331+ importSpecs . push ( `${ importName } as ${ alias } ` ) ;
1332+ } else {
1333+ importSpecs . push ( importName ) ;
1334+ }
1335+ }
1336+ const importsList = importSpecs . join ( ", " ) ;
12631337 if ( importsList ) {
12641338 finalImportLines . push (
12651339 `import { ${ importsList } } from "${ module } ";` ,
0 commit comments