quicktype-core
23.0.4923.0.54
~
Modified (4 files)
Index: package/dist/input/JSONSchemaInput.js
===================================================================
--- package/dist/input/JSONSchemaInput.js
+++ package/dist/input/JSONSchemaInput.js
@@ -557,8 +557,9 @@
}
function convertToType(schema, loc, typeAttributes) {
return __awaiter(this, void 0, void 0, function* () {
const enumArray = Array.isArray(schema.enum) ? schema.enum : undefined;
+ const isConst = schema.const !== undefined;
const typeSet = (0, collection_utils_1.definedMap)(schema.type, t => checkTypeList(t, loc));
function isTypeIncluded(name) {
if (typeSet !== undefined && !typeSet.has(name)) {
return false;
@@ -577,8 +578,11 @@
break;
}
return enumArray.find(predicate) !== undefined;
}
+ if (isConst) {
+ return name === "string";
+ }
return true;
}
const includedTypes = (0, collection_utils_1.setFilter)(schemaTypes, isTypeIncluded);
let producedAttributesForNoCases = undefined;
@@ -755,19 +759,20 @@
typeBuilder.setSetOperationMembers(unionType, new Set(typeRefs));
return unionType;
});
}
- const includeObject = enumArray === undefined && (typeSet === undefined || typeSet.has("object"));
- const includeArray = enumArray === undefined && (typeSet === undefined || typeSet.has("array"));
+ const includeObject = enumArray === undefined && !isConst && (typeSet === undefined || typeSet.has("object"));
+ const includeArray = enumArray === undefined && !isConst && (typeSet === undefined || typeSet.has("array"));
const needStringEnum = includedTypes.has("string") &&
enumArray !== undefined &&
enumArray.find((x) => typeof x === "string") !== undefined;
const needUnion = typeSet !== undefined ||
schema.properties !== undefined ||
schema.additionalProperties !== undefined ||
schema.items !== undefined ||
schema.required !== undefined ||
- enumArray !== undefined;
+ enumArray !== undefined ||
+ isConst;
const types = [];
if (needUnion) {
const unionTypes = [];
const numberAttributes = combineProducedAttributes(({ forNumber }) => forNumber);
@@ -782,10 +787,12 @@
const attributes = (0, Type_1.isNumberTypeKind)(kind) ? numberAttributes : undefined;
unionTypes.push(typeBuilder.getPrimitiveType(kind, attributes));
}
const stringAttributes = (0, TypeAttributes_1.combineTypeAttributes)("union", inferredAttributes, combineProducedAttributes(({ forString }) => forString));
- if (needStringEnum) {
- const cases = enumArray.filter(x => typeof x === "string");
+ if (needStringEnum || isConst) {
+ const cases = isConst
+ ? [schema.const]
+ : enumArray.filter(x => typeof x === "string");
unionTypes.push(typeBuilder.getStringType(stringAttributes, StringTypes_1.StringTypes.fromCases(cases)));
}
else if (includedTypes.has("string")) {
unionTypes.push(makeStringType(stringAttributes)); Index: package/dist/language/TypeScriptFlow.js
===================================================================
--- package/dist/language/TypeScriptFlow.js
+++ package/dist/language/TypeScriptFlow.js
@@ -14,9 +14,10 @@
justTypes: new RendererOptions_1.BooleanOption("just-types", "Interfaces only", false),
nicePropertyNames: new RendererOptions_1.BooleanOption("nice-property-names", "Transform property names to be JavaScripty", false),
declareUnions: new RendererOptions_1.BooleanOption("explicit-unions", "Explicitly name unions", false),
preferUnions: new RendererOptions_1.BooleanOption("prefer-unions", "Use union type instead of enum", false),
- preferTypes: new RendererOptions_1.BooleanOption("prefer-types", "Use types instead of interfaces", false)
+ preferTypes: new RendererOptions_1.BooleanOption("prefer-types", "Use types instead of interfaces", false),
+ preferConstValues: new RendererOptions_1.BooleanOption("prefer-const-values", "Use string instead of enum for string enums with single value", false)
});
const tsFlowTypeAnnotations = {
any: ": any",
anyArray: ": any[]",
@@ -86,8 +87,12 @@
return super.namerForObjectProperty();
}
}
sourceFor(t) {
+ if (this._tsFlowOptions.preferConstValues && t.kind === "enum" && t instanceof Type_1.EnumType && t.cases.size === 1) {
+ const item = t.cases.values().next().value;
+ return (0, Source_1.singleWord)(`"${(0, Strings_1.utf16StringEscape)(item)}"`);
+ }
if (["class", "object", "enum"].indexOf(t.kind) >= 0) {
return (0, Source_1.singleWord)(this.nameForNamedType(t));
}
return (0, TypeUtils_1.matchType)(t, _anyType => (0, Source_1.singleWord)("any"), _nullType => (0, Source_1.singleWord)("null"), _boolType => (0, Source_1.singleWord)("boolean"), _integerType => (0, Source_1.singleWord)("number"), _doubleType => (0, Source_1.singleWord)("number"), _stringType => (0, Source_1.singleWord)("string"), arrayType => {
@@ -215,8 +220,11 @@
this.emitLine("// import { Convert", topLevelNames, ' } from "./file";');
}
emitEnum(e, enumName) {
this.emitDescription(this.descriptionForType(e));
+ // enums with only one value are emitted as constants
+ if (this._tsFlowOptions.preferConstValues && e.cases.size === 1)
+ return;
if (this._tsFlowOptions.preferUnions) {
let items = "";
e.cases.forEach(item => {
if (items === "") { Index: package/package.json
===================================================================
--- package/package.json
+++ package/package.json
@@ -1,7 +1,7 @@
{
"name": "quicktype-core",
- "version": "23.0.49",
+ "version": "23.0.54",
"description": "The quicktype engine as a library",
"license": "Apache-2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -25,9 +25,9 @@
"readable-stream": "4.3.0",
"unicode-properties": "^1.4.1",
"urijs": "^1.19.1",
"wordwrap": "^1.0.0",
- "yaml": "^2.2.2"
+ "yaml": "^2.3.1"
},
"devDependencies": {
"@types/browser-or-node": "^1.3.0",
"@types/js-base64": "^3.3.1", Index: package/dist/language/TypeScriptFlow.d.ts
===================================================================
--- package/dist/language/TypeScriptFlow.d.ts
+++ package/dist/language/TypeScriptFlow.d.ts
@@ -16,8 +16,9 @@
nicePropertyNames: BooleanOption;
declareUnions: BooleanOption;
preferUnions: BooleanOption;
preferTypes: BooleanOption;
+ preferConstValues: BooleanOption;
};
export declare abstract class TypeScriptFlowBaseTargetLanguage extends JavaScriptTargetLanguage {
protected getOptions(): Option<any>[];
get supportsOptionalClassProperties(): boolean;