简体   繁体   中英

Closure Compiler JsDoc annotations to replace tests in source code?

Are the JSDoc annotations sufficient to test parameters validity?

Example: in the following "add" method, the parameter (Point object) is defined as Non-nullable Point type. In this method and using Closure Compiler, can I then skip the code required to test the value of this parameter:

  1. if (!point) {...
  2. if (!(point instanceof Point)) {...

Thanks

class Point{
  /**
   * Add x/y values to the Point object
   *
   * @param {!Point} point The x/y values to add.
   *
   */
  add(point) {
    this.x += point.x;
    this.y += point.y;
  }
}

Yes, provided any code calling that method is being compiled.

If you have code calling that method which is not being compiled, then the compiler cannot guarantee the type being passed. For example if you make a library to be called by non-compiled code. In that case you are probably exporting the add method to make it accessible to outside non-compiled code.

If you are not exporting the add method then only compiled code can access it.

See the section Checks Provided by the Compiler on page 408 in Closure The Definitive Guide by Michael Bolin. You need to specify certain flags to the compiler such as:

--jscomp_error=checkTypes
--warning_level=VERBOSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM