Tag Archives: ClosureCompiler

closure compiler bug?

When I compile the JavaScript in following with Closure Compiler, usually I get an warning message “WARNING – Bad type annotation. Unknown type ClassA”. But, sometimes the compiler put no warning message.

(function() {

  /**
   * @private
   * @constructor
   */
  function ClassA() {
  }

  /** @type {function()} */
  ClassA.prototype.say = function() {
    console.log("hoge");
  };

  /**
   * @private
   * @constructor
   */
  function ClassB() {
    /** @type {ClassA} */
    this.a = new ClassA();
  }

  /**
   * @expose
   */
  ClassB.prototype.say = function() {
    this.a.say();
  };

  /* export */
  window['ClassB_Singleton'] = new ClassB();
})();

This is the output with no warning message.

X:\>java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --warning_level VERBOSE a.js
(function(){function a(){}function b(){this.a=new a}a.prototype.say=function(){console.log("hoge")};b.prototype.say=function(){this.a.say()};window.ClassB_Singleton=new b})();

This is the output with warning message.

X:\>java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --warning_level VERBOSE a.js
a.js:20: WARNING - Bad type annotation. Unknown type ClassA
    /** @type {ClassA} */
               ^

0 error(s), 1 warning(s), 93.3% typed
(function(){function a(){}function b(){this.a=new a}a.prototype.say=function(){console.log("hoge")};b.prototype.say=function(){this.a.say()};window.ClassB_Singleton=new b})();

Which is the behavior by the compiler design?

# Of-course, it can be disabled completely with jscomp_off checkTypes option…