Chapter 15. Validation

Because JavaScript isn’t compiled before being deployed, web developers don’t have the extra compilation step to identify errors. JavaScript code validators partly fill this void by performing static analysis on your JavaScript code. You were introduced to JSLint and JSHint earlier in this book. In this chapter, you’ll learn how to incorporate JSHint into your build system to automatically analyze and verify your JavaScript code.

Note

JSHint is used in this chapter, because it comes with a prebuilt command-line file suitable for running with Rhino. JSLint, as of the time of this writing, doesn’t have a prebuilt command-line file, though some third parties have written utilities including command-line controls for JSLint.

Finding Files

The first step in validating files is to locate the files. There are two different tasks for this purpose: <fileset> and <filelist>. The <fileset> task is used when you want to include a large number of files based on a pattern. Specify the dir attribute as the directory to look in and then includes with a filename pattern, such as:

<fileset dir="./src" includes="**/*.js" />

This fileset includes all JavaScript files contained in the src directory. You can optionally specify some file patterns to exclude as well:

<fileset dir="./src" includes="**/*.js" excludes="**/*-test.js />

This fileset includes all JavaScript files except the ones that end with -test.js. This is a common practice for excluding unit test files that are ...

Get Maintainable JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.