@@ -554,16 +554,16 @@ export class JsonSchemaGenerator {
554
554
if ( comments . length ) {
555
555
definition . description = comments
556
556
. map ( ( comment ) => {
557
- const newlineNormalizedComment = comment . text . replace ( / \r \n / g, "\n" ) ;
557
+ const newlineNormalizedComment = comment . text . replace ( / \r \n / g, "\n" ) ;
558
558
559
- // If a comment contains a "{@link XYZ}" inline tag that could not be
560
- // resolved by the TS checker, then this comment will contain a trailing
561
- // whitespace that we need to remove.
562
- if ( comment . kind === "linkText" ) {
563
- return newlineNormalizedComment . trim ( ) ;
564
- }
559
+ // If a comment contains a "{@link XYZ}" inline tag that could not be
560
+ // resolved by the TS checker, then this comment will contain a trailing
561
+ // whitespace that we need to remove.
562
+ if ( comment . kind === "linkText" ) {
563
+ return newlineNormalizedComment . trim ( ) ;
564
+ }
565
565
566
- return newlineNormalizedComment ;
566
+ return newlineNormalizedComment ;
567
567
} )
568
568
. join ( "" ) . trim ( ) ;
569
569
}
@@ -692,12 +692,12 @@ export class JsonSchemaGenerator {
692
692
const value = extractLiteralValue ( propertyType ) ;
693
693
if ( value !== undefined ) {
694
694
definition . type = typeof value ;
695
- definition . enum = [ value ] ;
695
+ definition . const = value ;
696
696
} else if ( arrayType !== undefined ) {
697
697
if (
698
698
propertyType . flags & ts . TypeFlags . Object &&
699
699
( propertyType as ts . ObjectType ) . objectFlags &
700
- ( ts . ObjectFlags . Anonymous | ts . ObjectFlags . Interface | ts . ObjectFlags . Mapped )
700
+ ( ts . ObjectFlags . Anonymous | ts . ObjectFlags . Interface | ts . ObjectFlags . Mapped )
701
701
) {
702
702
definition . type = "object" ;
703
703
definition . additionalProperties = false ;
@@ -762,7 +762,7 @@ export class JsonSchemaGenerator {
762
762
if ( valDecl ?. initializer ) {
763
763
let initial = valDecl . initializer ;
764
764
765
- while ( ts . isTypeAssertion ( initial ) ) {
765
+ while ( ts . isTypeAssertionExpression ( initial ) ) {
766
766
initial = initial . expression ;
767
767
}
768
768
@@ -803,7 +803,7 @@ export class JsonSchemaGenerator {
803
803
const members : ts . NodeArray < ts . EnumMember > =
804
804
node . kind === ts . SyntaxKind . EnumDeclaration
805
805
? ( node as ts . EnumDeclaration ) . members
806
- : ts . createNodeArray ( [ node as ts . EnumMember ] ) ;
806
+ : ts . factory . createNodeArray ( [ node as ts . EnumMember ] ) ;
807
807
var enumValues : ( number | boolean | string | null ) [ ] = [ ] ;
808
808
const enumTypes : string [ ] = [ ] ;
809
809
@@ -854,7 +854,11 @@ export class JsonSchemaGenerator {
854
854
}
855
855
856
856
if ( enumValues . length > 0 ) {
857
- definition . enum = enumValues . sort ( ) ;
857
+ if ( enumValues . length > 1 ) {
858
+ definition . enum = enumValues . sort ( ) ;
859
+ } else {
860
+ definition . const = enumValues [ 0 ] ;
861
+ }
858
862
}
859
863
860
864
return definition ;
@@ -919,7 +923,7 @@ export class JsonSchemaGenerator {
919
923
if ( isOnlyBooleans ) {
920
924
pushSimpleType ( "boolean" ) ;
921
925
} else {
922
- const enumSchema : Definition = { enum : enumValues . sort ( ) } ;
926
+ const enumSchema : Definition = enumValues . length > 1 ? { enum : enumValues . sort ( ) } : { const : enumValues [ 0 ] } ;
923
927
924
928
// If all values are of the same primitive type, add a "type" field to the schema
925
929
if (
@@ -1240,15 +1244,15 @@ export class JsonSchemaGenerator {
1240
1244
const symbol = typ . getSymbol ( ) ;
1241
1245
// FIXME: We can't just compare the name of the symbol - it ignores the namespace
1242
1246
let isRawType =
1243
- ! symbol ||
1247
+ ! symbol ||
1244
1248
// Window is incorrectly marked as rawType here for some reason
1245
1249
( this . tc . getFullyQualifiedName ( symbol ) !== "Window" &&
1246
1250
( this . tc . getFullyQualifiedName ( symbol ) === "Date" ||
1247
1251
symbol . name === "integer" ||
1248
1252
this . tc . getIndexInfoOfType ( typ , ts . IndexKind . Number ) !== undefined ) ) ;
1249
1253
1250
1254
if ( isRawType && ( typ as any ) . aliasSymbol ?. escapedName && ( typ as any ) . types ) {
1251
- isRawType = false ;
1255
+ isRawType = false ;
1252
1256
}
1253
1257
1254
1258
// special case: an union where all child are string literals -> make an enum instead
0 commit comments