Fork me on GitHub

Kernow Soul

Ruby, Rails and JavaScript Consultant

Running JSLint Through TextMate

| Comments

UPDATE: I now use jsHint which is more configurable, there’s an excellent TextMate bundle available

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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

Comments