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
failureProperty The name of a property to set in case of build failure. You should also set haltOnFailure="false". 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
timeout How long JSLint has to run, in seconds. If not specified, there is no timeout. No

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

ass
If assignment expressions should be allowed
bitwise
If bitwise operators should be allowed
browser
If the standard browser globals should be predefined
closure
If google closure idioms should be tolerated
continue
If the continuation statement should be tolerated
debug
If debugger statements should be allowed
devel
If logging should be allowed (console, alert, etc.)
eqeq
If == should be allowed
es5
If es5 syntax should be allowed
evil
If eval should be allowed
forin
If for in statements need not filter
indent
The indentation factor
maxerr
The maximum number of errors to allow
maxlen
The maximum length of a source line
newcap
If constructor names capitalization is ignored
node
If node.js globals should be predefined
nomen
If names may have dangling _
passfail
If the scan should stop on first error
plusplus
If increment/decrement should be allowed
predef
The names of predefined global variables
properties
If all property names must be declared with /*properties*/
regexp
If the . should be allowed in regexp literals
rhino
If the rhino environment globals should be predefined
sloppy
If the 'use strict'; pragma is optional
stupid
If really stupid practices are tolerated
sub
If all forms of subscript notation are tolerated
todo
If todo comments are tolerated
unparam
If unused parameters should be tolerated
vars
If multiple var statements per function should be allowed
warnings
Unused
white
If sloppy whitespace is tolerated
windows
If ms windows-specific 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
plain
Produces three lines of text output per error. Shows location and evidence.
xml
Creates a single XML file describing all errors seen in the input. This is a custom XML format for jslint4java. You should prefer the JUnit xml formatter instead.
junit
Creates a directory full of junit xml files. These can be integrated into existing continuous build systems such as hudson or pulse.
checkstyle
Creates a single file XML, listing all errors seen in the input. This should be compatible with checkstyle's XMLLogger.
report
Makes an HTML file containing one JSLint report per input file.
Yes
destfile A location to write the output to.
  • For the plain formatter, you can redirect output to a file. If you leave this attribute out, then output will appear on the console.
  • For the xml and report formatters, you have to specify a file to write the output to.
  • For the junit formatter, you have to specify a directory. Each file linted will have one file created in that directory.
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-2.0.5.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-2.0.5.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, tolerating ecmascript 5 syntax, as well as predefining a couple of global variables we know we'll be using. Send the errors to the console, and also emit JUnit XML reports to the build directory.

  <jslint options="es5">
    <predef>jQuery,$$</predef>
    <formatter type="plain" />
    <formatter type="junit" destfile="${build.dir}/test-reports" />
    <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>!