(graph: TypeGraph, stringTypeMapping: StringTypeMapping)
| 66 | } |
| 67 | |
| 68 | export function combineClasses(graph: TypeGraph, stringTypeMapping: StringTypeMapping): TypeGraph { |
| 69 | let unprocessedClasses = graph |
| 70 | .allNamedTypesSeparated() |
| 71 | .classes.filter(c => !c.isFixed) |
| 72 | .toArray(); |
| 73 | const cliques: ClassType[][] = []; |
| 74 | |
| 75 | // FIXME: Don't build cliques one by one. Instead have a list of |
| 76 | // cliques-in-progress and iterate over all classes. Add the class |
| 77 | // to the first clique that it's part of. If there's none, make it |
| 78 | // into a new clique. |
| 79 | while (unprocessedClasses.length > 0) { |
| 80 | const classesLeft: ClassType[] = []; |
| 81 | const clique = [unprocessedClasses[0]]; |
| 82 | |
| 83 | for (let i = 1; i < unprocessedClasses.length; i++) { |
| 84 | const c = unprocessedClasses[i]; |
| 85 | if (isPartOfClique(c, clique)) { |
| 86 | clique.push(c); |
| 87 | } else { |
| 88 | classesLeft.push(c); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (clique.length > 1) { |
| 93 | cliques.push(clique); |
| 94 | } |
| 95 | |
| 96 | unprocessedClasses = classesLeft; |
| 97 | } |
| 98 | |
| 99 | function makeCliqueClass(clique: Set<ClassType>, builder: GraphRewriteBuilder<ClassType>): TypeRef { |
| 100 | assert(clique.size > 0, "Clique can't be empty"); |
| 101 | let inferredNames = OrderedSet<string>(); |
| 102 | let givenNames = OrderedSet<string>(); |
| 103 | clique.forEach(c => { |
| 104 | if (c.areNamesInferred) { |
| 105 | inferredNames = inferredNames.union(c.names); |
| 106 | } else { |
| 107 | givenNames = givenNames.union(c.names); |
| 108 | } |
| 109 | }); |
| 110 | const areNamesInferred = givenNames.isEmpty(); |
| 111 | const properties = getCliqueProperties(clique, builder); |
| 112 | return builder.getClassType(areNamesInferred ? inferredNames : givenNames, areNamesInferred, properties); |
| 113 | } |
| 114 | |
| 115 | function getCliqueProperties( |
| 116 | clique: Set<ClassType>, |
| 117 | builder: GraphRewriteBuilder<ClassType> |
| 118 | ): OrderedMap<string, TypeRef> { |
| 119 | let properties = OrderedMap<string, [Type, number, boolean]>(); |
| 120 | clique.forEach(c => { |
| 121 | c.properties.forEach((t, name) => { |
| 122 | const p = properties.get(name); |
| 123 | if (p) { |
| 124 | p[1] += 1; |
| 125 | // If one of the clique class's properties is nullable, |
no test coverage detected
searching dependent graphs…