@@ -826,7 +826,6 @@ export class ReflectionTransformer implements CustomTransformer {
826
826
} else if ( isArrowFunction ( node ) ) {
827
827
return this . decorateArrow ( node ) ;
828
828
} else if ( ( isNewExpression ( node ) || isCallExpression ( node ) ) && node . typeArguments && node . typeArguments . length > 0 ) {
829
-
830
829
if ( isCallExpression ( node ) ) {
831
830
const autoTypeFunctions = [ 'valuesOf' , 'propertiesOf' , 'typeOf' ] ;
832
831
if ( isIdentifier ( node . expression ) && autoTypeFunctions . includes ( getIdentifierName ( node . expression ) ) ) {
@@ -1823,7 +1822,7 @@ export class ReflectionTransformer implements CustomTransformer {
1823
1822
if ( isIdentifier ( narrowed . exprName ) ) {
1824
1823
const resolved = this . resolveDeclaration ( narrowed . exprName ) ;
1825
1824
if ( resolved && findSourceFile ( resolved . declaration ) !== this . sourceFile && resolved . importDeclaration ) {
1826
- ensureImportIsEmitted ( resolved . importDeclaration , narrowed . exprName ) ;
1825
+ this . resolveImport ( resolved . declaration , resolved . importDeclaration , narrowed . exprName , program ) ;
1827
1826
}
1828
1827
}
1829
1828
@@ -1982,6 +1981,8 @@ export class ReflectionTransformer implements CustomTransformer {
1982
1981
importDeclaration = declaration . parent ;
1983
1982
}
1984
1983
1984
+ // TODO: check if it's a built type here?
1985
+
1985
1986
if ( importDeclaration ) {
1986
1987
if ( importDeclaration . importClause && importDeclaration . importClause . isTypeOnly ) typeOnly = true ;
1987
1988
declaration = this . resolveImportSpecifier ( typeName . escapedText , importDeclaration , this . sourceFile ) ;
@@ -2030,7 +2031,37 @@ export class ReflectionTransformer implements CustomTransformer {
2030
2031
return this . f . createIdentifier ( '__Ω' + joinQualifiedName ( typeName ) ) ;
2031
2032
}
2032
2033
2033
- // TODO: what to do when the inlineExternalImports type depends on another inlineExternalImports type? should that be automatically resolved, or should the user specify that explicitly as well?
2034
+ protected resolveImport ( declaration : Node , importDeclaration : ImportDeclaration , typeName : Identifier , program : CompilerProgram ) {
2035
+ if ( isVariableDeclaration ( declaration ) ) {
2036
+ if ( declaration . type ) {
2037
+ declaration = declaration . type ;
2038
+ } else if ( declaration . initializer ) {
2039
+ declaration = declaration . initializer ;
2040
+ }
2041
+ }
2042
+
2043
+ ensureImportIsEmitted ( importDeclaration , typeName ) ;
2044
+
2045
+ // check if the referenced declaration has reflection disabled
2046
+ const declarationReflection = this . findReflectionConfig ( declaration , program ) ;
2047
+ if ( declarationReflection . mode !== 'never' ) {
2048
+ const declarationSourceFile = importDeclaration . getSourceFile ( ) ;
2049
+
2050
+ const runtimeTypeName = this . getDeclarationVariableName ( typeName ) ;
2051
+
2052
+ const builtType = isBuiltType ( runtimeTypeName , declarationSourceFile ) ;
2053
+
2054
+ if ( ! builtType && this . shouldInlineExternalImport ( importDeclaration , typeName , declarationReflection ) ) {
2055
+ this . embedDeclarations . set ( declaration , {
2056
+ name : typeName ,
2057
+ sourceFile : declarationSourceFile ,
2058
+ assignType : true ,
2059
+ } ) ;
2060
+ }
2061
+ }
2062
+ }
2063
+
2064
+ // TODO: what to do when the external type depends on another external type? should that be automatically resolved, or should the user explicitly specify that as well?
2034
2065
protected shouldInlineExternalImport ( importDeclaration : ImportDeclaration , entityName : EntityName , config : ReflectionConfig ) : boolean {
2035
2066
if ( ! ts . isStringLiteral ( importDeclaration . moduleSpecifier ) ) return false ;
2036
2067
if ( config . options . inlineExternalImports === true ) return true ;
@@ -2138,7 +2169,9 @@ export class ReflectionTransformer implements CustomTransformer {
2138
2169
}
2139
2170
2140
2171
if ( isModuleDeclaration ( declaration ) && resolved . importDeclaration ) {
2141
- if ( isIdentifier ( typeName ) ) ensureImportIsEmitted ( resolved . importDeclaration , typeName ) ;
2172
+ if ( isIdentifier ( typeName ) ) {
2173
+ this . resolveImport ( declaration , resolved . importDeclaration , typeName , program ) ;
2174
+ }
2142
2175
2143
2176
//we can not infer from module declaration, so do `typeof T` in runtime
2144
2177
program . pushOp (
@@ -2294,25 +2327,7 @@ export class ReflectionTransformer implements CustomTransformer {
2294
2327
}
2295
2328
2296
2329
if ( resolved . importDeclaration && isIdentifier ( typeName ) ) {
2297
- ensureImportIsEmitted ( resolved . importDeclaration , typeName ) ;
2298
-
2299
- // check if the referenced declaration has reflection disabled
2300
- const declarationReflection = this . findReflectionConfig ( declaration , program ) ;
2301
- if ( declarationReflection . mode !== 'never' ) {
2302
- const declarationSourceFile = resolved . importDeclaration . getSourceFile ( ) ;
2303
- // //check if the referenced file has reflection info emitted. if not, any is emitted for that reference
2304
- const typeVar = this . getDeclarationVariableName ( typeName ) ;
2305
- // //check if typeVar is exported in referenced file
2306
- const builtType = isBuiltType ( typeVar , declarationSourceFile ) ;
2307
-
2308
- if ( ! builtType && this . shouldInlineExternalImport ( resolved . importDeclaration , typeName , declarationReflection ) ) {
2309
- this . embedDeclarations . set ( declaration , {
2310
- name : typeName ,
2311
- sourceFile : declarationSourceFile ,
2312
- assignType : true ,
2313
- } ) ;
2314
- }
2315
- }
2330
+ this . resolveImport ( resolved . declaration , resolved . importDeclaration , typeName , program ) ;
2316
2331
}
2317
2332
program . pushFrame ( ) ;
2318
2333
if ( type . typeArguments ) {
0 commit comments