MCPcopy Index your code
hub / github.com/glideapps/quicktype / TypeGraph

Class TypeGraph

src/TypeGraph.ts:9–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7import { GraphRewriteBuilder, TypeRef, TypeBuilder, StringTypeMapping } from "./TypeBuilder";
8
9export class TypeGraph {
10 private _typeBuilder?: TypeBuilder;
11
12 // FIXME: OrderedMap? We lose the order in PureScript right now, though,
13 // and maybe even earlier in the TypeScript driver.
14 private _topLevels?: Map<string, Type> = Map();
15
16 private _types?: List<Type> = List();
17
18 constructor(typeBuilder: TypeBuilder) {
19 this._typeBuilder = typeBuilder;
20 }
21
22 private get isFrozen(): boolean {
23 return this._typeBuilder === undefined;
24 }
25
26 freeze = (topLevels: Map<string, TypeRef>, types: List<Type>): void => {
27 assert(!this.isFrozen, "Tried to freeze TypeGraph a second time");
28 assert(
29 types.every(t => t.typeRef.graph === this),
30 "Trying to freeze a graph with types that don't belong in it"
31 );
32 // The order of these three statements matters. If we set _typeBuilder
33 // to undefined before we deref the TypeRefs, then we need to set _types
34 // before, also, because the deref will call into typeAtIndex, which requires
35 // either a _typeBuilder or a _types.
36 this._types = types;
37 this._typeBuilder = undefined;
38 this._topLevels = topLevels.map(tref => tref.deref());
39 };
40
41 get topLevels(): Map<string, Type> {
42 assert(this.isFrozen, "Cannot get top-levels from a non-frozen graph");
43 return defined(this._topLevels);
44 }
45
46 typeAtIndex = (index: number): Type => {
47 if (this._typeBuilder !== undefined) {
48 return this._typeBuilder.typeAtIndex(index);
49 }
50 return defined(defined(this._types).get(index));
51 };
52
53 filterTypes<T extends Type>(
54 predicate: (t: Type) => t is T,
55 childrenOfType?: (t: Type) => Collection<any, Type>
56 ): OrderedSet<T> {
57 let seen = Set<Type>();
58 let types = List<T>();
59
60 function addFromType(t: Type): void {
61 if (seen.has(t)) return;
62 seen = seen.add(t);
63
64 const children = childrenOfType ? childrenOfType(t) : t.children;
65 children.forEach(addFromType);
66 if (predicate(t)) {

Callers

nothing calls this directly

Calls 5

filterTypesMethod · 0.95
assertFunction · 0.90
definedFunction · 0.90
separateNamedTypesFunction · 0.90
mapMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…