简体   繁体   English

Google Closure Annotating不会告诉我我错了

[英]Google Closure Annotating won't tell me I'm wrong

I'm trying out Google Closure, specifically the annotating stuff to enforce type safety. 我正在尝试使用Google Closure,特别是用于强制类型安全的注释内容。 To test I did something wrong, though the compiler won't tell me that it is... 为了测试我做错了什么,虽然编译器不会告诉我它是......

Here's the code: 这是代码:

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

So, I have a variable number which I say is a Number , and I try to assign a string to it. 所以,我有一个变量number ,我说是一个Number ,我尝试为它分配一个字符串。 This shouldn't be possible, right? 这不应该是可能的,对吗? Though the compiler won't tell me that... 虽然编译器不会告诉我......

Why won't it tell me that's wrong? 为什么不告诉我这是错的?

Closure Compiler uses warning levels to determine which checks are enabled during the compilation process. Closure Compiler使用警告级别来确定在编译过程中启用了哪些检查。 The three warning levels are: 三个警告级别是:

  • QUIET 安静
  • DEFAULT 默认
  • VERBOSE VERBOSE

For example, using compilation level SIMPLE_OPTIMIZATIONS , you will still get type-check warnings with the warning level set to 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;
}

Output 产量

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;
             ^

To understand exactly which checks are associated with each warning level, here is the relevant code from WarningLevels.java. 要准确了解哪些检查与每个警告级别相关联,请参阅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 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);
}

Notice that options.checkTypes = true; 请注意, options.checkTypes = true; is only set for the VERBOSE warning level. 仅为VERBOSE警告级别设置。 As Speransky Danil pointed out, type checking is also enabled when using compilation level ADVANCED_OPTIMIZATIONS . 正如Speransky Danil指出的那样,在使用编译级别ADVANCED_OPTIMIZATIONS时也会启用类型检查。

In addition, classes of warnings may be controlled individually with the Closure Compiler application (jar file) using compiler flags: 此外,可以使用编译器标志使用Closure Compiler应用程序(jar文件)单独控制警告类别:

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

The warning classes that may be specified are as follows: 可以指定的警告类如下:

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

For example, type checking warnings could be enabled individually: 例如,可以单独启用类型检查警告:

--jscomp_warning=checkTypes

You should just select advanced optimization mode: 您应该只选择高级优化模式:

// @compilation_level ADVANCED_OPTIMIZATIONS

Then for this code for example: 然后为此代码例如:

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

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

There will be warning: 会有警告:

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

I think you know, but if not, you can play with it online here: http://closure-compiler.appspot.com/home 我想你知道,但如果没有,你可以在网上玩它: http//closure-compiler.appspot.com/home

暂无
暂无

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

相关问题 fullcalendar 不会让我调整事件的大小(光标不显示,我做错了) - fullcalendar won't let me resize an event (cursor doesn't show, i'm doing it wrong) 谁能告诉我我做错了什么 - Can anybody tell me what I'm doing wrong Google不会告诉我哪个航点有误(Javascript API) - Google doesn't tell me which waypoint is wrong (Javascript API) 石头,纸,剪刀的增量得分不起作用,有人可以告诉我我做错了吗? - Rock, Paper, Scissors Increment Score not working, can somebody tell me what i'm doing wrong? 使用JS和SCSS的导航动画,你能告诉我我哪里出错了吗? - Navigation animation using JS and SCSS, can you tell me where I'm going wrong? 试图抓住输入值并显示,谁能告诉我我在这里做错了什么? - Trying to grap the input value and display, Can anyone tell me what i'm doing wrong here? Javascript-Google闭包注释属性类型 - Javascript - Google closure annotating attribute types 我正在尝试在 firebase 数据库上推送数据,但我收到此错误“无法读取属性“推送””,谁能告诉我我做错了什么? - I'm trying to push data on firebase database but I'm getting this error 'Cannot read property "push" ', can anyone tell me what I'm doing wrong? Discord.js - 类型错误:无法读取未定义的属性“声音”。 有人可以告诉我我做错了什么吗? - Discord.js - TypeError: Cannot read property 'voice' of undefined. Can someone tell me what I'm doing wrong? Vue.js 不会告诉我错误的行号 - Vue.js won't tell me line number of error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM