{"id":55,"date":"2008-08-22T21:42:34","date_gmt":"2008-08-23T01:42:34","guid":{"rendered":"http:\/\/hardlikesoftware.com\/weblog\/2008\/08\/22\/using-jslint-from-ant\/"},"modified":"2008-08-22T21:42:34","modified_gmt":"2008-08-23T01:42:34","slug":"using-jslint-from-ant","status":"publish","type":"post","link":"https:\/\/hardlikesoftware.com\/weblog\/2008\/08\/22\/using-jslint-from-ant\/","title":{"rendered":"Using JSLint from Ant"},"content":{"rendered":"<p>[This originally appeared on my dev2dev blog 16-Nov-2007]<br \/>\nHere are instructions for using JSLint from Ant to check your JavaScript for common potential problems.<br \/>\n<!--more--><\/p>\n<p>There is an online version of <a href=\"http:\/\/www.jslint.com\/\">JSLint<\/a>, which is cool, but we don&#8217;t build our software by pasting it into online forms right? It is natural to want to use JSLint from Ant assuming you&#8217;re in the Java world. The <a href=\"http:\/\/www.jslint.com\/lint.html\">JSLint documentation<\/a> gives pretty good motivation for why you would want to use JSLint as part of your build.<\/p>\n<p>Besides the web version JSLint comes in <abbr title=\"Windows Script Host\">WSH<\/abbr> or Rhino command line versions. I could exec a Rhino command line from Ant but I wanted something a little more integrated. I wanted to be able to do something like this:<\/p>\n<pre>\r\n&lt;jslint options=\"{passfail: false, browser: true}\"&gt;\r\n    &lt;fileset dir=\"${src.dir}\"&gt;\r\n        &lt;include name=\"javascript\/*.js\"\/&gt;\r\n        &lt;include name=\"lib\/js\/*.js\"\/&gt;\r\n    &lt;\/fileset&gt;\r\n&lt;\/jslint&gt;\r\n<\/pre>\n<p>I looked around for an existing ant task. There is a Java command line version <a href=\"http:\/\/code.google.com\/p\/jslint4java\/\">jslint4java<\/a> that has an ant task. I have not tried it so I can&#8217;t say anything about how it works.<\/p>\n<p>It seemed to me that since JSLint is written in JavaScript that it should be possible to use it with an ant <code>scriptdef<\/code> tag. Here is how to do it.<\/p>\n<ol>\n<li>Download <a href=\"http:\/\/www.jslint.com\/fulljslint.js\">fulljslint.js<\/a> and make a copy of it called jslintant.js.<\/li>\n<li>Depending on your version of ant you may need newer versions of Rhino and BSF. I used js.jar from Rhino 1.6R7 and BSF 2.4.0.<\/li>\n<li>Edit jslintant.js and add these lines to the very bottom of the file.\n<pre>\r\nimportClass(java.io.File);\r\nimportClass(Packages.org.apache.tools.ant.util.FileUtils);\r\nimportClass(java.io.FileReader);\r\n\r\nvar options = attributes.get(\"options\")\r\nvar fileset;\r\nvar ds;\r\nvar srcFiles;\r\nvar jsfile;\r\n\r\nproject.log(\"Attribute options = \" + options);\r\neval(\"options = \" + options + \";\");\r\n\r\nif (elements.get(\"fileset\").size() > 0) {\r\n    \/\/ should only be one fileset\r\n    fileset = elements.get(\"fileset\").get(0);\r\n\r\n    ds = fileset.getDirectoryScanner(project);\r\n    srcFiles = ds.getIncludedFiles();\r\n\r\n    \/\/ for each srcFile\r\n    for (i = 0; i < srcFiles.length; i++) {\r\n        jsfile = new File(fileset.getDir(project), srcFiles[i]);\r\n        checkFile(jsfile);\r\n    }\r\n}\r\n\r\nfunction checkFile(file, options) {\r\n    \/\/ read the file into a string and make it a real \r\n    \/\/ JavaScript string!\r\n    var reader = new FileReader(file);\r\n    \/\/ readFully returns java.lang.String\r\n    \/\/ new String makes it a java String object\r\n    var input = new String(FileUtils.readFully(reader)); \r\n    \/\/ this makes the type string, which is important\r\n    \/\/ because JSLINT assumes that input is an array \r\n    \/\/ if it is not typeof string.\r\n    input = input.toString();\r\n    if (!input) {\r\n        print(\"jslint: Couldn't open file '\" + file.toString() + \"'.\");\r\n        return;\r\n    }\r\n    if (!JSLINT(input, options)) {\r\n        project.log(\"jslint: Problems found in \" + file.toString());\r\n        for (var i = 0; i < JSLINT.errors.length; i += 1) {\r\n            var e = JSLINT.errors[i];\r\n            if (e) {\r\n                project.log('Lint at line ' + (e.line + 1) + ' character ' +\r\n                        (e.character + 1) + ': ' + e.reason);\r\n                project.log((e.evidence || '').\r\n                        replace(\/^\\s*(\\S*(\\s+\\S+)*)\\s*$\/, \"$1\"));\r\n                project.log('');\r\n            }\r\n        }\r\n    } else {\r\n        project.log(\"jslint: No problems found in \" + file.toString());\r\n    }\r\n}<\/pre>\n<\/li>\n<li>Add a scriptdef element to your ant target as follows.\n<pre>\r\n&lt;scriptdef name=\"jslint\" \r\n        src=\"${path_to_script}\/jslintant.js\" \r\n        language=\"javascript\"&gt;\r\n    &lt;attribute name=\"options\"\/&gt;\r\n    &lt;element name=\"fileset\" type=\"fileset\"\/&gt;\r\n&lt;\/scriptdef&gt;<\/pre>\n<\/li>\n<\/ol>\n<p>Now you can use the jslint task as shown above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[This originally appeared on my dev2dev blog 16-Nov-2007] Here are instructions for using JSLint from Ant to check your JavaScript for common potential problems.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,3],"tags":[],"_links":{"self":[{"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/posts\/55"}],"collection":[{"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hardlikesoftware.com\/weblog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}