返回列表

转载:JSHint配置介绍

默认分类 2014/06/04 01:17

原文地址:http://www.jshint.com/docs/options/

JSHint Options

This is a complete list of all configuration options accepted by JSHint. If you see something missing, you should either file a bug or email me.

Enforcing options

When set to true, these options will make JSHint produce more warnings about your code.

[bitwise](#bitwise) This option prohibits the use of bitwise operators such as `^` (XOR), `|` (OR) and others. Bitwise operators are very rare in JavaScript programs and quite often `&` is simply a mistyped `&&`.
[camelcase](#camelcase) This option allows you to force all variable names to use either camelCase style or UPPER_CASE with underscores.
[curly](#curly) This option requires you to always put curly braces around blocks in loops and conditionals. JavaScript allows you to omit curly braces when the block consists of only one statement, for example: while (day) shuffle();` However, in some circumstances, it can lead to bugs (you'd think that `sleep()` is a part of the loop while in reality it is not):
`while (day)
      shuffle();
      sleep();`
[eqeqeq](#eqeqeq) This options prohibits the use of `==` and `!=` in favor of `===` and `!==`. The former try to coerce values before comparing them which can lead to some unexpected results. The latter don't do any coercion so they are generally safer. If you would like to learn more about type coercion in JavaScript, we recommend [Truth, Equality and JavaScript](http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/) by Angus Croll.
[es3](#es3) This option tells JSHint that your code needs to adhere to ECMAScript 3 specification. Use this option if you need your program to be executable in older browsers—such as Internet Explorer 6/7/8/9—and other legacy JavaScript environments.
[forin](#forin) This option requires all `for in` loops to filter object's items. The for in statement allows for looping through the names of all of the properties of an object including those inherited throught the prototype chain. This behavior can lead to unexpected items in your object so it is generally safer to always filter inherited properties out as shown in the example:
`for (key in obj) {
      if (obj.hasOwnProperty(key)) {
        // We are sure that obj[key] belongs to the object and was not inherited.
      }
    }`
For more in-depth understanding of `for in` loops in JavaScript, read [Exploring JavaScript for-in loops](http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/) by Angus Croll.
[freeze](#freeze) This options prohibits overwriting prototypes of native objects such as `Array`, `Date` and so on.
`/* jshint freeze:true */
    Array.prototype.count = function (value) { return 4; };
    // -> Warning: Extending prototype of native object: 'Array'.`
[immed](#immed) This option prohibits the use of immediate function invocations without wrapping them in parentheses. Wrapping parentheses assists readers of your code in understanding that the expression is the result of a function, and not the function itself.
[indent](#indent) This option enforces specific tab width for your code. For example, the following code will trigger a warning on line 4:
`/*jshint indent:4 */

    if (cond) {
      doSomething(); // We used only two spaces for indentation here
    }`
[latedef](#latedef) This option prohibits the use of a variable before it was defined. JavaScript has function scope only and, in addition to that, all variables are always moved—or hoisted— to the top of the function. This behavior can lead to some very nasty bugs and that's why it is safer to always use variable only after they have been explicitly defined. Setting this option to "nofunc" will allow function declarations to be ignored. For more in-depth understanding of scoping and hoisting in JavaScript, read [JavaScript Scoping and Hoisting](http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting) by Ben Cherry.
[newcap](#newcap) This option requires you to capitalize names of constructor functions. Capitalizing functions that are intended to be used with `new` operator is just a convention that helps programmers to visually distinguish constructor functions from other types of functions to help spot mistakes when using `this`. Not doing so won't break your code in any browsers or environments but it will be a bit harder to figure out—by reading the code—if the function was supposed to be used with or without new. And this is important because when the function that was intended to be used with `new` is used without it, `this` will point to the global object instead of a new object.
[noarg](#noarg) This option prohibits the use of `arguments.caller` and `arguments.callee`. Both `.caller` and `.callee` make quite a few optimizations impossible so they were deprecated in future versions of JavaScript. In fact, ECMAScript 5 forbids the use of `arguments.callee` in strict mode.
[noempty](#noempty) This option warns when you have an empty block in your code. JSLint was originally warning for all empty blocks and we simply made it optional. There were no studies reporting that empty blocks in JavaScript break your code in any way.
[nonbsp](#nonbsp) This option warns about "non-breaking whitespace" characters. These characters can be entered with option-space on Mac computers and have a potential of breaking non-UTF8 web pages.
[nonew](#nonew) This option prohibits the use of constructor functions for side-effects. Some people like to call constructor functions without assigning its result to any variable:
`new MyConstructor();`
There is no advantage in this approach over simply calling `MyConstructor` since the object that the operator `new` creates isn't used anywhere so you should generally avoid constructors like this one.
[plusplus](#plusplus) This option prohibits the use of unary increment and decrement operators. Some people think that `++` and `--` reduces the quality of their coding styles and there are programming languages—such as Python—that go completely without these operators.
[quotmark](#quotmark) This option enforces the consistency of quotation marks used throughout your code. It accepts three values: `true` if you don't want to enforce one particular style but want some consistency, `"single"` if you want to allow only single quotes and `"double"` if you want to allow only double quotes.
[undef](#undef) This option prohibits the use of explicitly undeclared variables. This option is very useful for spotting leaking and mistyped variables.
`/*jshint undef:true */

    function test() {
      var myVar = 'Hello, World';
      console.log(myvar); // Oops, typoed here. JSHint with undef will complain
    }`
If your variable is defined in another file, you can use `/*global ... */` directive to tell JSHint about it.
[unused](#unused) This option warns when you define and never use your variables. It is very useful for general code cleanup, especially when used in addition to `undef`.
`/*jshint unused:true */

    function test(a, b) {
      var c, d = 2;

      return a + d;
    }

    test(1, 2);

    // Line 3: 'b' was defined but never used.
    // Line 4: 'c' was defined but never used.`
In addition to that, this option will warn you about unused global variables declared via `/*global ... */` directive. This can be set to `vars` to only check for variables, not function parameters, or `strict` to check all variables and parameters. The default (true) behavior is to allow unused parameters that are followed by a used parameter.
[strict](#strict) This option requires all functions to run in ECMAScript 5's strict mode. [Strict mode](https://developer.mozilla.org/en/JavaScript/Strict_mode) is a way to opt in to a restricted variant of JavaScript. Strict mode eliminates some JavaScript pitfalls that didn't cause errors by changing them to produce errors. It also fixes mistakes that made it difficult for the JavaScript engines to perform certain optimizations. _Note:_ This option enables strict mode for function scope only. It _prohibits_ the global scoped strict mode because it might break third-party widgets on your page. If you really want to use global strict mode, see the _globalstrict_ option.
[trailing](#trailing) This option makes it an error to leave a trailing whitespace in your code. Trailing whitespaces can be source of nasty bugs with multi-line strings in JavaScript:
`// This otherwise perfectly valid string will error if
    // there is a whitespace after \
    var str = "Hello \
    World";`
[maxparams](#maxparams) This option lets you set the max number of formal parameters allowed per function:
`/*jshint maxparams:3 */

    function login(request, onSuccess) {
      // ...
    }

    // JSHint: Too many parameters per function (4).
    function logout(request, isManual, whereAmI, onSuccess) {
      // ...
    }`
[maxdepth](#maxdepth) This option lets you control how nested do you want your blocks to be:
`/*jshint maxdepth:2 */

    function main(meaning) {
      var day = true;

      if (meaning === 42) {
        while (day) {
          shuffle();

          if (tired) { // JSHint: Blocks are nested too deeply (3).
              sleep();
          }
        }
      }
    }`
[maxstatements](#maxstatements) This option lets you set the max number of statements allowed per function:
`/*jshint maxstatements:4 */

    function main() {
      var i = 0;
      var j = 0;

      // Function declarations count as one statement. Their bodies
      // don't get taken into account for the outer function.
      function inner() {
        var i2 = 1;
        var j2 = 1;

        return i2 + j2;
      }

      j = i + j;
      return j; // JSHint: Too many statements per function. (5)
    }`
[maxcomplexity](#maxcomplexity) This option lets you control cyclomatic complexity throughout your code. Cyclomatic complexity measures the number of linearly independent paths through a program's source code. Read more about [cyclomatic complexity on Wikipedia](http://en.wikipedia.org/wiki/Cyclomatic_complexity).
[maxlen](#maxlen) This option lets you set the maximum length of a line.
### [Relaxing options](#relaxing-options)

When set to true, these options will make JSHint produce less warnings about your code.

<table class="options table table-bordered table-striped"><tbody><tr>
<td id="asi" class="name">[asi](#asi)</td>
<td class="desc">

This option suppresses warnings about missing semicolons. There is a lot of
FUD about semicolon spread by quite a few people in the community. The
common myths are that semicolons are required all the time (they are not) and
that they are unreliable. JavaScript has rules about semicolons which are
followed by _all_ browsers so it is up to you to decide whether you should or
should not use semicolons in your code.

For more information about semicolons in JavaScript read
[An Open Letter to JavaScript Leaders Regarding Semicolons](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding)
by Isaac Schlueter and [JavaScript Semicolon Insertion](http://inimino.org/~inimino/blog/javascript_semicolons).
</td>
</tr>
<tr>
<td id="boss" class="name">[boss](#boss)</td>
<td class="desc">

This option suppresses warnings about the use of assignments in cases where
comparisons are expected. More often than not, code like `if (a = 10) {}` is a
typo. However, it can be useful in cases like this one:

<pre>`for (var i = 0, person; person = people[i]; i++) {}`</pre>

You can silence this error on a per-use basis by surrounding the assignment
with parenthesis, such as:

<pre>`for (var i = 0, person; (person = people[i]); i++) {}`</pre></td>
</tr>
<tr>
<td id="debug" class="name">[debug](#debug)</td>
<td class="desc">

This option suppresses warnings about the `debugger` statements in your code.
</td>
</tr>
<tr>
<td id="eqnull" class="name">[eqnull](#eqnull)</td>
<td class="desc">

This option suppresses warnings about `== null` comparisons. Such comparisons
are often useful when you want to check if a variable is `null` or `undefined`.
</td>
</tr>
<tr>
<td id="esnext" class="name">[esnext](#esnext)</td>
<td class="desc">

This option tells JSHint that your code uses ECMAScript 6 specific syntax.
Note that these features are not finalized yet and not all browsers implement
them.

More info:

This option makes JSHint stop on the first error or warning. white

This option make JSHint check your source code against Douglas Crockford's JavaScript coding style. Unfortunately, his “The Good Parts” book aside, the actual rules are not very well documented.