Friday, 6 May 2011

Filtering JSLint errors via JSLint4Java

JavaScript is a nice language. However, as it's parsed line-by-line at runtime by the browser, there is the chance of unnoticed syntax errors creeping into production. This has happened to us a few times, and we have decided to take preventative measures to stop this happening, using the excellent JSLint tool.

We use a continuous integration system to automate building all our projects, ensuring all our unit tests pass without introducing regressions and that every commit to our code repository triggers a re-build of the project, and thus a run of the unit tests.

However, running JSLint through the web interface for all our JS files is a pain. Enter JSLint4Java. This library wraps JSLint with a Java API and allows JSLint to programmatically be called against many JavaScript files. This can be done from the command line, or as an Ant task.

We added the JSLint4Java Ant task as a build step to some projects, only to find that JSLint complained too much about things we didn't really care about, or consider errors. For example, "Mixed spaces and tabs." - we were getting reports of over 300+ errors across all our JS files.
Unfortunately, JSLint does not provide a way to ignore this error, thus giving the impression that our JavaScript was riddled with errors....which weren't really errors.


After digging around in the JSLint4Java code, we found a place to implement error filtering in the Ant task. We can now provide a comma-separated list to the jslint Ant task, and any errors specified in the filter will simply be ignored.
As a result of this, a project in our CI system went from 350+ JS errors to 40 after filtering out the 'mixed spaces and tabs' error.

We will submit this functionality back to the author of JSLint4Java, in the hopes that it will get patched into the project officially. Currently it's only usable from the Ant task, and not through the command line, but we do not use JSLint4Java from the command line.