Simplify unification a little
This commit is contained in:
Родитель
a7cbe345eb
Коммит
725000724d
|
@ -1,6 +1,6 @@
|
|||
"use strict";
|
||||
|
||||
import { Set, Map, OrderedSet } from "immutable";
|
||||
import { Set, OrderedSet } from "immutable";
|
||||
|
||||
import { TypeGraph } from "./TypeGraph";
|
||||
import { Type, UnionType, IntersectionType } from "./Type";
|
||||
|
@ -10,6 +10,7 @@ import { TypeRef, StringTypeMapping } from "./TypeBuilder";
|
|||
import { GraphRewriteBuilder } from "./GraphRewriting";
|
||||
import { unifyTypes, UnifyUnionBuilder } from "./UnifyClasses";
|
||||
import { messageAssert, ErrorMessage } from "./Messages";
|
||||
import { emptyTypeAttributes } from "./TypeAttributes";
|
||||
|
||||
export function flattenUnions(
|
||||
graph: TypeGraph,
|
||||
|
@ -21,16 +22,16 @@ export function flattenUnions(
|
|||
let needsRepeat = false;
|
||||
|
||||
function replace(types: Set<Type>, builder: GraphRewriteBuilder<Type>, forwardingRef: TypeRef): TypeRef {
|
||||
const unionBuilder = new UnifyUnionBuilder(builder, true, makeObjectTypes, true, (trefs, attributes) => {
|
||||
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) {
|
||||
return trefs[0];
|
||||
}
|
||||
needsRepeat = true;
|
||||
return builder.getUnionType(attributes, OrderedSet(trefs));
|
||||
return builder.getUnionType(emptyTypeAttributes, OrderedSet(trefs));
|
||||
});
|
||||
return unifyTypes(types, Map(), builder, unionBuilder, conflateNumbers, forwardingRef);
|
||||
return unifyTypes(types, emptyTypeAttributes, builder, unionBuilder, conflateNumbers, forwardingRef);
|
||||
}
|
||||
|
||||
const allUnions = graph.allTypesUnordered().filter(t => t instanceof UnionType) as Set<UnionType>;
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
"use strict";
|
||||
|
||||
import { Set, OrderedMap, OrderedSet, Map } from "immutable";
|
||||
import { Set, OrderedMap, OrderedSet } from "immutable";
|
||||
|
||||
import { Type, ClassProperty, UnionType, ObjectType } from "./Type";
|
||||
import { combineTypeAttributesOfTypes, assertIsObject } from "./TypeUtils";
|
||||
import { assertIsObject } from "./TypeUtils";
|
||||
import { TypeRef, TypeBuilder } from "./TypeBuilder";
|
||||
import { TypeLookerUp, GraphRewriteBuilder } from "./GraphRewriting";
|
||||
import { UnionBuilder, TypeRefUnionAccumulator } from "./UnionBuilder";
|
||||
import { panic, assert, defined, unionOfSets } from "./Support";
|
||||
import { TypeNames, namesTypeAttributeKind } from "./TypeNames";
|
||||
import { TypeAttributes, combineTypeAttributes, emptyTypeAttributes } from "./TypeAttributes";
|
||||
|
||||
function getCliqueProperties(
|
||||
clique: ObjectType[],
|
||||
makePropertyType: (attributes: TypeAttributes, types: OrderedSet<Type>) => TypeRef
|
||||
makePropertyType: (types: OrderedSet<Type>) => TypeRef
|
||||
): [OrderedMap<string, ClassProperty>, TypeRef | undefined, boolean] {
|
||||
let lostTypeAttributes = false;
|
||||
let propertyNames = OrderedSet<string>();
|
||||
|
@ -57,16 +56,10 @@ function getCliqueProperties(
|
|||
}
|
||||
|
||||
const unifiedAdditionalProperties =
|
||||
additionalProperties === undefined
|
||||
? undefined
|
||||
: makePropertyType(combineTypeAttributesOfTypes(additionalProperties), additionalProperties);
|
||||
additionalProperties === undefined ? undefined : makePropertyType(additionalProperties);
|
||||
|
||||
const unifiedPropertiesArray = properties.map(([name, types, isOptional]) => {
|
||||
let attributes = combineTypeAttributesOfTypes(types);
|
||||
attributes = namesTypeAttributeKind.setDefaultInAttributes(attributes, () =>
|
||||
TypeNames.make(OrderedSet([name]), OrderedSet(), true)
|
||||
);
|
||||
return [name, new ClassProperty(makePropertyType(attributes, types), isOptional)] as [string, ClassProperty];
|
||||
return [name, new ClassProperty(makePropertyType(types), isOptional)] as [string, ClassProperty];
|
||||
});
|
||||
const unifiedProperties = OrderedMap(unifiedPropertiesArray);
|
||||
|
||||
|
@ -100,7 +93,7 @@ export class UnifyUnionBuilder extends UnionBuilder<TypeBuilder & TypeLookerUp,
|
|||
private readonly _makeEnums: boolean,
|
||||
private readonly _makeObjectTypes: boolean,
|
||||
private readonly _makeClassesFixed: boolean,
|
||||
private readonly _unifyTypes: (typesToUnify: TypeRef[], typeAttributes: TypeAttributes) => TypeRef
|
||||
private readonly _unifyTypes: (typesToUnify: TypeRef[]) => TypeRef
|
||||
) {
|
||||
super(typeBuilder);
|
||||
}
|
||||
|
@ -151,17 +144,14 @@ export class UnifyUnionBuilder extends UnionBuilder<TypeBuilder & TypeLookerUp,
|
|||
.map(o => defined(o.getAdditionalProperties()).typeRef)
|
||||
);
|
||||
const allPropertyTypes = propertyTypes.union(additionalPropertyTypes).toArray();
|
||||
const tref = this.typeBuilder.getMapType(this._unifyTypes(allPropertyTypes, emptyTypeAttributes));
|
||||
const tref = this.typeBuilder.getMapType(this._unifyTypes(allPropertyTypes));
|
||||
this.typeBuilder.addAttributes(tref, typeAttributes);
|
||||
return tref;
|
||||
} else {
|
||||
const [properties, additionalProperties, lostTypeAttributes] = getCliqueProperties(
|
||||
objectTypes,
|
||||
(names, types) => {
|
||||
const [properties, additionalProperties, lostTypeAttributes] = getCliqueProperties(objectTypes, types => {
|
||||
assert(types.size > 0, "Property has no type");
|
||||
return this._unifyTypes(types.map(t => t.typeRef).toArray(), names);
|
||||
}
|
||||
);
|
||||
return this._unifyTypes(types.map(t => t.typeRef).toArray());
|
||||
});
|
||||
if (lostTypeAttributes) {
|
||||
this.typeBuilder.setLostTypeAttributes();
|
||||
}
|
||||
|
@ -189,7 +179,7 @@ export class UnifyUnionBuilder extends UnionBuilder<TypeBuilder & TypeLookerUp,
|
|||
typeAttributes: TypeAttributes,
|
||||
forwardingRef: TypeRef | undefined
|
||||
): TypeRef {
|
||||
const ref = this.typeBuilder.getArrayType(this._unifyTypes(arrays, Map()), forwardingRef);
|
||||
const ref = this.typeBuilder.getArrayType(this._unifyTypes(arrays), forwardingRef);
|
||||
this.typeBuilder.addAttributes(ref, typeAttributes);
|
||||
return ref;
|
||||
}
|
||||
|
@ -202,10 +192,10 @@ export function unionBuilderForUnification<T extends Type>(
|
|||
makeClassesFixed: boolean,
|
||||
conflateNumbers: boolean
|
||||
): UnionBuilder<TypeBuilder & TypeLookerUp, TypeRef[], TypeRef[]> {
|
||||
return new UnifyUnionBuilder(typeBuilder, makeEnums, makeObjectTypes, makeClassesFixed, (trefs, names) =>
|
||||
return new UnifyUnionBuilder(typeBuilder, makeEnums, makeObjectTypes, makeClassesFixed, trefs =>
|
||||
unifyTypes(
|
||||
Set(trefs.map(tref => tref.deref()[0])),
|
||||
names,
|
||||
emptyTypeAttributes,
|
||||
typeBuilder,
|
||||
unionBuilderForUnification(typeBuilder, makeEnums, makeObjectTypes, makeClassesFixed, conflateNumbers),
|
||||
conflateNumbers
|
||||
|
|
Загрузка…
Ссылка в новой задаче