MCPcopy Create free account
hub / github.com/glideapps/quicktype / combineClasses

Function combineClasses

src/CombineClasses.ts:68–154  ·  view source on GitHub ↗
(graph: TypeGraph, stringTypeMapping: StringTypeMapping)

Source from the content-addressed store, hash-verified

66}
67
68export 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,

Callers 1

RunClass · 0.90

Calls 2

isPartOfCliqueFunction · 0.85
rewriteMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…