Don't generate enums prematurely

This commit is contained in:
Mark Probst 2018-04-25 08:46:00 -07:00
Родитель cc87ade358
Коммит faf14e857a
7 изменённых файлов: 23 добавлений и 26 удалений

Просмотреть файл

@ -157,7 +157,7 @@ export function combineClasses(
clique,
attributes,
builder,
unionBuilderForUnification(builder, false, false, conflateNumbers),
unionBuilderForUnification(builder, false, false, false, conflateNumbers),
conflateNumbers,
forwardingRef
);

Просмотреть файл

@ -22,7 +22,7 @@ export function flattenUnions(
let needsRepeat = false;
function replace(types: Set<Type>, builder: GraphRewriteBuilder<Type>, forwardingRef: TypeRef): TypeRef {
const unionBuilder = new UnifyUnionBuilder(builder, makeObjectTypes, true, trefs => {
const unionBuilder = new UnifyUnionBuilder(builder, true, makeObjectTypes, true, trefs => {
assert(trefs.length > 0, "Must have at least one type to build union");
trefs = trefs.map(tref => builder.reconstituteType(tref.deref()[0]));
if (trefs.length === 1) {

Просмотреть файл

@ -127,7 +127,7 @@ export function inferMaps(
shouldBe,
c.getAttributes(),
builder,
unionBuilderForUnification(builder, false, false, conflateNumbers),
unionBuilderForUnification(builder, true, false, false, conflateNumbers),
conflateNumbers
),
forwardingRef

Просмотреть файл

@ -9,7 +9,6 @@ import { UnionBuilder, UnionAccumulator } from "./UnionBuilder";
import { isTime, isDateTime, isDate } from "./DateTime";
import { ClassProperty } from "./Type";
import { TypeAttributes, emptyTypeAttributes } from "./TypeAttributes";
import { StringTypes } from "./StringTypes";
// This should be the recursive type
// Value[] | NestedValueArray[]
@ -40,15 +39,7 @@ class InferenceUnionBuilder extends UnionBuilder<TypeBuilder, NestedValueArray,
private readonly _cjson: CompressedJSON,
private readonly _fixed: boolean
) {
super(typeBuilder);
}
protected makeEnum(
stringTypes: StringTypes,
typeAttributes: TypeAttributes,
forwardingRef: TypeRef | undefined
): TypeRef {
return this.typeBuilder.getStringType(typeAttributes, stringTypes, forwardingRef);
super(typeBuilder, false);
}
protected makeObject(

Просмотреть файл

@ -407,7 +407,7 @@ export function resolveIntersections(
);
const attributes = combineTypeAttributes(intersectionAttributes, extraAttributes);
const unionBuilder = new IntersectionUnionBuilder(builder);
const unionBuilder = new IntersectionUnionBuilder(builder, true);
const tref = unionBuilder.buildUnion(accumulator, true, attributes, forwardingRef);
if (unionBuilder.createdNewIntersections) {
needsRepeat = true;

Просмотреть файл

@ -90,11 +90,12 @@ function countProperties(
export class UnifyUnionBuilder extends UnionBuilder<TypeBuilder & TypeLookerUp, TypeRef[], TypeRef[]> {
constructor(
typeBuilder: TypeBuilder & TypeLookerUp,
makeEnums: boolean,
private readonly _makeObjectTypes: boolean,
private readonly _makeClassesFixed: boolean,
private readonly _unifyTypes: (typesToUnify: TypeRef[]) => TypeRef
) {
super(typeBuilder);
super(typeBuilder, makeEnums);
}
protected makeObject(
@ -173,16 +174,17 @@ export class UnifyUnionBuilder extends UnionBuilder<TypeBuilder & TypeLookerUp,
export function unionBuilderForUnification<T extends Type>(
typeBuilder: GraphRewriteBuilder<T>,
makeEnums: boolean,
makeObjectTypes: boolean,
makeClassesFixed: boolean,
conflateNumbers: boolean
): UnionBuilder<TypeBuilder & TypeLookerUp, TypeRef[], TypeRef[]> {
return new UnifyUnionBuilder(typeBuilder, makeObjectTypes, makeClassesFixed, trefs =>
return new UnifyUnionBuilder(typeBuilder, makeEnums, makeObjectTypes, makeClassesFixed, trefs =>
unifyTypes(
Set(trefs.map(tref => tref.deref()[0])),
emptyTypeAttributes,
typeBuilder,
unionBuilderForUnification(typeBuilder, makeObjectTypes, makeClassesFixed, conflateNumbers),
unionBuilderForUnification(typeBuilder, makeEnums, makeObjectTypes, makeClassesFixed, conflateNumbers),
conflateNumbers
)
);

Просмотреть файл

@ -282,20 +282,24 @@ export class TypeRefUnionAccumulator extends UnionAccumulator<TypeRef, TypeRef>
}
export abstract class UnionBuilder<TBuilder extends TypeBuilder, TArrayData, TObjectData> {
constructor(protected readonly typeBuilder: TBuilder) {}
constructor(protected readonly typeBuilder: TBuilder, private readonly _makeEnums: boolean) {}
protected makeEnum(
private makeEnum(
stringTypes: StringTypes,
typeAttributes: TypeAttributes,
forwardingRef: TypeRef | undefined
): TypeRef {
return this.typeBuilder.getEnumType(
typeAttributes,
defined(stringTypes.cases)
.keySeq()
.toOrderedSet(),
forwardingRef
);
if (this._makeEnums) {
return this.typeBuilder.getEnumType(
typeAttributes,
defined(stringTypes.cases)
.keySeq()
.toOrderedSet(),
forwardingRef
);
} else {
return this.typeBuilder.getStringType(typeAttributes, stringTypes, forwardingRef);
}
}
protected abstract makeObject(