@@ -975,7 +975,7 @@ export class ReflectionTransformer implements CustomTransformer {
975
975
break ;
976
976
}
977
977
978
- if ( this . embedDeclarations . size === 0 && this . external . libraryImports . size === 0 && allCompiled ) break ;
978
+ if ( this . embedDeclarations . size === 0 && this . external . compileExternalLibraryImports . size === 0 && allCompiled ) break ;
979
979
980
980
for ( const [ node , d ] of [ ...this . compileDeclarations . entries ( ) ] ) {
981
981
if ( d . compiled ) continue ;
@@ -993,14 +993,14 @@ export class ReflectionTransformer implements CustomTransformer {
993
993
}
994
994
}
995
995
996
- if ( this . external . libraryImports . size ) {
997
- for ( const imports of this . external . libraryImports . values ( ) ) {
998
- for ( const { declaration } of imports ) {
996
+ if ( this . external . compileExternalLibraryImports . size ) {
997
+ for ( const imports of this . external . compileExternalLibraryImports . values ( ) ) {
998
+ for ( const { declaration } of imports . values ( ) ) {
999
999
this . compiledDeclarations . add ( declaration ) ;
1000
1000
}
1001
1001
}
1002
- const entries = Array . from ( this . external . libraryImports . entries ( ) ) ;
1003
- this . external . libraryImports . clear ( ) ;
1002
+ const entries = Array . from ( this . external . compileExternalLibraryImports . entries ( ) ) ;
1003
+ this . external . compileExternalLibraryImports . clear ( ) ;
1004
1004
for ( const [ library , imports ] of entries ) {
1005
1005
if ( ! this . external . embeddedLibraryVariables . has ( library ) ) {
1006
1006
const objectLiteral = this . f . createObjectLiteralExpression ( ) ;
@@ -1020,8 +1020,8 @@ export class ReflectionTransformer implements CustomTransformer {
1020
1020
this . external . embeddedLibraryVariables . add ( library ) ;
1021
1021
}
1022
1022
1023
- for ( const value of imports ) {
1024
- this . external . setEmbeddingExternalLibraryImport ( value ) ;
1023
+ for ( const value of imports . values ( ) ) {
1024
+ this . external . startEmbeddingExternalLibraryImport ( value ) ;
1025
1025
newTopStatements . push ( this . createProgramVarForExternalLibraryImport ( value . declaration , value . name , value . sourceFile , value . module . packageId . name ) ) ;
1026
1026
this . external . finishEmbeddingExternalLibraryImport ( ) ;
1027
1027
}
@@ -1132,6 +1132,23 @@ export class ReflectionTransformer implements CustomTransformer {
1132
1132
newTopStatements . push ( assignType ) ;
1133
1133
}
1134
1134
1135
+ if ( this . tempResultIdentifier ) {
1136
+ newTopStatements . push (
1137
+ this . f . createVariableStatement (
1138
+ undefined ,
1139
+ this . f . createVariableDeclarationList (
1140
+ [ this . f . createVariableDeclaration (
1141
+ this . tempResultIdentifier ,
1142
+ undefined ,
1143
+ undefined ,
1144
+ undefined
1145
+ ) ] ,
1146
+ ts . NodeFlags . None
1147
+ )
1148
+ )
1149
+ ) ;
1150
+ }
1151
+
1135
1152
if ( newTopStatements . length ) {
1136
1153
// we want to keep "use strict", or "use client", etc at the very top
1137
1154
const indexOfFirstLiteralExpression = this . sourceFile . statements . findIndex ( v => isExpressionStatement ( v ) && isStringLiteral ( v . expression ) ) ;
@@ -1326,6 +1343,12 @@ export class ReflectionTransformer implements CustomTransformer {
1326
1343
const narrowed = node as ClassDeclaration | ClassExpression ;
1327
1344
//class nodes have always their own program, so the start is always fresh, means we don't need a frame
1328
1345
1346
+ if ( this . external . isEmbeddingExternalLibraryImport ( ) ) {
1347
+ const declaration = this . nodeConverter . convertClassToInterface ( narrowed ) ;
1348
+ this . extractPackStructOfType ( declaration , program ) ;
1349
+ break ;
1350
+ }
1351
+
1329
1352
if ( node ) {
1330
1353
const members : ClassElement [ ] = [ ] ;
1331
1354
@@ -1673,6 +1696,10 @@ export class ReflectionTransformer implements CustomTransformer {
1673
1696
const narrowed = node as MethodSignature | MethodDeclaration | CallSignatureDeclaration | ConstructorTypeNode
1674
1697
| ConstructSignatureDeclaration | ConstructorDeclaration | ArrowFunction | FunctionExpression | FunctionTypeNode | FunctionDeclaration ;
1675
1698
1699
+ if ( this . external . isEmbeddingExternalLibraryImport ( ) && ( isFunctionDeclaration ( node ) || isFunctionExpression ( node ) || isMethodDeclaration ( node ) ) ) {
1700
+ console . log ( getNameAsString ( narrowed . name ) ) ;
1701
+ }
1702
+
1676
1703
const config = this . findReflectionConfig ( narrowed , program ) ;
1677
1704
if ( config . mode === 'never' ) return ;
1678
1705
@@ -2043,11 +2070,19 @@ export class ReflectionTransformer implements CustomTransformer {
2043
2070
// return node;
2044
2071
// }
2045
2072
2073
+ protected getRuntimeTypeName ( typeName : EntityName ) : Identifier {
2074
+ return this . f . createIdentifier ( getRuntimeTypeName ( getNameAsString ( typeName ) ) ) ;
2075
+ }
2076
+
2077
+ protected getExternalRuntimeTypeName ( typeName : EntityName ) : Identifier {
2078
+ const { module } = this . external . getEmbeddingExternalLibraryImport ( ) ;
2079
+ return this . f . createIdentifier ( `${ getExternalRuntimeTypeName ( module . packageId . name ) } .${ getNameAsString ( typeName ) } ` ) ;
2080
+ }
2081
+
2046
2082
protected getDeclarationVariableName ( typeName : EntityName ) : Identifier {
2047
- const name = getNameAsString ( typeName ) ;
2048
- return this . external . isEmbeddingExternalLibraryImport ( ) && ! this . external . globalTypeNames . has ( name )
2049
- ? this . f . createIdentifier ( `${ getExternalRuntimeTypeName ( this . external . getEmbeddingExternalLibraryImport ( ) . module . packageId . name ) } .${ name } ` )
2050
- : this . f . createIdentifier ( getRuntimeTypeName ( name ) ) ;
2083
+ return this . external . isEmbeddingExternalLibraryImport ( ) && ! this . external . knownGlobalTypeNames . has ( getNameAsString ( typeName ) )
2084
+ ? this . getExternalRuntimeTypeName ( typeName )
2085
+ : this . getRuntimeTypeName ( typeName ) ;
2051
2086
}
2052
2087
2053
2088
protected resolveImportExpression ( declaration : Node , importDeclaration : ImportDeclaration , typeName : Identifier , expression : Expression , program : CompilerProgram ) : Expression {
@@ -2063,18 +2098,18 @@ export class ReflectionTransformer implements CustomTransformer {
2063
2098
if ( declarationReflection . mode !== 'never' ) {
2064
2099
const declarationSourceFile = importDeclaration . getSourceFile ( ) ;
2065
2100
2066
- const runtimeTypeName = this . getDeclarationVariableName ( typeName ) ;
2101
+ const runtimeTypeName = this . getRuntimeTypeName ( typeName ) ;
2067
2102
2068
2103
const builtType = isBuiltType ( runtimeTypeName , declarationSourceFile ) ;
2069
2104
2070
2105
const isEmbeddingExternalLibraryImport = this . external . isEmbeddingExternalLibraryImport ( ) ;
2071
2106
2072
2107
if ( isEmbeddingExternalLibraryImport || ( ! builtType && this . external . shouldInlineExternalLibraryImport ( importDeclaration , typeName , declarationReflection ) ) ) {
2073
- const { module } = this . external . embedExternalLibraryImport ( typeName , declaration , declarationSourceFile , importDeclaration ) ;
2108
+ const { module } = this . external . processExternalLibraryImport ( typeName , declaration , declarationSourceFile , importDeclaration ) ;
2109
+ const newExpression = this . f . createPropertyAccessExpression ( this . f . createIdentifier ( getExternalRuntimeTypeName ( module . packageId . name ) ) , typeName ) ;
2074
2110
return ! isEmbeddingExternalLibraryImport && ( isClassDeclaration ( declaration ) || isFunctionDeclaration ( declaration ) )
2075
- // TODO: figure out what to do with referenced classes/functions in external declaration files
2076
- ? this . wrapWithAssignType ( expression , this . f . createPropertyAccessExpression ( this . f . createIdentifier ( getExternalRuntimeTypeName ( module . packageId . name ) ) , typeName ) )
2077
- : this . f . createPropertyAccessExpression ( this . f . createIdentifier ( getExternalRuntimeTypeName ( module . packageId . name ) ) , typeName ) ;
2111
+ ? this . wrapWithAssignType ( expression , newExpression )
2112
+ : newExpression
2078
2113
}
2079
2114
}
2080
2115
@@ -2180,6 +2215,7 @@ export class ReflectionTransformer implements CustomTransformer {
2180
2215
if ( isModuleDeclaration ( declaration ) && resolved . importDeclaration ) {
2181
2216
let expression : Expression = serializeEntityNameAsExpression ( this . f , typeName ) ;
2182
2217
if ( isIdentifier ( typeName ) ) {
2218
+ console . log ( 2 , getNameAsString ( typeName ) ) ;
2183
2219
expression = this . resolveImportExpression ( declaration , resolved . importDeclaration , typeName , expression , program )
2184
2220
}
2185
2221
@@ -2228,8 +2264,7 @@ export class ReflectionTransformer implements CustomTransformer {
2228
2264
return ;
2229
2265
}
2230
2266
2231
- const runtimeTypeName = this . getDeclarationVariableName ( typeName )
2232
- // const runtimeTypeName = this.f.createIdentifier(getRuntimeTypeName(getNameAsString(typeName)));
2267
+ const runtimeTypeName = this . getRuntimeTypeName ( typeName ) ;
2233
2268
2234
2269
//to break recursion, we track which declaration has already been compiled
2235
2270
if ( ! this . compiledDeclarations . has ( declaration ) && ! this . compileDeclarations . has ( declaration ) ) {
@@ -2240,18 +2275,14 @@ export class ReflectionTransformer implements CustomTransformer {
2240
2275
return ;
2241
2276
}
2242
2277
2243
- if ( this . external . hasSourceFile ( declarationSourceFile ) ) {
2244
- if ( this . external . isEmbeddingExternalLibraryImport ( ) ) {
2245
- this . external . embedExternalLibraryImport ( typeName , declaration , declarationSourceFile , resolved . importDeclaration ) ;
2246
- }
2278
+ if ( this . external . hasSourceFile ( declarationSourceFile ) && this . external . isEmbeddingExternalLibraryImport ( ) ) {
2279
+ this . external . processExternalLibraryImport ( typeName , declaration , declarationSourceFile , resolved . importDeclaration ) ;
2247
2280
} else {
2248
2281
const isGlobal = resolved . importDeclaration === undefined && declarationSourceFile . fileName !== this . sourceFile . fileName ;
2249
2282
const isFromImport = resolved . importDeclaration !== undefined ;
2250
2283
2251
2284
if ( isGlobal ) {
2252
- if ( this . external . isEmbeddingExternalLibraryImport ( ) ) {
2253
- this . external . addGlobalType ( getNameAsString ( typeName ) ) ;
2254
- }
2285
+ this . external . knownGlobalTypeNames . add ( getNameAsString ( typeName ) ) ;
2255
2286
this . embedDeclarations . set ( declaration , {
2256
2287
name : typeName ,
2257
2288
sourceFile : declarationSourceFile
@@ -2281,7 +2312,7 @@ export class ReflectionTransformer implements CustomTransformer {
2281
2312
const builtType = isBuiltType ( runtimeTypeName , found ) ;
2282
2313
if ( ! builtType ) {
2283
2314
if ( ! this . external . shouldInlineExternalLibraryImport ( resolved . importDeclaration , typeName , declarationReflection ) ) return ;
2284
- this . external . embedExternalLibraryImport ( typeName , declaration , declarationSourceFile , resolved . importDeclaration ) ;
2315
+ this . external . processExternalLibraryImport ( typeName , declaration , declarationSourceFile , resolved . importDeclaration ) ;
2285
2316
} else {
2286
2317
//check if the referenced file has reflection info emitted. if not, any is emitted for that reference
2287
2318
const reflection = this . findReflectionFromPath ( found . fileName ) ;
@@ -2310,7 +2341,7 @@ export class ReflectionTransformer implements CustomTransformer {
2310
2341
}
2311
2342
2312
2343
const index = program . pushStack (
2313
- program . forNode === declaration ? 0 : this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , runtimeTypeName )
2344
+ program . forNode === declaration ? 0 : this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , this . getDeclarationVariableName ( typeName ) )
2314
2345
) ;
2315
2346
if ( type . typeArguments ) {
2316
2347
for ( const argument of type . typeArguments ) {
@@ -2343,8 +2374,6 @@ export class ReflectionTransformer implements CustomTransformer {
2343
2374
return ;
2344
2375
}
2345
2376
2346
- const isEmbeddingExternalLibraryImport = this . external . isEmbeddingExternalLibraryImport ( ) ;
2347
-
2348
2377
let body : Identifier | Expression = isIdentifier ( typeName ) ? typeName : this . createAccessorForEntityName ( typeName ) ;
2349
2378
if ( resolved . importDeclaration && isIdentifier ( typeName ) ) {
2350
2379
body = this . resolveImportExpression ( resolved . declaration , resolved . importDeclaration , typeName , body , program ) ;
@@ -2355,8 +2384,17 @@ export class ReflectionTransformer implements CustomTransformer {
2355
2384
this . extractPackStructOfType ( typeArgument , program ) ;
2356
2385
}
2357
2386
}
2358
- const index = program . pushStack ( this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , body ) ) ;
2359
- program . pushOp ( isClassDeclaration ( declaration ) ? ReflectionOp . classReference : ReflectionOp . functionReference , index ) ;
2387
+ if ( this . external . isEmbeddingExternalLibraryImport ( ) ) {
2388
+ if ( isClassDeclaration ( declaration ) ) {
2389
+ program . pushOp ( ReflectionOp . objectLiteral ) ;
2390
+ } else {
2391
+ const index = program . pushStack ( this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , body ) ) ;
2392
+ program . pushOp ( ReflectionOp . function , index ) ;
2393
+ }
2394
+ } else {
2395
+ const index = program . pushStack ( this . f . createArrowFunction ( undefined , undefined , [ ] , undefined , undefined , body ) ) ;
2396
+ program . pushOp ( isClassDeclaration ( declaration ) ? ReflectionOp . classReference : ReflectionOp . functionReference , index ) ;
2397
+ }
2360
2398
program . popFrameImplicit ( ) ;
2361
2399
} else if ( isTypeParameterDeclaration ( declaration ) ) {
2362
2400
this . resolveTypeParameter ( declaration , type , program ) ;
0 commit comments