Validate JS in TextMate text selection
August 12th, 2009
I like TextMate -- always have (despite the noise made by the VIM fan boys:). Some of my favorite tools are the syntax checkers. I make a habit of pressing Ctrl-Shift-V to verify the syntax of my Ruby code. I find problems early before I run my specs. I also use the excellent Validate Syntax command provided by the JavaScript Tools bundle.
Today my pair pointed out how nice it would be to syntax check JavaScript within a haml file (i.e. in a :javascript filter or plain %script block). Unfortunately, the Validate Syntax command only operates on files and not text selections. Then my pair showed me "Filter Through Command ...". This built-in TextMate tool can take a text selection, run it through any system command, and return the results. The results can be used in any number of ways such as replacing the selected text, displayed as HTML in a dialog, or rendered in a tooltip.
Given that knowledge, I set off trying to figure out how to validate the syntax of a JavaScript text selection. Maybe I could hook into the JavaScript Tools bundle, after all, this code did what I wanted -- just not on the input I wanted. The Bundle Editor showed me that the key to the Validate Syntax command was the following:
"${TM_RUBY:-ruby}" "$TM_BUNDLE_SUPPORT/bin/lint.rb"
This command runs runs a ruby script. On my system this equated to the following (rather long-winded) command:
ruby ~/Library/Application Support/TextMate/Bundles/JavaScript Tools.tmbundle/Support/bin/lint.rb
Taking a look at lint.rb I saw that it simply called out to the 'jsl' native binary included with the bundle (in the same /bin directory). This was the clue I needed. If I could pass a text selection to jsl then I could use the 'Filter Through Command ...' tool. Running 'jsl' immediately showed me the usage I needed:
Usage: jsl [-help:conf] [-conf filename] [-process filename] [+recurse|-recurse] [-stdin] [-nologo] [-nofilelisting] [-nocontext] [-nosummary] [-output-format ______]The
-stdin option will read the input from stdin instead of a file -- perfect!
So to make things easy, I created a soft link (ln -s) from ~/bin/jsl to the jsl executable. Then I simply brought up 'Filter Through Command ...', passed it a command-line of 'jsl -stdin' and displayed the result in a tooltip. Voila! I've now got embedded JavaScript syntax checking in the bag!


Your Response