Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Closure Compiler 100% typed

How can I get my application to be 100% typed in regard to google closure compiler?

I already tagged everything with jsdoc comments. Is it even possible to get 100? I'm at 64,6%

like image 651
Sven Hecht Avatar asked Nov 19 '10 23:11

Sven Hecht


3 Answers

It IS possible to achieve 100%. My own projects are 100% typed. The closure compiler can output warnings about expressions with unknown types. Unfortunately there is no command line option to enable this feature. You have to modify the source code to enable it:

  1. Download the current sources:

    git clone https://code.google.com/p/closure-compiler/

  2. Edit src/com/google/javascript/jscomp/CompilerOptions.java and change the line reportUnknownTypes = CheckLevel.OFF to reportUnknownTypes = CheckLevel.WARNING

  3. Compile the closure-compiler by simply calling ant in the root directory. The modified compiler.jar is now in the build directory.

When you use this modified compiler on your scripts then it spits out lots of warnings about all the expressions for which the compiler couldn't determine the type. When you fix all these warnings in your code then it is 100% typed.

like image 72
kayahr Avatar answered Oct 30 '22 11:10

kayahr


The compiler has a flag you can set to make unknown types output a warning.

--jscomp_warning=reportUnknownTypes

You will also need to increase the warning level.

--warning_level=VERBOSE
like image 45
Cristian Avatar answered Oct 30 '22 10:10

Cristian


I tried compiling the goog.net.XhrIo as a test:

goog.require('goog.Uri.QueryData');
goog.require('goog.debug.ErrorHandler');
goog.require('goog.net.XhrIo');

goog.net.XhrIo;

when I compile this I have this result:

20-nov-2010 1:12:21 com.google.javascript.jscomp.LoggerErrorManager printSummary
WARNING: 0 error(s), 1 warning(s), 91,5% typed
JSC_USELESS_CODE. Suspicious code. This code lacks side-effects. Is there a bug? at test.js line 5 : 0

It seems as if the closure library itself is not 100% typed and I don't think the goal is to reach 100%. Javascript is not a statically typed language. Closure tries to bring some benefits of statically typed languages into javascript. Which is good. But that doesn't mean you have to bring in the burdens of this type of languages.

EDIT:

I tried to compile an empty file and the result was 90.4%. I think this means that the base.js with all the primitive functions is not 100% typed. So I did some more experimenting and Found out that when I also put type information on all my local variables, the percentage goes up. I don't think it is necessary to put type information on all your local variables. I mean imagine this:

/** @type {number} */
var i = 0;
for(i = 0; i < 10; i++) {
  // do something
}

This can't be the goal of compiling with the closure compiler. This is proven by the fact that compiling base.js doesn't result in 100%. I try to keep this number between 85% and 95% in my development. Depending on your time, programming style and the application you're working on this can vary off course. The best guideline is to try to keep the percentage constant throughout the development of your application, whether it's 60% or 90%. Just do as much typing as needed to keep you comfortable with your own code.

like image 43
Jan Avatar answered Oct 30 '22 10:10

Jan