简体   繁体   English

忽略Google Closure中一个文件的编译器警告

[英]ignore compiler warning from one file in Google Closure

I'm using an external library (Phonegap) in a fairly large Closure project. 我在一个相当大的Closure项目中使用外部库(Phonegap)。 Unfortunately Phonegap generates a ton was compiler warnings (all "dangerous use of this"). 不幸的是,Phonegap生成了一个吨编译器警告(所有“危险使用此”)。 Enough that it makes searching through the compiler output for warning about my own code pretty annoying. 足够它使搜索编译器输出警告我自己的代码非常烦人。

Is there a way to silence just the warnings from one file? 有没有办法让一个文件中的警告静音?

I suppose you mean type warnings when you are using VERBOSE or checkTypes. 我猜你的意思是在使用VERBOSE或checkTypes时键入警告。

Put the following into any file: 将以下内容放入任何文件中:

/**
 * @fileoverview
 * @suppress {checkTypes}
 */

to turn off type checking for that file only . 关闭该文件的类型检查。 You can @suppress many other things as well. 你也可以@suppress许多其他的东西。 Read the Closure Compiler docs for more details. 阅读Closure Compiler文档以获取更多详细信息。

Dangerous use of "this" 危险使用“这个”

However, if you are talking about "dangerous use of this" warnings, DO NOT ignore them. 但是,如果您正在谈论“危险使用此”警告,请不要忽视它们。 They refer to places where: 他们指的地方:

  1. You have a namespace 你有一个命名空间
  2. You defined a function within that namespace 您在该命名空间中定义了一个函数
  3. You use "this" inside that function -- and this can refer to the namespace 你在该函数中使用“this” - 这可以引用命名空间
  4. That namespace may be flattened by the compiler 该命名空间可能会被编译器弄平

For example: 例如:

foo.bar.hello = "Hello World!";
foo.bar.baz = function() {
   alert(this.hello);
};
foo.bar.baz();    // this --> foo.bar

The "alert" statment will be flagged by a compiler warning of "dangerous use of this". “警报”声明将由编译器警告“危险使用此”进行标记。 Why? 为什么? Remember, if the compiler flattens the "foo.bar" namespace: 请记住,如果编译器展 “foo.bar”命名空间:

$foo$bar$hello$ = "Hello World!";
$foo$bar$baz$ = function() { alert(this.$hello$); }
$foo$bar$baz$();   // this --> window

Notice I am using debug variables renaming here. 注意我在这里使用调试变量重命名。 In reality, "$foo$bar$baz" may be renamed just to "a". 实际上,“$ foo $ bar $ baz”可能只是重命名为“a”。

You can see immediately that the call to foo.bar.baz() will fail because "this" no longer refers to "foo.bar", but refers to the global object. 您可以立即看到对foo.bar.baz()的调用将失败,因为“this”不再引用“foo.bar”,而是引用全局对象。 You code will crash with a loud CRANK! 您的代码会因响亮的CRANK而崩溃

Exception cases when "this" is OK “这个”没问题的例外情况

Now, there are cases where usage of "this" is OK. 现在,有些情况下使用“this”即可。 For example, in event handlers. 例如,在事件处理程序中。 "this" will automatically point to the DOM node that raised that event. “this”将自动指向引发该事件的DOM节点。

In these cases, you need to use the following type of JsDoc directive: 在这些情况下,您需要使用以下类型的JsDoc指令:

/** @this {Node} */

to specify the type expected for "this" in order to shut the compiler up. 指定“this”的预期类型以关闭编译器。

There should be a --warning_level option that you can use with ClosureCompiler.jar 应该有一个可以与ClosureCompiler.jar一起使用的--warning_level选项

VERBOSE, QUIET, DEFAULT are options. VERBOSE,QUIET,DEFAULT是选项。

You should compile the PhoneGap separately with QUIET. 您应该使用QUIET单独编译PhoneGap。

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

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