How to run JSLint in command line on Windows

I use JSLint for JavaScript code quality control.

How to run JSLint in Windows command line?

Windows has it’s own JavaScript build-in engine Windows Script Host. It starts by command line tool cscript. Thus it easy to run JavaScript programs under Widnows, we just need to learn JSLint to get arguments from command line through cscript and send messages back to console.

1. Prepare jslint,js

Lets first download the latest jslint from github.

At the end of jslint.js we have to put this peace of code:

(function() {
    // sub - tolerate inefficient subscripting: not dot notation
    // white - tolerate messy white space
    // nomen - tolerate dangling _ in identifiers 
    // vars - tolerate many var statements per function
    // browser - assume a browser 
    // plusplus - tolerate ++ and --
    // sloppy - tolerate missing 'use strict' pragma
    // bitwise - allow bitwise operators
    var options = {sub:true, white:true, nomen:true, vars:true, browser:true, plusplus:true, sloppy:true, bitwise:true};

    if (!JSLINT(WScript.StdIn.ReadAll(), options)) {
        for (var i = 0 ; i < JSLINT.errors.length ; i++) {
            var e = JSLINT.errors[i];
            WScript.StdErr.Write(
                (e.line + 1) + '\t' +
                (e.character + 1) + '\t' +
                (e.reason) + '\t' +
                (e.evidence || '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1") + '\n'
            );
        }
        WScript.Quit(1);
    }
})();

Here options variabel contains parameters to run JSLint. This is a set of parameters that I usually use in my projects.

2. Command file

Lets write a command file that will start cscript with jslint.js and give it an argument – the name of the checking file.

@echo %1
@cscript //Nologo %~dp0\jslint.js < %1

The first line prints the argument – a name of the file to check. It can be helpful as a working indicator when check multiple files. The second line starts jslint.js and sends an argument to it. The weird combination of %~dp0 means current directory (read more about variable substitution in Windows command line here or simply write FOR /? in console).

Name this command file jslint.cmd and place it in the same folder where jslint.js is. Add path to this folder to our system PATH variable.

That’s it. Now we can check JavaScript files with JSLint from Windows command line. If JSLint finds a problem it writes about it to console. For example:

C:\inetpub\wwwroot\php\wallboard\gui>jslint common.js
common.js
671     64      '_nCookieTTL' was used before it was defined.

3. ANT Integration

But we need an automated task to check many files in project at once and for continues integration, right? Lets write an ANT-task.

<project name="wallboard" default="all">
    <target name="Check JavaScript">
        <apply executable="jslint.cmd" failonerror="true">
            <fileset dir="${basedir}">
                <patternset>
                    <include name="**/*.js"/>
                    <exclude name="**/extjs/**"/>
                    <exclude name="**/jquery.js"/>
                </patternset>
            </fileset>
        </apply>
    </target>
  <target name="all" description="Check JavaScript in project" depends="Check JavaScript">
    <echo>Done!</echo>
  </target>
</project>

Save file as jslilnt.xml in the project folder. Now we can run from command line:

C:\inetpub\wwwroot\php\wallboard>ant -f jslint.xml
Buildfile: C:\inetpub\wwwroot\php\wallboard\jslint.xml

Check JavaScript:
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\agents.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\common.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\guicomponent.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\models.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\monitor.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\skillgroups.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\talks.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\teams.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\titles.js
    [apply] C:\inetpub\wwwroot\php\wallboard\gui\viewport.js

all:
     [echo] Done!

BUILD SUCCESSFUL
Total time: 9 seconds