Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): Implement type literal AST for TS #5057

Merged
merged 8 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions generators/commons/src/ast/AbstractWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export class AbstractWriter {
this.writeInternal(indentedText);
}

/**
* Writes arbitrary text without indentation
* @param text
*/
public writeNoIndent(text: string): void {
this.writeInternal(text);
}

/**
* Writes a node
* @param node
Expand Down
3 changes: 2 additions & 1 deletion generators/typescript/codegen/src/ast/CodeBlock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CodeBlock as CommonCodeBlock } from "@fern-api/generator-commons";
import { AstNode, Writer } from "../typescript";
import { AstNode } from "./core/AstNode";
import { Writer } from "./core/Writer";

export declare namespace CodeBlock {
/* Write arbitrary code */
Expand Down
255 changes: 255 additions & 0 deletions generators/typescript/codegen/src/ast/TypeLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import { assertNever } from "@fern-api/core-utils";
import { AstNode, Writer } from "./core";
import { Type } from "./Type";

type InternalTypeLiteral = Array_ | Boolean_ | Number_ | Object_ | String_ | Tuple;

interface Array_ {
type: "array";
valueType: Type;
fields: ArrayField[];
multiline: boolean;
leftBrace: "[";
rightBrace: "]";
}

interface Boolean_ {
type: "boolean";
value: boolean;
}

interface Number_ {
type: "number";
value: number;
}

interface Object_ {
type: "object";
fields: ObjectField[];
multiline: boolean;
leftBrace: "{";
rightBrace: "}";
}

interface String_ {
type: "string";
value: string;
}

interface Tuple {
type: "tuple";
fields: TupleField[];
multiline: boolean;
leftBrace: "[";
rightBrace: "]";
}

interface IterableLiteral<T extends IterableLiteralField> {
fields: T[];
multiline?: boolean;
leftBrace: string;
rightBrace: string;
}

type IterableLiteralField = ArrayField | ObjectField | TupleField;

interface ArrayField {
type: "arrayField";
value: TypeLiteral;
}

interface ObjectField {
type: "objectField";
name: string;
valueType: Type;
value: TypeLiteral;
}

interface TupleField {
type: "tupleField";
valueType: Type;
value: TypeLiteral;
}

export class TypeLiteral extends AstNode {
private constructor(public readonly internalType: InternalTypeLiteral) {
super();
}

public write(writer: Writer): void {
switch (this.internalType.type) {
case "array": {
this.writeIterable(writer, this.internalType);
break;
}
case "boolean": {
writer.write(this.internalType.value.toString());
break;
}
case "number": {
// N.B. Defaults to decimal; further work needed to support alternatives like hex, binary, octal, etc.
writer.write(this.internalType.value.toString());
break;
}
case "object": {
this.writeIterable(writer, this.internalType);
break;
}
case "string": {
if (this.internalType.value.includes("\n")) {
writer.write("`");
const parts = this.internalType.value.split("\n");
const head = parts[0] + "\n";
const tail = parts.slice(1).join("\n");
writer.write(head.replaceAll("`", "\\`"));
writer.writeNoIndent(tail.replaceAll("`", "\\`"));
writer.write("`");
} else {
writer.write(`"${this.internalType.value.replaceAll('"', '\\"')}"`);
}
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - for readability, what if we made a couple helper functions for this? I imagine something like:

if (this.internalType.value.includes("\n") {
  this.writeStringWithBackticks(this.internalType.value);
  break;
}
this.writeStringWithQuotes(this.internalType.value);
break;

Then I imagine the helper might be as simple as the following:

function writeStringWithBackticks({writer, value} : {writer: Writer; value: string; }) {
  writer.write("`");
  writer.writeNoIndent(this.internalType.value.replaceAll("`", "\\`"));
  writer.write("`");
}

Similarly, writeStringWithQuotes could use the same structure:

function writeStringWithBackticks({writer, value} : {writer: Writer; value: string; }) {
  writer.write('"');
  writer.write(this.internalType.value.replaceAll('"', '\\"'));
  writer.write('"');
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline - this actually handles multi-line strings better than I expected and we should do something similar elsewhere. Nicely done!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
case "tuple": {
this.writeIterable(writer, this.internalType);
break;
}
default: {
assertNever(this.internalType);
}
}
}

private writeIterable(writer: Writer, value: IterableLiteral<IterableLiteralField>) {
if (value.fields.length === 0) {
// Don't allow "multiline" empty collections.
writer.write(`${value.leftBrace}${value.rightBrace}`);
} else if (value.multiline ?? false) {
writer.writeLine(`${value.leftBrace}`);
writer.indent();
for (const elem of value.fields) {
this.writeIterableField(writer, elem);
writer.writeLine(",");
}
writer.dedent();
writer.write(`${value.rightBrace}`);
} else {
writer.write(`${value.leftBrace}`);
const init = value.fields.slice(0, -1);
const last = value.fields[value.fields.length - 1];
for (const elem of init) {
this.writeIterableField(writer, elem);
writer.write(", ");
}
// Need for eslint; last cannot be null because of the first if
if (last != null) {
this.writeIterableField(writer, last);
}
writer.write(`${value.rightBrace}`);
}
}

private writeIterableField(writer: Writer, value: IterableLiteralField) {
switch (value.type) {
case "objectField": {
writer.write(`${value.name}: `);
value.value.write(writer);
break;
}
case "arrayField":
case "tupleField": {
value.value.write(writer);
break;
}
default: {
assertNever(value);
}
}
}

/* Static factory methods for creating a TypeLiteral */
public static array({
valueType,
fields,
multiline
}: {
valueType: Type;
fields: ArrayField[];
multiline?: boolean;
}): TypeLiteral {
return new this({
type: "array",
valueType,
fields,
multiline: multiline ?? false,
leftBrace: "[",
rightBrace: "]"
});
}

public static arrayField(value: TypeLiteral): ArrayField {
return {
type: "arrayField",
value
};
}

public static boolean(value: boolean): TypeLiteral {
return new this({ type: "boolean", value });
}

public static number(value: number): TypeLiteral {
return new this({ type: "number", value });
}

public static object({ fields, multiline }: { fields: ObjectField[]; multiline?: boolean }): TypeLiteral {
return new this({
type: "object",
fields,
multiline: multiline ?? false,
leftBrace: "{",
rightBrace: "}"
});
}

public static objectField({
name,
valueType,
value
}: {
name: string;
valueType: Type;
value: TypeLiteral;
}): ObjectField {
return {
type: "objectField",
name,
valueType,
value
};
}

public static string(value: string): TypeLiteral {
return new this({
type: "string",
value
});
}

public static tuple({ fields, multiline }: { fields: TupleField[]; multiline?: boolean }): TypeLiteral {
return new this({
type: "tuple",
fields,
multiline: multiline ?? false,
leftBrace: "[",
rightBrace: "]"
});
}

public static tupleField({ valueType, value }: { valueType: Type; value: TypeLiteral }): TupleField {
return {
type: "tupleField",
valueType,
value
};
}
}
Loading
Loading