My favorites | Sign in
Project Logo
                
Search
for
Updated Sep 06, 2008 by d...@happygiraffe.net
Labels: Featured
JSLintAntTask  
A custom task for ant which invokes jslint4java

Introduction

JSLint is a lint-like tool for JavaScript files. It finds problematic constructs and warns you about them.

jslint4java wraps it, so it's callable from Java. In addition, it provides an ant task so that it can be invoked automatically as part of your build procedure. For more detail on precisely what is checked, please see the original documentation.

Presently, jslint4java requires Java 5 and ant 1.6.5.

Parameters

Attribute Description Required
encoding Specify the encoding of the JavaScript files. Defaults to system encoding. No
haltOnFailure Should the build stop if JSLint reports an error? Defaults to true. No
options A comma separated list of options to pass to JSLint. No

The valid list of options is defined by the Option enum, and comprises of:

  • adsafe – If use of some browser features should be restricted
  • bitwise – If bitwise operators should not be allowed
  • browser – If the standard browser globals should be predefined
  • cap – If upper case html should be allowed
  • debug – If debugger statements should be allowed
  • eqeqeq – If === should be required
  • evil – If eval should be allowed
  • fragment – If html fragments should be allowed
  • laxbreak – If line breaks should not be checked
  • nomen – If names should be checked
  • onevar — If only one var statement per function should be allowed
  • passfail – If the scan should stop on first error
  • plusplus – If increment/decrement should not be allowed
  • regexp — If the . should not be allowed in regexp literals
  • rhino – If the rhino environment globals should be predefined
  • safe — If use of some browser features should be restricted
  • sidebar — If the system object should be predefined
  • strict — Require the \"use strict\"; pragma
  • sub — If all forms of subscript notation are tolerated
  • undef – If undefined variables are errors
  • white – If strict whitespace rules apply
  • widget – If the yahoo widgets globals should be predefined

Parameters specified as nested elements

fileset

One or more filesets may be specified in order to identify javascript files to check. This uses the standard ant fileset data type.

formatter

Zero or more formatter elements may be specified in order to control output from jslint. Each formatter element has two attributes.

Attribute Description Required
type Either plain or xml. Yes
destfile A location to write the output to. Defaults to the console. No

If no formatter elements are present, then no output will be produced. However, the build will still fail if the validation fails.

Examples

First, you need to define the task in your build file.

  <taskdef name="jslint"
             classname="net.happygiraffe.jslint.ant.JSLintTask"
             classpath="/path/to/jslint4java-1.2+rhino.jar" />

If you already have rhino in your classpath, you may use the jslint4java-1.2.jar instead.

You may also use the antlibs facility to pull in JSLint.

  <project xmlns:jsl="antlib:net.happygiraffe.jslint">
    …
  </project>

Doing so will require you to access the jslint task as jsl:jslint.

To check a directory for all .js files, and emit all errors to the console:

  <jslint>
    <formatter type="plain" />
    <fileset dir="web/js" includes="*.js" />
  </jslint>

To check a directory for all .js files, excluding packed files. Send any problems to a file jslint.out.

  <jslint>
    <formatter type="plain" destfile="${build.dir}/jslint.out" />
    <fileset dir="web/js" includes="**/*.js" excludes="**/*.pack.js" />
  </jslint>

To check a directory of JavaScript files, whilst warning about whitespace issues and undefined variables. Send the errors to the console, and also emit an XML report to the build directory.

  <jslint options="undef,white">
    <formatter type="plain" />
    <formatter type="xml" destfile="${build.dir}/jslint.xml" />
    <fileset dir="web/js" includes="**/*.js" excludes="**/*.pack.js" />
  </jslint>

Comment by CShamis, Feb 15, 2008

JSLint doesn't seem to support redirecting the standard out to capture the report.

Comment by chathu03j, May 12, 2008

hi is there any way I can capture the output to write it to a file?

Comment by praveen.kailas, May 28, 2008

Guys i have added support for logging to a text file, if you are interested, i can send you the jar files & the source files.

Comment by chathu03j, Jun 02, 2008

Yeah, if you can please send them

Thanks.

Comment by praveen.kailas, Jun 03, 2008

Chathu, the latest source from svn has reporting capability. Just download the sources and build them.

I work on a project that is still in Java 1.4, so i backported this to work with jdk 1.4. So what i have is my own version of the sources & it has different options.

I have created the jars for the latest version from svn. You can download them from here http://www.4shared.com/dir/7430194/d2e639ac/JSLint.html

Just follow the instructions on the wiki

Comment by amareswar.polimera, Oct 28, 2008

Praveen, the link you specified is not valid. Can you send me the latest files.?

Comment by CShamis, Dec 23, 2008

I recently upgraded to 1.2.1 and have no problems. But, in case that's not possible; a cheap and easy workaround to log the output is to create an ant task that calls another ant task. Like so:

<target name="jslint" description="Checks JavaScript files for problems.">
  <echo message="Linting JavaScript..." />
  <ant antfile="build.xml" target="jslint-actual" output="${reports}/jslint.txt" /> 
</target>

Then, just create your "jslint-actual" task as normal.

Comment by itlogo, Apr 22, 2009

Is there any way to pass the "predef" option to define custom global variables?


Sign in to add a comment
Hosted by Google Code