-
Notifications
You must be signed in to change notification settings - Fork 8
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
TypeScript AST #40
TypeScript AST #40
Conversation
Sets up a struct-based TypeScript ast, loosely inspired by the go/ast package. Notable: - no position information, as we only do rendering, not parsing - rendering is handled using String() functions, not a separate printer package
Makes the generator "use" the ast by returning a []ast.Node instead of writing into a bytes.Buffer. Those ast.Nodes however are of type ast.Raw, so the ast itself is not really used yet.
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.
O.M.G. This is just wonderful. I'm so delighted to see this!
Tests are passing, and as long as that's the case, i'm happy to merge whenever. What more do you feel ought to be done before merging? And, for anything we defer, i'd love to see brief follow-up issues posted, so that we don't lose track of the TODOs.
return []byte("\n" + file.String()), nil | ||
} | ||
|
||
func GenerateAST(val cue.Value, c Config) (*ts.File, error) { |
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.
One thing i like about having GenerateAST()
with a return type of ts.File
is the opportunity it affords the caller to more easily mess around with the final output - e.g., how Grafana has to, for now at least, inject its own import statement(s).
To that end - unless i missed it? - i'd love to see AST types for representing TS imports.
@@ -21,8 +21,7 @@ type {{.name}} = {{ Join .tokens " | "}}; | |||
{{- if .default }} | |||
|
|||
{{if .export}}export {{end -}} | |||
const default{{.name}}: {{.name}} = {{.default}};{{end}} | |||
`) | |||
const default{{.name}}: {{.name}} = {{.default}};{{end}}`) |
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 hate templates, lolsob
return fmt.Sprintf("%s %s: %s = %s;", tok, v.Name, v.Type, v.Value) | ||
} | ||
|
||
type Type interface { |
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.
My brain is stuttering over all the use of Type
. Can we use Kind
instead, particularly for consistency with the attribute names?
Oh also, just to note - there's another half of the story here, perhaps harder than this, where we create some intermediate structures to represent our analysis of the CUE inputs rather than just working with raw But that can, and should, proceed orthogonally to this. Just making the note :) |
@sh0rez nudge to create those follow-up issues |
Introduces a (printing only) TypeScript AST and refactors the generator to use it:
ts/ast
: structs withString()
methods for printing what they representts
: higher level package, things likeUnion
for generating a union type. Not heavily used yet.go/ast
internals