简体   繁体   English

使用Closure Compiler JsDoc注释替换源代码中的测试?

[英]Closure Compiler JsDoc annotations to replace tests in source code?

Are the JSDoc annotations sufficient to test parameters validity? JSDoc注释是否足以测试参数的有效性?

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: 然后,在这种方法中并使用Closure Compiler,我可以跳过测试该参数值所需的代码:

  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. 在这种情况下,您可能要导出add方法,以使其可以被外部未编译的代码访问。

If you are not exporting the add method then only compiled code can access it. 如果不导出add方法,则只有编译后的代码可以访问它。

See the section Checks Provided by the Compiler on page 408 in Closure The Definitive Guide by Michael Bolin. 请参阅《 封闭式 Michael Bolin 权威指南》中第408页的“ 由编译器提供的检查 ”部分。 You need to specify certain flags to the compiler such as: 您需要为编译器指定某些标志,例如:

--jscomp_error=checkTypes
--warning_level=VERBOSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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