views:

2096

answers:

4

I have seen other text editors use extensions to allow syntax checkers such as JSLint, is this possible with Notepad++?

+20  A: 

I have managed to get two lint programs to run using the notepad++'s NppExec Plugin.

The NppExec plugin is usually installed by default and can be found under plugins -> NppExec. (Using NppExec 0.3 RC1 and Notepad++ 5.1+).

1) JSLint

first download the WSH version of jslint from http://www.jslint.com.
Modify the last part of the file as follows:

(function() {
    if(!JSLINT(WScript.StdIn.ReadAll(),{passfail:false})) {
     var e;
     for(var i in JSLINT.errors) {
      e=JSLINT.errors[i];
      WScript.StdOut.WriteLine('Lint at line '+(e.line+1)+' character '+(e.character+1)+': '+e.reason);
      WScript.StdOut.WriteLine('    '+(e.evidence||'').replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1"));
     }
     WScript.Quit(1);
    }
}());

(Pre-modified version here)
This causes JSLint to output all of the errors, not just the first one.

Next, Notepad++'s NppExec doesn't allow the use of StdIn so I wrote a batch file to actually execute the command.
This also allowed me to add a config file that is inserted before all javascript files. The options can be seen here. The batch file looks like this:

@copy /b "C:\Program Files\jslint\conf.txt"+%1 "C:\Program Files\jslint\lastoutput.txt" > temp.txt
@cscript /Nologo "C:\Program Files\jslint\jslint.js" < "C:\Program Files\jslint\lastoutput.txt"

You may need to modify the paths depending on where you put the jslint.js file. The conf.txt file looks like this:

/*jslint forin:true*/

Make sure there is no return carriage at the end of this line. If there is a return carriage all the lines counts will be off by one.

Finally, the command I entered into NppExec is:

"C:\Program Files\jslint\jslint.bat" "$(FULL_CURRENT_PATH)"


2) Javascript Lint

Javascript lint is a slightly less strict parser and was much easier to implement.

First grab a copy of the windows version from http://www.javascriptlint.com/download/ and unzip it. Then the NppExec command is:

"C:\Program Files\JavascriptLint\jsl.exe" -conf "C:\Program Files\JavascriptLint\jsl.default.conf" -process "$(FULL_CURRENT_PATH)"

(note: Most instructions for Javascript Lint will say to add "pauseatend" to the end of the command, I found this caused problems in Notepad++ so I left it off)

Hope this helps someone,
Cheers,
Andy.

alumb
Thank you for the Javascript Lint reference. I like it much better than JSLint but most people tend to leave it out.
Gordon Tucker
Followed your instructions for JSLint and notepad++ and it worked. thanks.
N30
+1 for describing how to display all errors
nimcap
A: 

This isn't an answer but this code:

(function() {
    if(!JSLINT(WScript.StdIn.ReadAll(),{passfail:false})) {
        var e;
        for(var i in JSLINT.errors) {
                e=JSLINT.errors[i];
                WScript.StdOut.WriteLine('Lint at line '+(e.line+1)+' character '+(e.character+1)+': '+e.reason);
                WScript.StdOut.WriteLine('    '+(e.evidence||'').replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1"));
        }
        WScript.Quit(1);
    } }());

Is giving me this on Notepad++ console:

Lint at line 79 character 8: Stopping, unable to continue. (39% scanned). C:\Program Files\JSLint\jslint.js(4637, 17) Microsoft JScript runtime error: 'line' is null or not an object

Any idea?

Juanra
A: 

If you get Input Error: There is no script engine for file extension ".JS" when implementing alumb's method to get JSLint in Notepad++, it means you may have to change the association of the .JS extension on Windows by typing the following command in cmd.exe:

assoc .JS=JSFile
Sonja
A: 

for javascript lint, dont forget to add the NppExec console output filter '%ABSFILE%(%LINE%): *'

peter