开发者

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%


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.


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


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.


All the credits go to @kayahr but since time went by. I wanted to provide an update to his answer which is based on a old version of the compiler.

You need to recompile the source with type checking enforced. To do so:

  1. download the source git clone https://code.google.com/p/closure-compiler/ and go to closure-compiler

  2. Edit src/com/google/javascript/jscomp/CompilerOptions.java and set checkTypes to true in. You can also play around with all kinds of compilation and optimization variables.

  3. Compile the code by running ant

Watch out though, tightenTypes is experimental and quite broken.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜