katratxo on Software Development

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

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!

About these ads

Written by katratxo

December 13, 2010 at 5:57 pm

6 Responses

Subscribe to comments with RSS.

  1. Thanks for using jslint4java! I’m glad it’s working for you.

    Dominic Mitchell

    December 15, 2010 at 9:12 am

    • Hi Dominic,
      Thanks to you for making this excellent wrapper.
      Just to let you know, we are using it for checking static JS files, but also for checking generated JS code.

      Best Regards,

      Iván

      katratxo

      December 16, 2010 at 8:51 pm

  2. One more thing: Your “jslint” script should use “$@” rather than plain $1 so it can accept more than one argument.

    Dominic Mitchell

    December 15, 2010 at 9:13 am

    • I’ll incorporate your suggestion.

      Thanks!

      katratxo

      December 16, 2010 at 8:53 pm

  3. […] Want to run JSLint automatically when using mercurial? Take a look at Running JSLint as Mercurial precommit hook. […]

  4. […] have been working with the previous version of the Mercurial JSLint precommit hook for several months now, and we pretty happy with the results, it catches the most common JavaScript […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: