views:

1322

answers:

5

I'm used to having my compiler complain when I do something stupid like a typo on a variable name but JavaScript has a habit of letting this pass.

Are there any static analysis tools for JavaScript?

Preferably a free one (either beer or speech) because I'm a tightarse.

+2  A: 

I like Jslint for this sort of thing...

Ishmael
Cool. I found an Eclipse plugin for JSLint at http://www.rockstarapps.com/joomla-1.5.8/products/jslint-eclipse-plugin.html which looks quite good too. Note that one needs to install from the 'Eclipse Update site for the Beta Version of jsLex 1.2.2' to get the JSLint functionality.
Mat
+7  A: 

I agree that JSLint is the best place to start. Note that JavaScript Lint is distinct from JSLint. I’d also suggest checking out Jsure (jsure.org has been taken over by something quite different), which in my limited testing did better than either of them, though with some rough edges in the implementation—the Intel Mac version crashed on startup for me, though the PowerPC version ran fine even on Intel, and the Linux version ran fine as well. (The developer, Berke Durak, said he'd get back to me when this was fixed, but I haven't heard from him.)

Don’t expect as much from JavaScript static analysis as you get from a good C checker. As Durak told me, “any non-trivial analysis is very difficult due to Javascript's dynamic nature.”

(Another, even more obscure Mac-only bug, this time with JSLint’s Konfabulator widget: Dragging a BBEdit document icon onto the widget moves the document to the trash. The developer, Douglas Crockford, hadn’t tried the widget on a Mac.)

10 August 2009: Today at the Static Analysis Symposium, Simon Holm Jensen presented a paper on TAJS: Type Analyzer for JavaScript, written with Anders Møller and Peter Thiemann. The paper doesn’t mention the above tools, but Jensen told me he’d looked at some of them and wasn’t impressed. The code for TAJS should be available sometime this summer.

Flash Sheridan
the jsure link is broken.found it here: http://aurochs.fr/jsure.html
Sven Hecht
+4  A: 

Google's "Closure" JS compiler produces configurable warnings and errors at compile-time. It definitely finds misspelled variables and methods, plus arity mistakes. If you're willing to write JsDoc the Closure way, it can do a lot with type information, too.

The YUI "Compressor" tool can produce warnings too, but haven't tried it yet.

I haven't had much luck with the Aptana IDE, built on Eclipse, but other people like it. See Stack Overflow discussion of JS IDEs.

The IntelliJ IDE, which isn't free last I checked, has frickin' excellent JS support. It will detect and highlight misspelled vars and methods as you type, and more. It's got autocomplete, too.

awhyte
A: 

Our SD ECMAScript CloneDR is a tool for finding exact and near-miss copies of duplicated code across large JavaScript source code bases.

It uses the language syntax to guide the detection, so it will find clones in spite of format changes, inserted/deleted comments, renamed variables and even some inserted/deleted statements.

The site has a sample CloneDR run on Google's Closure library.

Ira Baxter
has anyone tried this one yet? Didn't find any download or order buttons...
Sven Hecht
@Sven: ask at the website.
Ira Baxter
A: 

Full disclosure, I'm behind this: http://www.toptensoftware.com/minime which does minification, obfuscation and a reasonable set of lint style checks.

cantabilesoftware