【问题标题】:Suppressing or resolving compiler errors in goog.base抑制或解决 goog.base 中的编译器错误
【发布时间】:2015-04-09 04:49:02
【问题描述】:

我在源代码上使用 Closure Compiler,最近决定通过 --jscomp_warning=reportUnknownTypes 启用最严格的模式。唉,它在goog.base 本身内部触发了很多警告!我已经解决了我自己代码中的所有问题,现在我正在寻找一种方法来消除/删除闭包库代码中的错误。

我尝试修复 base.js 中的错误,但很快意识到这是不可行的。文件中大约有 108 个错误,在大多数情况下它们是真正的错误,因为 goog.base 不太关心类型:在那里定义像 {?}{*}.

我还尝试使用--warnings_whitelist_file 来消除我不关心的警告,但它也不起作用。对于错误:

..\js\google\base.js:204: WARNING - could not determine the type of this expression cur[part] = opt_object; ^

我在白名单文件中尝试了不同的形式,但都没有奏效:

..\\js\\google\\base.js:204 could not determine the type of this expression ..\\js\\google\\base.js:204 WARNING - could not determine the type of this expression ..\js\google\base.js:204 could not determine the type of this expression ..\js\google\base.js:204 WARNING - could not determine the type of this expression ../js/google/base.js:204 could not determine the type of this expression ../js/google/base.js:204 WARNING - could not determine the type of this expression ..\js\google\base.js:204 WARNING - could not determine the type of this expression cur[part] = opt_object;

是否有人有可行的解决方案来启用此模式,并且不会因闭包库本身的错误而收到垃圾邮件?

我使用最新的闭包编译器,它是: Version: v20150315 Built on: 2015/03/17 14:18

【问题讨论】:

  • reportUnknownTypes 非常垃圾,一般不推荐。总有一天我们会转而使用github.com/google/closure-compiler/issues/152(实现该检查的更细微的版本),我们可能会建议打开它。
  • 好吧,它是垃圾邮件,但是一旦我修复了所有错误(虽然有时是黑客),代码变得更加正确。几个未被注意到的错误是固定的。所以,尽管有很多麻烦,但它是一种非常有用的模式,我想继续使用它。
  • 顺便说一句,我是 githab 上的“ihsoft” :) 最近编译器的错误来自我。
  • 如果您使用 Java API 运行编译器,则可以配置警告保护以不显示警告。还应该注意的是,虽然类型值是未知的,但它们应该是未知的,更好的检查不会警告声明为未知的值。作为一致性框架的一部分,有一个更有限但仍然非常有用的检查(未知的 this 检查和未知的类属性检查)

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


【解决方案1】:

所以,我决定在base.js 中使用静音警告。为此,我调查了编译器源代码(感谢作者,它是开源的),我发现标志描述与其实际效果不符。描述说:

A file containing warnings to suppress. Each line should be of the form <file-name>:<line-number>? <warning-description>

但事实上,这个守卫会拦截匹配的错误并将它们转换为警告。此外,行号被完全忽略。我认为在这种行为中没有使用此标志,但我敢打赌作者有充分的理由以这种方式实现它。

无论如何,我从主分支分叉并修复了标志的行为。现在匹配的错误和警告实际上被抑制(静音)。现在在匹配中考虑行号。

白名单文件的语法与错误/警告输出非常相似。例如,如果您收到如下错误(或警告):

..\js\google\base.js:120: ERROR - could not determine the type of this expression if (goog.getObjectByName(namespace)) { ^

那么白名单文件中的相关记录将是:

..\js\google\base.js:120 could not determine the type of this expression

您可以将多行错误的内容添加为 cmets,在它们前面加上 #,但只有第一行将用于匹配。

如果这是您需要的,您可以通过以下两种方式之一获取二进制文件:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2013-02-21
    • 2018-05-31
    相关资源
    最近更新 更多