quicktype-core 23.3.22 → 23.3.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,16 +11,67 @@ const TypeUtils_1 = require("../Type/TypeUtils");
11
11
  const UnifyClasses_1 = require("../UnifyClasses");
12
12
  function flattenUnions(graph, stringTypeMapping, conflateNumbers, makeObjectTypes, debugPrintReconstitution) {
13
13
  let needsRepeat = false;
14
+ // During flattening, recursive unions can appear again while their replacement
15
+ // is still being built. For example, flattening `A | M` can later ask for
16
+ // `A | (A | M)` through an array item or object property. Those are the same
17
+ // union by associativity/idempotence, so remember each in-progress flattened
18
+ // union by a normalized key of its non-union member refs. Looking up that key
19
+ // lets recursive occurrences reuse the replacement's forwarding ref instead
20
+ // of allocating another finite unrolling of the same cycle.
21
+ const unionKeyToRef = new Map();
22
+ function addUnionKeyAtoms(t, atoms, seen) {
23
+ const index = t.index;
24
+ if (seen.has(index))
25
+ return;
26
+ seen.add(index);
27
+ if (t instanceof Type_1.UnionType) {
28
+ for (const m of t.members) {
29
+ addUnionKeyAtoms(m, atoms, seen);
30
+ }
31
+ }
32
+ else {
33
+ atoms.add((0, TypeRef_1.typeRefIndex)(t.typeRef));
34
+ }
35
+ }
36
+ function unionKeyForTypes(types) {
37
+ const atoms = new Set();
38
+ const seen = new Set();
39
+ for (const t of types) {
40
+ addUnionKeyAtoms(t, atoms, seen);
41
+ }
42
+ return Array.from(atoms)
43
+ .sort((a, b) => a - b)
44
+ .join(",");
45
+ }
14
46
  function replace(types, builder, forwardingRef) {
15
- const unionBuilder = new UnifyClasses_1.UnifyUnionBuilder(builder, makeObjectTypes, true, (trefs) => {
47
+ unionKeyToRef.set(unionKeyForTypes(types), forwardingRef);
48
+ let unionBuilder;
49
+ const unifyTypeRefs = (trefs) => {
16
50
  (0, Support_1.assert)(trefs.length > 0, "Must have at least one type to build union");
17
- trefs = trefs.map((tref) => builder.reconstituteType((0, TypeRef_1.derefTypeRef)(tref, graph)));
18
- if (trefs.length === 1) {
19
- return trefs[0];
51
+ const maybeReconstituted = builder.lookupTypeRefs(trefs, undefined, false);
52
+ if (maybeReconstituted !== undefined) {
53
+ return maybeReconstituted;
54
+ }
55
+ const typesToUnify = new Set(trefs.map((tref) => (0, TypeRef_1.derefTypeRef)(tref, graph)));
56
+ if ((0, collection_utils_1.iterableSome)(typesToUnify, (t) => t instanceof Type_1.IntersectionType)) {
57
+ trefs = trefs.map((tref) => builder.reconstituteType((0, TypeRef_1.derefTypeRef)(tref, graph)));
58
+ if (trefs.length === 1) {
59
+ return trefs[0];
60
+ }
61
+ needsRepeat = true;
62
+ return builder.getUnionType(TypeAttributes_1.emptyTypeAttributes, new Set(trefs));
63
+ }
64
+ const key = unionKeyForTypes(typesToUnify);
65
+ const maybeRef = unionKeyToRef.get(key);
66
+ if (maybeRef !== undefined) {
67
+ return maybeRef;
20
68
  }
21
- needsRepeat = true;
22
- return builder.getUnionType(TypeAttributes_1.emptyTypeAttributes, new Set(trefs));
23
- });
69
+ return builder.withForwardingRef(undefined, (nestedForwardingRef) => {
70
+ unionKeyToRef.set(key, nestedForwardingRef);
71
+ return (0, UnifyClasses_1.unifyTypes)(typesToUnify, TypeAttributes_1.emptyTypeAttributes, builder, unionBuilder, conflateNumbers, nestedForwardingRef);
72
+ });
73
+ };
74
+ unionBuilder = new UnifyClasses_1.UnifyUnionBuilder(builder, makeObjectTypes, true, unifyTypeRefs);
24
75
  return (0, UnifyClasses_1.unifyTypes)(types, TypeAttributes_1.emptyTypeAttributes, builder, unionBuilder, conflateNumbers, forwardingRef);
25
76
  }
26
77
  const allUnions = (0, collection_utils_1.setFilter)(graph.allTypesUnordered(), (t) => t instanceof Type_1.UnionType);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quicktype-core",
3
- "version": "23.3.22",
3
+ "version": "23.3.23",
4
4
  "description": "The quicktype engine as a library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",