Loading ...
Sorry, an error occurred while loading the content.

361Re: [jslint] Feature request: exit codes

Expand Messages
  • Arthur Blake
    Jan 13, 2009
    • 0 Attachment
      Hope this might help someone- I use jslint in my build process from ant as
      well and it works great (although it's a really old version of jslint...)
      If you care to do some hacking, I'm sure you can get the most recent version
      working.

      Sent to Doug in 2006:
      ---------------------------
      I've written a slightly more sophisticated front end for the rhino version
      of jslint so I can use it within ant or on the command line.
      I'm not sure if you care, but you are welcome to use it, mangle it, post it,
      toss it in the trash, or whatever, if you like.
      Best Wishes.
      Arthur Blake

      //the following code is pasted onto the end of a your original jslint.js
      file into a new file, named jslint_rhino.js

      /*
      07-24-2006 Arthur Blake
      The following section was added so that jslint could be run from rhino (and
      thus ant)
      more easily. It is a little more sophisticated than the original rhino
      version on the jslint.com
      site. It can process multiple files in a single argument (as the
      antprogram may pass arguments in this way)
      and also will display all of the jslint errors found instead of stopping on
      the first error found.
      It also will truncate "evidence" lines that exceed 200 characters so that
      the output won't be overloaded
      when processing JSMin or slightly JSMin processed .js files.
      */

      var i;
      var files = [];
      var numFiles=0;
      var splits;

      // get all files that will be processed from command line into one array
      // this extra processing below is necessary because multiple files can be
      passed in on a single arg.
      // when invoked with the java task inside ant.
      for (i=0; i < arguments.length ; i++) {
      splits = arguments[i].split(' ');
      for (j=0; j < splits.length; j++) {
      files.push(splits[j]);
      numFiles++;
      }
      }

      print ("JSLint! ( http://www.jslint.com) processing " + numFiles + " file" +
      (numFiles!==1?"s":"") + ".");

      for (i=0; i<numFiles; i++) {
      try {
      processFile(i+1,files[i]);
      } catch (e) {
      print("!!!!! jslint threw exception !!!!! " + e);
      }
      }

      function processFile(filenum, file) {
      print();
      var n,e,k;
      var massagedEvidence;
      if (file)
      {
      if (jslint(readFile(file))) {
      print("(" + filenum + " of " + numFiles + ") " + file + ": no js lint
      found!");
      } else {
      n = jslint.errors.length;
      print ("(" + filenum + " of " + numFiles + ") " + file + ": " + n + "
      lint" +(n!==1?"s":"") + " found:");

      for (k=0; k <n; k++) {
      e = jslint.errors[k];
      if (e) {
      print(' ' + (k+1) + '. line ' + (e.line + 1) + ' character ' +
      (e.character + 1) + ': ' + e.reason);
      massagedEvidence= (e.evidence ||
      '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
      if (massagedEvidence.length >200) {
      massagedEvidence = massagedEvidence.slice(0,200) + ' ....
      (truncated) ';
      }
      print(' ' + massagedEvidence);
      } else {
      print(' ' + (k+1) + '. line ' + '?' + ' character ' + '?' + ': '
      + '?');
      print(' ' + '?');
      }
      }
      }
      }
      }


      I pre-compile the .js with the rhino js compiler for speed like so:

      java -cp /rhino/js.jar org.mozilla.javascript.tools.jsc.Main jslint_rhino.js

      And, here is an example of how I invoke it within my ant build script-

      <!--

      Run JSLint against all .js files in the project.

      from http://www.jslint.com:

      JSLint takes a JavaScript source and scans it. If it finds a problem, it
      returns a message
      describing the problem and an approximate location within the source.
      The problem is not
      necessarily an error, although it often is. JSLint looks at some style
      conventions as well
      as structural problems. It does not prove that your program is correct.
      It just provides
      another set of eyes to help spot problems.

      -->
      <target name="javascript.jslint" depends="base.init" description="run all
      .js source code through the jslint javascript verifier to find common
      mistakes and javascript problems">
      <fileset dir="${webapp.dir}" id="javascript.files">
      <include name="**/*.js"/>
      <exclude name="**/processed_js/**"/>
      </fileset>

      <pathconvert pathsep=" " property="javascript.files.list"
      refid="javascript.files"/>

      <!-- run the jslint script as a pre-compiled .js (rhino javascript
      runtime) -->
      <java classname="jslint_rhino">
      <classpath>
      <pathelement path="${scripts.dir}/jslint"/>
      <pathelement location="${scripts.dir}/AntLib/js.jar"/>
      </classpath>
      <arg value="${javascript.files.list}"/>
      </java>
      </target>


      On Tue, Jan 13, 2009 at 12:44 PM, <dom@...> wrote:

      > On Tue, Jan 13, 2009 at 04:59:14PM +0000, Daniel Cassidy wrote:
      > > 2009/1/9 M. Lorton <mlorton@... <mlorton%40yahoo.com>>
      > >
      > > > The current version of JSlint (at least on Rhino) exits with code 1 if
      > > > it cannot read the file and 0 otherwise. If it instead would exit
      > > > with 0 only if the file was read AND was free of lint, I could use
      > > > JSlint in for pre-commit checking under SVN.
      > > > .
      > >
      > > Take a look at JSLint4Java, which embeds JSLint and does what you want,
      > as
      > > well as being usable as an Ant task.
      >
      > jslint4java works, but it is in need of an update to the latest jslint.
      > Unfortunately I've left it in a bit of a state. I'm hoping to get to
      > working on it soon´┐Ż
      >
      > -Dom
      >
      >


      [Non-text portions of this message have been removed]
    • Show all 6 messages in this topic