@@ -818,7 +818,7 @@ function convertVariable(
818
818
const type = context . checker . getTypeOfSymbolAtLocation ( symbol , declaration ) ;
819
819
820
820
if (
821
- isEnumLiteral ( declaration as ts . VariableDeclaration ) &&
821
+ isEnumLike ( context . checker , type , declaration ) &&
822
822
symbol . getJsDocTags ( ) . some ( ( tag ) => tag . name === "enum" )
823
823
) {
824
824
return convertVariableAsEnum ( context , symbol , exportSymbol ) ;
@@ -852,27 +852,15 @@ function convertVariable(
852
852
context . finalizeDeclarationReflection ( reflection , symbol , exportSymbol ) ;
853
853
}
854
854
855
- function isEnumLiteral ( declaration : ts . VariableDeclaration ) {
856
- if (
857
- ! hasAllFlags ( declaration . parent . flags , ts . NodeFlags . Const ) ||
858
- ! declaration . initializer ||
859
- ! ts . isAsExpression ( declaration . initializer ) ||
860
- declaration . initializer . type . getText ( ) !== "const"
861
- ) {
862
- // Not an as-const expression
863
- return false ;
864
- }
865
-
866
- const body = declaration . initializer . expression ;
867
- if ( ! ts . isObjectLiteralExpression ( body ) ) {
855
+ function isEnumLike ( checker : ts . TypeChecker , type : ts . Type , location : ts . Node ) {
856
+ if ( ! hasAllFlags ( type . flags , ts . TypeFlags . Object ) ) {
868
857
return false ;
869
858
}
870
859
871
- return body . properties . every (
872
- ( prop ) =>
873
- ts . isPropertyAssignment ( prop ) &&
874
- ts . isStringLiteral ( prop . initializer )
875
- ) ;
860
+ return type . getProperties ( ) . every ( ( prop ) => {
861
+ const propType = checker . getTypeOfSymbolAtLocation ( prop , location ) ;
862
+ return propType . isStringLiteral ( ) ;
863
+ } ) ;
876
864
}
877
865
878
866
function convertVariableAsEnum (
@@ -889,22 +877,24 @@ function convertVariableAsEnum(
889
877
const rc = context . withScope ( reflection ) ;
890
878
891
879
const declaration = symbol . declarations ! [ 0 ] as ts . VariableDeclaration ;
892
- const init = ( declaration . initializer as ts . AsExpression )
893
- . expression as ts . ObjectLiteralExpression ;
880
+ const type = context . checker . getTypeAtLocation ( declaration ) ;
894
881
895
- for ( const prop of init . properties as readonly ts . PropertyAssignment [ ] ) {
896
- const childSymbol = context . checker . getSymbolAtLocation ( prop . name ) ;
882
+ for ( const prop of type . getProperties ( ) ) {
897
883
const reflection = rc . createDeclarationReflection (
898
884
ReflectionKind . EnumMember ,
899
- childSymbol ,
885
+ prop ,
900
886
void 0
901
887
) ;
902
888
903
- reflection . defaultValue = JSON . stringify (
904
- ( prop . initializer as ts . StringLiteral ) . text
889
+ const propType = context . checker . getTypeOfSymbolAtLocation (
890
+ prop ,
891
+ declaration
905
892
) ;
893
+ assert ( propType . isStringLiteral ( ) ) ;
894
+
895
+ reflection . defaultValue = JSON . stringify ( propType . value ) ;
906
896
907
- rc . finalizeDeclarationReflection ( reflection , childSymbol , void 0 ) ;
897
+ rc . finalizeDeclarationReflection ( reflection , prop , void 0 ) ;
908
898
}
909
899
910
900
// Skip converting the type alias, if there is one
0 commit comments