Javascript validation with JSHint in Eclipse

Besides all the Java code in the RAP project, we also have more than 250 JavaScript files which total up to 75k lines of code. For such an amount of code, you should have some kind of code analysis that detects common coding problems like unintentional global variables. We use the JSEclipse plug-in for JavaScript editing which detects some, but not many JavaScript problems.

A while ago, we tried to use JSLint, a tool written by JavaScript guru Douglas Crockford. Unfortunately, this tool produces several thousand warnings on our code base, many of which were not really problems but debatable coding style issues and there was no way to turn them off. JSLint’s lack of customizability recently lead to a fork named JSHint that is going to provide more flexible configuration options.

Like JSLint, JSHint is written in JavaScript, but can be run on the command line using tools like Rhino or JavaScriptCore. I tried JSHint on our codebase with good results using a shell script that runs it on top of Rhino. Unfortunately, checking all our 250+ *.js files keeps my machine busy for 5 minutes and 40 seconds and effectively turns it into a fan heater. This is not because JSHint itself is so demanding, but because for every file, a JVM has to be started, Rhino has to be loaded, then Rhino has to parse and load the JSHint JavaScript library, and then finally, jshint can parse and validate the source file.

Encouraged by the good results I tried to find a solution that doesn’t have this overhead. And as an Eclipse hacker and user, I certainly wanted to integrate the tool into my daily working environment. The result is a simple, yet efficient JSHint Eclipse integration that validates the same bunch of *.js files in less than 15 seconds.

jshint eclipse screenshot Javascript validation with JSHint in Eclipse

This speedup could be achieved by exploiting the way Eclipse builds projects: It uses the same builder instance to visit all files of the project recursively. That makes it possible to load and configure the JSHint library only once for the entire project and reuse it for all files being checked. Of course, validating all files of a project is only necessary for a full rebuild. During normal work, single files are being validated instantly when they have changed.

Although the configuration options are still somewhat basic, this integration proved to be very helpful already. I thought that it may be useful for others as well and decided to build and publish a first version. It’s available on the jshint-eclipse page. There’s an update site that let’s you install the plug-in right into your Eclipse IDE. If you find the plug-in useful, have ideas for improvements, find a problem or want to contribute, I’m happy to hear about it. To report problems, please use the github issue tracker.

2 Responses to “Javascript validation with JSHint in Eclipse”

  1. Mike Henke says:

    Do you have an example how to plug into an ant task? Also can it be outputted so Jenkin’s validation plugin will read and display the errors?

    example of a jslint ant target called inspection.jslint – https://github.com/mhenke/Cloudy-With-A-Chance-Of-Tests/blob/develop/build.xml

  2. Ralf Sternberg says:

    Hi Mike,

    that’s a good point. Integration into Hudson/Jenkins will be the next step for our project, too. It should be simple to use the JSHint class in a custom ant task. The class JSHint has a main method with an usage example: https://github.com/eclipsesource/jshint-eclipse/blob/master/com.eclipsesource.jshint/src/com/eclipsesource/jshint/JSHint.java
    Basically, it’s like this:

    JSHint jshint = new JSHint();
    jshint.init();
    jshint.configure( new Configuration() );
    jshint.check( jscode, new MyErrorHandler() );

    There are also some examples in this unit test: https://github.com/eclipsesource/jshint-eclipse/blob/master/com.eclipsesource.jshint.test/src/com/eclipsesource/jshint/JSHint_Test.java

    Using this code in headless mode would require to split the jshint bundle into core and ui, which will be a good idea anyway…

    Regards, Ralf

2 responses so far

Written by . Published in Categories: EclipseSource News, Planet Eclipse

Author:
Published:
Jan 26th, 2012
Follow:

Twitter Google+ GitHub