Google Closure Annotating不会告诉我我错了

时间:2012-09-02 14:46:28

标签: javascript compiler-warnings google-closure-compiler google-closure

我正在尝试使用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我说是Number,我尝试给它分配一个字符串。这不应该是可能的,对吗?虽然编译器不会告诉我......

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

2 个答案:

答案 0 :(得分:7)

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

  • QUIET
  • DEFAULT
  • VERBOSE

例如,使用编译级别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中的相关代码。

QUIET

/**
 * 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;
}

DEFAULT

/**
 * 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;
}

VERBOSE

/**
 * 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

可以指定的警告类如下:

  • accessControls
  • ambiguousFunctionDecl
  • checkRegExp
  • checkTypes
  • checkVars
  • 常量
  • constantProperty
  • 弃用
  • duplicateMessage
  • es5Strict
  • externsValidation
  • fileoverviewTags
  • globalThis
  • internetExplorerChecks
  • invalidCasts
  • missingProperties
  • nonStandardJsDocs
  • strictModuleDepCheck
  • typeInvalidation
  • undefinedNames
  • undefinedVars
  • unknownDefines
  • uselessCode
  • 能见度

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

--jscomp_warning=checkTypes

答案 1 :(得分: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