Should I worry about these JSLint warnings on HTML5Shiv.min.js version 3.7.2?

蓝咒 提交于 2019-12-23 04:01:36

问题


I copied a version of HTML5Shiv.min.js from this Cloudflare link, and when I import the file into Adobe Brackets, the JSLint compiler tells me that the script contains the following errors:

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

Is this really going to work if I decide to use it? Code with missing statements and unreachable operators. Maybe JSLint is not up to date or something is off, but I would like to get a second opinion on this if possible.

Thank you.


回答1:


Don't sweat it. You're fine to use the library. The only "missing" line is "use strict";, which is something JSLint likes, but you're not required to use it. (Here's a decent discussion of use strict.)

JSLint and other linters look for two types of "errors": functional and style. Many of the style errors that JSLint finds will actually translate to logical errors you want to avoid. It's a great tool.

At the same time, you can make style errors that JSLint doesn't like without breaking your code's function, especially in minified code. Don't worry when you see JSLint identifying style errors in third-party code -- or anyone's minified code. They're not necessarily functional issues.

As a rule, you should exempt 3rd party libraries from your linting. There's nothing you can do about external libraries unless you want to fork them and lint them yourself, which is, well, insane. ;^) And, again, minified code often takes shortcuts that aren't lint-friendly. Lint your code before you minify to keep its quality high, but don't worry about QAing libraries you shouldn't be touching anyhow. Assume they have another method for ensuring high quality, which might include using a different linter, or a linter with a different set of rules.

And here's a "dirty" secret...

jQuery borks JSLint too...

jQuery, even unminified, doesn't lint either, for instance. Even if I add a line to tell JSLint to lay off of the whitespace "error", missing "use strict", and let it know it should assume a browser, I get...

'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.

etc etc etc. And nobody is going to argue that jQuery is bogus, you know? So don't worry if Cloudfire or any other file gives you the same set of errors.

Bottom line: Don't sweat lint yelling at you about libraries, especially minified ones. Linters are code quality tools for your code. If others have other means of keeping their code working and it tests well for your uses, leave the lib alone. ;^)



来源:https://stackoverflow.com/questions/29177993/should-i-worry-about-these-jslint-warnings-on-html5shiv-min-js-version-3-7-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!