| 33 | } |
| 34 | |
| 35 | class InferenceUnionBuilder extends UnionBuilder<NestedValueArray, NestedValueArray, any> { |
| 36 | private _numValues?: number; |
| 37 | |
| 38 | constructor( |
| 39 | typeBuilder: TypeGraphBuilder, |
| 40 | typeName: string, |
| 41 | isInferred: boolean, |
| 42 | private readonly _typeInference: TypeInference, |
| 43 | private readonly _cjson: CompressedJSON |
| 44 | ) { |
| 45 | super(typeBuilder, typeName, isInferred); |
| 46 | } |
| 47 | |
| 48 | setNumValues = (n: number): void => { |
| 49 | if (this._numValues !== undefined) { |
| 50 | return panic("Can only set number of values once"); |
| 51 | } |
| 52 | this._numValues = n; |
| 53 | }; |
| 54 | |
| 55 | protected makeEnum(enumCases: string[]): TypeRef | null { |
| 56 | assert(enumCases.length > 0); |
| 57 | const numValues = defined(this._numValues); |
| 58 | if (numValues >= MIN_LENGTH_FOR_ENUM && enumCases.length < Math.sqrt(numValues)) { |
| 59 | return this.typeBuilder.getEnumType(this.typeName, true, OrderedSet(enumCases)); |
| 60 | } |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | protected makeClass(classes: NestedValueArray, maps: any[]): TypeRef { |
| 65 | assert(maps.length === 0); |
| 66 | return this._typeInference.inferClassType(this._cjson, this.typeName, this.isInferred, classes); |
| 67 | } |
| 68 | |
| 69 | protected makeArray(arrays: NestedValueArray): TypeRef { |
| 70 | return this.typeBuilder.getArrayType( |
| 71 | this._typeInference.inferType(this._cjson, pluralize.singular(this.typeName), this.isInferred, arrays) |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | function canBeEnumCase(s: string): boolean { |
| 77 | if (s.length === 0) return true; // FIXME: Do we really want this? |
nothing calls this directly
no test coverage detected
searching dependent graphs…