1

我从这个Cloudflare链接复制了一个 HTML5Shiv.min.js 版本,当我将文件导入 Adob​​e Brackets 时,JSLint 编译器告诉我脚本包含以下错误:

4   Missing 'use strict' statement.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   'c' is already defined.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Expected ';' and instead saw '='.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Unreachable '=' after 'return'.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Expected an identifier and instead saw '='.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Stopping. (100% scanned).
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

如果我决定使用它,这真的会起作用吗?缺少语句和无法访问的运算符的代码。也许 JSLint 不是最新的或者有些东西已经关闭,但如果可能的话,我想获得第二个意见。

谢谢你。

4

1 回答 1

4

不要出汗。你可以使用图书馆。唯一“缺少”的行是"use strict";,这是 JSLint 喜欢的,但您不需要使用它。(这是一个体面的讨论use strict。)

JSLint 和其他 linter 寻找两种类型的“错误”:功能性样式。JSLint 发现的许多样式错误实际上会转化为您想要避免的逻辑错误。这是一个很棒的工具。

同时,您可以在不破坏代码功能的情况下产生 JSLint 不喜欢的样式错误,尤其是在缩小代码中。当您看到 JSLint 识别第三方代码或任何人的缩小代码中的样式错误时,请不要担心。它们不一定是功能问题。

通常,您应该免除 3rd 方库的 linting。对于外部库,你无能为力,除非你想自己 fork 和 lint,这太疯狂了。;^) 而且,缩小代码通常采用对 lint 不友好的快捷方式。在你缩小代码之前检查你的代码以保持它的高质量,但不要担心你不应该接触的 QAing 库。假设他们有另一种确保高质量的方法,这可能包括使用不同的 linter,或具有不同规则集的 linter。

这是一个“肮脏”的秘密......

jQuery borks JSLint 也...

例如,即使是un minified jQuery,也不会 lint。即使我添加一行来告诉 JSLint 取消空格“错误”,缺少“使用严格”,并让它知道它应该假设一个浏览器,我得到......

'module' was used before it was defined.
    if ( typeof module === "object" && typeof module.exports === "object" ) {
line 18 character 44'module' was used before it was defined.
    if ( typeof module === "object" && typeof module.exports === "object" ) {
line 26 character 3'module' was used before it was defined.
        module.exports = global.document ?
line 39 character 3Unexpected 'typeof'. Use '===' to compare directly with undefined.
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
line 49 character 5 Combine this with the previous 'var' statement.
    var slice = deletedIds.slice;
line 51 character 5 Combine this with the previous 'var' statement.
    var concat = deletedIds.concat;
line 53 character 5 Combine this with the previous 'var' statement.
    var push = deletedIds.push;
line 55 character 5 Combine this with the previous 'var' statement.

等等等等没有人会争辩说 jQuery 是假的,你知道吗?因此,如果 Cloudfire 或任何其他文件给您同样的错误集,请不要担心。

底线:不要对图书馆大喊大叫,尤其是缩小的图书馆。Linter是代码质量工具。如果其他人有其他方法来保持他们的代码正常工作并且它为您的使用进行了很好的测试,请不要理会 lib。;^)

于 2015-03-21T01:39:38.403 回答