jslint4java ant task

jslint4java 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. To get started quickly, see the examples.

jslint4java requires Java 5 and ant 1.7.

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
jslint The path to an alternative jslint.js. If you want to use a newer version than the bundled one. 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 adsafe should be enforced
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
css
If css workarounds should be tolerated
debug
If debugger statements should be allowed
devel
If logging should be allowed (console, alert, etc.)
eqeqeq
If === should be required
evil
If eval should be allowed
forin
If for in statements must filter
fragment
If html fragments should be allowed
immed
If immediate invocations must be wrapped in parens
indent
The number of spaces used for indentation (default is 4)
laxbreak
If line breaks should not be checked
maxerr
The maximum number of warnings reported (default is 50)/dd>
newcap
If constructor names must be capitalized
nomen
If names should be checked
on
If html event handlers should be allowed
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 variables should be declared before used
white
If strict whitespace rules apply
widget
If the yahoo widgets globals should be predefined

If a parameter is required (as in the case of indent), then it can be supplied by appending an equals and the value. e.g. indent=2.

Parameters specified as nested elements

resource collection

Any kind of resource collection may be specified as a subelement. Most of the time, this will mean a fileset.

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.

predef

You can specify a comma separated list of global variables inside a <predef> element. This can be useful for functions that you know are globally available in your environment, e.g. jQuery.

Examples

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

  <taskdef name="jslint"
           classname="com.googlecode.jslint4java.ant.JSLintTask"
           classpath="/path/to/jslint4java-1.3.3.jar" />

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

  <project xmlns:jsl="antlib:com.googlecode.jslint4java">
    …
  </project>

Doing so will require you to access the jslint task as jsl:jslint. This also means that you have to install jslint4java-1.3.3.jar in ~/.ant/libs.

If you want to keep the jslint4java jar checked in as part of your project, you have to combine the above two approaches. For example, assuming you have placed the jar in lib-build:

<project xmlns:jsl="antlib:com.googlecode.jslint4java">
  <path id="ant.tasks.classpath">
    <fileset dir="lib-build" include="*.jar" />
  </path>
  <taskdef uri="antlib:com.googlecode.jslint4java"
           resource="com/googlecode/jslint4java/antlib.xml"
           classpathref="ant.tasks.classpath" />
  …
</project>

Again, this means you have to refer to the 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, as well as predefining a couple of global variables we know we'll be using. Send the errors to the console, and also emit an XML report to the build directory.

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

NB: In order to specify a dollar sign, it has to be doubled so that ant doesn't attempt to treat it as a property. If you wanted to list the $$ function that Prototype uses, you'd have to say <predef>$$$$</predef>!