@@ -14,8 +14,14 @@ import type { CreateFileOptions } from '@sap-cloud-sdk/generator-common/internal
1414 * @internal
1515 */
1616export const potentialExternalImportDeclarations = [
17- [ 'moment' , 'Moment' , 'Duration' ] ,
18- [ 'bignumber.js' , 'BigNumber' ]
17+ {
18+ moduleSpecifier : 'moment' ,
19+ namedImports : [ 'Moment' , 'Duration' ]
20+ } ,
21+ {
22+ moduleSpecifier : 'bignumber.js' ,
23+ defaultImport : 'BigNumber'
24+ }
1925] ;
2026
2127/**
@@ -25,32 +31,35 @@ export function externalImportDeclarationsTsMorph(
2531 properties : VdmMappedEdmType [ ]
2632) : ImportDeclarationStructure [ ] {
2733 return potentialExternalImportDeclarations
28- . map ( ( [ moduleSpecifier , ...namedImports ] ) =>
29- externalImportDeclarationTsMorph (
30- properties ,
31- moduleSpecifier ,
32- namedImports
33- )
34+ . map ( importDeclaration =>
35+ externalImportDeclarationTsMorph ( properties , importDeclaration )
3436 )
3537 . filter (
36- declaration => declaration . namedImports && declaration . namedImports . length
38+ declaration =>
39+ ( declaration . namedImports && declaration . namedImports . length ) ||
40+ declaration . defaultImport
3741 ) ;
3842}
3943
40- /**
41- * @internal
42- */
43- export function externalImportDeclarationTsMorph (
44+ function externalImportDeclarationTsMorph (
4445 properties : VdmMappedEdmType [ ] ,
45- moduleSpecifier : string ,
46- namedImports : string [ ]
46+ {
47+ moduleSpecifier,
48+ namedImports = [ ] ,
49+ defaultImport
50+ } : ( typeof potentialExternalImportDeclarations ) [ number ]
4751) : ImportDeclarationStructure {
4852 return {
4953 kind : StructureKind . ImportDeclaration ,
5054 moduleSpecifier,
5155 namedImports : namedImports . filter ( namedImport =>
5256 properties . map ( prop => prop . jsType ) . includes ( namedImport )
53- )
57+ ) ,
58+ defaultImport :
59+ defaultImport &&
60+ properties
61+ . map ( prop => prop . jsType )
62+ . find ( jsType => jsType === defaultImport )
5463 } ;
5564}
5665
@@ -132,7 +141,7 @@ export function enumTypeImportDeclarations(
132141 ) ;
133142}
134143
135- // Only supports named imports
144+ // Does not support writer functions or strings as named imports
136145/**
137146 * @internal
138147 */
@@ -142,47 +151,41 @@ export function mergeImportDeclarations(
142151 return importDeclarations
143152 . reduce (
144153 ( mergedDeclarations : ImportDeclarationStructure [ ] , importDeclaration ) => {
145- const sameModuleSpecifier = mergedDeclarations . find (
154+ const mergedDeclaration = mergedDeclarations . find (
146155 declaration =>
147156 declaration . moduleSpecifier === importDeclaration . moduleSpecifier &&
148157 declaration . isTypeOnly === importDeclaration . isTypeOnly
149158 ) ;
150- if ( sameModuleSpecifier ) {
151- if ( ! sameModuleSpecifier . namedImports ) {
152- sameModuleSpecifier . namedImports = [
153- ...( importDeclaration . namedImports as string [ ] )
154- ] ;
155- } else if ( sameModuleSpecifier . namedImports instanceof Array ) {
156- sameModuleSpecifier . namedImports = [
157- ...sameModuleSpecifier . namedImports ,
158- ...( importDeclaration . namedImports as string [ ] )
159- ] ;
160- } else {
161- sameModuleSpecifier . namedImports = [
162- sameModuleSpecifier . namedImports ,
163- ...( importDeclaration . namedImports as string [ ] )
164- ] ;
159+ if ( mergedDeclaration ) {
160+ const mergedNamedImports = mergedDeclaration . namedImports || [ ] ;
161+ const newNamedImports = importDeclaration . namedImports || [ ] ;
162+ if (
163+ ! Array . isArray ( mergedNamedImports ) ||
164+ ! Array . isArray ( newNamedImports )
165+ ) {
166+ throw new Error (
167+ 'mergeImportDeclarations only supports array or undefined named imports. This should never happen.'
168+ ) ;
165169 }
170+ mergedDeclaration . namedImports = unique ( [
171+ ...mergedNamedImports ,
172+ ...newNamedImports
173+ ] ) ;
174+
175+ mergedDeclaration . defaultImport =
176+ mergedDeclaration . defaultImport || importDeclaration . defaultImport ;
166177 } else {
167178 mergedDeclarations . push ( importDeclaration ) ;
168179 }
169180 return mergedDeclarations ;
170181 } ,
171182 [ ]
172183 )
173- . map ( importDeclaration => {
174- if ( ! importDeclaration . namedImports ) {
175- importDeclaration . namedImports = undefined ;
176- } else if ( importDeclaration . namedImports instanceof Array ) {
177- importDeclaration . namedImports = unique ( importDeclaration . namedImports ) ;
178- } else {
179- importDeclaration . namedImports = [ importDeclaration . namedImports ] ;
180- }
181- return importDeclaration ;
182- } )
183184 . filter (
184185 importDeclaration =>
185- importDeclaration . namedImports && importDeclaration . namedImports . length
186+ ( importDeclaration . namedImports &&
187+ importDeclaration . namedImports . length ) ||
188+ importDeclaration . defaultImport
186189 ) ;
187190}
188191
0 commit comments