From bad60ad513fc6775b981985b753570ecb1b0da91 Mon Sep 17 00:00:00 2001 From: ajgateno Date: Thu, 31 Oct 2024 19:40:53 -0400 Subject: [PATCH] delete generics and make the world a better place --- .../typescript/codegen/src/ast/TypeLiteral.ts | 96 +++++-------------- 1 file changed, 24 insertions(+), 72 deletions(-) diff --git a/generators/typescript/codegen/src/ast/TypeLiteral.ts b/generators/typescript/codegen/src/ast/TypeLiteral.ts index 6850603a1c0..8406c9360d8 100644 --- a/generators/typescript/codegen/src/ast/TypeLiteral.ts +++ b/generators/typescript/codegen/src/ast/TypeLiteral.ts @@ -43,10 +43,6 @@ interface Tuple { values: TypeLiteral[]; } -interface IterableLiteral { - values: TypeLiteral[]; -} - export class TypeLiteral extends AstNode { private constructor(public readonly internalType: InternalTypeLiteral) { super(); @@ -55,7 +51,7 @@ export class TypeLiteral extends AstNode { public write(writer: Writer): void { switch (this.internalType.type) { case "array": { - this.writeArray({ writer, array: this.internalType }); + this.writeIterable({ writer, iterable: this.internalType }); break; } case "boolean": { @@ -80,7 +76,7 @@ export class TypeLiteral extends AstNode { break; } case "tuple": { - this.writeTuple({ writer, tuple: this.internalType }); + this.writeIterable({ writer, iterable: this.internalType }); break; } default: { @@ -99,80 +95,36 @@ export class TypeLiteral extends AstNode { writer.write("`"); } - private writeArray({ writer, array }: { writer: Writer; array: Array_ }): void { - this.writeIterable({ - writer, - iterable: array, - leftBrace: "[", - rightBrace: "]", - writeField: (value: TypeLiteral) => value.write(writer) - }); - } - - private writeObject({ writer, object }: { writer: Writer; object: Object_ }): void { - const values: TypeLiteral[] = []; - const valuesToNames = new Map(); - for (const field of object.fields) { - values.push(field.value); - valuesToNames.set(field.value, field.name); - } - const iterable = { values }; - this.writeIterable({ - writer, - iterable, - leftBrace: "{", - rightBrace: "}", - writeField: (value: TypeLiteral) => { - const name = valuesToNames.get(value); - if (name != null) { - writer.write(`${name}: `); - value.write(writer); - } else { - throw Error( - `BUG: Could not find name for field value ${JSON.stringify(value)} in ${JSON.stringify( - object - )}.` - ); - } + private writeIterable({ writer, iterable }: { writer: Writer; iterable: Array_ | Tuple }): void { + if (iterable.values.length === 0) { + // Don't allow "multiline" empty iterables. + writer.write("[]"); + } else { + writer.writeLine("["); + writer.indent(); + for (const value of iterable.values) { + value.write(writer); + writer.writeLine(","); } - }); - } - - private writeTuple({ writer, tuple }: { writer: Writer; tuple: Tuple }): void { - this.writeIterable({ - writer, - iterable: tuple, - leftBrace: "[", - rightBrace: "]", - writeField: (value: TypeLiteral) => value.write(writer) - }); + writer.dedent(); + writer.write("]"); + } } - private writeIterable({ - writer, - iterable, - leftBrace, - rightBrace, - writeField - }: { - writer: Writer; - iterable: IterableLiteral; - leftBrace: string; - rightBrace: string; - writeField: (value: TypeLiteral) => void; - }): void { - if (iterable.values.length === 0) { - // Don't allow "multiline" empty collections. - writer.write(`${leftBrace}${rightBrace}`); + private writeObject({ writer, object }: { writer: Writer; object: Object_ }): void { + if (object.fields.length === 0) { + // Don't allow "multiline" empty objects. + writer.write("{}"); } else { - writer.writeLine(`${leftBrace}`); + writer.writeLine("{"); writer.indent(); - for (const value of iterable.values) { - writeField(value); + for (const field of object.fields) { + writer.write(`${field.name}: `); + field.value.write(writer); writer.writeLine(","); } writer.dedent(); - writer.write(`${rightBrace}`); + writer.write("}"); } }