found drama

get oblique

a jshint pre-commit hook for git

by Rob Friesel

JavaScript is a wily beast: optional semi-colons and braces 1, sneaky performance hits, promiscuous equality operators and other dirty tricks… If you’re not careful, you could get yourself in trouble.

Fortunately, there are tools out there to help. One of the recent additions to the JavaScript developer’s code quality toolkit is jshint. 2 Even more fortunate: right out of the box, jshint can give you feedback about your code on the command line via JavaScriptCore, Node, or Rhino. This makes jshint a great candidate for use as a pre-commit hook in your version control.

Now, my VCS of choice is git, so I offer up to you this git pre-commit hook for linting your JavaScript with jshint:

A couple of notes about the pre-commit hook:

  1. jshint is going to lint the JavaScript as it is saved on the filesystem, not as it’s being committed. Pay attention to what errors are reported and get familiar with the --no-verify flag. 3
  2. Shell scripting pedants may find that JSC could actually spit out exit status codes outside the acceptable range.

May this pre-commit hook save your butt as often as it has saved mine.

  1. Well… “optional braces in some places”.[]
  2. If you haven’t heard of it, jshint is a fork of Crockford’s jslint, and it has been described by some as “the kinder, gentler jslint”.[]
  3. Apparently, even when you try to save yourself from your JavaScript self, you’ve still got to watch yourself.[]

About Rob Friesel

Software engineer by day. Science fiction writer by night. Weekend homebrewer, beer educator at Black Flannel, and Certified Cicerone. Author of The PhantomJS Cookbook and a short story in Please Do Not Remove. View all posts by Rob Friesel →

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*