Get typed AST from Rescript file

I believe I want to do the reverse of How do I extract the rescript AST?

I have a Rescript file and I want to inspect the (typed) AST of a file.
How can I do this? I don’t really know any ocaml, so if I can just dump something to json that would be ideal. But if that is not possible, some ocaml pointers would do as well.

Getting the typed AST isn’t possible without OCaml, but there are some loose plans to expose a subset of the relevant parts of the typed AST as JSON via the @rescript/tools package.

A hack, but what you can try today is running the doc command from @rescript/tools on your file: rescript-vscode/tools/README.md at master · rescript-lang/rescript-vscode · GitHub

That will dump out JSON using the typed AST for that file. It’s intended for driving doc generation, but you might find what you need in there already.

Thanks! That would be something like npx rescript-tools doc src/CreateOrder.res right?
This isn’t doing anything right now, so I probably missing something?

I installed sudo apt install musl and this started working.
That json does (more or less) contain what I need.
Thanks a bunch @zth!

Related shower thought: maybe producing some web assembly binary (assuming that is easy in OCAML) that exposes this could be an interesting route to explore.

Which part, for dumping out info?

Btw, did you manage to do anything with the docs json?

Btw: how does the decoder output look like (RescriptTools.Docgen.decodeFromJson)?

There’s no decoder, it’s just an identity function. The full structure can be modelled using v11 variants (which is an achievement in itself!).

1 Like

Oh okay. Didn’t look into the code. Just saw the last code block in the README.

2 Likes

Let’s see if I can find some time to get an initial experimental version out where we can dump typed info to JSON, and we can build on it from there. Easy access to a structured way of working with type info in not just OCaml can hopefully enable some cool innovation.

2 Likes

Yes, for getting AST information.
I was able to solve my use case with the JSON documentation dump.
I needed the exported function names and their input and return type, which I was able to find in the JSON.

Couldn’t we generate a json schema of rescript AST? Then one could use pretty much any tool to work on it, including rescript using rescript-json-schema for example.

There is btw some more interest in this from the compiler discussions: How can I get AST/CST of a rescript file in JSON format? · rescript-lang/rescript-compiler · Discussion #6622 · GitHub