忽略Google Closure中一个文件的编译器警告 [英] ignore compiler warning from one file in Google Closure

查看:125
本文介绍了忽略Google Closure中一个文件的编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个相当大的Closure项目中使用外部库(Phonegap)。不幸的是,Phonegap生成了一个吨编译器警告(所有危险使用此)。足够它使搜索编译器输出警告我自己的代码非常烦人。

I'm using an external library (Phonegap) in a fairly large Closure project. Unfortunately Phonegap generates a ton was compiler warnings (all "dangerous use of this"). 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?

推荐答案

我想你的意思是在使用VERBOSE或checkTypes时键入警告。

I suppose you mean type warnings when you are using VERBOSE or checkTypes.

将以下内容放入任何文件中:

Put the following into any file:

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

关闭仅为该文件的类型检查 。您也可以 @suppress 等许多其他内容。阅读Closure Compiler文档了解更多详情。

to turn off type checking for that file only. You can @suppress many other things as well. Read the Closure Compiler docs for more details.

但是,如果你是谈论危险使用此警告,不要忽略它们。它们指的是:

However, if you are talking about "dangerous use of this" warnings, DO NOT ignore them. They refer to places where:


  1. 你有一个命名空间

  2. 你在那个命名空间中定义了一个函数

  3. 你在该函数中使用this - 这可以引用命名空间

  4. 该命名空间可能被夷为平地由编译器

  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
  4. That namespace may be flattened by the compiler

例如:

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

警报声明将被编译器警告危险使用此 。为什么?请记住,如果编译器展平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$hello$ = "Hello World!";
$foo$bar$baz$ = function() { alert(this.$hello$); }
$foo$bar$baz$();   // this --> window

注意我在这里使用调试变量重命名。实际上,$ foo $ bar $ baz可能只是重命名为a。

Notice I am using debug variables renaming here. In reality, "$foo$bar$baz" may be renamed just to "a".

你可以立即看到对 foo的调用.bar.baz()将失败,因为this不再引用foo.bar,而是引用全局对象。您的代码会因大声 CRANK而崩溃!

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. You code will crash with a loud CRANK!

现在,有些情况下使用this就可以了。例如,在事件处理程序中。 this将自动指向引发该事件的DOM节点。

Now, there are cases where usage of "this" is OK. For example, in event handlers. "this" will automatically point to the DOM node that raised that event.

在这些情况下,您需要使用以下类型的JsDoc指令:

In these cases, you need to use the following type of JsDoc directive:

/** @this {Node} */

指定this的预期类型以关闭编译器。

to specify the type expected for "this" in order to shut the compiler up.

这篇关于忽略Google Closure中一个文件的编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆