Recently at ClojureD, Josef introduced me to a tool which quickly rose to fame (in the JavaScript world): prettier. It is an opinionated code formatter for various languages - this paradigm probably got popular ever since Go started to ship such a formatter in the core tooling. I knew the paradigm and prettier itself from blog posts, but didn't really try to use it so far.

Generally speaking I liked the idea of having my code automatically formatted in Emacs whilst adhering to configured linters such as eslint. However, from the docs it was much too complicated to use. More importantly, though, the documentation asks to configure it through files which are usually checked in (like package.json or .eslintrc.json). For new or our own projects, this is fine. However, for customer projects, I don't want to impose new tools. Additionally, it is a whole lot of repetitive work to set up such tooling for every single project which kind of defeats it's ulterior motive. In these days, who doesn't work on a couple dozen code bases in parallel?(;

Therefore, I did what every self-respecting engineer would do in this scenario: I wrote a little Elisp wrapper. This wrapper implements an interactive function autoformat which is a thin wrapper around command-line based code autoformatters which it utilizes through a strategy pattern. At this moment, the tools prettier and prettier-eslint-cli are implemented. With those, autoformatting a wide variety of languages/formats used on the web is possible. To add a new language/framework, the only change is to add such a command-line tool into a separate strategy function. If the new language/framework has a command-line tool which takes code into stdin and formats it to stdout, this is trivial to do.

You can find the documented code in my Emacs repository:

If you like it, feel free to fork the repository, make pull requests or just give it a star^^

Happy hacking!

  • Emacs
  • programming
  • javascript
  • elisp
  • eslint
  • prettier