Skip to content

Commit 5bbfc5a

Browse files
assign TypeGraph to TypeBuilder after init
1 parent 215f094 commit 5bbfc5a

4 files changed

Lines changed: 20 additions & 13 deletions

File tree

packages/quicktype-core/src/GraphRewriting.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { EqualityMap, mapMap } from "collection-utils";
22

33
import { type TypeAttributes, combineTypeAttributes, emptyTypeAttributes } from "./attributes/TypeAttributes";
44
import { assert, indentationString, panic } from "./support/Support";
5-
import { type ClassProperty, type MaybeTypeIdentity, type PrimitiveTypeKind, type Type, TypeBuilder } from "./Type";
5+
import { type ClassProperty, type MaybeTypeIdentity, type PrimitiveTypeKind, type Type } from "./Type";
6+
import { TypeBuilder } from "./Type/TypeBuilder";
67
import { type StringTypeMapping } from "./Type/TypeBuilderUtils";
78
import { type TypeGraph } from "./Type/TypeGraph";
89
import {
@@ -228,7 +229,6 @@ export abstract class BaseGraphRewriteBuilder extends TypeBuilder implements Typ
228229
protected readonly debugPrint: boolean
229230
) {
230231
super(
231-
originalGraph.serial + 1,
232232
stringTypeMapping,
233233
alphabetizeProperties,
234234
false,

packages/quicktype-core/src/Run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { type MultiFileRenderResult, type TargetLanguage } from "./TargetLanguag
2222
import { type TransformedStringTypeKind } from "./Type";
2323
import { TypeBuilder } from "./Type/TypeBuilder";
2424
import { type StringTypeMapping } from "./Type/TypeBuilderUtils";
25-
import { type TypeGraph } from "./Type/TypeGraph";
25+
import { TypeGraph } from "./Type/TypeGraph";
2626
import { noneToAny, optionalToNullable, removeIndirectionIntersections } from "./Type/TypeGraphUtils";
2727
import { type FixMeOptionsType } from "./types";
2828

@@ -291,13 +291,13 @@ class Run implements RunContext {
291291
const stringTypeMapping = this.stringTypeMapping;
292292
const conflateNumbers = !targetLanguage.supportsUnionsWithBothNumberTypes;
293293
const typeBuilder = new TypeBuilder(
294-
0,
295294
stringTypeMapping,
296295
this._options.alphabetizeProperties,
297296
this._options.allPropertiesOptional,
298297
this._options.checkProvenance,
299298
false
300299
);
300+
typeBuilder.typeGraph = new TypeGraph(typeBuilder, 0, this._options.checkProvenance);
301301

302302
return { targetLanguage, stringTypeMapping, conflateNumbers, typeBuilder };
303303
}

packages/quicktype-core/src/Type/TypeBuilder.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import {
1111
} from "collection-utils";
1212

1313
import { StringTypes, stringTypesTypeAttributeKind } from "../attributes/StringTypes";
14-
import {
15-
type TypeAttributes,
16-
combineTypeAttributes,
17-
emptyTypeAttributes
18-
} from "../attributes/TypeAttributes";
14+
import { type TypeAttributes, combineTypeAttributes, emptyTypeAttributes } from "../attributes/TypeAttributes";
1915
import { assert, defined, panic } from "../support/Support";
2016

2117
import { provenanceTypeAttributeKind } from "./ProvenanceTypeAttributeKind";
@@ -42,11 +38,12 @@ import {
4238
unionTypeIdentity
4339
} from "./Type";
4440
import { type StringTypeMapping, stringTypeMappingGet } from "./TypeBuilderUtils";
45-
import { TypeGraph } from "./TypeGraph";
41+
import { type TypeGraph } from "./TypeGraph";
4642
import { type TypeRef, assertTypeRefGraph, derefTypeRef, makeTypeRef, typeRefIndex } from "./TypeRef";
4743

4844
export class TypeBuilder {
49-
public readonly typeGraph: TypeGraph;
45+
// @ts-expect-error must manually set TypeGraph
46+
private _typeGraph: TypeGraph;
5047

5148
protected readonly topLevels: Map<string, TypeRef> = new Map();
5249

@@ -57,7 +54,6 @@ export class TypeBuilder {
5754
private _addedForwardingIntersection = false;
5855

5956
public constructor(
60-
typeGraphSerial: number,
6157
private readonly _stringTypeMapping: StringTypeMapping,
6258
public readonly canonicalOrder: boolean,
6359
private readonly _allPropertiesOptional: boolean,
@@ -68,7 +64,16 @@ export class TypeBuilder {
6864
!_addProvenanceAttributes || !inheritsProvenanceAttributes,
6965
"We can't both inherit as well as add provenance"
7066
);
71-
this.typeGraph = new TypeGraph(this, typeGraphSerial, _addProvenanceAttributes || inheritsProvenanceAttributes);
67+
}
68+
69+
public get typeGraph(): TypeGraph {
70+
assert(!!this._typeGraph, "TypeBuilder must have a TypeGraph");
71+
return this._typeGraph;
72+
}
73+
74+
/** typeGraph must be set externally to prevent import cycle of TypeGraph constructor */
75+
public set typeGraph(typeGraph: TypeGraph) {
76+
this._typeGraph = typeGraph;
7277
}
7378

7479
public addTopLevel(name: string, tref: TypeRef): void {

packages/quicktype-core/src/Type/TypeGraph.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export class TypeGraph {
295295
debugPrintReconstitution,
296296
replacer
297297
);
298+
builder.typeGraph = new TypeGraph(builder, this.serial + 1, this._haveProvenanceAttributes);
298299
const newGraph = builder.finish();
299300

300301
this.checkLostTypeAttributes(builder, newGraph);
@@ -333,6 +334,7 @@ export class TypeGraph {
333334
map,
334335
debugPrintRemapping
335336
);
337+
builder.typeGraph = new TypeGraph(builder, this.serial + 1, this._haveProvenanceAttributes);
336338
const newGraph = builder.finish();
337339

338340
this.checkLostTypeAttributes(builder, newGraph);

0 commit comments

Comments
 (0)