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

Conversation

ajgateno
Copy link
Contributor

This PR implements an AST node to represent type literals in the typescript codegen module.

Copy link

github-actions bot commented Oct 31, 2024

@ajgateno ajgateno force-pushed the alberto/ts-ast-basic-object-instantiation branch from 8c9a312 to 6f454e5 Compare October 31, 2024 21:58
@ajgateno ajgateno marked this pull request as ready for review October 31, 2024 21:58
Comment on lines 99 to 110
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

Comment on lines +39 to +44
interface Tuple {
type: "tuple";
// TODO: In theory this should be a tuple type, not an array of types
valueTypes: Type[];
values: TypeLiteral[];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the value and valueType should be a single interface so that it's impossible for the length to be inconsistent.

@ajgateno ajgateno merged commit 4c82998 into main Oct 31, 2024
51 checks passed
@ajgateno ajgateno deleted the alberto/ts-ast-basic-object-instantiation branch October 31, 2024 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants