Continuous Integration with Source Code Format Validation for Node.js Using jsfmt


Recently I started exploring Go and Node.js on different projects. Soon I found out that there are some things which I really like about Go, but are missing on Node.js, such as the gofmt tool. Gofmt is a tool that automatically formats Go source code. The nice thing about it is that endless discussions among your team members on how the source code should be formatted are more or less a thing of the past. However, such discussions among JavaScript (JS) developers are not uncommon and can take hours, which is very counterproductive. This is especially painful when people start reformatting other people’s source code, because then they are messing up the git history and git blame becomes useless. IMHO people in one team should agree on some code styling rules and follow them. I as a Java developer am used to this, because in almost every big Java project on which I have worked uses checkstyle and incorporates this into the Continuous Integration (CI) build, thus if you are not following the checkstyle rules your build will not pass the CI. Problem solved.

So I started looking for similar tools for Node.js and I found jsfmt, which is beautifier/formatter for JS, but it can also validate JS code formatting. Great, exactly what I was looking for. The problem is that jsfmt works on individual files, so I wrote a simple bash script called checkfmt-js.sh which will check the format of all *.js and *.json files in your Node.js project:

This shell script only validates your JS source code, it does not change it. It also skips all automatically downloaded packages in the node_modules sub-folder.

Note that if you are not happy with the presets of jsfmt you can overwrite them via a .jsfmtrc file.

CI Integration

With the help of the checkfmt-js.sh script we can integrate source code format validations as part of our CI build, so that the build will fail if the developer submits a malformated JS/JSON file. Here is a sample configuration for Travis CI or Vexor CI for accomplishing this task:

Auto Format

Of course you also want to format your files with jsfmt, so that your CI build does not fail all the time. To do this with jsfmt on a single JS file use:

jsfmt -w my-source-file.js

For a JSON file make sure to add the -j flag too.

If you are using the Atom editor you can install the atom-jsfmt plugin, which will auto-format your JS code every time you save, so you don’t have to think about it.

apm install atom-jsfmt

Alternatively, you can use the following fmt-js.sh script to format of all *.js and *.json files in your Node.js project:

Alternatives

As an alternative to jsfmt one can also use JSCS — JavaScript Code Style, which is a code style linter for programmatically enforcing your style guide. It can also auto-format your JS files. Jscs has more features and configuration options than jsfmt. However, I like the simplicity of jsfmt and the fact that it is very similar to gofmt, which makes the tool switching between Go and Node.js a bit easier.

Conclusion

In conclusion, I believe that imposing the same code format for all developers is a good idea. It makes the source version control history more meaningful. In addition, it stops the pointless source code style wars, and good programmers can focus more on what really matters - programming. Therefore source code formatting validation should be part of your CI build.