Announcing `tidy-codegen`

Hey, everyone! I’d like to announce the initial release of tidy-codegen. tidy-codegen makes it super easy to construct PureScript syntax and print it using the tidy formatter.

The key design space of this library is that it lets you construct the types from purescript-language-cst-parser directly (just without whitespace annotations). This means you can parse external code and embed it directly! In fact, as part of our snapshot testing, we parse Tidy.Codegen, pull out all the example code, parse that, and codegenerate a module which then codegenerates a module from those examples (see here and here). A codegenerator codegenerator :confounded:. So meta!

Additionally, tidy-codegen exposes a CodegenT transformer, which tracks imports and exports so you don’t have to. Just use importFrom wherever you need to, and all your imports will be tracked, deduplicated, and sorted.

Hope y’all find it useful!


@natefaubion Do you use it for solving real-life practical use cases? Just interesting to hear about it.

I wrote this to address common codegen issues I’ve had at work with our existing codegen scheme. We codegen a lot of API related stuff, and it can get brittle. I wanted something that would not only let me generate and format PureScript without any hassles, but let me do so in a modular way that doesn’t result in import warnings :smiley:, which is why I also wrote the Tidy.Codegen.Monad module.


Other practical use cases include syntactic tooling. Being able to easily parse existing code, and generate stuff based on it could be really nice basis for refactoring, or boilerplate generators like lenses/prisms.


As always, work of genius)


Very, very cool.

Boilerplate generator for lenses sounds like an amazing project for someone to do…i have to think that a lens analogue to, for ex. RegExr: Learn, Build, & Test RegEx would be useful by beginners and experts alike.

1 Like