views:

1627

answers:

8

Javascript code can be tough to maintain.
I am looking for tools that will help me ensure a reasonable quality level.
So far I have found JsUNit, a very nice unit test framework for javascript. Tests can be run automatically from ant on any browser available.
I have not found yet some javascript equivalent of PMD, checkstyle, Findbug...

Do you know any static code analysis tool for javascript ?

A: 

JSLint

Hank Gay
Thanks but how to use it in an automated build ?
Alexandre Victoor
+1  A: 

A quick Google for "jslint ant task" reveals jslint4java, which apparently includes an Ant task.

Hank Gay
The jslint4java ant task should be fairly easy to embed in maven using the antrun plugin.
Dominic Mitchell
I documented how to do so: http://happygiraffe.net/blog/2009/07/28/jslint4maven/
Dominic Mitchell
A: 

I've worked on the SweetDEV RIA project which is a Java tag library composed of several "Web 2.0/Ajax/JavaScript" components.

The maven 2 build process includes some in-house plugins which launches JSLint (code verifier), JsMin (code minifier), JsDoc generation (JavaDoc like documentation), JsUnit (unit tests) and Selenium (in browser) tests .

You may take a look on the SweetDEV RIA maven plugins repository.

paulgreg
+1  A: 

This project looks close:

http://dev.abiss.gr/mvn-jstools/index.html

It generates a report with JsLint. It doesn't look like it hooks into the test phase of the build lifecycle, so I don't think it will reject a build if jslint finds issues (which is what I'd like to do on my projects).

James Cooper
+2  A: 

This is an old thread, but if you're interested in running Jasmine for BDD testing in your maven project, I wrote this jasmine-maven-plugin for exactly this purpose (that is, improving JS quality by encouraging TDD of it).

http://github.com/searls/jasmine-maven-plugin

Justin Searls
A: 

The new jslint-maven-plugin looks useful. It wraps jslint4java, executing JSLint during the test phase of your build.

Wachunga
+1  A: 

I've used the following code to run JSLint as part of the COMPILE phase in Maven.

It downloads jslint4java from maven repository so you don't need anything else.

If JSLint found problems in javascript files, the build will fail.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <taskdef name="jslint" classname="com.googlecode.jslint4java.ant.JSLintTask" classpath="${settings.localRepository}/com/googlecode/jslint4java/jslint4java-ant/1.4.2/jslint4java-ant-1.4.2.jar" />
                            <jslint options="white,browser,devel,undef,eqeqeq,plusplus,bitwise,regexp,strict,newcap,immed">
                                <predef>Ext,Utils</predef>
                                <formatter type="plain" />
                                <fileset dir="${basedir}/src/main/resources/META-INF/resources/js" includes="**/*.js" />
                            </jslint>
                        </target>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.googlecode.jslint4java</groupId>
                    <artifactId>jslint4java-ant</artifactId>
                    <version>1.4.2</version>
                </dependency>
            </dependencies>
        </plugin>
Gian Marco Gherardi