6

我正在尝试 Google Closure,特别是用于强制类型安全的注释内容。为了测试我做错了什么,尽管编译器不会告诉我它是......

这是代码:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
 * A card.
 * @constructor
 * @param {String} cardName The exact name of the card
 * @param {Kinetic.Layer} layer The layer for the card
 */
function CardObject(cardName, layer)
{
    /** @type {Number} */
    var number = cardName;
}

所以,我有一个变量number,我说它是 a Number,我尝试为它分配一个字符串。这应该是不可能的吧?虽然编译器不会告诉我...

为什么它不告诉我这是错的?

4

2 回答 2

7

Closure Compiler 使用警告级别来确定在编译过程中启用了哪些检查。三个警告级别是:

  • 安静的
  • 默认
  • 详细

例如,使用编译级别SIMPLE_OPTIMIZATIONS,您仍然会收到警告级别设置为的类型检查警告VERBOSE

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// @warning_level VERBOSE
// ==/ClosureCompiler==

/**
 * A card.
 * @constructor
 * @param {String} cardName The exact name of the card
 * @param {Kinetic.Layer} layer The layer for the card
 */
function CardObject(cardName, layer)
{
    /** @type {Number} */
    var number = cardName;
}

输出

Number of warnings: 2

JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type Kinetic.Layer at line 5
character 10  
* @param {Kinetic.Layer} layer The layer for the card
          ^
JSC_TYPE_MISMATCH: initializing variable
found   : (String|null|undefined)
required: (Number|null) at line 10 character 13
var number = cardName;
             ^

要准确了解与每个警告级别相关联的检查,以下是来自 WarningLevels.java 的相关代码。

安静的

/**
 * Silence all non-essential warnings.
 */
private static void silenceAllWarnings(CompilerOptions options) {
  // Just use a ShowByPath warnings guard, so that we don't have
  // to maintain a separate class of warnings guards for silencing warnings.
  options.addWarningsGuard(
      new ShowByPathWarningsGuard(
          "the_longest_path_that_cannot_be_expressed_as_a_string"));

  // Allow passes that aren't going to report anything to be skipped.

  options.checkRequires = CheckLevel.OFF;
  options.checkProvides = CheckLevel.OFF;
  options.checkMissingGetCssNameLevel = CheckLevel.OFF;
  options.aggressiveVarCheck = CheckLevel.OFF;
  options.checkTypes = false;
  options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.OFF);
  options.checkUnreachableCode = CheckLevel.OFF;
  options.checkMissingReturn = CheckLevel.OFF;
  options.setWarningLevel(DiagnosticGroups.ACCESS_CONTROLS, CheckLevel.OFF);
  options.setWarningLevel(DiagnosticGroups.CONST, CheckLevel.OFF);
  options.setWarningLevel(DiagnosticGroups.CONSTANT_PROPERTY, CheckLevel.OFF);
  options.checkGlobalNamesLevel = CheckLevel.OFF;
  options.checkSuspiciousCode = false;
  options.checkGlobalThisLevel = CheckLevel.OFF;
  options.setWarningLevel(DiagnosticGroups.GLOBAL_THIS, CheckLevel.OFF);
  options.setWarningLevel(DiagnosticGroups.ES5_STRICT, CheckLevel.OFF);
  options.checkCaja = false;
}

默认

/**
 * Add the default checking pass to the compilation options.
 * @param options The CompilerOptions object to set the options on.
 */
private static void addDefaultWarnings(CompilerOptions options) {
  options.checkSuspiciousCode = true;
  options.checkUnreachableCode = CheckLevel.WARNING;
  options.checkControlStructures = true;
}

详细

/**
 * Add all the check pass that are possibly relevant to a non-googler.
 * @param options The CompilerOptions object to set the options on.
 */
private static void addVerboseWarnings(CompilerOptions options) {
  addDefaultWarnings(options);

  // checkSuspiciousCode needs to be enabled for CheckGlobalThis to get run.
  options.checkSuspiciousCode = true;
  options.checkGlobalThisLevel = CheckLevel.WARNING;
  options.checkSymbols = true;
  options.checkMissingReturn = CheckLevel.WARNING;

  // checkTypes has the side-effect of asserting that the
  // correct number of arguments are passed to a function.
  // Because the CodingConvention used with the web service does not provide a
  // way for optional arguments to be specified, these warnings may result in
  // false positives.
  options.checkTypes = true;
  options.checkGlobalNamesLevel = CheckLevel.WARNING;
  options.aggressiveVarCheck = CheckLevel.WARNING;
  options.setWarningLevel(
      DiagnosticGroups.MISSING_PROPERTIES, CheckLevel.WARNING);
  options.setWarningLevel(
      DiagnosticGroups.DEPRECATED, CheckLevel.WARNING);
}

请注意,options.checkTypes = true;仅为 VERBOSE 警告级别设置。正如Speransky Danil指出的那样,在使用编译级别时也启用了类型检查ADVANCED_OPTIMIZATIONS

此外,警告类别可以通过使用编译器标志的 Closure Compiler 应用程序(jar 文件)单独控制:

  • --jscomp_off
  • --jscomp_warning
  • --jscomp_error

可以指定的警告等级如下:

  • 访问控制
  • ambiguousFunctionDecl
  • 检查正则表达式
  • 检查类型
  • 检查变量
  • 常量
  • 常量属性
  • 已弃用
  • 重复消息
  • es5严格
  • 外部验证
  • 文件概述标签
  • 全球这个
  • internetExplorerChecks
  • 无效转换
  • 缺少属性
  • 非标准JsDocs
  • 严格模块深度检查
  • 类型失效
  • 未定义名称
  • 未定义变量
  • 未知定义
  • 无用代码
  • 能见度

例如,可以单独启用类型检查警告:

--jscomp_warning=checkTypes
于 2012-09-02T15:45:12.807 回答
0

您应该只选择高级优化模式:

// @compilation_level ADVANCED_OPTIMIZATIONS

然后对于此代码,例如:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
* @param {String} str
*/
function func(str) {
  /** @type {Number} */
  var num = str;
}

会有警告:

JSC_TYPE_MISMATCH: initializing variable
found   : (String|null)
required: (Number|null) at line 6 character 10
var num = str;

我想你知道,但如果不知道,你可以在这里在线玩它:http: //closure-compiler.appspot.com/home

于 2012-09-02T15:22:48.160 回答