JSLint, the popular JavaScript static code analysis tool written by Douglas Crockford, can fairly easily be incorporated into your builds through the use of node and the jslint node module. The following steps will show you how to add JSLint to an existing rake build:
Step 1: Install Node
If you don’t already have it, go download and install node from http://nodejs.org/.
Step 2: Add JSLint Method
The following jslint method uses the Node Package Manager (npm) to install the jslint module into the current folder if it does not already exist. If node or npm are not found in the execution path then an error message is printed and the method simply returns. The method uses a hash config parameter for its parameters.
def jslint(config)
tool = 'jslint'
flags = config['flags'] || ''
node = which('node')
if(node.nil?)
puts "Could not find node in your path."
return
end
npm = which('npm')
if(npm.nil?)
puts "Could not find npm in your path."
return
end
if(!File.directory?("node_modules/#{tool}"))
sh "\"#{npm}\" install #{tool}"
end
scripts = config['scripts'] || [""]
sh "\"#{node}\" node_modules/#{tool}/bin/#{tool}.js #{flags} #{scripts}"
end
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
sep = File::ALT_SEPARATOR || File::SEPARATOR
exe = "#{path}#{sep}#{cmd}#{ext}"
return exe if File.executable? exe
}
end
return nil
end
Step 3: Add a Lint Task
After including the jslint method defined above, add a new rake task which calls the method with the desired config values:
task :lint do
config = { 'flags' => '--white',
'scripts' => FileList.new("src/**/app/*.js") }
jslint(config)
end
That’s all you need. Now you can run “rake lint” or tie this into to your existing process as you see fit. Enjoy!

