Don't generate enums prematurely
This commit is contained in:
Родитель
cc87ade358
Коммит
faf14e857a
|
@ -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(
|
||||
|
|
Загрузка…
Ссылка в новой задаче