I’ve been developing with JavaScript a lot recently and found JSLint to be a rather useful tool for picking up syntax errors. Being primarily a ruby developer I have a nasty habit of omitting semicolons from the end of lines.
I found an article on running JSLint through a textmate command but found Ryan’s script didn’t quite work with the output from the version of JSLint I’m using so I thought I’d share my modifications.
#!/usr/bin/env ruby
require 'cgi'
lint = `/usr/bin/java -jar ~/Library/JSLint/js.jar ~/Library/JSLint/jslint.js "$TM_FILEPATH"`
lint.gsub!(/(line \d+ character \d+:) ([^\n]+)\n([^\n]+)/m) do
"<p><strong>#{CGI.escapeHTML($1)}</strong> #{CGI.escapeHTML($2)}</p>" <<
($3 ? "<pre>#{CGI.escapeHTML($3)}</pre>" : '')
end
lint.gsub!(/^(jslint:.*at )/, '')
print <<HTML
<!doctype>
<html>
<head>
<style type="text/css">
p { margin-bottom: 0; }
pre {
background: #f5f5f5;
border: 1px solid #cfcfcf;
font-size: 12px;
margin-top: 2px;
padding: 2px 4px;
}
</style>
</head>
<body>
#{lint}
</body>
</html>
HTML