ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanouil Batsis <Emmanouil.Bat...@eurodyn.com>
Subject [solved] Re: Use JSLint to validate .js files?
Date Fri, 15 Jul 2005 14:03:38 GMT

Emmanouil Batsis wrote:

> This is a shot in the dark but i was wondering whether some other 
> disturbed individual has thought about validating javascript files in 
> Ant builds by feeding them to the JSLint [1] script via Rhino or 
> something. 



Ok, here's a usable draft implementation, hope someone finds it handy. 
It's the first time i am using ecmascript inside Ant; it really rocks!

<scriptdef name="jslint" language="javascript">
    <element name="fileset" type="fileset"/>
    <![CDATA[
    /**************************
    Put  JSLint code below this comment!
    **************************/

    /** used to format JSLint's output for the command line */
    function html2text( html ){
       var text = html.replace( /<[^>]*>/g, '' );
       text = text.replace( /&quot;/g, '"' );
       text = text.replace( /&lt;/g, '<' );
       text = text.replace( /&amp;/g, '&' );
       return text;
    }
    function go(sScript){
        jslint.laxLineEnd = false;//!document.forms.mrof.strict.checked;
        jslint.plusplus = true;//document.forms.mrof.plusplus.checked;
        jslint.cap = false;//document.forms.mrof.cap.checked;
        jslint.jscript = false;//document.forms.mrof.jscript.checked;
        jslint(sScript);
        var r = jslint.report();
        if (r.substr(0, 2) != 'ok') {
            //r = r.entityify();
        }
        //output.setHTML(r);
        project.log(html2text(r));
    }
   
   
    var filesets = elements.get("fileset");
    project.log("Number of filesets = " + filesets.size());
    // iterate over given filesets
    for (i = 0; i < filesets.size(); ++i) {
        var fileset = filesets.get(i);
        var fileSetBaseDir = fileset.getDir(project);
        project.log("Processing fileset " + i + ", basedir = " + 
fileSetBaseDir);
        var directoryScanner = fileset.getDirectoryScanner(project);
        var filePaths = directoryScanner.getIncludedFiles();
        // iterate over files in current fileset
        for(i=0; i < filePaths.length; i++){
            var file = new java.io.File(fileSetBaseDir, filePaths[i]);
            project.log("\nProcessing: "+file.getAbsolutePath());
            var bufferedReader = new java.io.BufferedReader(new 
java.io.FileReader(file));
            var sScript = "";
            while ((line = bufferedReader.readLine())){
                sScript += (line + "\n");
            };
            go(sScript)
            //project.log("File "+filePaths[i]+" contents:\n"+sScript);
        };
    };
    ]]>
</scriptdef>

<jslint>
    <!-- accepts any number of filesets -->
    <fileset dir="war/jsp/common/js">
        <include name="*.js"/>
    </fileset>
</jslint>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message