Skip to content

Commit d9484e8

Browse files
authored
use const instead of enum (#535)
1 parent 2df0bea commit d9484e8

File tree

10 files changed

+35
-61
lines changed

10 files changed

+35
-61
lines changed

test/programs/argument-id/schema.MyObject.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"ReferenceType": {
66
"properties": {
77
"reference": {
8-
"enum": [
9-
true
10-
],
8+
"const": true,
119
"type": "boolean"
1210
}
1311
},

test/programs/enums-value-in-interface/schema.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"definitions": {
44
"A.B": {
5-
"enum": [
6-
0
7-
],
5+
"const": 0,
86
"type": "number"
97
}
108
},

test/programs/generate-all-types/schema.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"definitions": {
44
"MyEnum": {
5-
"enum": [
6-
0
7-
],
5+
"const": 0,
86
"type": "number"
97
},
108
"MyInterface": {

test/programs/satisfies-keyword/schema.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"properties": {
44
"a": {
5-
"enum": [
6-
"a"
7-
],
5+
"const": "a",
86
"type": "string"
97
},
108
"b": {
11-
"enum": [
12-
1
13-
],
9+
"const": 1,
1410
"type": "number"
1511
},
1612
"c": {
17-
"enum": [
18-
true
19-
],
13+
"const": true,
2014
"type": "boolean"
2115
}
2216
},

test/programs/strict-null-checks/schema.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,17 @@
1414
"type": "number"
1515
},
1616
"valTrue": {
17-
"enum": [
18-
true
19-
],
17+
"const": true,
2018
"type": "boolean"
2119
},
2220
"valTrueOpt": {
23-
"enum": [
24-
true
25-
],
21+
"const": true,
2622
"type": "boolean"
2723
},
2824
"valTrueOrNull": {
2925
"anyOf": [
3026
{
31-
"enum": [
32-
true
33-
],
27+
"const": true,
3428
"type": "boolean"
3529
},
3630
{

test/programs/type-union-tagged/schema.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"Circle": {
1616
"properties": {
1717
"kind": {
18-
"enum": [
19-
"circle"
20-
],
18+
"const": "circle",
2119
"type": "string"
2220
},
2321
"radius": {
@@ -36,9 +34,7 @@
3634
"type": "number"
3735
},
3836
"kind": {
39-
"enum": [
40-
"rectangle"
41-
],
37+
"const": "rectangle",
4238
"type": "string"
4339
},
4440
"width": {
@@ -55,9 +51,7 @@
5551
"Square": {
5652
"properties": {
5753
"kind": {
58-
"enum": [
59-
"square"
60-
],
54+
"const": "square",
6155
"type": "string"
6256
},
6357
"size": {

test/programs/unique-names-multiple-subdefinitions/schema.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
"SubObject.0eb4e9af": {
1414
"properties": {
1515
"is": {
16-
"enum": [
17-
"SubObject_1"
18-
],
16+
"const": "SubObject_1",
1917
"type": "string"
2018
}
2119
},

test/programs/unique-names/schema.MyObject.f2191116.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"properties": {
44
"is": {
55
"type": "string",
6-
"enum": [
7-
"MyObject_1"
8-
]
6+
"const": "MyObject_1"
97
}
108
},
119
"required": [

test/programs/unique-names/schema.MyObject.fded1213.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"properties": {
44
"is": {
55
"type": "string",
6-
"enum": [
7-
"MyObject_2"
8-
]
6+
"const": "MyObject_2"
97
}
108
},
119
"required": [

typescript-json-schema.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -554,16 +554,16 @@ export class JsonSchemaGenerator {
554554
if (comments.length) {
555555
definition.description = comments
556556
.map((comment) => {
557-
const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n");
557+
const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n");
558558

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+
}
565565

566-
return newlineNormalizedComment;
566+
return newlineNormalizedComment;
567567
})
568568
.join("").trim();
569569
}
@@ -692,12 +692,12 @@ export class JsonSchemaGenerator {
692692
const value = extractLiteralValue(propertyType);
693693
if (value !== undefined) {
694694
definition.type = typeof value;
695-
definition.enum = [value];
695+
definition.const = value;
696696
} else if (arrayType !== undefined) {
697697
if (
698698
propertyType.flags & ts.TypeFlags.Object &&
699699
(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)
701701
) {
702702
definition.type = "object";
703703
definition.additionalProperties = false;
@@ -762,7 +762,7 @@ export class JsonSchemaGenerator {
762762
if (valDecl?.initializer) {
763763
let initial = valDecl.initializer;
764764

765-
while (ts.isTypeAssertion(initial)) {
765+
while (ts.isTypeAssertionExpression(initial)) {
766766
initial = initial.expression;
767767
}
768768

@@ -803,7 +803,7 @@ export class JsonSchemaGenerator {
803803
const members: ts.NodeArray<ts.EnumMember> =
804804
node.kind === ts.SyntaxKind.EnumDeclaration
805805
? (node as ts.EnumDeclaration).members
806-
: ts.createNodeArray([node as ts.EnumMember]);
806+
: ts.factory.createNodeArray([node as ts.EnumMember]);
807807
var enumValues: (number | boolean | string | null)[] = [];
808808
const enumTypes: string[] = [];
809809

@@ -854,7 +854,11 @@ export class JsonSchemaGenerator {
854854
}
855855

856856
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+
}
858862
}
859863

860864
return definition;
@@ -919,7 +923,7 @@ export class JsonSchemaGenerator {
919923
if (isOnlyBooleans) {
920924
pushSimpleType("boolean");
921925
} else {
922-
const enumSchema: Definition = { enum: enumValues.sort() };
926+
const enumSchema: Definition = enumValues.length > 1 ? { enum: enumValues.sort() } : { const: enumValues[0] };
923927

924928
// If all values are of the same primitive type, add a "type" field to the schema
925929
if (
@@ -1240,15 +1244,15 @@ export class JsonSchemaGenerator {
12401244
const symbol = typ.getSymbol();
12411245
// FIXME: We can't just compare the name of the symbol - it ignores the namespace
12421246
let isRawType =
1243-
!symbol ||
1247+
!symbol ||
12441248
// Window is incorrectly marked as rawType here for some reason
12451249
(this.tc.getFullyQualifiedName(symbol) !== "Window" &&
12461250
(this.tc.getFullyQualifiedName(symbol) === "Date" ||
12471251
symbol.name === "integer" ||
12481252
this.tc.getIndexInfoOfType(typ, ts.IndexKind.Number) !== undefined));
12491253

12501254
if (isRawType && (typ as any).aliasSymbol?.escapedName && (typ as any).types) {
1251-
isRawType = false;
1255+
isRawType = false;
12521256
}
12531257

12541258
// special case: an union where all child are string literals -> make an enum instead

0 commit comments

Comments
 (0)