-
Notifications
You must be signed in to change notification settings - Fork 146
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
Conversation
8c9a312
to
6f454e5
Compare
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; |
There was a problem hiding this comment.
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('"');
}
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
interface Tuple { | ||
type: "tuple"; | ||
// TODO: In theory this should be a tuple type, not an array of types | ||
valueTypes: Type[]; | ||
values: TypeLiteral[]; | ||
} |
There was a problem hiding this comment.
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.
This PR implements an AST node to represent type literals in the typescript codegen module.