19

I know that the Google Closure Compiler does type checking—but are there any alternatives, preferably that aren't so tightly coupled with a library and optimizer?

If not, is there any way to have the Google Closure Compiler only do static analysis?

(By static analysis here, I mean things like defining types for arguments and so on that I can run through something to give me warnings if I make a typo or pass the wrong type.)

Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
  • I think you are going at it in the opposite direction. Type checking is a feature of the compiler's compilation process. It is there to facilitate compilation, not really intended to be used stand-alone as it depends quite a lot on compiler features. However, you don't need to use the Closure Library to use the compiler. – Stephen Chung Jul 14 '11 at 04:02
  • I know that—that's why I'm looking for alternatives. – Aaron Yodaiken Jul 14 '11 at 04:33
  • Well, there is something called the Closure-Linter that may do what you want... – Stephen Chung Jul 14 '11 at 04:46
  • 1
    What does the compiler so that you can't just ignore the output file? – John Dec 20 '11 at 20:51
  • @luxun Your website looks a lot like mine. – Engineer Oct 23 '12 at 13:05
  • I found a Javascript function that can check the types of a function's arguments: http://stackoverflow.com/a/13926334/975097 – Anderson Green Dec 18 '12 at 05:29
  • I'm echoing John's comment. Use Closure-compiler with SIMPLE_OPTIMZIATIONS and VERBOSE warnings and ignore the output file. – Chad Killingsworth May 15 '13 at 14:16
  • @StephenChung The primary benefit of type checking is to catch basic errors early. Languages from the ML family and derivatives (OCaml, SML, F#, Haskell) infer types, i.e. the programmer does not need to specify the type of the variables. In other words the compiler works for the programmer, not the other way around. – Martin Jambon Jan 05 '14 at 08:11

3 Answers3

2

There's Doctor JS, which is a Mozilla project that primarily (as I understand it, at least) does type-checking for JS.

gsnedders
  • 5,532
  • 2
  • 30
  • 41
  • This seems useful, although as far as I can see there is no way to run this directly from the command line (for use inside a build/testing script, for example). Is there such a means of access? – Aaron Yodaiken Jul 13 '11 at 00:14
  • I tried it on one of my scripts and it didn't work - said it wasn't working. Then tried it on a very short script and it did work, so I don't know if it just doesn't work on larger scripts or if it has bugs or what. – jfriend00 Jul 13 '11 at 00:49
  • It's available on [Github](https://github.com/mozilla/doctorjs), so you can try installing and running it locally. – Tikhon Jelvis Jan 14 '12 at 01:29
0

Microsoft's AJAX Minifier is a little more relaxed about the amount of prep you need to do to a JS file to get useful results out of it. You can run it with defaults and get out a highly minified file that still works with outside code: http://ajaxmin.codeplex.com/

But, both Closure Compiler and Ajax Minifier can only do very limited static analysis beyond basic linting, because of how Javascript is designed. Accessing an undeclared property may just be checking for undefined, assigning an undeclared variable just means declaring it in the global scope, assigning an object to a variable that contained a number is legal, etc. There's a lot that's legal in JS that your typical language (Java, C#) considers out of bounds, so without declaring types, boundaries and expectations for a specific compiler you're unfortunately limited in the errors you can prevent.

I'd be a bit more interested in something that can transform between the big 2 (MS and Google). It would be useful for IDE support, testing code size with advanced optimizations, etc.

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
0

I have been quite happy with the intellij idea / webstorms editor, which parses jsdoc and does its own static analysis to flag potential or actual type safety errors. It has proven quite useful, although a bit of work was needed to get inheritance to work with some common frameworks. Due to the tons of approaches possible with javascript prototypal inheritance, the compiler needs a bit more help than for other languages.

It's a commercial tool, but I'm able to use it for java, php, javascript, python and ruby projects, all with some pretty decent static analysis and refactoring helpers. I used to do a lot with emacs and running node.js processes for jshint and closure compiler, but this is a lot less brittle.

wesen
  • 631
  • 6
  • 10