Jslint in TextMate

The previous post detailed a way to simply run JsLint from TextMate. I put together this shell script that can be run as a command on the selected files in the project drawer and on the current document not in a project window, which is more robust than the previous script. I will try to upload a bundle for this later, but for now, you can create the command in the bundle editor yourself. Make sure that the input is none and that the output is set to the HTML option. Also, *very important*, make sure that you add the unix path to jsl to your shell variables in TextMate using the variable name "JSLINT".

CODE:
  1. if [[ -n "$TM_SELECTED_FILES" ]]
  2. then
  3.  
  4.    # THIS WILL RUN JSLINT ON THE SELECTED
  5.    # FILES IN THE PROJECT DRAWER
  6.    eval arr=("$TM_SELECTED_FILES")
  7.  
  8.    for (( i = 0; i <${#arr[@]}; i++ )); do
  9.  
  10.       filePath=${arr[$i]}
  11.       pathLen=${#filePath}-3
  12.       pathEnd=${filePath:$pathLen:3}
  13.  
  14.       if [ $pathEnd == ".js" ]
  15.          then
  16.             outputFormat='<br/><a href="txmt://open?url=file://__FILE__&line=__LINE__">__FILE__</a> (Line __LINE__):<br/>__ERROR__<br/>'
  17.             $JSLINT -process "$filePath" -output-format "$outputFormat" -context -nologo
  18.          else
  19.             outputFormat='<a href="txmt://open?url=file://__FILE__&line=__LINE__">__FILE__</a> (Line __LINE__):<br/>__ERROR__<br/><br/>'
  20.             $JSLINT -process "$filePath/*" -output-format "$outputFormat" +recurse -context -nofilelisting -nologo
  21.       fi
  22.  
  23.       echo "<br/><br/>"
  24.  
  25.    done
  26.    
  27. else
  28.    
  29.    # THIS RUNS JSLINT ON THE CURRENT FILE
  30.    filePath=$TM_FILEPATH
  31.    outputFormat='<br/><a href="txmt://open?url=file://__FILE__&line=__LINE__">__FILE__</a> (Line __LINE__):<br/>__ERROR__<br/>'
  32.    $JSLINT -process "$filePath" -output-format "$outputFormat" -context -nologo
  33.  
  34. fi

If you would like to see which files were skipped due to no error or warning, you can remove the "-nofilelisting" portion of the else statement in the for loop. I especially like the way that you can specify a URL in the html output in TextMate that will allow you to link back to documents that can be opened in TextMate, even down to the line. I'm not sure where that is referenced specifically, but its a pretty cool trick; I found that litte tidbit somewhere out there - I lost it in my browser. Anyway, the above script dumps your jslint output in the HTML output window, and clicking the links for the errors will take you to the document and line where the error was found.

3 Comments

  1. Posted July 31, 2008 at 10:53 am | Permalink

    Where do you specify the jslint url? What is the jsl command? is it the javascript file of the jslint parser from the jslint site?

    Thanks

  2. Posted July 31, 2008 at 11:05 am | Permalink

    Ah, sorry about that, forgot to specify… take a look at this Javascript Lint site. This executable is the one I am using.

  3. Posted July 31, 2008 at 11:40 am | Permalink

    OK! I thought you were speaking about jslint.com, the parser made in JavaScript by Douglas Crockford. javascriptlint.com is completely a different thing. Thanks anyway.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*