Question

I'd like to use JSLint, but I am wary of tools that have access to my unfiltered source code. Is there an offline version or is there another similar tool that does "lint error checking" for JavaScript offline?

Edit: One with a GUI and that shows you a styled list of errors, instead of a command line interface?

Was it helpful?

Solution

If you like the JSLint web interface, you can do File > Save Page As... and Save as type: Web Page, complete (in Firefox, doing it in Internet Explorer may be slightly different) to a local folder.

I change the name to jslint.htm to get it under 8.3 with no spaces.

It seems to work when saved locally.

Three things:

  1. This may violate his license, although I leave the Copyright intact and don't modify any of his code, and technically my web browser already created a copy of his site on my local HD, so I'm not sure whether I'm in violation or not and I'm not a lawyer so I'll keep doing this until I get a letter telling me to stop.
  2. The page may somehow still be able to send your code to the Internet, although the chance of it being possible is very remote. That said, the WSH or Rhino versions could probably send the code you submit to the Internet easier than a version in a locally saved web page could (if you're paranoid).
  3. You'll get behind on any bug fixes or updates Douglas does. But the same thing applies to the WSH or Rhino versions if you don't update them regularly.

OTHER TIPS

JSLint can be run offline with either WSH or Rhino:

http://www.jslint.com/lint.html#try

Edit: In the two years since this question was asked, JSLint has dropped support for Rhino and WSH. I encourage anyone interested in linting their code to also check out JSHint. It's a fork of JSLint which aims to be more flexible than the original, but also happens to support Node, Rhino, and WSH (in addition to browsers, of course).

Yes:

Basically, you just need an embedded JavaScript compiler to run jslint.js.

There's another JS Linter, called JavaScript Lint, that has both online and downloadable command line versions. I use the downloadable version all time. I've been thinking about integrating it into SVN as part of a hook. I like it better than JSLint because it has more options and seems to detect more things. It can be configured to treat certain identifiers as predefined, for toolkits and the like, which allows it to check for usage of undefined variables, which I'm pretty sure JSLint can't do.

If you're in a Java environment, you may find my jslint4java tool useful. It comes in a command line variant, and can also be integrated into an ant script. No GUI, because I suck badly at those. :-)

YSlow for Firebug has this feature built in

If you use TextMate, I've made a bundle that runs JSLint and displays the output in a graphical window. It's all self-contained; nothing else needs to be installed to use it:

View JSLint.tmbundle at GitHub

I have a tool for running JSLint from the command line with either the SpiderMonkey shell or Rhino. It also includes a plugin for Vim that allows you to press a button to automatically highlight any problem lines while editing a file:

http://github.com/hallettj/jslint.vim/

Try the Google Closure Linter. It has more features than JSLint, too.

Cory Bennet has a good post on command-line JSLint. Much of your setup will be getting SpiderMonkey working. Not so bad if you're running Linux; a wee harder if you're using Windows and Cygwin. And here are some more discussion and comparison with Rhino run-times.

UPDATE: 2013.11 - if you're using node.js on the commandline (and why wouldn't you?) you can run both jslint and the new eslint. [NB: I have contributed bug-reports and test-code to eshint.] Both of these are way easier to setup than what I have above....

Since JSLint itself is written in JavaScript, you can run it offline by copying the HTML and referenced JavaScript files locally. IE7's "Save As..." "webpage, complete" feature does this just fine.

There is a great Yahoo Widget:

http://ajaxian.com/archives/jslint-multi

It is open source, if you are paranoid you can audit the code.

alt text http://jslint-multi-widget.googlecode.com/files/jslint_multi_v1_scrshot.png

I have JSLint with Node.js validating code in my deploy scripts to ensure I don't accidentally push code that could break my site. It can save a lot of time handling customer support issues later.

If you're using TextMate, the JSLintMate bundle has a simple interface, supports both JSLint and JSHint, and lets you set custom options in various ways (e.g., a config file per project, global options across all projects).

JSLintMate screenshots

If you already use ruby gems then you may install jslint gem in one command : http://rubygems.org/gems/jslint

Instead of downloading with a browser, you can also directly download the sources. This has the advantage that you can update them with the versioning system, and read the commented source code.

git clone https://github.com/douglascrockford/JSLint.git

This gives you HTML and JS source files, but you cannot use them as they are. Follow the build process in README. In a nutshell, you need to git clone two other JS projects of Douglas Crockford, then concatenate (and minimise) some JS files from all three git repositories into web_jslint.js.

I did it like this, with the YUI Compressor:

cat ../ADsafe/adsafe.js ../JSON-js/json2.js intercept.js jslint.js > /tmp/web_jslint.uncomp.js
java -jar ../../yuicompressor-2.4.7.jar /tmp/web_jslint.uncomp.js > web_jslint.js

Then open jslint.html with your browser.

It's pretty easy to recreate what Crockford has on JSLint.com. JSLint.com's online version of the .js is a minified conglomeration of a few files that includes some overhead I don't quite understand, like that ADSAFE stuff. Let's strip it down to a simplest case wrapper instead.

Building your own HTML wrapper for JSLint:

Here's the code to a web page that'll look for jslint.js (the latest version of JSLint can currently be found in github here as raw text) in the same directory and fire away in a similar fashion as JSLint.com does now.

<html>
    <head>
        <script src="jslint.js"></script>

        <script>
            function jslintalizeMe()
            {
                var i, divOut, errs, errsMsg = "";

                divOut = document.getElementById("errors");
                divOut.innerHTML = "";

                if (!JSLINT(document.forms[0].elements[0].value))
                {
                    errs = JSLINT.errors;
                    for (i=0; i < errs.length; i++)
                    {
                        err = errs[i];
                        if (null !== err)
                        {
                            if (undefined !== err.id)
                            {
                                errsMsg += "Error: " 
                                + err.code 
                                + " -- line " 
                                + err.line 
                                + " char " 
                                + err.character + "<br />"
                                + "    " 
                                + err.evidence + "<br />"
                                + "    " +
                                 err.reason + "<br /><br />\n";
                            }
                            else
                            {
                                errsMsg += err.reason;
                            }
                        }
                    }
                    divOut.innerHTML = errsMsg;
                }
            }
        </script>

    </head>

    <body>

        <form>
            <textarea rows="24" cols="80"
                placeholder="// Paste quality code here"></textarea>
            <br />
            <button onclick="jslintalizeMe();return false;">JSLint</button>
        </form>

        <div id="errors"></div>
    </body>
</html>

^ From an old blog post of mine.

It's up to the proverbial reader to make the GUI more gooey, but this reports as well as the JSLint site does now.

sample JSLint output from wrapper code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top