-
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
Merged
+385
−4
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d06d94b
Implement type literal AST for TS
ajgateno 62f9833
start writing test
ajgateno 6f454e5
Add tests
ajgateno 946944d
Always assume multiline on collections and remove braces from defns
ajgateno fae1c6f
simplify iterable
ajgateno a35da48
write string with backticks to helper
ajgateno 4c9a9f0
clean up type definitions and iterable impl
ajgateno bad60ad
delete generics and make the world a better place
ajgateno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
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 | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Then I imagine the helper might be as simple as the following:
Similarly,
writeStringWithQuotes
could use the same structure: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