【问题标题】:Type checking with closure compiler not implicitly on?没有隐式打开闭包编译器的类型检查?
【发布时间】:2013-04-17 13:14:10
【问题描述】:

我正在编译文件并获得工作编译代码,但注释似乎完全被忽略了;没有警告没有错误。使用 calcdeps.py 通过以下命令编译我的代码:

set calc="D:\software\closure compiler\library\closure\bin\calcdeps.py"
c:\Python27\python.exe %calc% ^
--path D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\ ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\Mediator.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\DomDependent.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\WorkFlow.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\Messenger.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\data.js ^
--compiler_jar "D:\software\closure compiler\compiler.jar" ^
--output_mode compiled ^
--compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" ^
--compiler_flags="--formatting=PRETTY_PRINT" ^
--output_file D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\main.js
pause

例如在 Messenger.js 我有一个函数:

/**
 * Replaces words in matches with a yes/no/all box
 * @param {Array} matches contains the items of myApp.data that matched words in text
 * @param {string} text contains the cleaned up user input (no html)
 */
myApp.Messenger.builtReplacewithOptions=function(matches,text){

变量matches必须是一个数组,所有调用这个函数的代码都用一个数组调用它。为了测试类型检查,我将数组更改为字符串,如下所示:

 * @param {string} matches contains the items of myApp.data that matched words in text

再次编译但没有给出警告或错误。尝试在批处理文件中向编译器添加参数:

--compiler_flags="--jscomp_warning=checkTypes" ^

现在我收到警告。我的问题是:我必须打开各种检查吗?有没有办法让所有检查都打开而我只明确关闭一些?

【问题讨论】:

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


    【解决方案1】:

    可以设置标志--warning_level=VERBOSE,相当于

    --jscomp_warning=checkTypes --jscomp_error=checkVars \
    --jscomp_warning=deprecated --jscomp_error=duplicate \
    --jscomp_warning=globalThis --jscomp_warning=missingProperties \
    --jscomp_warning=undefinedNames --jscomp_error=undefinedVars
    

    仍有一些检查将被关闭,如果需要,您必须明确启用它们。默认情况下无法启用 all

    有关警告/错误类型的完整列表,请参阅https://code.google.com/p/closure-compiler/wiki/Warnings

    【讨论】:

    • 谢谢,它有效。收到很多关于 goog.events 具有未知类型的警告,并且 console.log 已损坏,但它做了我想要它做的事情。修复了console.log,现在使用调解器将console.log调用传递给日志的(调解器)事件处理程序,并使用myApp.mediator.trigger(“log”,someObject)从编译的代码中记录日志;对 console.log 的实际调用是从 html 页面(未编译的代码)完成的。直到我弄清楚如何使用 goog.debug 但我不确定这是否像 firebug 的 console.log 一样信息丰富。
    • 未知类型是由于缺少 deps.js 文件(提供前向类型声明)。我不知道为什么 Closure Library 的人默认不这样做。 console.log 出了什么问题?
    • @John console.log 必须是 window.console.log。我一直想提交一个更改来解决这个问题,但它在我的清单上已经远远落后了。
    • 奇怪,它只需要成为默认外部的一部分:code.google.com/p/closure-compiler/source/browse/contrib/…
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多