quicktype-core 23.0.51 → 23.0.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -558,6 +558,7 @@ function addTypesInSchema(resolver, typeBuilder, references, attributeProducers)
558
558
  function convertToType(schema, loc, typeAttributes) {
559
559
  return __awaiter(this, void 0, void 0, function* () {
560
560
  const enumArray = Array.isArray(schema.enum) ? schema.enum : undefined;
561
+ const isConst = schema.const !== undefined;
561
562
  const typeSet = (0, collection_utils_1.definedMap)(schema.type, t => checkTypeList(t, loc));
562
563
  function isTypeIncluded(name) {
563
564
  if (typeSet !== undefined && !typeSet.has(name)) {
@@ -578,6 +579,9 @@ function addTypesInSchema(resolver, typeBuilder, references, attributeProducers)
578
579
  }
579
580
  return enumArray.find(predicate) !== undefined;
580
581
  }
582
+ if (isConst) {
583
+ return name === "string";
584
+ }
581
585
  return true;
582
586
  }
583
587
  const includedTypes = (0, collection_utils_1.setFilter)(schemaTypes, isTypeIncluded);
@@ -756,8 +760,8 @@ function addTypesInSchema(resolver, typeBuilder, references, attributeProducers)
756
760
  return unionType;
757
761
  });
758
762
  }
759
- const includeObject = enumArray === undefined && (typeSet === undefined || typeSet.has("object"));
760
- const includeArray = enumArray === undefined && (typeSet === undefined || typeSet.has("array"));
763
+ const includeObject = enumArray === undefined && !isConst && (typeSet === undefined || typeSet.has("object"));
764
+ const includeArray = enumArray === undefined && !isConst && (typeSet === undefined || typeSet.has("array"));
761
765
  const needStringEnum = includedTypes.has("string") &&
762
766
  enumArray !== undefined &&
763
767
  enumArray.find((x) => typeof x === "string") !== undefined;
@@ -766,7 +770,8 @@ function addTypesInSchema(resolver, typeBuilder, references, attributeProducers)
766
770
  schema.additionalProperties !== undefined ||
767
771
  schema.items !== undefined ||
768
772
  schema.required !== undefined ||
769
- enumArray !== undefined;
773
+ enumArray !== undefined ||
774
+ isConst;
770
775
  const types = [];
771
776
  if (needUnion) {
772
777
  const unionTypes = [];
@@ -783,8 +788,10 @@ function addTypesInSchema(resolver, typeBuilder, references, attributeProducers)
783
788
  unionTypes.push(typeBuilder.getPrimitiveType(kind, attributes));
784
789
  }
785
790
  const stringAttributes = (0, TypeAttributes_1.combineTypeAttributes)("union", inferredAttributes, combineProducedAttributes(({ forString }) => forString));
786
- if (needStringEnum) {
787
- const cases = enumArray.filter(x => typeof x === "string");
791
+ if (needStringEnum || isConst) {
792
+ const cases = isConst
793
+ ? [schema.const]
794
+ : enumArray.filter(x => typeof x === "string");
788
795
  unionTypes.push(typeBuilder.getStringType(stringAttributes, StringTypes_1.StringTypes.fromCases(cases)));
789
796
  }
790
797
  else if (includedTypes.has("string")) {
@@ -17,6 +17,7 @@ export declare const tsFlowOptions: {
17
17
  declareUnions: BooleanOption;
18
18
  preferUnions: BooleanOption;
19
19
  preferTypes: BooleanOption;
20
+ preferConstValues: BooleanOption;
20
21
  };
21
22
  export declare abstract class TypeScriptFlowBaseTargetLanguage extends JavaScriptTargetLanguage {
22
23
  protected getOptions(): Option<any>[];
@@ -15,7 +15,8 @@ exports.tsFlowOptions = Object.assign({}, JavaScript_1.javaScriptOptions, {
15
15
  nicePropertyNames: new RendererOptions_1.BooleanOption("nice-property-names", "Transform property names to be JavaScripty", false),
16
16
  declareUnions: new RendererOptions_1.BooleanOption("explicit-unions", "Explicitly name unions", false),
17
17
  preferUnions: new RendererOptions_1.BooleanOption("prefer-unions", "Use union type instead of enum", false),
18
- preferTypes: new RendererOptions_1.BooleanOption("prefer-types", "Use types instead of interfaces", false)
18
+ preferTypes: new RendererOptions_1.BooleanOption("prefer-types", "Use types instead of interfaces", false),
19
+ preferConstValues: new RendererOptions_1.BooleanOption("prefer-const-values", "Use string instead of enum for string enums with single value", false)
19
20
  });
20
21
  const tsFlowTypeAnnotations = {
21
22
  any: ": any",
@@ -87,6 +88,10 @@ class TypeScriptFlowBaseRenderer extends JavaScript_1.JavaScriptRenderer {
87
88
  }
88
89
  }
89
90
  sourceFor(t) {
91
+ if (this._tsFlowOptions.preferConstValues && t.kind === "enum" && t instanceof Type_1.EnumType && t.cases.size === 1) {
92
+ const item = t.cases.values().next().value;
93
+ return (0, Source_1.singleWord)(`"${(0, Strings_1.utf16StringEscape)(item)}"`);
94
+ }
90
95
  if (["class", "object", "enum"].indexOf(t.kind) >= 0) {
91
96
  return (0, Source_1.singleWord)(this.nameForNamedType(t));
92
97
  }
@@ -216,6 +221,9 @@ class TypeScriptRenderer extends TypeScriptFlowBaseRenderer {
216
221
  }
217
222
  emitEnum(e, enumName) {
218
223
  this.emitDescription(this.descriptionForType(e));
224
+ // enums with only one value are emitted as constants
225
+ if (this._tsFlowOptions.preferConstValues && e.cases.size === 1)
226
+ return;
219
227
  if (this._tsFlowOptions.preferUnions) {
220
228
  let items = "";
221
229
  e.cases.forEach(item => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quicktype-core",
3
- "version": "23.0.51",
3
+ "version": "23.0.53",
4
4
  "description": "The quicktype engine as a library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",