Extracting an API outline from an Elm file

when I’m designing APIs in Elm, I like to start with just the types—this is pretty common advice around these parts! But when I’m refactoring stuff, or talking about a design I want to be able to use the same technique to guide my thinking.

So, here’s a shell script I use to get a rough idea of how a module is structured:

elm_outline() {
    grep -E '^(type|--|\w+ : .+)' $MODULE

a couple caveats: this will grab all top-level comments (so section headers like -- FOO work fine) but no doc comments or type constructors and you will have to remove non-exposed variables manually. Despite that, I’ve found this useful for a while, so I wanted to share it here!

You can see an example of the output in Interesting query pattern for repeatedly bisecting lists.


Nice idea. At the moment I am writing a pretty printer for Elm code that has been parsed by stil4m/elm-syntax. Once that is working it would be easy to get a bit more sophisticated with this. For example, only print out stuff that is exposed, or print type constructors only when exposed, or cope better with function signatures that are split over multiple lines and so on.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.