katratxo on Software Development

tail -f /var/log/brain | grep -i software

Archive for December 2010

Running JSLint as Mercurial precommit hook

with 6 comments

JSLint is a tool that helps you identify potential problems in JavaScript code. Some experienced developers may disagree with the coding style enforced by the tool, but if you are beginning with JavaScript this tool helps you stay out of troubles.

Mercurial hooks

Mercurial offers a powerful mechanism to let you perform automated actions in response to events that occur in a repository …

Mercurial: The Definitive Guide

The idea is to automatically check all the modified JavaScript files when trying to perform a commit. If the tool (JSLint) complains on at least on file, the commit is aborted.

The components

jslint4java

jslint4java is just a Java wrapper around JSLint and gives you the possibility to run it command line, e.g.:

java -jar jslint4java-1.4.4.jar application.js
jslint:application.js:11:9:Line breaking error ')'.
jslint:application.js:11:10:Missing semicolon.

Some of the options are for the tool are:

Usage: jslint4java [options] file.js ...
Options:
--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.)
--encoding  Specify the input encoding
--eqeqeq    If === should be required
--es5       If es5 syntax should be allowed
--evil      If eval should be allowed
# Many more (...)

jslint

jslint is a shell script wrapper for calling jslint4java, e.g.

#!/bin/sh
# Note: Modify the path where the .jar is located
java -jar /home/iperdomo/tools/jslint4java/jslint4java-1.4.4.jar --evil $1

jscheck-hg

jscheck-hg is a simple shell script that checks for modified or added JavaScript files and jslint them

#!/bin/sh
# Note: Modify the path location for jslint (the previous script)
hg st -man | awk '/.js$/' | xargs -rn1 path/to/jslint || exit 1

Defining jscheck-hg as precommit hook

With all the components in place, you just need to add the hook to the repository metadata, for this modify the .hg/hgrc file, e.g.

[hooks]
precommit = /path/to/jscheck-hg

Openbravo specifics

Last Friday, the modules for 3.0 were merged into pi, so all the components required for setting this hook are already in the repository. jslint4java, jslint, jscheck-hg are part of the org.openbravo.client.kernel module.

You just need to add the precommit hook to your repository using the jscheck-hg available in org.openbravo.client.kernel/jslint. Open your .hg/hgrc and add the following lines:

[hooks]
precommit = ./modules/org.openbravo.client.kernel/jslint/jscheck-hg

That’s all. Now every commit that contains a JavaScript file modification, it needs to pass the JSLint check.

Happy coding!

Written by katratxo

December 13, 2010 at 5:57 pm