@@ -819,6 +819,55 @@ export class JsonSchemaGenerator {
819
819
return undefined ;
820
820
}
821
821
822
+ private getInitializerValue ( initializer ?: ts . Expression | ts . LiteralToken ) : any {
823
+ let val ;
824
+ if ( initializer === undefined ) {
825
+ return ;
826
+ }
827
+ switch ( initializer . kind ) {
828
+ case ts . SyntaxKind . NumericLiteral :
829
+ const txt = initializer . getText ( ) ;
830
+ if ( txt . includes ( "." ) ) {
831
+ val = Number . parseFloat ( initializer . getText ( ) ) ;
832
+ } else {
833
+ val = Number . parseInt ( initializer . getText ( ) ) ;
834
+ }
835
+ break ;
836
+ case ts . SyntaxKind . StringLiteral :
837
+ val = ( initializer as ts . StringLiteral ) . text ;
838
+ break ;
839
+ case ts . SyntaxKind . FalseKeyword :
840
+ val = false ;
841
+ break ;
842
+ case ts . SyntaxKind . TrueKeyword :
843
+ val = true ;
844
+ break ;
845
+ }
846
+ return val ;
847
+ }
848
+
849
+ private getDeclarationValue ( declaration ?: ts . Declaration ) : any {
850
+ let val : any ;
851
+ if ( declaration === undefined ) {
852
+ return ;
853
+ }
854
+ switch ( declaration . kind ) {
855
+ case ts . SyntaxKind . VariableDeclaration :
856
+ val = this . getInitializerValue ( ( declaration as ts . VariableDeclaration ) . initializer ) ;
857
+ break ;
858
+ case ts . SyntaxKind . EnumDeclaration :
859
+ const enumDecl = declaration as ts . EnumDeclaration ;
860
+ val = enumDecl . members . reduce ( ( prev , curr ) => {
861
+ const v = this . getInitializerValue ( curr . initializer ) ;
862
+ prev [ curr . name . getText ( ) ] = v ;
863
+ return prev ;
864
+ } , { } as { [ k : string ] : any } ) ;
865
+
866
+ break ;
867
+ }
868
+ return val ;
869
+ }
870
+
822
871
private getDefinitionForProperty ( prop : ts . Symbol , node : ts . Node ) : Definition | null {
823
872
if ( prop . flags & ts . SymbolFlags . Method ) {
824
873
return null ;
@@ -847,31 +896,30 @@ export class JsonSchemaGenerator {
847
896
initial = initial . expression ;
848
897
}
849
898
850
- if ( ( < any > initial ) . expression ) {
851
- // node
852
- console . warn ( "initializer is expression for property " + propertyName ) ;
853
- } else if ( ( < any > initial ) . kind && ( < any > initial ) . kind === ts . SyntaxKind . NoSubstitutionTemplateLiteral ) {
854
- definition . default = initial . getText ( ) ;
855
- } else {
856
- try {
857
- const sandbox = { sandboxvar : null as any } ;
858
- vm . runInNewContext ( "sandboxvar=" + initial . getText ( ) , sandbox ) ;
899
+ try {
900
+ const sandbox : Record < string , any > = { sandboxvar : null as any } ;
901
+ // Put user symbols into sandbox
902
+ Object . entries ( this . userSymbols )
903
+ . filter ( ( [ _ , sym ] ) => sym . valueDeclaration )
904
+ . forEach ( ( [ name , sym ] ) => {
905
+ sandbox [ name ] = this . getDeclarationValue ( sym . valueDeclaration ) ;
906
+ } ) ;
907
+ vm . runInNewContext ( "sandboxvar=" + initial . getText ( ) , sandbox ) ;
859
908
860
- const val = sandbox . sandboxvar ;
861
- if (
862
- val === null ||
863
- typeof val === "string" ||
864
- typeof val === "number" ||
865
- typeof val === "boolean" ||
866
- Object . prototype . toString . call ( val ) === "[object Array]"
867
- ) {
868
- definition . default = val ;
869
- } else if ( val ) {
870
- console . warn ( "unknown initializer for property " + propertyName + ": " + val ) ;
871
- }
872
- } catch ( e ) {
873
- console . warn ( "exception evaluating initializer for property " + propertyName ) ;
909
+ const val = sandbox . sandboxvar ;
910
+ if (
911
+ val === null ||
912
+ typeof val === "string" ||
913
+ typeof val === "number" ||
914
+ typeof val === "boolean" ||
915
+ Object . prototype . toString . call ( val ) === "[object Array]"
916
+ ) {
917
+ definition . default = val ;
918
+ } else if ( val ) {
919
+ console . warn ( "unknown initializer for property " + propertyName + ": " + val ) ;
874
920
}
921
+ } catch ( e ) {
922
+ console . warn ( "exception evaluating initializer for property " + propertyName ) ;
875
923
}
876
924
}
877
925
@@ -1700,6 +1748,7 @@ export function buildGenerator(
1700
1748
1701
1749
function inspect ( node : ts . Node , tc : ts . TypeChecker ) {
1702
1750
if (
1751
+ node . kind === ts . SyntaxKind . VariableDeclaration ||
1703
1752
node . kind === ts . SyntaxKind . ClassDeclaration ||
1704
1753
node . kind === ts . SyntaxKind . InterfaceDeclaration ||
1705
1754
node . kind === ts . SyntaxKind . EnumDeclaration ||
0 commit comments