ariya.io About Talks Articles

Ant Task to Validate JavaScript

3 min read

Apache Ant is quite popular for automating build-related tasks, especially in the enterprise environment and continuous integration systems. Many organizations which already standardized on Ant and start to adopt web application development would want to explore the possibility of keeping Ant for a variety of JavaScript-related workflow. One of the tasks is to ensure that the JavaScript program is syntactically valid. It turns out that this is not difficult at all.

A few weeks ago, I have written about validating JavaScript files from the command-line, text editor, git pre commit hook, or continuous integration system. In this installment, we will expand the approach so that the syntax verification can be invoked as an Ant task. So that you can follow along, I have prepared a git repository, github.com/ariya/ant-javascript-validate, which serves as a complete example.

The working principle is very similar as the previous blog post. We utilize the power of Ant scripting feature and ince Bean Scripting Framework (BSF) supports running JavaScript code (using

Mozilla Rhino under the hood), we will use it to run Esprima-based syntax validator. For convenience, this will be wrapped as an Ant task called jsvalidate.

The example repository demonstrates the usage. After you clone the repo, all you need to do is running:

ant validate-javascript

which will give an output similar to this:

validate-javascript:
[jsvalidate] /Users/ariya/ant-jsvalidate/src/invalid.js:
[jsvalidate]   Error: Line 1: Missing catch or finally after try
[jsvalidate] /Users/ariya/ant-jsvalidate/src/strictmode-invalid.js:
[jsvalidate]   Line 3: Octal literals are not allowed in strict mode.
[jsvalidate]   Line 4: Parameter name eval or arguments is not allowed in strict mode
[jsvalidate]   Line 5: Parameter name eval or arguments is not allowed in strict mode
[jsvalidate]   Line 6: Strict mode function may not have duplicate parameter names
[jsvalidate]   Line 7: Parameter name eval or arguments is not allowed in strict mode
[jsvalidate]   Line 8: Function name may not be eval or arguments in strict mode
[jsvalidate]   Line 9: Function name may not be eval or arguments in strict mode
[jsvalidate]   Line 10: Use of future reserved word in strict mode
[jsvalidate]   Line 10: Strict mode code may not include a with statement

If you open build.xml, here it is obvious that validate-javascript is the target which uses jsvalidate task to run the syntax validation on every *.js files under a specific directory. Obviously, you can tweak it to suit your application configuration.

The implementation of jsvalidate task itself is very simple, just about 50 lines. The spirit its very much the same like the command-line tool esvalidate (part of Esprima). Given the file set, read the content of every file, check the syntax, and shows the errors (if any). Esprima code is loaded by reading lib/esprima.js and evaluate it. The included report is minimalistic but informative. If there is at least one syntax error, the task will be marked as failed. As an exercise for the reader, tweak the task so that you can output the validation result in the format of JUnit XML.

Who says Java and JavaScript can’t live together in harmony?

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook