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.

2 comments:

Andrey said...

Hello Steven,
I faced similar issue with hundreds of 'false alarms' trying to apply different options in jslint4java, but still it does not work.
Can you please provide example of Ant script, so error filtering can be done?

Thank you in advance,
Andre

Steven Sproat said...

Hi,

I submitted a Pull request here: https://github.com/happygiraffe/jslint4java/pull/10

It may be incompatible with the latest codebase but you should be able to extract the code into the current code.

I'm not sure if I can publish the JAR I have of my changes?