Intro

I became acquainted with JSLint last year when rolling out the tool within my company’s development environment. So what is it? Well, where better to get a concise description of what it is than from the JSLint.com itself?

JSLint is a JavaScript program that looks for problems in JavaScript programs. It is a code quality tool.

JSLint strips out some of the common errors and problematic syntax commonly found within JavaScript. When run against our code base, it identified a range of errors, some of which were fairly explanatory, while others needed a little digging before their meaning was clear to me.

The JSLint site often provides an insight into the motivation behind some of its messages, but stops short of documenting in detail all its 150+. It became clear to me that there was no central repository for researching exactly what each error meant, which is why I decided to write this series of articles.

Quite often, the lack of documented justification for a message can give rise to questions regarding its relevance or usefulness. For me, each is covered in the following email exchange I had with Douglas Crockford:

I wrote:

Is there a development pipeline for JSLint? I.e. do you have a plan in advance of the changes/enhancements you would like to make, or is it more ad-hoc? Also, how do you decide upon what needs done?

Douglas Crockford’s reply:

JSLint is a rather old program now, so it is all about bugs. When I find bugs (in my programs or in the programs of others) I think about how they might have been detected. In most cases, that leads to futility, but sometimes I see that by changing the programming model, there is a chance to make JSLint more effective.

In most cases, however it the reasons should be fairly apparent.

List of Messages

Note: This is a big list, so it will take some time to complete! I’m happy to take requests. Just leave a comment below and I’ll see what I can do.

['{a}'] is better written in dot notation
{a} ({b}% scanned)
‘{a}’ has not been fully defined yet.
‘{a}’ is a function (deprecated).
‘{a}’ is a statement label.
‘{a}’ is already defined.
‘{a}’ is not a label.
‘{a}’ is not allowed.
‘{a}’ is not defined.
‘{a}’ should not be greater than ‘{b}’.
‘{a}’ used out of scope.
‘{a}’ was used before it was defined
A ‘<{a}>’ must be within ‘<{b}>’.
A constructor name should start with an uppercase letter.
A css file should begin with @charset ‘UTF-8′;
A dot following a number can be confused with a decimal point (deprecated)
A leading decimal point can be confused with a dot ‘.{a}’.
A regular expression literal can be confused with ‘/=’.
A trailing decimal point can be confused with a dot ‘{a}’.
ADsafe autocomplete violation.
ADsafe lib violation.
Adsafe name violation ‘{a}’.
ADsafe script placement violation.
ADsafe script violation.
ADsafe subscript ‘{a}’.
ADsafe unapproved script source.
ADSAFE: Use the fragment option.
Adsafe violation: ‘{a}’.
ADsafe violation: An id must have a ‘{a}’ prefix
ADSAFE violation: bad id.
ADsafe violation: Disallowed tag ‘{a}’:
ADsafe violation: id does not match.
ADSAFE violation: missing ID_.
ADsafe violation: Missing ADSAFE.go.
ADsafe violation: Missing ADSAFE.id or ADSAFE.go.
ADsafe violation: Wrap the widget in a div.
Attribute ‘{a}’ not all lower case.
Avoid HTML event handlers.
Bad assignment.
Bad number ‘{a}’.
Bad constructor.
Bad entity
Bad for in variable ‘{a}’.
Bad hex color ‘{a}’.
Bad HTML string
Bad id: ‘{a}’.
Bad invocation.
Bad number ‘{a}’.
Bad operand.
Bad style.
Bad type.
Bad url {a}.
Confusing regular expression
Confusing use of ‘{a}’.
Currently, ADsafe does not operate on whole HTML documents. It operates on fragments and .js files.
Dangerous comment.
Do not assign to the exception parameter.
Do not use ‘new’ for side effects.
Do not use {a} as a constructor.
Do not wrap function literals in parens unless they are to be immediately invoked.
document.write can be a form of eval.
Don’t use octal: ‘{a}’. Use ‘\\u….’ instead.
Don’t make functions within a loop.
Duplicate ‘{a}’.
Empty Block
Eval is Evil.
Expected ‘{a}’ and instead saw ‘{b}’.
Empty case.
Empty class.
Expected –>.
Expected ‘<\/’ and instead saw ‘</’.
Expected ‘{a}’ and instead saw ‘{b}’.
Expected ‘{a}’ at column {b}, not column {c} (JSLint Style Guide)
Expected ‘{a}’ to have an indentation of {b} instead of {c} (JSLint Style Guide)
Expected ‘{a}’ to match ‘{b}’ from line {c} and instead saw ‘{d}’.
Expected (value) in set {a} function.
Expected a ‘break’ statement before ‘case’.
Expected a ‘break’ statement before ‘default’.
Expected a class, and instead saw #.{a}.
Expected a conditional expression and instead saw an assignment.
Expected a CSS media type, and instead saw ‘{a}’.
Expected a CSS selector, and instead saw {a}.
Expected a JSON value.
Expected a lang code, and instead saw :{a}.
Expected a linear unit and instead saw ‘{a}’.
Expected a name and instead saw ‘{a}’.
Expected a non-standard style attribute and instead saw ‘{a}’.
Expected a number and instead saw ‘{a}’.
Expected a number between 0 and 1 and instead saw ‘{a}’
Expected a percentage and instead saw ‘{a}’
Expected a positive number and instead saw ‘{a}’
Expected a pseudo, and instead saw :{a}.
Expected a small integer and instead saw ‘{a}’
Expected a small number and instead saw ‘{a}’
Expected a string and instead saw {a}.
Expected a style attribute, and instead saw ‘{a}’. (Spelling mistake in module – spelled ‘Excepted’
Expected a style pattern, and instead saw ‘{a}’.
Expected a tagName, and instead saw {a}.
Expected a type, and instead saw {a}.
Expected an assignment or function call and instead saw an expression.
Expected an at-rule, and instead saw @{a}.
Expected an attribute, and instead saw [{a}].
Expected an attribute value and instead saw ‘{a}’.
Expected an id, and instead saw #{a}.
Expected an identifier and instead saw ‘{a}’ (a reserved word).
Expected an identifier and instead saw ‘{a}’.
Expected an identifier in an assignment and instead saw a function invocation.
Expected an operator and instead saw ‘{a}’.
Expected exactly one space between ‘{a}’ and ‘{b}‘ (JSLint Style Guide)
Expected to see a statement and instead saw a block.
Function statements are not invocable. Wrap the function expression in parens.
Function statements should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.
HTML confusion in regular expression ‘<{a}’.
Implied eval is evil. Pass a function instead of a string.
Insecure ‘{a}’.
It is not necessary to initialize ‘{a}’ to ‘undefined’.
JavaScript URL.
Label ‘{a}’ on {b} statement.
lang is deprecated.
Line too long.
Missing ‘{a}’.
Missing ‘{a}’ after {b}.
Missing ‘break’ after ‘case’
Missing ‘case’
Missing name in function statement.
Missing option value.
Missing property name.
Missing radix parameter.
Missing space between’{a}’ and {b} (JSLint Style Guide)
Missing ‘use strict’ statement.
Missing url.
Mixed spaces and tabs (JSLint Style Guide)
Move the invocation into the parens that contain the function.
Move ‘var’ declarations to the top of the function.
Nested comment.
Nested not.
Only properties should be deleted.
Read only.
Redefinition of ‘{a}’.
Reserved name ‘{a}’.
Spaces are hard to count. Use {{a}}
Strange loop.
Strict violation.
The ‘&&’ subexpression should be wrapped in parens.
The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.
The Function constructor is eval.
The second argument to lib must be a function.
This is an ES5 feature.
Too many errors.
type is unnecessary.
Unclosed comment.
Unclosed regular expression.
Unclosed string.
Undefined {b} ‘{a}’.
Unescaped ‘{a}’.
Unexpected ‘{a}’.
Unexpected ‘in’. Compare with undefined, or use the hasOwnProperty method instead.
Unexpected {a} in ‘{b}’.
Unexpected character ‘{a}’ in {b}.
Unexpected comment.
Unexpected control character {a}.
Unexpected control character in regular expression.
Unexpected dangling ‘_’ in ‘{a}’.
Unexpected ‘in’. Compare with undefined, or use the hasOwnProperty method instead.
Unexpected parameter ‘{a}’ in get {b} function.
Unexpected space between ‘{a}’ and ‘{b}’ (JSLint Style Guide).
Unnecessary “use strict”.
Unreachable ‘{a}’ after ‘{b}’.
Unrecognized style attribute ‘{a}’.
Unrecognized tag ‘<{a}>’.
Unsafe character.
Unused variable: {a}
Use a named parameter.
Use the || operator.
Use the array literal notation [].
Use the charAt method.
Use the isNaN function to compare with NaN.
Use the function form of “use strict”.
Use the object literal notation {}.
Variable {a} was not declared correctly.
Weird assignment.
Weird condition.
Weird construction. Delete ‘new’.
Weird program.
Weird relation.
Weird ternary.
Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.
Wrap the /regexp/ literal in parens to disambiguate the slash operator.

Messages By Option

JSLint has a number of configurable options. The list below categorises the same error messages by their associated JSLint option. Not all messages above will have a configurable option, which means not all messages can be turned off. So, even with all the optional rules disabled, JSLint will still require a certain standard of JavaScript code!

evil: Eval is Evil.
nocaps: A constructor name should start with an uppercase letter.

Bibliography

Each article has its own links, but for completeness, below is a list of all the links that where helpful, or are referenced in the above articles.

A Survey of the JavaScript Programming Language – An introduction to the JavaScript Programming Language by Douglas Crockford for professional programmers

Dot Notation and Square Bracket Notation in JavaScript. In JavaScript, everything is an object. Put simply, this means that any variable can have properties which are other objects. There are two different syntaxes for accessing properties which this article explains and compares.

The latest JavaScript specification (EC5) can be found here on the ECMA website.

The Mozilla JavaScript reference is a great language resource for JavaScript

JavaScript operators – Refer to this article for more information on all JavaScript operators, including arithmetic, relational, string, logical, bitwise, and a few others that don’t really fit into any category.

Eval is Evil Part One and Part Two. Two articles by Microsoft Guru, Eric Lippert, on the evils of eval.

Eval isn’t the only thing that is evil in JavaScript. So are global variables, as detailed in Douglas Crockford’s Global Domination article

Strict Mode Is Coming To Town – A YUI Blug article describing the use of strict mode as introduced in ECMAScript, Fifth Edition

JavaScript and Object Oriented Programming (OOP) – A basic discussion on the use of objects in JavaScript.

Customise and extend an object using Expando Properties.

For more on numbers in JavaScript, refer to The Complete JavaScript Number Reference.

Read about anonymous functions at Helephant.com, and a related article on late binding at AListApart.com

Refer to the JavaScript kit event object for more information on JavaScript events.

The JavaScript this object is discussed in more depth over at QuirksMode.org.

For more information on function declarations and function expressions, refer to this article at JavaScript, JavaScript.
Read about JavaScript Variable Hoisting at JavaScript Scoping and Hoisting and My variable declaration got hoisted.

For more information on closures, refer to Closures in JavaScript by James Padolsey and closure convention more commonly known as the JavaScript Module Pattern

To demystify the mire of Function Declarations, Function Expressions and Function Statements, refer the article Named function expressions demystified by Juriy “kangax” Zaytsev.

Read a bit more about fallthrough in switch statements.

It’s always worth knowing the correct terminology to use. Find out about literal values.

Resources

Test your JavaScript code online against JSLint now at the home of JSLint

Find out how to integrate JSLint with your IDE

Want to test some JavaScript without the hassle of creating a file and downloading any dependent libraries? Then try JSFiddle.

Safely convert between JSON strings and objects using the JSON2.js Library

Further help and assistance with JSLint, JavaScript, and just about any programming related problems will almost certainly be forthcoming at Stackoverflow.com

Stackoverflow

And on the subject of StackOverflow, here are the articles that assisted me in writing some of the posts:

JSLint: “Use the array literal notation []” for var os_map = {}.

Solution for JSLint Errors

, , ,
Trackback

38 comments untill now

  1. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  2. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  3. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  4. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  5. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  6. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  7. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  8. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  9. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  10. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  11. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  12. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  13. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  14. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  15. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  16. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  17. [...] This article is one of a series on the error and warning messages produced by JSLint. [...]

  18. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  19. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  20. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  21. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  22. [...] article is one of a series on the error and warning messages produced by JSLint. eval-is-evil, JavaScript, jslint, jslint-messages Address: [...]

  23. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, javascript-for-in, jslint, jslint-messages Address: [...]

  24. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  25. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, javascript-variables, jslint, jslint-messages Address: [...]

  26. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages, strange loop Address: [...]

  27. Hi, I have a message you have not put in your list: Stopping.
    I’m using the JSLint plugin for Notepad++. I’m looking at a 588 line file, and I have 6 warnings.
    No matter what options I select, the last message is always: “Line xx, column yy: Stopping. (52% scanned).”
    I would only expect this message if the message count limit were exceeded. Any ideas? I may look at the source and contact DC directly via sourceforge.

  28. JSLint wills top scanning on errors. What are the warnings/errors you are getting? Can you post them here? Also, can you send me the source file, so I can have a look? (blog [at] jameswiseman.com)

  29. I have sent you the source file, error messages, and JSLint options.

  30. [...] un petit guide en anglais des messages d'erreur remontés par JSLint. Celui-ci n'est pas encore complet, mais il [...]

  31. It just so happens that you have stumbled across one of the perceived issues with JSLint that lead its users create a ‘Community Driven Fork’ in jsHint.

    It is stopping because it takes exception with this line:

    for(var i=0; i<l_Count; i++) {

    For which it gives the error "Move 'var' declarations to the top of the function." (as you can see).

    JavaScript lacks block, but does have function scope, so any variables are hoisted to the top of the nearest available scope, which is often the global scope, or the function in which the variable is declared. In the code you sent me, the scope will be to:

    getMSAuth: function() {

    So, regardless of where you declare it in that function it will act as if it were declared at the top. Crockford recommends that you do just this.

    So, to fix it do as it says:

    getMSAuth: function() {
    var i=0;

    for(i=0; i<l_Count; i++) {

    You have three others in your code, so you'll need to fix them.

    Is this an 'error' scenario? Personally, I think not. I have an app that scans our company's entire JavaScript code base, and it's annoying when it just stops scanning a file because it deems it has found an error too heinous to allow it to proceed (there are other 'error' conditions that stop it).

  32. James, thanks very much for taking the time to look at this. JSLint is back on my list again.

    Even though JSHint lets me specify jQuery as an environment, I still prefer JSLint. It’s so useful having it integrated into Notepad++.

  33. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  34. [...] does it mean? Even this blog post doens’t provide [...]

  35. [...] article is one of a series on the error and warning messages produced by JSLint. arguments, JavaScript, jslint, jslint-messages Address: [...]

  36. [...] article is one of a series on the error and warning messages produced by JSLint. JavaScript, jslint, jslint-messages Address: [...]

  37. edziffell @ 2012-01-09 14:33

    Problem at line 8 character 53: type is unnecessary.

    The line is indented with 2 tabs 4sps each, to get to the “<" in the script tag. What is this trying to tell me?

  38. I can’t tell from your comment. Can you save the code in a JSfiddle and post the URL: http://jsfiddle.net/

Add your comment now