返回列表

JSLint, JSHint error list, option list

默认分类 2014/11/01 20:32

Documentation

JSHint is a program that flags suspicious usage in programs written in JavaScript. The core project consists of a library itself as well as a CLI program distributed as a Node module.

More docs: List of all JSHint options · CLI flags · Writing your own reporter · FAQ

Basic usage

The easiest way to use JSHint is to install it as a Node program. To do so, simply run the following command in your terminal (flag -g installs JSHint globally on your system, omit it if you want to install JSHint in the current working directory):

$ npm install jshint -g

After you've done that you should be able to use the jshint program. The simplest use case would be linting a single file or all JavaScript files in a directory:

$ jshint myfile.js
myfile.js: line 10, col 39, Octal literals are not allowed in strict mode.

1 error

If a file path is a dash (-) then JSHint will read from standard input.

Configuration

JSHint comes with a default set of warnings but it was designed to be very configurable. There are three main ways to configure your copy of JSHint: you can either specify the configuration file manually via the --config flag, use a special file .jshintrc or put your config into your projects package.json file under the jshintConfig property. In case of .jshintrc, JSHint will start looking for this file in the same directory as the file that's being linted. If not found, it will move one level up the directory tree all the way up to the filesystem root. (Note that if the input comes from stdin, JSHint doesn't attempt to find a configuration file)

This setup allows you to have different configuration files per project. Place your file into the project root directory and, as long as you run JSHint from anywhere within your project directory tree, the same configuration file will be used.

Configuration file is a simple JSON file that specifies which JSHint options to turn on or off. For example, the following file will enable warnings about undefined and unused variables and tell JSHint about a global variable named MY_GLOBAL.

{
    "undef": true,
    "unused": true,
    "predef": [ "MY_GLOBAL" ]
}

Inline configuration

In addition to using configuration files you can configure JSHint from within your files using special comments. These comments start either with jshint or global and are followed by a comma-separated list of values. For example, the following snippet will enable warnings about undefined and unused variables and tell JSHint about a global variable named MY_GLOBAL.

/* jshint undef: true, unused: true */
/* global MY_GLOBAL */

You can use both multi- and single-line comments to configure JSHint. These comments are function scoped meaning that if you put them inside a function they will affect only this function's code.

Directives

Here's a list of configuration directives supported by JSHint:

jshint

A directive for setting JSHint options.

/* jshint strict: true */

jslint

A directive for setting JSHint-compatible JSLint options.

/* jslint vars: true */

globals

A directive for telling JSHint about global variables that are defined elsewhere. If value isfalse (default), JSHint will consider that variable as read-only. Use it together with theundef option.

/* global MY_LIB: false */

You can also blacklist certain global variables to make sure they are not used anywhere in the current file.

/* global -BAD_LIB */

exported

A directive for telling JSHint about global variables that are defined in the current file but used elsewhere. Use it together with the unused option.

/* exported EXPORTED_LIB */

members

A directive for telling JSHint about all properties you intend to use. This directive is deprecated.

ignore

A directive for telling JSHint to ignore a block of code.

// Code here will be linted with JSHint.
/* jshint ignore:start */
// Code here will be linted with ignored by JSHint.
/* jshint ignore:end */

All code in between ignore:start and ignore:end won't be passed to JSHint so you can use any language extension such as Facebook React. Additionally, you can ignore a single line with a trailing comment:

ignoreThis(); // jshint ignore:line

Options

Most often, when you need to tune JSHint to your own taste, all you need to do is to find an appropriate option. Trying to figure out how JSHint options work can be confusing and frustrating (and we're working on fixing that!) so please read the following couple of paragraphs carefully.

JSHint has two types of options: enforcing and relaxing. The former are used to make JSHint more strict while the latter are used to suppress some warnings. Take the following code as an example:

function main(a, b) {
    return a == null;
}

This code will produce the following warning when run with default JSHint options:

line 2, col 14, Use '===' to compare with 'null'.

Let's say that you know what you're doing and want to disable the produced warning but, in the same time, you're curious whether you have any variables that were defined but never used. What you need to do, in this case, is to enable two options: one relaxing that will suppress the === null warning and one enforcing that will enable checks for unused variables. In your case these options are unused and eqnull.

/*jshint unused:true, eqnull:true */
function main(a, b) {
    return a == null;
}

After that, JSHint will produce the following warning while linting this example code:

demo.js: line 2, col 14, 'main' is defined but never used.

demo.js: line 2, col 19, 'b' is defined but never used.

Sometimes JSHint doesn't have an appropriate option that disables some particular warning. In this case you can use jshint directive to disable warnings by their code. Let's say that you have a file that was created by combining multiple different files into one:

"use strict";
/* ... */

// From another file
function b() {
    "use strict";
/* ... */
}

This code will trigger a warning about an unnecessary directive in function b. JSHint sees that there's already a global "use strict" directive and informs you that all other directives are redundant. But you don't want to strip out these directives since the file was auto-generated. The solution is to run JSHint with a flag --verbose and note the warning code (W034 in this case):

$ jshint --verbose myfile.js
myfile.js: line 6, col 3, Unnecessary directive "use strict". (W034)

Then, to hide this warning, just add the following snippet to your file:

/* jshint -W034 */

A couple things to note:

  1. This syntax works only with warnings (code starts with W), it doesn't work with errors (code starts with E).
  2. This syntax will disable all warnings with this code. Some warnings are more generic than others so be cautious.

To re-enable a warning that has been disabled with the above snippet you can use:

/* jshint +W034 */

This is especially useful when you have code which causes a warning but that you know is safe in the context. In these cases you can disable the warning as above and then re-enable the warning afterwards:

var y = Object.create(null);
// ...
/*jshint -W089 */
for (var prop in y) {
    // ...
}
/*jshint +W089 */

This page contains a list of all options supported by JSHint.

Switch statements

By default JSHint warns when you omit break or return statements within switch statements:

switch (cond) {
    case "one":
        doSomething(); // JSHint will warn about missing 'break' here.
    case "two":
        doSomethingElse();
}

If you really know what you're doing you can tell JSHint that you intended the case block to fall through by adding a /* falls through */ comment:

switch (cond) {
    case "one":
        doSomething();
        /* falls through */
    case "two":
        doSomethingElse();
}

[JSLint] and the popular alternatives [JSHint] and [ESLint] are brilliant tools that make your code more consistent. But sometimes it's hard to understand exactly what they're asking you to change. JSLint goes so far as to be proud of the fact that it will "hurt your feelings".

JSLint Error Explanations is designed to help you improve your JavaScript by understanding the sometimes cryptic error messages produced by JSLint, JSHint and ESLint, and teaching you how to avoid such errors.

[JSLint] [JSHint] [ESLint] {a} is already defined

The "{a} is already defined" error is thrown when JSLint or JSHint encounters a declaration with an identifier that has been used in a previous declaration. This applies to both variab…

Also known as

[JSLint] [JSHint] [ESLint] The array literal notation [] is preferrable

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 1.0.…

Also known as

[JSLint] [JSHint] [ESLint] Avoid arguments.{a}

The "Avoid arguments.{a}" error is thrown when JSLint, JSHint or ESLint encounters a reference to the callee or caller property of an arguments object. The text of this warning can the…

Also known as

[JSLint] [JSHint] [ESLint] Bad assignment

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint the warning given has…

Also known as

[JSLint] [JSHint] Bad constructor

The "Bad constructor" error is thrown when JSLint or JSHint encounters the new operator followed by a literal value. In the following example we are attempting to apply the new operato…

Also known as

[JSLint] [JSHint] Variable {a} was not declared correctly

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 2.1.…

Also known as

[JSLint] [JSHint] [ESLint] Bad for-in variable '{a}'

This warning has existed in several forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version…

Also known as

[JSHint] [ESLint] Value of '{a}' may be overwritten in IE8 and earlier

The "Value of '{a}' may be overwritten in IE8 and earlier" error is thrown when JSHint or ESLint encounters a try...catch statement in which the catch identifier is the same as…

Also known as

[JSLint] [JSHint] Combine this with the previous 'var' statement

This warning has existed in two forms in JSLint and JSHint. It was introduced in JSLint in June 2011 and has remained in both tools ever since. In JSLint the warning given is "Combine this with the previous…

Also known as

[JSLint] [JSHint] Confusing minuses

This warning has existed in a few forms in both JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since. In JSHint prior to version 1.0.0 the warning give…

Also known as

[JSLint] [JSHint] Confusing pluses

This warning has existed in a few forms in both JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since. In JSHint prior to version 1.0.0 the warning give…

Also known as

[JSHint] const '{a}' has already been declared

The "const '{a}' has already been declared" error is thrown when JSHint encounters a constant declaration with an identifier that has already been used in a previous constant d…

Also known as

[JSLint] [JSHint] [ESLint] Unexpected dangling '_' in '{a}'

The "Unexpected dangling '_' in '{a}'" error is thrown when JSLint, JSHint or ESLint encounters an identifier that begins or ends with the underscore character. JSHint…

Also known as

[JSLint] [JSHint] [ESLint] All 'debugger' statements should be removed

This warning has existed in various forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is "…

Also known as

[JSLint] [JSHint] [ESLint] ['{a}'] is better written in dot notation

The "['{a}'] is better written in dot notation" error is thrown when JSLint, JSHint or ESLint encounters an attempt to access a property using a string literal within a pair of…

Also known as

[JSLint] [JSHint] [ESLint] Duplicate key '{a}'

This warning has existed in three forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is the gene…

Also known as

[JSLint] [JSHint] [ESLint] Empty class

The "Empty class" error is thrown when JSLint, JSHint (only versions before 1.0.0) or ESLint encounters a regular expression literal containing an empty character class. The following…

[JSLint] This is an ES5 feature

This is one of many generic error messages uses by JSLint in a number of situations. Most of these cases are covered in detail by dedicated articles. Following is a list of situations that will…

[JSHint] ES5 option is now set per default

The "ES5 option is now set per default" error is thrown when JSHint (version 2.0.0 and above only) encounters the es5 option with a value of true. Here's an example in which we set…

Also known as

[JSLint] [JSHint] [ESLint] Bad escapement of EOL. Use option multistr if needed

This warning has existed in various forms across the three main linters. It was introduced in the original version of JSHint and has remained (in a way) in all three tools from some point since. In JSLint the wa…

Also known as

[JSLint] [JSHint] [ESLint] eval is evil

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "eval i…

Also known as

[JSLint] [JSHint] [ESLint] Do not assign to the exception parameter

The "Do not assign to the exception parameter" error is thrown when JSLint, JSHint or ESLint encounters an assignment inside a catch block to the identifer associated with that block.…

Also known as

[JSLint] Expected exactly one space between '{a}' and '{b}'

The "Expected exactly one space between '{a}' and '{b}'" error is thrown when JSLint encounters a number of spaces that is not equal to one in the following situations…

[JSLint] Expected a string and instead saw '{a}'

The "Expected a string and instead saw '{a}'" error is thrown when JSLint encounters a comparison operator in which one of the operands is a typeof expression and the other ope…

[JSLint] Expected parameter (value) in set '{a}' function

The "Expected parameter (value) in set '{a}' function" error is thrown when JSLint encounters property setter function in which the first parameter is not named value. In the f…

[JSHint] [ESLint] Extending prototype of native object: '{a}'

The "Extending prototype of native object: '{a}'" error, and the alternative "{a} prototype is read only, properties should not be added" error, is thrown when JSHint…

Also known as

[JSLint] [JSHint] [ESLint] Extra comma. (it breaks older versions of IE)

This warning has existed in various forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is the gen…

Also known as

[JSLint] [JSHint] [ESLint] The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

The "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype" error is thrown when JSLint encounters a for-in statement in which the…

Also known as

[JSLint] [JSHint] [ESLint] The Function constructor is eval

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint and JSHint prior to version 1.0…

Also known as

[JSLint] [JSHint] Function statements should not be placed in blocks

The "Function statements should not be placed in blocks" error (and the alternative "Function declarations should not be placed in blocks" error) is thrown when JSLint or JSH…

Also known as

[JSLint] [JSHint] [ESLint] Don't make functions within a loop

The "Don't make functions within a loop" error is thrown when JSLint, JSHint and ESLint encounter a function expression in a for, while or do statement body. In the following examp…

Also known as

[JSLint] [JSHint] [ESLint] Use the function form of 'use strict'

The "Use the function form of 'use strict'" error is thrown when JSLint, JSHint or ESLint encounters a strict mode directive in the outermost scope of the code. In the followin…

Also known as

[JSLint] [JSHint] get/set are ES5 features

This warning has existed in two forms across JSLint and JSHint. It was introduced in May 2011 version of JSLint and remained in both tools for a period of time. In JSLint between May 2011 and August 2013 the mes…

Also known as

[JSHint] Option 'validthis' can't be used in a global scope

The "Option 'validthis' can't be used in a global scope" error is thrown when JSHint encounters the validthis option in a global scope. Here's a silly example in which…

Also known as

[JSHint] 'hasOwnProperty' is a really bad name

The "'hasOwnProperty' is a really bad name" error is thrown when JSHint encounters an assignment to an object property with the identifier hasOwnProperty. This applies to both…

Also known as

[JSLint] [JSHint] [ESLint] Implied eval is evil. Pass a function instead of a string

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 1.0.…

Also known as

[JSLint] [JSHint] [ESLint] It is not necessary to initialize '{a}' to 'undefined'

The "It is not necessary to initialize '{a}' to 'undefined'" error is thrown when JSLint, JSHint or ESLint encounters a variable statement in which the variable is expl…

Also known as

[JSHint] Invalid typeof value '{a}'

The "Invalid typeof value '{a}'" error is thrown when JSHint encounters a comparison with a typeof expression on one side and an invalid string literal on the other. In the fol…

Also known as

[JSLint] [JSHint] Function statements are not invocable. Wrap the whole function invocation in parens

The "Function statements are not invocable. Wrap the whole function invocation in parens" error (and the alternative "Function declarations are not invocable" error) is throw…

Also known as

[JSLint] [JSHint] [ESLint] A leading decimal point can be confused with a dot: '{a}'

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint versions dated before May 2013 t…

Also known as

[JSLint] [JSHint] [ESLint] Do not use {a} as a constructor

The "Do not use {a} as a constructor" error is thrown when JSLint, JSHint or ESLint encounters a call to String, Number, Boolean, Math or JSON preceded by the new operator. In the foll…

Also known as

[JSLint] [JSHint] [ESLint] Missing '()' invoking a constructor

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is the generic…

Also known as

[JSLint] [JSHint] [ESLint] Missing radix parameter

The "Missing radix parameter" error is thrown when JSLint, JSHint or ESLint encounters a call to the parseInt function that only has one argument. As of JSHint 2.3.0 the warning will o…

Also known as

[JSLint] [JSHint] [ESLint] Missing 'use strict' statement

The "Missing 'use strict' statement" error is thrown when JSLint, JSHint and ESLint encounter a function that does not contain the strict mode directive, and none of whose ance…

Also known as

[JSHint] Mixed double and single quotes

The "Mixed double and single quotes" error is thrown when JSHint encounters string literal delimited by double or single quote characters when a string literal delimited by the other h…

Also known as

[JSLint] [ESLint] Move the invocation into the parens that contain the function

The "Move the invocation into the parens that contain the function" error is thrown when JSLint and ESLint encounter an immediately invoked function expression in which the invoking pa…

[JSLint] Move 'var' declarations to the top of the function

The "Move 'var' declarations to the top of the function" error is thrown when JSLint encounters a variable declaration in a for or for-in statement initialiser. Here's an e…

[JSLint] Nested comment

The "Nested comment" error is thrown when JSLint encounters the character sequence /* inside a multiline comment. Here's an example: /* This is a multiline comment. /* It's…

[JSLint] [JSHint] [ESLint] Do not use 'new' for side effects

The "Do not use 'new' for side effects" error is thrown when JSLint, JSHint or ESLint encounters a function invocation preceded by the new operator when not part of an assignme…

Also known as

[JSLint] [JSHint] [ESLint] '{a}' is not a function

The "'{a}' is not a function" error is thrown when JSLint, JSHint or ESLint encounters an attempt to invoke the Math object as a function. JSLint and ESLint (but not JSHint) wi…

Also known as

[JSLint] [JSHint] [ESLint] '{a}' is not a label

This warning has existed in three forms across the three main linters. It was introduced in the original version of JSLint and has remained (in a way) in all three tools ever since. In JSLint the warning given i…

Also known as

[JSLint] [JSHint] [ESLint] '{a}' was used before it was defined

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "'…

Also known as

[JSLint] [JSHint] [ESLint] The object literal notation {} is preferrable

This warning has existed in various forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint versions dated May 2013 onwa…

Also known as

[JSLint] [JSHint] [ESLint] Don't use octal: '{a}'. Use '\u...' instead

This warning has existed in three forms across the three main linters. It was introduced in the original version of JSLint and has remained (in a way) in all three tools ever since. In JSLint the warning given i…

Also known as