1

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.

Eamon Bohan
  • 522
  • 2
  • 8
  • 22
  • Of course it will work – do you really think the HTML5Shiv script would have gotten that popular, if it didn’t? What you need to realize is that a linter is a tool that alerts you to _potential_ problems, that are not necessarily real _errors_. – CBroe Mar 21 '15 at 00:56
  • Alright, I get that, but an appeal to the masses is not good enough for me. I just wanted to understand what was going on. But thanks. I am just beginning to learn JScript and some of those errors sound like the type of thing that would not compile in other languages! – Eamon Bohan Mar 21 '15 at 01:09
  • 1
    Well, what works for the masses can’t be that wrong at least ;-) And the web developer community is usually quite engaged when it comes to this kind of scripts – if someone brings up such a solution to solve a common problem, then it is reviewed by others and improved upon where necessary. – CBroe Mar 21 '15 at 01:22
  • 1
    And again, such linter tools have the main purpose of alerting to _potential_ problems, and to write scripts to a “stricter” standard. They can often say, “this looks like it _might_ cause a problem”, but that does not necessarily mean that there is in fact an error as such. They are a means to write “cleaner” code, but not necessarily code that will work “better” than the one that was there to begin with. And someone writing a script like this might go for a “dirtier” solution to a problem, simply to keep the script smaller. – CBroe Mar 21 '15 at 01:24

1 Answers1

4

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. ;^)

Community
  • 1
  • 1
ruffin
  • 16,507
  • 9
  • 88
  • 138