title: Textmate Javascript Validate Syntax Command
body: <p>I spend a lot of time in TextMate 2 writing PHP and a lesser amount writing Javascript.</p>
  
  <p>I've always found the Validate Syntax command in Textmate very useful as a final quick sanity check before saving. I was wondering today if there were a way to do something similar for Javascript and I think I've found a solution in acorn:</p>
  
  <p><a href="https://github.com/ternjs/acorn" rel="nofollow noreferrer">https://github.com/ternjs/acorn</a></p>
  
  <p>Running something along these lines:</p>
  
  <pre><code>acorn --silent &lt;file-here&gt;; echo $?
  </code></pre>
  
  <p>Returns either a 0 if it's valid or a 1 if it's not. If not it also returns an error with the line where the syntax error occurs:</p>
  
  <pre><code>Unexpected token (50:1)
  1
  </code></pre>
  
  <p>Seems like it's almost perfect for use in a simple Validate Syntax command.</p>
  
  <p>But that's where I ran into a brick wall of ignorance. I'm not sure how to get from there to an actual command in TextMate and looking at the PHP example and a few others left me no further along partly because I've got almost no Ruby experience and it appears to that's how commands are usually written in TextMate.</p>
  
  <p>Anyone with more experience writing TextMate commands care to take a pass at it?</p>
  
  <hr>
  
  <p>Based on Graham's suggestion and additional help here's a working command:</p>
  
  <pre><code>#!/usr/bin/env bash
  #Write scope of JS to a temp file
  echo "$(&lt;/dev/stdin)" &gt; ${TMPDIR}acorn-validation.js;
  #Capture output of acorn syntax check (Note that acorn sends the output to STDERR thus the 2&gt;&amp;1)
  ACORN_OUTPUT=$( (acorn --silent ${TMPDIR}acorn-validation.js) 2&gt;&amp;1 );
  
  echo 'Running syntax check with acorn...';
  
  if [[ "" == $ACORN_OUTPUT ]]; then
      echo 'No syntax errors detected';
  fi
  
  if [[ "" != $ACORN_OUTPUT ]]; then
      #Find the line/column value
      LINE=$(echo $ACORN_OUTPUT | grep -oE '([0-9]+:[0-9]+)';);
      echo '';
      echo 'Syntax error on '${LINE};
      echo ${ACORN_OUTPUT/($LINE)/};
      #Send cursor to the place where the error occured
      LINE=(${LINE//:/ });
      open "txmt://open/?url=file://${TM_FILEPATH}&amp;line=${LINE[0]}&amp;column=${LINE[1]}";
  fi
  </code></pre>
  
  <p>Make sure Input is set to Scope and Output is set to Show in Tool Tip.</p>