From 850f0cc97d5c9a002492703ee731e5c426415c05 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sat, 29 Aug 2015 00:28:23 -0400 Subject: [PATCH] Docs: Add linting for second half of rule docs (refs #2271) --- .editorconfig | 5 +- docs/rules/brace-style.md | 6 +- docs/rules/func-style.md | 10 +- docs/rules/no-div-regex.md | 2 +- docs/rules/no-dupe-keys.md | 12 +- docs/rules/no-empty-label.md | 2 +- docs/rules/no-fallthrough.md | 20 +- docs/rules/no-floating-decimal.md | 10 +- docs/rules/no-func-assign.md | 12 +- docs/rules/no-implicit-coercion.md | 24 ++- docs/rules/no-implied-eval.md | 14 +- docs/rules/no-inline-comments.md | 24 +-- docs/rules/no-inner-declarations.md | 34 +++- docs/rules/no-invalid-regexp.md | 20 +- docs/rules/no-invalid-this.md | 86 +++------ docs/rules/no-irregular-whitespace.md | 23 ++- docs/rules/no-iterator.md | 10 +- docs/rules/no-label-var.md | 6 +- docs/rules/no-labels.md | 15 +- docs/rules/no-lone-blocks.md | 22 ++- docs/rules/no-lonely-if.md | 22 ++- docs/rules/no-loop-func.md | 23 +-- docs/rules/no-mixed-requires.md | 23 ++- docs/rules/no-mixed-spaces-and-tabs.md | 56 ++++-- docs/rules/no-multi-spaces.md | 38 ++-- docs/rules/no-multi-str.md | 8 +- docs/rules/no-multiple-empty-lines.md | 30 +-- docs/rules/no-native-reassign.md | 6 +- docs/rules/no-negated-in-lhs.md | 36 +++- docs/rules/no-nested-ternary.md | 8 +- docs/rules/no-new-func.md | 8 +- docs/rules/no-new-object.md | 8 +- docs/rules/no-new-require.md | 6 +- docs/rules/no-new-wrappers.md | 16 +- docs/rules/no-new.md | 6 +- docs/rules/no-obj-calls.md | 8 +- docs/rules/no-octal-escape.md | 12 +- docs/rules/no-octal.md | 14 +- docs/rules/no-param-reassign.md | 28 ++- docs/rules/no-path-concat.md | 7 +- docs/rules/no-plusplus.md | 8 +- docs/rules/no-process-env.md | 6 +- docs/rules/no-process-exit.md | 8 +- docs/rules/no-proto.md | 8 +- docs/rules/no-redeclare.md | 17 +- docs/rules/no-regex-spaces.md | 8 +- docs/rules/no-reserved-keys.md | 2 + docs/rules/no-restricted-modules.md | 32 ++-- docs/rules/no-restricted-syntax.md | 6 +- docs/rules/no-return-assign.md | 18 +- docs/rules/no-script-url.md | 4 +- docs/rules/no-self-compare.md | 4 +- docs/rules/no-sequences.md | 24 ++- docs/rules/no-shadow-restricted-names.md | 20 +- docs/rules/no-shadow.md | 67 +++++-- docs/rules/no-space-before-semi.md | 2 +- docs/rules/no-spaced-func.md | 10 +- docs/rules/no-sparse-arrays.md | 8 +- docs/rules/no-sync.md | 8 +- docs/rules/no-ternary.md | 10 +- docs/rules/no-this-before-super.md | 22 +-- docs/rules/no-throw-literal.md | 18 +- docs/rules/no-trailing-spaces.md | 12 +- docs/rules/no-undef-init.md | 8 +- docs/rules/no-undef.md | 17 +- docs/rules/no-undefined.md | 12 +- docs/rules/no-underscore-dangle.md | 10 +- docs/rules/no-unexpected-multiline.md | 8 +- docs/rules/no-unneeded-ternary.md | 8 +- docs/rules/no-unreachable.md | 14 +- docs/rules/no-unused-expressions.md | 10 +- docs/rules/no-unused-vars.md | 44 ++++- docs/rules/no-use-before-define.md | 16 +- docs/rules/no-useless-call.md | 28 +-- docs/rules/no-useless-concat.md | 14 +- docs/rules/no-var.md | 16 +- docs/rules/no-void.md | 13 +- docs/rules/no-warning-comments.md | 55 +++--- docs/rules/no-with.md | 5 +- docs/rules/no-wrap-func.md | 6 +- docs/rules/object-curly-spacing.md | 37 ++-- docs/rules/object-shorthand.md | 12 +- docs/rules/one-var.md | 86 +++++---- docs/rules/operator-assignment.md | 20 +- docs/rules/operator-linebreak.md | 53 +++--- docs/rules/padded-blocks.md | 36 ++-- docs/rules/prefer-arrow-callback.md | 8 +- docs/rules/prefer-const.md | 28 +-- docs/rules/prefer-reflect.md | 171 +++++++++--------- docs/rules/prefer-spread.md | 20 +- docs/rules/prefer-template.md | 8 +- docs/rules/quote-props.md | 74 +++++--- docs/rules/quotes.md | 91 +++++++--- docs/rules/radix.md | 8 +- docs/rules/require-yield.md | 10 +- docs/rules/semi-spacing.md | 34 ++-- docs/rules/semi.md | 22 ++- docs/rules/sort-vars.md | 24 ++- docs/rules/space-after-keywords.md | 24 +-- docs/rules/space-before-blocks.md | 26 ++- docs/rules/space-before-function-paren.md | 52 ++++-- .../space-before-function-parentheses.md | 8 +- docs/rules/space-before-keywords.md | 12 +- docs/rules/space-in-brackets.md | 8 +- docs/rules/space-in-parens.md | 98 +++++++--- docs/rules/space-infix-ops.md | 68 +++---- docs/rules/space-return-throw-case.md | 18 +- docs/rules/space-unary-ops.md | 137 ++++++-------- docs/rules/spaced-comment.md | 129 +++++++------ docs/rules/strict.md | 42 ++--- docs/rules/use-isnan.md | 8 +- docs/rules/valid-jsdoc.md | 33 +++- docs/rules/valid-typeof.md | 14 +- docs/rules/vars-on-top.md | 57 +++--- docs/rules/wrap-iife.md | 50 +++-- docs/rules/wrap-regex.md | 8 +- docs/rules/yoda.md | 48 ++--- 117 files changed, 1694 insertions(+), 1214 deletions(-) diff --git a/.editorconfig b/.editorconfig index 19f2c73242c..646b08e97fc 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,6 +10,9 @@ insert_final_newline = true [docs/rules/linebreak-style.md] end_of_line = disabled -[docs/rules/indent.md] +[{docs/rules/{indent.md,no-mixed-spaces-and-tabs.md}] indent_style = disabled indent_size = disabled + +[docs/rules/no-trailing-spaces.md] +trim_trailing_whitespace = false diff --git a/docs/rules/brace-style.md b/docs/rules/brace-style.md index 13180eadd5a..f5cf0999068 100644 --- a/docs/rules/brace-style.md +++ b/docs/rules/brace-style.md @@ -87,7 +87,7 @@ else { /*error Closing curly brace does not appear on the same line } ``` -The following patterns adhere to one true brace style and do not cause warnings: +The following patterns adhere to one true brace style and are not considered warnings: ```js /*eslint brace-style: 2*/ @@ -164,7 +164,7 @@ if (foo) { } ``` -The following patterns adhere to Stroustrup style and do not cause warnings: +The following patterns adhere to Stroustrup style and are not considered warnings: ```js /*eslint brace-style: [2, "stroustrup"]*/ @@ -243,7 +243,7 @@ if (foo) { /*error Opening curly brace appears on the same line as con } ``` -The following patterns adhere to Allman style and do not cause warnings: +The following patterns adhere to Allman style and are not considered warnings: ```js /*eslint brace-style: [2, "allman"]*/ diff --git a/docs/rules/func-style.md b/docs/rules/func-style.md index ac187fa2867..a5405752258 100644 --- a/docs/rules/func-style.md +++ b/docs/rules/func-style.md @@ -53,7 +53,7 @@ The following patterns are considered warnings: var foo = function() { /*error Expected a function declaration.*/ // ... -} ; +}; ``` ```js @@ -69,16 +69,20 @@ The following patterns are not considered warnings: ```js /*eslint func-style: [2, "declaration"]*/ +function foo() { + // ... +} + +// Methods (functions assigned to objects) are not checked by this rule SomeObject.foo = function() { // ... }; ``` - ```js /*eslint func-style: [2, "expression"]*/ -SomeObject.foo = function() { +var foo = function() { // ... }; ``` diff --git a/docs/rules/no-div-regex.md b/docs/rules/no-div-regex.md index 398d0d1171a..20c78ecda42 100644 --- a/docs/rules/no-div-regex.md +++ b/docs/rules/no-div-regex.md @@ -18,7 +18,7 @@ The following patterns are considered warnings: function bar() { return /=foo/; } /*error A regular expression literal can be confused with '/='.*/ ``` -The following patterns adhere to this rule: +The following patterns are not considered warnings: ```js /*eslint no-div-regex: 2*/ diff --git a/docs/rules/no-dupe-keys.md b/docs/rules/no-dupe-keys.md index aec0c206359..520260c868a 100644 --- a/docs/rules/no-dupe-keys.md +++ b/docs/rules/no-dupe-keys.md @@ -18,19 +18,19 @@ The following patterns are considered warnings: ```js /*eslint no-dupe-keys: 2*/ -var foo = { /*error Duplicate key 'bar'.*/ +var foo = { bar: "baz", - bar: "qux" + bar: "qux" /*error Duplicate key 'bar'.*/ }; -var foo = { /*error Duplicate key 'bar'.*/ +var foo = { "bar": "baz", - bar: "qux" + bar: "qux" /*error Duplicate key 'bar'.*/ }; -var foo = { /*error Duplicate key '1'.*/ +var foo = { 0x1: "baz", - 1: "qux" + 1: "qux" /*error Duplicate key '1'.*/ }; ``` diff --git a/docs/rules/no-empty-label.md b/docs/rules/no-empty-label.md index 08530b68489..9769de1980a 100644 --- a/docs/rules/no-empty-label.md +++ b/docs/rules/no-empty-label.md @@ -12,7 +12,7 @@ The following patterns are considered warnings: ```js /*eslint no-empty-label: 2*/ -labeled: /*error Unexpected label labeled*/ +labeled: /*error Unexpected label "labeled"*/ var x = 10; ``` diff --git a/docs/rules/no-fallthrough.md b/docs/rules/no-fallthrough.md index edfd2899532..62988696f4e 100644 --- a/docs/rules/no-fallthrough.md +++ b/docs/rules/no-fallthrough.md @@ -65,8 +65,10 @@ This rule is aimed at eliminating unintentional fallthrough of one case to the o The following patterns are considered warnings: ```js +/*eslint no-fallthrough: 2*/ + switch(foo) { - case 1: + case 1: /*error Expected a "break" statement before "case".*/ doSomething(); case 2: @@ -77,6 +79,8 @@ switch(foo) { The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-fallthrough: 2*/ + switch(foo) { case 1: doSomething(); @@ -86,13 +90,15 @@ switch(foo) { doSomething(); } -switch(foo) { - case 1: - doSomething(); - return; +function bar(foo) { + switch(foo) { + case 1: + doSomething(); + return; - case 2: - doSomething(); + case 2: + doSomething(); + } } switch(foo) { diff --git a/docs/rules/no-floating-decimal.md b/docs/rules/no-floating-decimal.md index f4012aad166..f0347bc7ea4 100644 --- a/docs/rules/no-floating-decimal.md +++ b/docs/rules/no-floating-decimal.md @@ -17,14 +17,18 @@ This rule is aimed at eliminating floating decimal points and will warn whenever The following patterns are considered warnings: ```js -var num = .5; -var num = 2.; -var num = -.7; +/*eslint no-floating-decimal: 2*/ + +var num = .5; /*error A leading decimal point can be confused with a dot.*/ +var num = 2.; /*error A trailing decimal point can be confused with a dot.*/ +var num = -.7; /*error A leading decimal point can be confused with a dot.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-floating-decimal: 2*/ + var num = 0.5; var num = 2.0; ``` diff --git a/docs/rules/no-func-assign.md b/docs/rules/no-func-assign.md index 394f9db383f..a5663475a90 100644 --- a/docs/rules/no-func-assign.md +++ b/docs/rules/no-func-assign.md @@ -14,24 +14,30 @@ This rule is aimed at flagging probable mistakes and issues in the form of overw The following patterns are considered warnings: ```js +/*eslint no-func-assign: 2*/ + function foo() {} -foo = bar; +foo = bar; /*error 'foo' is a function.*/ function foo() { - foo = bar; + foo = bar; /*error 'foo' is a function.*/ } ``` Unlike the same rule in JSHint, the following pattern is also considered a warning: ```js -foo = bar; +/*eslint no-func-assign: 2*/ + +foo = bar; /*error 'foo' is a function.*/ function foo() {} ``` The following patterns are not considered warnings: ```js +/*eslint no-func-assign: 2*/ + var foo = function () {} foo = bar; diff --git a/docs/rules/no-implicit-coercion.md b/docs/rules/no-implicit-coercion.md index 93683110f47..192ae96ec84 100644 --- a/docs/rules/no-implicit-coercion.md +++ b/docs/rules/no-implicit-coercion.md @@ -48,13 +48,19 @@ This rule has three options. The following patterns are considered warnings: ```js -var b = !!foo; -var b = ~foo.indexOf("."); // only with `indexOf`/`lastIndexOf` method calling. +/*eslint no-implicit-coercion: 2*/ + +var b = !!foo; /*error use `Boolean(foo)` instead.*/ +var b = ~foo.indexOf("."); /*error use `foo.indexOf(".") !== -1` instead.*/ +// only with `indexOf`/`lastIndexOf` method calling. + ``` The following patterns are not considered warnings: ```js +/*eslint no-implicit-coercion: 2*/ + var b = Boolean(foo); var b = foo.indexOf(".") !== -1; @@ -66,13 +72,17 @@ var n = ~foo; // This is a just binary negating. The following patterns are considered warnings: ```js -var n = +foo; -var n = 1 * foo; +/*eslint no-implicit-coercion: 2*/ + +var n = +foo; /*error use `Number(foo)` instead.*/ +var n = 1 * foo; /*error use `Number(foo)` instead.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-implicit-coercion: 2*/ + var b = Number(foo); var b = parseFloat(foo); var b = parseInt(foo, 10); @@ -83,12 +93,16 @@ var b = parseInt(foo, 10); The following patterns are considered warnings: ```js -var n = "" + foo; +/*eslint no-implicit-coercion: 2*/ + +var n = "" + foo; /*error use `String(foo)` instead.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-implicit-coercion: 2*/ + var b = String(foo); ``` diff --git a/docs/rules/no-implied-eval.md b/docs/rules/no-implied-eval.md index ee7c26a0312..36403f25743 100644 --- a/docs/rules/no-implied-eval.md +++ b/docs/rules/no-implied-eval.md @@ -27,20 +27,24 @@ This rule aims to eliminate implied `eval()` through the use of `setTimeout()`, The following patterns are considered warnings: ```js -setTimeout("alert('Hi!');", 100); +/*eslint no-implied-eval: 2*/ + +setTimeout("alert('Hi!');", 100); /*error Implied eval. Consider passing a function instead of a string.*/ -setInterval("alert('Hi!');", 100); +setInterval("alert('Hi!');", 100); /*error Implied eval. Consider passing a function instead of a string.*/ -execScript("alert('Hi!')"); +execScript("alert('Hi!')"); /*error Implied eval. Consider passing a function instead of a string.*/ -window.setTimeout("count = 5", 10); +window.setTimeout("count = 5", 10); /*error Implied eval. Consider passing a function instead of a string.*/ -window.setInterval("foo = bar", 10); +window.setInterval("foo = bar", 10); /*error Implied eval. Consider passing a function instead of a string.*/ ``` The following patterns are not warnings: ```js +/*eslint no-implied-eval: 2*/ + setTimeout(function() { alert("Hi!"); }, 100); diff --git a/docs/rules/no-inline-comments.md b/docs/rules/no-inline-comments.md index 21b6e8af206..217f79ca0bb 100644 --- a/docs/rules/no-inline-comments.md +++ b/docs/rules/no-inline-comments.md @@ -14,32 +14,28 @@ This rule takes no arguments. The following patterns are considered warnings: ```js -var a = 1; // declaring a to 1 -``` +/*eslint no-inline-comments: 2*/ + +var a = 1; // declaring a to 1 /*error Unexpected comment inline with code.*/ -```js function getRandomNumber(){ -return 4; // chosen by fair dice roll. - // guaranteed to be random. + return 4; // chosen by fair dice roll. /*error Unexpected comment inline with code.*/ + // guaranteed to be random. } -``` -```js -/* A block comment before code */ var a = 2; -``` +/* A block comment before code */ var b = 2; /*error Unexpected comment inline with code.*/ -```js -var a = 3; /* A block comment after code */ +var c = 3; /* A block comment after code */ /*error Unexpected comment inline with code.*/ ``` The following patterns are not warnings: ```js +/*eslint no-inline-comments: 2*/ + // This is a comment above a line of code var foo = 5; -``` -```js -var foo = 5; +var bar = 5; //This is a comment below a line of code ``` diff --git a/docs/rules/no-inner-declarations.md b/docs/rules/no-inner-declarations.md index d25e8e81c70..6b7c1af40d4 100644 --- a/docs/rules/no-inner-declarations.md +++ b/docs/rules/no-inner-declarations.md @@ -56,28 +56,44 @@ function doSomething() { This rule requires that function declarations and, optionally, variable declarations be in the root of a program or the body of a function. +### Options + +This rule takes a single option to specify whether it should check just function declarations or both function and variable declarations. The default is `"functions"`. Setting it to `"both"` will apply the same rules to both types of declarations. + +You can set the option in configuration like this: + +```json +"no-inner-declarations": [2, "both"] +``` + The following patterns are considered warnings: ```js +/*eslint no-inner-declarations: 2*/ + if (test) { - function doSomething() { } + function doSomething() { } /*error Move function declaration to program root.*/ } function doSomethingElse() { if (test) { - function doAnotherThing() { } + function doAnotherThing() { } /*error Move function declaration to function body root.*/ } } +``` + +With "both" option to check variable declarations, the following are considered warnings: + +```js +/*eslint no-inner-declarations: [2, "both"]*/ -// With "both" option to check variable declarations if (test) { - var foo = 42; + var foo = 42; /*error Move variable declaration to program root.*/ } -// With "both" option to check variable declarations function doAnotherThing() { if (test) { - var bar = 81; + var bar = 81; /*error Move variable declaration to function body root.*/ } } ``` @@ -85,6 +101,8 @@ function doAnotherThing() { The following patterns are considered valid: ```js +/*eslint no-inner-declarations: 2*/ + function doSomething() { } function doSomethingElse() { @@ -111,10 +129,6 @@ function doAnotherThing() { } ``` -## Options - -This rule takes a single option to specify whether it should check just function declarations or both function and variable declarations. The default is `"functions"`. Setting it to `"both"` will apply the same rules to both types of declarations. - ## When Not To Use It The function declaration portion rule will be rendered obsolete when [block-scoped functions](https://bugzilla.mozilla.org/show_bug.cgi?id=585536) land in ES6, but until then, it should be left on to enforce valid constructions. Disable checking variable declarations when using [block-scoped-var](block-scoped-var.md) or if declaring variables in nested blocks is acceptable despite hoisting. diff --git a/docs/rules/no-invalid-regexp.md b/docs/rules/no-invalid-regexp.md index dd6ff273469..c2f5a20c34d 100644 --- a/docs/rules/no-invalid-regexp.md +++ b/docs/rules/no-invalid-regexp.md @@ -7,28 +7,24 @@ This rule validates string arguments passed to the `RegExp` constructor. The following patterns are considered warnings: ```js -RegExp('[']) -``` +/*eslint no-invalid-regexp: 2*/ -```js -RegExp('.', 'z') // invalid flags -``` +RegExp('[') /*error Invalid regular expression: /[/: Unterminated character class*/ -```js -new RegExp('\\') +RegExp('.', 'z') /*error Invalid flags supplied to RegExp constructor 'z'*/ + +new RegExp('\\') /*error Invalid regular expression: /\/: \ at end of pattern*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-invalid-regexp: 2*/ + RegExp('.') -``` -```js new RegExp -``` -```js this.RegExp('[') ``` @@ -36,7 +32,7 @@ this.RegExp('[') ECMAScript 6 adds the "u" ([unicode](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.unicode)) and "y" ([sticky](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.sticky)) flags. You can enable these to be recognized as valid by adding the following to your `.eslintrc` file: -```js +```json "ecmaFeatures": { "regexYFlag": true, "regexUFlag": true diff --git a/docs/rules/no-invalid-this.md b/docs/rules/no-invalid-this.md index 22f6dcfbb39..ae9839ad6c5 100644 --- a/docs/rules/no-invalid-this.md +++ b/docs/rules/no-invalid-this.md @@ -33,84 +33,70 @@ This rule warns below **only** under the strict mode. Please note your code in ES2015 Modules/Classes is always the strict mode. ```js -this.a = 0; -baz(() => this); -``` +/*eslint no-invalid-this: 2*/ + +this.a = 0; /*error Unexpected `this`.*/ +baz(() => this); /*error Unexpected `this`.*/ -```js (function() { - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ })(); -``` -```js function foo() { - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ } -``` -```js var foo = function() { - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ }; -``` -```js foo(function() { - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ }); -``` -```js obj.foo = () => { // `this` of arrow functions is the outer scope's. - this.a = 0; + this.a = 0; /*error Unexpected `this`.*/ }; -``` -```js var obj = { aaa: function() { return function foo() { // There is in a method `aaa`, but `foo` is not a method. - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ }; } }; -``` -```js class Foo { static foo() { - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ } } -``` -```js foo.forEach(function() { - this.a = 0; - baz(() => this); + this.a = 0; /*error Unexpected `this`.*/ + baz(() => this); /*error Unexpected `this`.*/ }); ``` ### The following patterns are not considered warnings: ```js +/*eslint no-invalid-regexp: 2*/ + function Foo() { // OK, this is in a legacy style constructor. this.a = 0; baz(() => this); } -``` -```js class Foo { constructor() { // OK, this is in a constructor. @@ -118,63 +104,49 @@ class Foo { baz(() => this); } } -``` -```js var obj = { foo: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; } }; -``` -```js var obj = { foo() { // OK, this is in a method (this function is on object literal). this.a = 0; } }; -``` -```js var obj = { get foo() { // OK, this is in a method (this function is on object literal). return this.a; } }; -``` -```js var obj = Object.create(null, { foo: {value: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; }} }); -``` -```js Object.defineProperty(obj, "foo", { value: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; } -}; -``` +}); -```js Object.defineProperties(obj, { foo: {value: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; }} -}; -``` +}); -```js function Foo() { this.foo = function foo() { // OK, this is in a method (this function assigns to a property). @@ -182,23 +154,17 @@ function Foo() { baz(() => this); }; } -``` -```js obj.foo = function foo() { // OK, this is in a method (this function assigns to a property). this.a = 0; }; -``` -```js Foo.prototype.foo = function foo() { // OK, this is in a method (this function assigns to a property). this.a = 0; }; -``` -```js class Foo { foo() { // OK, this is in a method. @@ -206,24 +172,18 @@ class Foo { baz(() => this); } } -``` -```js var foo = (function foo() { // OK, the `bind` method of this function is called directly. this.a = 0; }).bind(obj); -``` -```js foo.forEach(function() { // OK, `thisArg` of `.forEach()` is given. this.a = 0; baz(() => this); }, thisArg); -``` -```js /** @this Foo */ function foo() { // OK, this function has a `@this` tag in its JSDoc comment. diff --git a/docs/rules/no-irregular-whitespace.md b/docs/rules/no-irregular-whitespace.md index ab0646a61ee..d9075bd99c4 100644 --- a/docs/rules/no-irregular-whitespace.md +++ b/docs/rules/no-irregular-whitespace.md @@ -43,48 +43,51 @@ With this rule enabled the following characters will cause warnings outside of s \u205f - Medium Mathematical Space \u3000 - Ideographic Space - The following examples are considered warnings: ```js -function thing(){ +/*eslint no-irregular-whitespace: 2*/ + +function thing() /**/{ /*error Irregular whitespace not allowed*/ return 'test'; } -function thing(){ +function thing( /**/){ /*error Irregular whitespace not allowed*/ return 'test'; } -function thing(){ +function thing /**/(){ /*error Irregular whitespace not allowed*/ return 'test'; } -function thing(){ +function thing᠎/**/(){ /*error Irregular whitespace not allowed*/ return 'test'; } function thing() { - return 'test'; + return 'test'; /**/ /*error Irregular whitespace not allowed*/ } function thing() { - return 'test'; + return 'test'; /**/ /*error Irregular whitespace not allowed*/ } ``` The following patterns are not considered warnings: ```js +/*eslint no-irregular-whitespace: 2*/ + function thing() { - return 'thing'; + return ' thing'; } function thing() { - return 'thing'; + return '​thing'; } function thing() { - return 'thing'; + return 'th ing'; } ``` diff --git a/docs/rules/no-iterator.md b/docs/rules/no-iterator.md index e6b39c77029..7e152106756 100644 --- a/docs/rules/no-iterator.md +++ b/docs/rules/no-iterator.md @@ -17,19 +17,23 @@ This rule is aimed at preventing errors that may arise from using the `__iterato The following patterns are considered warnings: ```js -Foo.prototype.__iterator__ = function() { +/*eslint no-iterator: 2*/ + +Foo.prototype.__iterator__ = function() { /*error Reserved name '__iterator__'.*/ return new FooIterator(this); }; -foo.__iterator__ = function () {}; +foo.__iterator__ = function () {}; /*error Reserved name '__iterator__'.*/ -foo["__iterator__"] = function () {}; +foo["__iterator__"] = function () {}; /*error Reserved name '__iterator__'.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-iterator: 2*/ + var __iterator__ = foo; // Not using the `__iterator__` property. ``` diff --git a/docs/rules/no-label-var.md b/docs/rules/no-label-var.md index 2267478444c..3790ac8ca65 100644 --- a/docs/rules/no-label-var.md +++ b/docs/rules/no-label-var.md @@ -7,9 +7,11 @@ This rule aims to create clearer code by disallowing the bad practice of creatin The following patterns are considered warnings: ```js +/*eslint no-label-var: 2*/ + var x = foo; function bar() { -x: +x: /*error Found identifier with same name as label.*/ for (;;) { break x; } @@ -19,6 +21,8 @@ x: The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-label-var: 2*/ + // The variable that has the same name as the label is not in scope. function foo() { diff --git a/docs/rules/no-labels.md b/docs/rules/no-labels.md index c6623585817..b980568992d 100644 --- a/docs/rules/no-labels.md +++ b/docs/rules/no-labels.md @@ -23,25 +23,29 @@ This rule aims to eliminate the use of labeled statements in JavaScript. It will The following patterns are considered warnings: ```js -label: +/*eslint no-labels: 2*/ + +label: /*error Unexpected labeled statement.*/ while(true) { // ... } -label: +label: /*error Unexpected labeled statement.*/ while(true) { - break label; + break label; /*error Unexpected label in break statement.*/ } -label: +label: /*error Unexpected labeled statement.*/ while(true) { - continue label; + continue label; /*error Unexpected label in continue statement.*/ } ``` The following patterns are not warnings: ```js +/*eslint no-labels: 2*/ + var f = { label: "foo" }; @@ -53,7 +57,6 @@ while (true) { while (true) { continue; } - ``` ## When Not To Use It diff --git a/docs/rules/no-lone-blocks.md b/docs/rules/no-lone-blocks.md index 4e7a9ae854e..e541b505593 100644 --- a/docs/rules/no-lone-blocks.md +++ b/docs/rules/no-lone-blocks.md @@ -17,29 +17,34 @@ This rule aims to eliminate unnecessary and potentially confusing blocks at the The following patterns are considered warnings: ```js -{} +/*eslint no-lone-blocks: 2*/ + +{} /*error Block is redundant.*/ if (foo) { bar(); - { + { /*error Nested block is redundant.*/ baz(); } } function bar() { - { + { /*error Nested block is redundant.*/ baz(); } } { - function foo() {} + function foo() {} /*error Block is redundant.*/ } ``` The following patterns are not considered warnings: ```js +/*eslint-env es6*/ +/*eslint no-lone-blocks: 2*/ + while (foo) { bar(); } @@ -65,8 +70,15 @@ function bar() { { class Foo {} } +``` + +In strict mode, with `ecmaFeatures: { blockBindings: true }`, the following will not warn: + +```js +/*eslint-env es6*/ +/*eslint no-lone-blocks: 2*/ +"use strict"; -// In strict mode, with blockBindings: true { function foo() {} } diff --git a/docs/rules/no-lonely-if.md b/docs/rules/no-lonely-if.md index 173ba694e8d..37c107ab231 100644 --- a/docs/rules/no-lonely-if.md +++ b/docs/rules/no-lonely-if.md @@ -3,11 +3,11 @@ If an `if` statement is the only statement in the `else` block of a parent `if` statement, it is often clearer to combine the two to using `else if` form. ```js -if (...) { - ... +if (foo) { + // ... } else { - if (...) { - ... + if (bar) { + // ... } } ``` @@ -15,10 +15,10 @@ if (...) { should be rewritten as ```js -if (...) { - ... -} else if (...) { - ... +if (foo) { + // ... +} else if (bar) { + // ... } ``` @@ -29,10 +29,12 @@ This rule warns when an `if` statement's `else` block contains only another `if` The following patterns are considered warnings: ```js +/*eslint no-lonely-if: 2*/ + if (condition) { // ... } else { - if (anotherCondition) { + if (anotherCondition) { /*error Unexpected if as the only statement in an else block.*/ // ... } } @@ -41,6 +43,8 @@ if (condition) { The following patterns are not considered warnings: ```js +/*eslint no-lonely-if: 2*/ + if (condition) { // ... } else if (anotherCondition) { diff --git a/docs/rules/no-loop-func.md b/docs/rules/no-loop-func.md index 0ea15198a75..a1dcf1b1d61 100644 --- a/docs/rules/no-loop-func.md +++ b/docs/rules/no-loop-func.md @@ -32,29 +32,26 @@ This error is raised to highlight a piece of code that may not work as you expec The following patterns are considered warnings: ```js +/*eslint no-loop-func: 2*/ + for (var i=10; i; i--) { - (function() { return i; })(); + (function() { return i; })(); /*error Don't make functions within a loop*/ } -``` -```js while(i) { - var a = function() { return i; }; + var a = function() { return i; }; /*error Don't make functions within a loop*/ a(); } -``` -```js do { - function a() { return i; }; + function a() { return i; }; /*error Don't make functions within a loop*/ a(); } while (i); -``` -```js let foo = 0; for (let i=10; i; i--) { - var a = function() { return foo; }; // Bad, function is referencing block scoped variable in the outer scope. + // Bad, function is referencing block scoped variable in the outer scope. + var a = function() { return foo; }; /*error Don't make functions within a loop*/ a(); } ``` @@ -62,21 +59,19 @@ for (let i=10; i; i--) { The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-loop-func: 2*/ + var a = function() {}; for (var i=10; i; i--) { a(); } -``` -```js for (var i=10; i; i--) { var a = function() {}; // OK, no references to variables in the outer scopes. a(); } -``` -```js for (let i=10; i; i--) { var a = function() { return i; }; // OK, all references are referring to block scoped variable in the loop. a(); diff --git a/docs/rules/no-mixed-requires.md b/docs/rules/no-mixed-requires.md index 9acbadaba3d..b58024e8913 100644 --- a/docs/rules/no-mixed-requires.md +++ b/docs/rules/no-mixed-requires.md @@ -13,7 +13,7 @@ When this rule is enabled, all `var` statements must satisfy the following condi This rule comes with one boolean option called `grouping` which is turned off by default. You can set it in your `eslint.json`: -```js +```json { "no-mixed-requires": [1, true] } @@ -50,6 +50,8 @@ var fs = require('fs'), // "core" \ The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-mixed-requires: 2*/ + // only require declarations (grouping off) var eventEmitter = require('events').EventEmitter, myUtils = require('./utils'), @@ -69,16 +71,23 @@ var foo = require('foo' + VERSION), The following patterns are considered warnings: ```js -// mixing require and other declarations -var fs = require('fs'), +/*eslint no-mixed-requires: 2*/ + +var fs = require('fs'), /*error Do not mix 'require' and other declarations.*/ i = 0; +``` + +The following patterns are considered warnings when grouping is turned on: + +```js +/*eslint no-mixed-requires: [2, true]*/ -// invalid because of mixed types "core" and "file" (grouping on) -var fs = require('fs'), +// invalid because of mixed types "core" and "file" +var fs = require('fs'), /*error Do not mix core, module, file and computed requires.*/ async = require('async'); -// invalid because of mixed types "file" and "unknown" (grouping on) -var foo = require('foo'), +// invalid because of mixed types "file" and "unknown" +var foo = require('foo'), /*error Do not mix core, module, file and computed requires.*/ bar = require(getBarModuleName()); ``` diff --git a/docs/rules/no-mixed-spaces-and-tabs.md b/docs/rules/no-mixed-spaces-and-tabs.md index 579bdde02b2..6597d74c41c 100644 --- a/docs/rules/no-mixed-spaces-and-tabs.md +++ b/docs/rules/no-mixed-spaces-and-tabs.md @@ -6,48 +6,64 @@ Most code conventions require either tabs or spaces be used for indentation. As The `no-mixed-spaces-and-tabs` rule is aimed at flagging any lines of code that are indented with a mixture of tabs and spaces. +### Options + +* Smart Tabs + +This option suppresses warnings about mixed tabs and spaces when the latter are used for alignment only. This technique is called [SmartTabs](http://www.emacswiki.org/emacs/SmartTabs). The option is turned off by default. + +You can enable this option by using the following configuration: + +```json +"no-mixed-spaces-and-tabs": [2, "smart-tabs"] +``` + The following patterns are considered warnings: ```js +/*eslint no-mixed-spaces-and-tabs: 2*/ + function add(x, y) { ---->..return x + y; +// --->..return x + y; + + return x + y; /*error Mixed spaces and tabs.*/ } function main() { ---->var x = 5, ---->....y = 7; +// --->var x = 5, +// --->....y = 7; + + var x = 5, + y = 7; /*error Mixed spaces and tabs.*/ } ``` The following patterns are not warnings: ```js -function add(x, y) { ---->return x + y; -} +/*eslint no-mixed-spaces-and-tabs: 2*/ -/* - * When the SmartTabs option is enabled the following - * does not produce a warning. - */ -function main() { ---->var x = 5, ---->....y = 7; +function add(x, y) { +// --->return x + y; + return x + y; } ``` -### Options - -* Smart Tabs +When the SmartTabs option is enabled the following does not produce a warning: -This option suppresses warnings about mixed tabs and spaces when the latter are used for alignment only. This technique is called [SmartTabs](http://www.emacswiki.org/emacs/SmartTabs). The option is turned off by default. +```js +/*eslint no-mixed-spaces-and-tabs: [2, "smart-tabs"]*/ -You can enable this option by using the following configuration: +function main() { +// --->var x = 5, +// --->....y = 7; -```json -"no-mixed-spaces-and-tabs": [2, "smart-tabs"] + var x = 5, + y = 7; +} ``` + ## Further Reading * [Smart Tabs](http://www.emacswiki.org/emacs/SmartTabs) diff --git a/docs/rules/no-multi-spaces.md b/docs/rules/no-multi-spaces.md index 7759e65ec51..38e572892b5 100644 --- a/docs/rules/no-multi-spaces.md +++ b/docs/rules/no-multi-spaces.md @@ -23,44 +23,32 @@ This rule aims to disallow multiple whitespace around logical expressions, condi The following patterns are considered warnings: ```js -var a = 1; -``` +/*eslint no-multi-spaces: 2*/ -```js -if(foo === "bar") {} -``` +var a = 1; /*error Multiple spaces found before '1'.*/ -```js -a << b -``` +if(foo === "bar") {} /*error Multiple spaces found before '==='.*/ -```js -var arr = [1, 2]; -``` +a << b /*error Multiple spaces found before 'b'.*/ -```js -a ? b: c +var arr = [1, 2]; /*error Multiple spaces found before '2'.*/ + +a ? b: c /*error Multiple spaces found before 'b'.*/ ``` The following patterns are not warnings: ```js +/*eslint no-multi-spaces: 2*/ + var a = 1; -``` -```js if(foo === "bar") {} -``` -```js a << b -``` -```js var arr = [1, 2]; -``` -```js a ? b: c ``` @@ -73,6 +61,7 @@ With this option, the following patterns are not warnings: ```js /* eslint no-multi-spaces: 2 */ /* eslint key-spacing: [2, { align: "value" }] */ + var obj = { first: "first", second: "second" @@ -89,8 +78,9 @@ The default `Property` exception can be disabled by setting it to `false`, so th ```js /* eslint no-multi-spaces: [2, { exceptions: { "Property": false } }] */ /* eslint key-spacing: [2, { align: "value" }] */ + var obj = { - first: "first", + first: "first", /*error Multiple spaces found before '"first"'.*/ second: "second" }; ``` @@ -99,10 +89,14 @@ You may wish to align variable declarations or import declarations with spaces. ```js /* eslint no-multi-spaces: [2, { exceptions: { "VariableDeclarator": true } }] */ + var someVar = 'foo'; var someOtherVar = 'barBaz'; +``` +```js /* eslint no-multi-spaces: [2, { exceptions: { "ImportDeclaration": true } }] */ + import mod from 'mod'; import someOtherMod from 'some-other-mod'; ``` diff --git a/docs/rules/no-multi-str.md b/docs/rules/no-multi-str.md index d24a315cdf3..3b509ceed87 100644 --- a/docs/rules/no-multi-str.md +++ b/docs/rules/no-multi-str.md @@ -16,13 +16,17 @@ This rule is aimed at preventing the use of multiline strings. The following generates a warning: ```js -var x = "Line 1 \ +/*eslint no-multi-str: 2*/ + +/*error Multiline support is limited to browsers supporting ES5 only.*/ var x = "Line 1 \ Line 2"; ``` The following does not generate a warning: -```javascript +```js +/*eslint no-multi-str: 2*/ + var x = "Line 1\n" + "Line 2"; ``` diff --git a/docs/rules/no-multiple-empty-lines.md b/docs/rules/no-multiple-empty-lines.md index ac83580079e..721cbe77d90 100644 --- a/docs/rules/no-multiple-empty-lines.md +++ b/docs/rules/no-multiple-empty-lines.md @@ -7,15 +7,23 @@ Some developers prefer to have multiple blank lines removed, while others feel t This rule aims to reduce the scrolling required when reading through your code. It will warn when the maximum amount of empty lines has been exceeded. +### Options + +You can configure the depth as an option by using the second argument in your configuration. For example, this sets the rule as an error (code is 2) with a maximum tolerated blank lines of 2: + +```json +"no-multiple-empty-lines": [2, {"max": 2}] +``` + The following patterns are considered warnings: ```js -// no-multiple-empty-lines: [1, {max: 2}] // Maximum of 2 empty lines. -var foo = 5; - +/*eslint no-multiple-empty-lines: [2, {max: 2}]*/ +var foo = 5; + /*error Multiple blank lines not allowed.*/ var bar = 3; ``` @@ -23,28 +31,22 @@ var bar = 3; The following patterns are not warnings: ```js +/*eslint no-multiple-empty-lines: [2, {max: 2}]*/ -// no-multiple-empty-lines: [1, {max: 2}] // Maximum of 2 empty lines. var foo = 5; var bar = 3; +``` + +```js +/*eslint no-multiple-empty-lines: [2, {max: 4}]*/ -// no-multiple-empty-lines: [1, {max: 4}] // Maximum of 4 empty lines. var foo = 5; var bar = 3; - -``` - -### Options - -You can configure the depth as an option by using the second argument in your configuration. For example, this sets the rule as an error (code is 2) with a maximum tolerated blank lines of 2: - -```json -"no-multiple-empty-lines": [2, {"max": 2}] ``` ## When Not To Use It diff --git a/docs/rules/no-native-reassign.md b/docs/rules/no-native-reassign.md index ba9ae72a5b5..3502253789f 100644 --- a/docs/rules/no-native-reassign.md +++ b/docs/rules/no-native-reassign.md @@ -13,14 +13,16 @@ The native objects reported by this rule are the `builtin` variables from [globa The following patterns are considered warnings: ```js -String = new Object(); +/*eslint no-native-reassign: 2*/ + +String = new Object(); /*error String is a read-only native object.*/ ``` ## Options This rule accepts an `exceptions` option, which can be used to specify a list of builtins for which reassignments will be allowed: -```js +```json { "rules": { "no-native-reassign": [2, {"exceptions": ["Object"]}] diff --git a/docs/rules/no-negated-in-lhs.md b/docs/rules/no-negated-in-lhs.md index c8d4ce67389..2ec3052cf21 100644 --- a/docs/rules/no-negated-in-lhs.md +++ b/docs/rules/no-negated-in-lhs.md @@ -5,19 +5,49 @@ This error is raised to highlight a potential error. Commonly, when a developer intends to write ```js -if(!(a in b)) // do something +if(!(a in b)) { + // do something +} ``` they will instead write ```js -if(!a in b) // do something +if(!a in b) { + // do something +} ``` If one intended the original behaviour, the left operand should be explicitly coerced to a string like below. ```js -if(('' + !a) in b) // do something +if(('' + !a) in b) { + // do something +} +``` + +The following patterns are considered warnings: + +```js +/*eslint no-negated-in-lhs: 2*/ + +if(!a in b) { /*error The `in` expression's left operand is negated*/ + // do something +} +``` + +The following patterns are not considered warnings: + +```js +/*eslint no-negated-in-lhs: 2*/ + +if(!(a in b)) { + // do something +} + +if(('' + !a) in b) { + // do something +} ``` ## When Not To Use It diff --git a/docs/rules/no-nested-ternary.md b/docs/rules/no-nested-ternary.md index baf7f11c700..63874625c6e 100644 --- a/docs/rules/no-nested-ternary.md +++ b/docs/rules/no-nested-ternary.md @@ -13,14 +13,18 @@ The `no-nested-ternary` rule aims to increase the clarity and readability of cod The following patterns are considered warnings: ```js -var thing = foo ? bar : baz === qux ? quxx : foobar; +/*eslint no-nested-ternary: 2*/ -foo ? baz === qux ? quxx() : foobar() : bar(); +var thing = foo ? bar : baz === qux ? quxx : foobar; /*error Do not nest ternary expressions*/ + +foo ? baz === qux ? quxx() : foobar() : bar(); /*error Do not nest ternary expressions*/ ``` The following patterns are considered okay and could be used alternatively: ```js +/*eslint no-nested-ternary: 2*/ + var thing; if (foo) { diff --git a/docs/rules/no-new-func.md b/docs/rules/no-new-func.md index e85e83915ad..bf85b200ca2 100644 --- a/docs/rules/no-new-func.md +++ b/docs/rules/no-new-func.md @@ -13,13 +13,17 @@ This is considered by many to be a bad practice due to the difficult in debuggin This error is raised to highlight the use of a bad practice. By passing a string to the Function constructor, you are requiring the engine to parse that string much in the way it has to when you call the eval function. ```js -var x = new Function("a", "b", "return a + b"); -var x = Function("a", "b", "return a + b"); +/*eslint no-new-func: 2*/ + +var x = new Function("a", "b", "return a + b"); /*error The Function constructor is eval.*/ +var x = Function("a", "b", "return a + b"); /*error The Function constructor is eval.*/ ``` The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-new-func: 2*/ + var x = function (a, b) { return a + b; }; diff --git a/docs/rules/no-new-object.md b/docs/rules/no-new-object.md index 45fe5762515..e0fbbe340d5 100644 --- a/docs/rules/no-new-object.md +++ b/docs/rules/no-new-object.md @@ -23,14 +23,18 @@ This rule aims to eliminate use of the `Object` constructor. As such, it warns w The following patterns are considered warnings: ```js -var myObject = new Object(); +/*eslint no-new-object: 2*/ + +var myObject = new Object(); /*error The object literal notation {} is preferrable.*/ -var myObject = new Object; +var myObject = new Object; /*error The object literal notation {} is preferrable.*/ ``` The following patterns are not warnings: ```js +/*eslint no-new-object: 2*/ + var myObject = new CustomObject(); var myObject = {}; diff --git a/docs/rules/no-new-require.md b/docs/rules/no-new-require.md index f3e5dc6ab59..880d899c93a 100644 --- a/docs/rules/no-new-require.md +++ b/docs/rules/no-new-require.md @@ -27,12 +27,16 @@ This rule aims to eliminate use of the `new require` expression. As such, it war The following pattern is considered a warning: ```js -var appHeader = new require('app-header'); +/*eslint no-new-require: 2*/ + +var appHeader = new require('app-header'); /*error Unexpected use of new with require.*/ ``` The following pattern is not a warning: ```js +/*eslint no-new-require: 2*/ + var AppHeader = require('app-header'); ``` diff --git a/docs/rules/no-new-wrappers.md b/docs/rules/no-new-wrappers.md index 5d9fe58b591..c15d701740c 100644 --- a/docs/rules/no-new-wrappers.md +++ b/docs/rules/no-new-wrappers.md @@ -42,18 +42,22 @@ This rule aims to eliminate the use of `String`, `Number`, and `Boolean` with th The following patterns are considered warnings: ```js -var stringObject = new String("Hello world"); -var numberObject = new Number(33); -var booleanObject = new Boolean(false); +/*eslint no-new-wrappers: 2*/ -var stringObject = new String; -var numberObject = new Number; -var booleanObject = new Boolean; +var stringObject = new String("Hello world"); /*error Do not use String as a constructor.*/ +var numberObject = new Number(33); /*error Do not use Number as a constructor.*/ +var booleanObject = new Boolean(false); /*error Do not use Boolean as a constructor.*/ + +var stringObject = new String; /*error Do not use String as a constructor.*/ +var numberObject = new Number; /*error Do not use Number as a constructor.*/ +var booleanObject = new Boolean; /*error Do not use Boolean as a constructor.*/ ``` The following patterns are not warnings: ```js +/*eslint no-new-wrappers: 2*/ + var text = String(someValue); var num = Number(someValue); diff --git a/docs/rules/no-new.md b/docs/rules/no-new.md index 160d84e123b..bdbf5afc86a 100644 --- a/docs/rules/no-new.md +++ b/docs/rules/no-new.md @@ -21,12 +21,16 @@ This rule is aimed at maintaining consistency and convention by disallowing cons The following patterns are considered warnings: ```js -new Thing(); +/*eslint no-new: 2*/ + +new Thing(); /*error Do not use 'new' for side effects.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-new: 2*/ + var thing = new Thing(); Thing(); diff --git a/docs/rules/no-obj-calls.md b/docs/rules/no-obj-calls.md index c9140cfd7c0..5c206c628b2 100644 --- a/docs/rules/no-obj-calls.md +++ b/docs/rules/no-obj-calls.md @@ -13,13 +13,17 @@ This rule is aimed at preventing the accidental calling of global objects as fun The following patterns are considered warnings: ```js -var x = Math(); -var y = JSON(); +/*eslint no-obj-calls: 2*/ + +var x = Math(); /*error 'Math' is not a function.*/ +var y = JSON(); /*error 'JSON' is not a function.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-obj-calls: 2*/ + var x = math(); var y = json(); ``` diff --git a/docs/rules/no-octal-escape.md b/docs/rules/no-octal-escape.md index 8942a5e0379..1009edf243a 100644 --- a/docs/rules/no-octal-escape.md +++ b/docs/rules/no-octal-escape.md @@ -2,7 +2,7 @@ As of version 5 of the ECMAScript specification, octal escape sequences are a deprecated feature and should not be used. It is recommended that Unicode escapes be used instead. -```js +``` var foo = "Copyright \251"; ``` @@ -12,13 +12,17 @@ The rule is aimed at preventing the use of a deprecated JavaScript feature, the The following patterns are considered warnings: -```js -var foo = "Copyright \251"; +``` +/*eslint no-octal-escape: 2*/ + +var foo = "Copyright \251"; /*error Don't use octal: '\251'. Use '\u....' instead.*/ ``` The following patterns are not considered warnings: -```js +``` +/*eslint no-octal-escape: 2*/ + var foo = "Copyright \u00A9"; // unicode var foo = "Copyright \xA9"; // hexadecimal diff --git a/docs/rules/no-octal.md b/docs/rules/no-octal.md index 17cc3d4e5a1..14bc7a8249f 100644 --- a/docs/rules/no-octal.md +++ b/docs/rules/no-octal.md @@ -2,7 +2,7 @@ Octal literals are numerals that begin with a leading zero, such as: -```js +``` var num = 071; // 57 ``` @@ -16,14 +16,18 @@ The rule is aimed at preventing the use of a deprecated JavaScript feature, the The following patterns are considered warnings: -```js -var num = 071; -var result = 5 + 07; +``` +/*eslint no-octal: 2*/ + +var num = 071; /*error Octal literals should not be used.*/ +var result = 5 + 07; /*error Octal literals should not be used.*/ ``` The following patterns are not considered warnings: -```js +``` +/*eslint no-octal: 2*/ + var num = "071"; ``` diff --git a/docs/rules/no-param-reassign.md b/docs/rules/no-param-reassign.md index a509bda4214..dd8817b3073 100644 --- a/docs/rules/no-param-reassign.md +++ b/docs/rules/no-param-reassign.md @@ -22,40 +22,40 @@ This rule takes one option, an object, with a property `"props"`. ### The following patterns are considered warnings: ```js +/*eslint no-param-reassign: 2*/ + function foo(bar) { - bar = 13; + bar = 13; /*error Assignment to function parameter 'bar'.*/ } -``` -```js function foo(bar) { - bar++; + bar++; /*error Assignment to function parameter 'bar'.*/ } ``` When `{"props": true}`: ```js +/*eslint no-param-reassign: [2, { "props": true }]*/ + function foo(bar) { - bar.prop = "value"; + bar.prop = "value"; /*error Assignment to function parameter 'bar'.*/ } -``` -```js function foo(bar) { - delete bar.aaa; + delete bar.aaa; /*error Assignment to function parameter 'bar'.*/ } -``` -```js function foo(bar) { - bar.aaa++; + bar.aaa++; /*error Assignment to function parameter 'bar'.*/ } ``` ### The following patterns are not warnings: ```js +/*eslint no-param-reassign: 2*/ + function foo(a) { var b = a; } @@ -64,18 +64,16 @@ function foo(a) { When `{"props": false}`: ```js +/*eslint no-param-reassign: [2, { "props": false }]*/ + function foo(bar) { bar.prop = "value"; } -``` -```js function foo(bar) { delete bar.aaa; } -``` -```js function foo(bar) { bar.aaa++; } diff --git a/docs/rules/no-path-concat.md b/docs/rules/no-path-concat.md index 39bf25371c4..656530dd2ad 100644 --- a/docs/rules/no-path-concat.md +++ b/docs/rules/no-path-concat.md @@ -29,16 +29,19 @@ This rule aims to prevent string concatenation of directory paths in Node.js The following patterns are considered warnings: ```js +/*eslint no-path-concat: 2*/ -var fullPath = __dirname + "/foo.js"; +var fullPath = __dirname + "/foo.js"; /*error Use path.join() or path.resolve() instead of + to create paths.*/ -var fullPath = __filename + "/foo.js"; +var fullPath = __filename + "/foo.js"; /*error Use path.join() or path.resolve() instead of + to create paths.*/ ``` The following patterns are not warnings: ```js +/*eslint no-path-concat: 2*/ + var fullPath = dirname + "/foo.js"; ``` diff --git a/docs/rules/no-plusplus.md b/docs/rules/no-plusplus.md index c62b77cebe5..bb50ffd76cc 100644 --- a/docs/rules/no-plusplus.md +++ b/docs/rules/no-plusplus.md @@ -35,16 +35,20 @@ This rule is aimed at flagging the use of `++` and `--`. Some believe that the u The following patterns are considered warnings: ```js +/*eslint no-plusplus: 2*/ + var foo = 0; -foo++; +foo++; /*error Unary operator '++' used.*/ var bar = 42; -bar--; +bar--; /*error Unary operator '--' used.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-plusplus: 2*/ + var foo = 0; foo += 1; diff --git a/docs/rules/no-process-env.md b/docs/rules/no-process-env.md index 36fe749f386..0633b6bcd13 100644 --- a/docs/rules/no-process-env.md +++ b/docs/rules/no-process-env.md @@ -10,7 +10,9 @@ This rule is aimed at discouraging use of `process.env` to avoid global dependen The following patterns are considered warnings: ```js -if(process.env.NODE_ENV === "development") { +/*eslint no-process-env: 2*/ + +if(process.env.NODE_ENV === "development") { /*error Unexpected use of process.env.*/ //... } ``` @@ -18,6 +20,8 @@ if(process.env.NODE_ENV === "development") { The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-process-env: 2*/ + var config = require("./config"); if(config.env === "development") { diff --git a/docs/rules/no-process-exit.md b/docs/rules/no-process-exit.md index 7c4df802256..0d492ea5d3e 100644 --- a/docs/rules/no-process-exit.md +++ b/docs/rules/no-process-exit.md @@ -26,13 +26,17 @@ This rule aims to prevent the use of `process.exit()` in Node.js JavaScript. As The following patterns are considered warnings: ```js -process.exit(1); -process.exit(0); +/*eslint no-process-exit: 2*/ + +process.exit(1); /*error Don't use process.exit(); throw an error instead.*/ +process.exit(0); /*error Don't use process.exit(); throw an error instead.*/ ``` The following patterns are not warnings: ```js +/*eslint no-process-exit: 2*/ + Process.exit(); var exit = process.exit; ``` diff --git a/docs/rules/no-proto.md b/docs/rules/no-proto.md index 55654248738..7791637f782 100644 --- a/docs/rules/no-proto.md +++ b/docs/rules/no-proto.md @@ -9,14 +9,18 @@ When an object is created `__proto__` is set to the original prototype property The following patterns are considered warnings: ```js -var a = obj.__proto__; +/*eslint no-proto: 2*/ -var a = obj["__proto__"]; +var a = obj.__proto__; /*error The '__proto__' property is deprecated.*/ + +var a = obj["__proto__"]; /*error The '__proto__' property is deprecated.*/ ``` The following patterns are considered okay and could be used alternatively: ```js +/*eslint no-proto: 2*/ + var a = Object.getPrototypeOf(obj); ``` diff --git a/docs/rules/no-redeclare.md b/docs/rules/no-redeclare.md index 176ecd11864..464c7da68a9 100644 --- a/docs/rules/no-redeclare.md +++ b/docs/rules/no-redeclare.md @@ -9,15 +9,19 @@ This rule is aimed at eliminating variables that have multiple declarations in t The following patterns are considered warnings: ```js +/*eslint no-redeclare: 2*/ + var a = 3; -var a = 10; // redeclared +var a = 10; /*error "a" is already defined*/ ``` The following patterns are considered okay and do not cause warnings: ```js +/*eslint no-redeclare: 2*/ + var a = 3; -... +// ... a = 10; ``` @@ -39,13 +43,18 @@ If this is `true`, this rule checks with built-in global variables such as `Obje When `{"builtinGlobals": true}`, the following patterns are considered warnings: ```js -var Object = 0; // redeclared of the built-in globals. +/*eslint no-redeclare: [2, { "builtinGlobals": true }]*/ + +var Object = 0; /*error "Object" is already defined*/ ``` When `{"builtinGlobals": true}` and under `browser` environment, the following patterns are considered warnings: ```js -var top = 0; // redeclared of the built-in globals. +/*eslint-env browser*/ +/*eslint no-redeclare: [2, { "builtinGlobals": true }]*/ + +var top = 0; /*error "top" is already defined*/ ``` * Note: The `browser` environment has many built-in global variables, `top` is one of them. diff --git a/docs/rules/no-regex-spaces.md b/docs/rules/no-regex-spaces.md index c09b6794078..912a7392617 100644 --- a/docs/rules/no-regex-spaces.md +++ b/docs/rules/no-regex-spaces.md @@ -21,12 +21,18 @@ This rule aims to eliminate errors due to multiple spaces inside of a regular ex The following patterns are considered warnings: ```js -var re = /foo bar/; +/*eslint no-regex-spaces: 2*/ + +var re = /foo bar/; /*error Spaces are hard to count. Use {3}.*/ ``` The following patterns are not warnings: ```js +/*eslint no-regex-spaces: 2*/ + +var re = /foo {3}bar/; + var re = new RegExp("foo bar"); ``` diff --git a/docs/rules/no-reserved-keys.md b/docs/rules/no-reserved-keys.md index 0e74a77fb60..9fb6dfff5bb 100644 --- a/docs/rules/no-reserved-keys.md +++ b/docs/rules/no-reserved-keys.md @@ -1,5 +1,7 @@ # Disallow Use of Reserved Words as Keys (no-reserved-keys) +**Replacement notice:** This rule was removed in ESLint v1.0 and replaced by the [quote-props](quote-props.md) rule. + ECMAScript 3 described as series of keywords and reserved words, such as `if` and `public`, that are used or intended to be used for a core language feature. The specification also indicated that these keywords and reserved words could not be used as object property names without being enclosed in strings. An error occurs in an ECMAScript 3 environment when you use a keyword or reserved word in an object literal. For example: ```js diff --git a/docs/rules/no-restricted-modules.md b/docs/rules/no-restricted-modules.md index e5c5fc79d75..60c6cf5ddde 100644 --- a/docs/rules/no-restricted-modules.md +++ b/docs/rules/no-restricted-modules.md @@ -10,41 +10,41 @@ Blocking the `os` module can be useful if you don't want to allow any operating This rule allows you to specify modules that you don't want to use in your application. -The following patterns are considered warnings: +### Options -__config:__ `no-restricted-modules: [2, "fs"]` +The syntax to specify restricted modules looks like this: -```js -var fs = require('fs'); +```json +"no-restricted-modules": [2, <...moduleNames>] ``` -__config:__ `no-restricted-modules: [2, "cluster"]` +The following patterns are considered warnings: ```js -var fs = require(' cluster '); -``` - -The following patterns are not warnings: +/*eslint no-restricted-modules: [2, "fs"]*/ -__config:__ `no-restricted-modules: [2, "fs"]` +var fs = require('fs'); /*error 'fs' module is restricted from being used.*/ +``` ```js -var crypto = require('crypto'); -``` +/*eslint no-restricted-modules: [2, "cluster"]*/ -### Options +var fs = require(' cluster '); /*error 'cluster' module is restricted from being used.*/ +``` -The syntax to specify restricted modules looks like this: +The following patterns are not warnings: ```js -"no-restricted-modules": [, <...moduleNames>] +/*eslint no-restricted-modules: [2, "fs"]*/ + +var crypto = require('crypto'); ``` ### Examples To restrict the use of all Node.js core modules (via https://github.com/joyent/node/tree/master/lib): -```js +```json "no-restricted-modules": [2, "assert","buffer","child_process","cluster","crypto","dgram","dns","domain","events","freelist","fs","http","https","module","net","os","path","punycode","querystring","readline","repl","smalloc","stream","string_decoder","sys","timers","tls","tracing","tty","url","util","vm","zlib" ], diff --git a/docs/rules/no-restricted-syntax.md b/docs/rules/no-restricted-syntax.md index 0aaf75500c2..66a09ca32ed 100644 --- a/docs/rules/no-restricted-syntax.md +++ b/docs/rules/no-restricted-syntax.md @@ -20,14 +20,14 @@ This rule takes a list of strings where strings denote the node types: The following patterns are considered warnings: -```js +``` /* eslint no-restricted-syntax: [2, "FunctionExpression", "WithStatement"] */ -with (me) { +with (me) { /*error Using "WithStatement" is not allowed.*/ dontMess(); } -var doSomething = function () {}; +var doSomething = function () {}; /*error Using "FunctionExpression" is not allowed.*/ ``` The following patterns are not considered warnings: diff --git a/docs/rules/no-return-assign.md b/docs/rules/no-return-assign.md index c78d56839df..b06e39310a5 100644 --- a/docs/rules/no-return-assign.md +++ b/docs/rules/no-return-assign.md @@ -31,18 +31,22 @@ It disallows assignments unless they are enclosed in parentheses. The following patterns are considered warnings: ```js +/*eslint no-return-assign: 2*/ + function doSomething() { - return foo = bar + 2; + return foo = bar + 2; /*error Return statement should not contain assignment.*/ } function doSomething() { - return foo += 2; + return foo += 2; /*error Return statement should not contain assignment.*/ } ``` The following patterns are not warnings: ```js +/*eslint no-return-assign: 2*/ + function doSomething() { return foo == bar + 2; } @@ -64,22 +68,26 @@ All assignments are treated as warnings. The following patterns are considered warnings: ```js +/*eslint no-return-assign: [2, "always"]*/ + function doSomething() { - return foo = bar + 2; + return foo = bar + 2; /*error Return statement should not contain assignment.*/ } function doSomething() { - return foo += 2; + return foo += 2; /*error Return statement should not contain assignment.*/ } function doSomething() { - return (foo = bar + 2); + return (foo = bar + 2); /*error Return statement should not contain assignment.*/ } ``` The following patterns are not warnings: ```js +/*eslint no-return-assign: [2, "always"]*/ + function doSomething() { return foo == bar + 2; } diff --git a/docs/rules/no-script-url.md b/docs/rules/no-script-url.md index ae9666ea7cc..a3c3af413fc 100644 --- a/docs/rules/no-script-url.md +++ b/docs/rules/no-script-url.md @@ -7,7 +7,9 @@ Using `javascript:` URLs is considered by some as a form of `eval`. Code passed The following patterns are considered warnings: ```js -location.href = "javascript:void(0)"; +/*eslint no-script-url: 2*/ + +location.href = "javascript:void(0)"; /*error Script URL is a form of eval.*/ ``` ## Compatibility diff --git a/docs/rules/no-self-compare.md b/docs/rules/no-self-compare.md index 546bfefcec2..66c44c570b3 100644 --- a/docs/rules/no-self-compare.md +++ b/docs/rules/no-self-compare.md @@ -9,8 +9,10 @@ The only time you would compare a variable against itself is when you are testin This error is raised to highlight a potentially confusing and potentially pointless piece of code. There are almost no situations in which you would need to compare something to itself. ```js +/*eslint no-self-compare: 2*/ + var x = 10; -if (x === x) { +if (x === x) { /*error Comparing to itself is potentially pointless.*/ x = 20; } ``` diff --git a/docs/rules/no-sequences.md b/docs/rules/no-sequences.md index 5b19414772d..9221662a8e1 100644 --- a/docs/rules/no-sequences.md +++ b/docs/rules/no-sequences.md @@ -21,25 +21,29 @@ This rule forbids the use of the comma operator, with the following exceptions: The following patterns are considered warnings: -```js -foo = doSomething, val; +``` +/*eslint no-sequences: 2*/ + +foo = doSomething, val; /*error Unexpected use of comma operator.*/ -do {} while (doSomething(), !!test); +do {} while (doSomething(), !!test); /*error Unexpected use of comma operator.*/ -for (; doSomething(), !!test; ); +for (; doSomething(), !!test; ); /*error Unexpected use of comma operator.*/ -if (doSomething(), !!test); +if (doSomething(), !!test); /*error Unexpected use of comma operator.*/ -switch (val = foo(), val) {} +switch (val = foo(), val) {} /*error Unexpected use of comma operator.*/ -while (val = foo(), val < 42); +while (val = foo(), val < 42); /*error Unexpected use of comma operator.*/ -with (doSomething(), val) {} +with (doSomething(), val) {} /*error Unexpected use of comma operator.*/ ``` The following patterns are not considered warnings: -```js +``` +/*eslint no-sequences: 2*/ + foo = (doSomething(), val); (0,eval)("doSomething();"); @@ -54,7 +58,7 @@ switch ((val = foo(), val)) {} while ((val = foo(), val < 42)); -with ((doSomething(), val)) {} +// with ((doSomething(), val)) {} ``` ## When Not To Use It diff --git a/docs/rules/no-shadow-restricted-names.md b/docs/rules/no-shadow-restricted-names.md index 1e21a49cbfe..e165d4b5829 100644 --- a/docs/rules/no-shadow-restricted-names.md +++ b/docs/rules/no-shadow-restricted-names.md @@ -12,29 +12,25 @@ Then any code used within the same scope would not get the global `undefined`, b The following patterns are considered warnings: -```js -function NaN(){} ``` +/*eslint no-shadow-restricted-names: 2*/ -```js -!function(Infinity){}; -``` +function NaN(){} /*error Shadowing of global property "NaN".*/ -```js -var undefined; -``` +!function(Infinity){}; /*error Shadowing of global property "Infinity".*/ -```js -try {} catch(eval){} +var undefined; /*error Shadowing of global property "undefined".*/ + +try {} catch(eval){} /*error Shadowing of global property "eval".*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-shadow-restricted-names: 2*/ + var Object; -``` -```js function f(a, b){} ``` diff --git a/docs/rules/no-shadow.md b/docs/rules/no-shadow.md index 75e01d70b40..0c4d6fcf9e1 100644 --- a/docs/rules/no-shadow.md +++ b/docs/rules/no-shadow.md @@ -18,38 +18,30 @@ This rule aims to eliminate shadowed variable declarations. The following patterns are considered warnings: ```js +/*eslint no-shadow: 2*/ + var a = 3; function b() { - var a = 10; + var a = 10; /*error a is already declared in the upper scope.*/ } -``` -```js -var a = 3; var b = function () { - var a = 10; + var a = 10; /*error a is already declared in the upper scope.*/ } -``` -```js -var a = 3; -function b(a) { +function b(a) { /*error a is already declared in the upper scope.*/ a = 10; } b(a); -``` - -```js -var a = 3; if (true) { - let a = 5; + let a = 5; /*error a is already declared in the upper scope.*/ } ``` ### Options -This rule takes one option, an object, with properties `"builtinGlobals"`, `"hoist"`. +This rule takes one option, an object, with properties `"builtinGlobals"` and `"hoist"`. ```json { @@ -65,8 +57,10 @@ If this is `true`, this rule checks with built-in global variables such as `Obje When `{"builtinGlobals": true}`, the following patterns are considered warnings: ```js +/*eslint no-shadow: [2, { "builtinGlobals": true }]*/ + function foo() { - var Object = 0; // shadowed the built-in globals. + var Object = 0; /*error Object is already declared in the upper scope.*/ } ``` @@ -78,9 +72,45 @@ The option has three settings: * `functions` (by default) - reports shadowing before the outer functions are defined. * `never` - never report shadowing before the outer variables/functions are defined. -Thought with the following codes: +##### { "hoist": "all" } + +With `"hoist"` set to `"all"`, both `let a` and `let b` in the `if` statement are considered warnings. ```js +/*eslint no-shadow: [2, { "hoist": "all" }]*/ + +if (true) { + let a = 3; /*error a is already declared in the upper scope.*/ + let b = 6; /*error b is already declared in the upper scope.*/ +} + +let a = 5; +function b() {} +``` + +##### { "hoist": "functions" } (default) + +With `"hoist"` set to `"functions"`, `let b` is considered a warning. But `let a` in the `if` statement is not considered a warning, because it is before `let a` of the outer scope. + +```js +/*eslint no-shadow: [2, { "hoist": "functions" }]*/ + +if (true) { + let a = 3; + let b = 6; /*error b is already declared in the upper scope.*/ +} + +let a = 5; +function b() {} +``` + +##### { "hoist": "never" } + +With `"hoist"` set to `"never"`, neither `let a` nor `let b` in the `if` statement are considered warnings, because they are before the declarations of the outer scope. + +```js +/*eslint no-shadow: [2, { "hoist": "never" }]*/ + if (true) { let a = 3; let b = 6; @@ -90,9 +120,6 @@ let a = 5; function b() {} ``` -* `all` - Both `let a` and `let b` in the `if` statement are considered warnings. -* `functions` - `let b` is considered warnings. But `let a` in the `if` statement is not considered warnings. Because there is it before `let a` of the outer scope. -* `never` - Both `let a` and `let b` in the `if` statement are not considered warnings. Because there are those before each declaration of the outer scope. ## Further Reading diff --git a/docs/rules/no-space-before-semi.md b/docs/rules/no-space-before-semi.md index aa2031a0ad2..3327b773d5b 100644 --- a/docs/rules/no-space-before-semi.md +++ b/docs/rules/no-space-before-semi.md @@ -7,7 +7,7 @@ JavaScript allows for placing unnecessary spaces between an expression and the c Space issues can also cause code to look inconsistent and harder to read. ```js -var thing = function { +var thing = function () { var test = 12 ; } ; ``` diff --git a/docs/rules/no-spaced-func.md b/docs/rules/no-spaced-func.md index a9939d25cc9..26689774918 100644 --- a/docs/rules/no-spaced-func.md +++ b/docs/rules/no-spaced-func.md @@ -13,17 +13,19 @@ fn () The following patterns are considered warnings: ```js -fn () -``` +/*eslint no-spaced-func: 2*/ -```js -fn +fn () /*error Unexpected space between function name and paren.*/ + +fn /*error Unexpected space between function name and paren.*/ () ``` The following patterns are not warnings: ```js +/*eslint no-spaced-func: 2*/ + fn() ``` diff --git a/docs/rules/no-sparse-arrays.md b/docs/rules/no-sparse-arrays.md index f224aef75cb..8ffcdd82de1 100644 --- a/docs/rules/no-sparse-arrays.md +++ b/docs/rules/no-sparse-arrays.md @@ -23,13 +23,17 @@ This rule aims to eliminate sparse arrays that are defined by extra commas. The following patterns are considered warnings: ```js -var items = [,]; -var colors = [ "red",, "blue" ]; +/*eslint no-sparse-arrays: 2*/ + +var items = [,]; /*error Unexpected comma in middle of array.*/ +var colors = [ "red",, "blue" ]; /*error Unexpected comma in middle of array.*/ ``` The following patterns are not warnings: ```js +/*eslint no-sparse-arrays: 2*/ + var items = []; var items = new Array(23); diff --git a/docs/rules/no-sync.md b/docs/rules/no-sync.md index f0761e547f7..b2d136da6d8 100644 --- a/docs/rules/no-sync.md +++ b/docs/rules/no-sync.md @@ -9,14 +9,18 @@ This rule is aimed at preventing synchronous methods from being called in Node.j The following patterns are considered warnings: ```js -fs.existsSync(somePath); +/*eslint no-sync: 2*/ -var contents = fs.readFileSync(somePath).toString(); +fs.existsSync(somePath); /*error Unexpected sync method: 'existsSync'.*/ + +var contents = fs.readFileSync(somePath).toString(); /*error Unexpected sync method: 'readFileSync'.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-sync: 2*/ + obj.sync(); async(function() { diff --git a/docs/rules/no-ternary.md b/docs/rules/no-ternary.md index b49476fa022..972674c0037 100644 --- a/docs/rules/no-ternary.md +++ b/docs/rules/no-ternary.md @@ -13,18 +13,22 @@ The `no-ternary` rule aims to disallow the use of ternary operators. The following patterns are considered warnings: ```js -var foo = isBar ? baz : qux; +/*eslint no-ternary: 2*/ + +var foo = isBar ? baz : qux; /*error Ternary operator used.*/ -foo ? bar() : baz(); +foo ? bar() : baz(); /*error Ternary operator used.*/ function quux() { - return foo ? bar : baz; + return foo ? bar : baz; /*error Ternary operator used.*/ } ``` The following patterns are considered okay and could be used alternatively: ```js +/*eslint no-ternary: 2*/ + var foo; if (isBar) { diff --git a/docs/rules/no-this-before-super.md b/docs/rules/no-this-before-super.md index 1eaadfb54dc..d71dff3bc51 100644 --- a/docs/rules/no-this-before-super.md +++ b/docs/rules/no-this-before-super.md @@ -11,36 +11,32 @@ This rule is aimed to flag `this`/`super` keywords before `super()` callings. The following patterns are considered warnings: ```js +/*eslint no-this-before-super: 2*/ + class A extends B { constructor() { - this.a = 0; // this is before `super()`. + this.a = 0; /*error "this" is not allowed before super()*/ super(); } } -``` -```js class A extends B { constructor() { - this.foo(); // this is before `super()`. + this.foo(); /*error "this" is not allowed before super()*/ super(); } } -``` -```js class A extends B { constructor() { - super.foo(); // this is before `super()`. + super.foo(); /*error "super" is not allowed before super()*/ super(); } } -``` -```js class A extends B { constructor() { - super(this.foo()); // `super()` is not called yet. + super(this.foo()); /*error "this" is not allowed before super()*/ } } ``` @@ -48,23 +44,21 @@ class A extends B { The following patterns are not considered warnings: ```js +/*eslint no-this-before-super: 2*/ + class A { constructor() { this.a = 0; // OK, this class doesn't have an `extends` clause. } } -``` -```js class A extends B { constructor() { super(); this.a = 0; // OK, this is after `super()`. } } -``` -```js class A extends B { foo() { this.a = 0; // OK. this is not in a constructor. diff --git a/docs/rules/no-throw-literal.md b/docs/rules/no-throw-literal.md index b15546cf7b6..c0f06d4b9be 100644 --- a/docs/rules/no-throw-literal.md +++ b/docs/rules/no-throw-literal.md @@ -12,26 +12,30 @@ This rule is aimed at maintaining consistency when throwing exception by disallo The following patterns are considered warnings: ```js -throw "error"; +/*eslint no-throw-literal: 2*/ -throw 0; +throw "error"; /*error Expected an object to be thrown.*/ -throw undefined; +throw 0; /*error Expected an object to be thrown.*/ -throw null; +throw undefined; /*error Do not throw undefined.*/ + +throw null; /*error Expected an object to be thrown.*/ var err = new Error(); -throw "an " + err; +throw "an " + err; /*error Expected an object to be thrown.*/ // err is recast to a string literal var err = new Error(); -throw `${err}` +throw `${err}` /*error Expected an object to be thrown.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-throw-literal: 2*/ + throw new Error(); throw new Error("error"); @@ -51,6 +55,8 @@ try { Due to the limits of static analysis, this rule cannot guarantee that you will only throw `Error` objects. For instance, the following cases do not throw an `Error` object, but they will not be considered warnings: ```js +/*eslint no-throw-literal: 2*/ + var err = "error"; throw err; diff --git a/docs/rules/no-trailing-spaces.md b/docs/rules/no-trailing-spaces.md index fc4bf9dbf91..b6c6074e1a9 100644 --- a/docs/rules/no-trailing-spaces.md +++ b/docs/rules/no-trailing-spaces.md @@ -7,15 +7,19 @@ Sometimes in the course of editing files, you can end up with extra whitespace a The following patterns are considered warnings: ```js +/*eslint no-trailing-spaces: 2*/ + // spaces, tabs and unicode whitespaces // are not allowed at the end of lines -var foo = 0;••••• -var baz = 5;•• +var foo = 0;//••••• /*error Trailing spaces not allowed.*/ +var baz = 5;//•• /*error Trailing spaces not allowed.*/ ``` The following patterns are not warnings: ```js +/*eslint no-trailing-spaces: 2*/ + var foo = 0; var baz = 5; @@ -36,7 +40,9 @@ You can enable this option in your config like this: With this option enabled, the following patterns are not warnings: ```js +/*eslint no-trailing-spaces: [2, { "skipBlankLines": true }]*/ + var foo = 0; -•••• +//•••• var baz = 5; ``` diff --git a/docs/rules/no-undef-init.md b/docs/rules/no-undef-init.md index 24622a547d2..832829e9a64 100644 --- a/docs/rules/no-undef-init.md +++ b/docs/rules/no-undef-init.md @@ -23,13 +23,17 @@ This rule aims to eliminate variable declarations that initialize to `undefined` The following patterns are considered warnings: ```js -var foo = undefined; -let bar = undefined; +/*eslint no-undef-init: 2*/ + +var foo = undefined; /*error It's not necessary to initialize 'foo' to undefined.*/ +let bar = undefined; /*error It's not necessary to initialize 'bar' to undefined.*/ ``` The following patterns are not warnings: ```js +/*eslint no-undef-init: 2*/ + var foo; let bar; ``` diff --git a/docs/rules/no-undef.md b/docs/rules/no-undef.md index 9db81f33b72..3a474501fb9 100644 --- a/docs/rules/no-undef.md +++ b/docs/rules/no-undef.md @@ -9,14 +9,18 @@ This rule can help you locate potential ReferenceErrors resulting from misspelli The following code causes 2 warnings, as the globals `someFunction` and `b` have not been declared. ```js -var a = someFunction(); // 'someFunction' is not defined. -b = 10; // 'b' is not defined. +/*eslint no-undef: 2*/ + +var a = someFunction(); /*error "someFunction" is not defined.*/ +b = 10; /*error "b" is not defined.*/ ``` In this code, no warnings are generated, since the global variables have been properly declared in a `/*global */` block. ```js /*global someFunction b:true*/ +/*eslint no-undef: 2*/ + var a = someFunction(); b = 10; ``` @@ -25,19 +29,26 @@ By default, variables declared in `/*global */` are considered read-only. Assign ```js /*global b*/ -b = 10; // 'b' is read only. +/*eslint no-undef: 2*/ + + +b = 10; /*error "b" is read only.*/ ``` Use the `variable:true` syntax to indicate that a variable can be assigned to. ```js /*global b:true*/ +/*eslint no-undef: 2*/ + b = 10; ``` Explicitly checking an undefined identifier with `typeof` causes no warning. ```js +/*eslint no-undef: 2*/ + if (typeof UndefinedIdentifier === "undefined") { // do something ... } diff --git a/docs/rules/no-undefined.md b/docs/rules/no-undefined.md index 3b942ad3d3d..8cee5a29f34 100644 --- a/docs/rules/no-undefined.md +++ b/docs/rules/no-undefined.md @@ -39,15 +39,17 @@ This rule aims to eliminate the use of `undefined`, and as such, generates a war The following patterns are considered warnings: ```js -var foo = undefined; +/*eslint no-undefined: 2*/ -var undefined = "foo"; +var foo = undefined; /*error Unexpected use of undefined.*/ -if (foo === undefined) { +var undefined = "foo"; /*error Unexpected use of undefined.*/ + +if (foo === undefined) { /*error Unexpected use of undefined.*/ // ... } -function foo(undefined) { +function foo(undefined) { /*error Unexpected use of undefined.*/ // ... } ``` @@ -55,6 +57,8 @@ function foo(undefined) { The following patterns are not warnings: ```js +/*eslint no-undefined: 2*/ + var foo = void 0; var Undefined = "foo"; diff --git a/docs/rules/no-underscore-dangle.md b/docs/rules/no-underscore-dangle.md index b290957b5d8..71ad95c160f 100644 --- a/docs/rules/no-underscore-dangle.md +++ b/docs/rules/no-underscore-dangle.md @@ -17,14 +17,18 @@ This rule aims to eliminate the use of dangling underscores in identifiers. The following patterns are considered warnings: ```js -var foo_; -var __proto__ = {}; -foo._bar(); +/*eslint no-underscore-dangle: 2*/ + +var foo_; /*error Unexpected dangling "_" in "foo_".*/ +var __proto__ = {}; /*error Unexpected dangling "_" in "__proto__".*/ +foo._bar(); /*error Unexpected dangling "_" in "_bar".*/ ``` The following patterns are not warnings: ```js +/*eslint no-underscore-dangle: 2*/ + var _ = require('underscore'); var obj = _.contains(items, item); obj.__proto__ = {}; diff --git a/docs/rules/no-unexpected-multiline.md b/docs/rules/no-unexpected-multiline.md index f3027b54d87..2c3a6e7df79 100644 --- a/docs/rules/no-unexpected-multiline.md +++ b/docs/rules/no-unexpected-multiline.md @@ -18,16 +18,20 @@ This rule is aimed at ensuring that two unrelated consecutive lines are not acci The following patterns are considered warnings: ```js +/*eslint no-unexpected-multiline: 2*/ + var foo = bar -(1 || 2).baz(); +(1 || 2).baz(); /*error Unexpected newline between function and ( of function call.*/ var hello = 'world' -[1, 2, 3].forEach(addNumber); +[1, 2, 3].forEach(addNumber); /*error Unexpected newline between object and [ of property access.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-unexpected-multiline: 2*/ + var foo = bar; (1 || 2).baz(); diff --git a/docs/rules/no-unneeded-ternary.md b/docs/rules/no-unneeded-ternary.md index 632d6109cf5..167d0d3dafd 100644 --- a/docs/rules/no-unneeded-ternary.md +++ b/docs/rules/no-unneeded-ternary.md @@ -40,9 +40,11 @@ This rule enforces a coding style where it disallows conditional expressions tha The following patterns are considered warnings: ```js -var a = x === 2 ? true : false; +/*eslint no-unneeded-ternary: 2*/ -var a = x ? true : false; +var a = x === 2 ? true : false; /*error Unnecessary use of boolean literals in conditional expression*/ + +var a = x ? true : false; /*error Unnecessary use of boolean literals in conditional expression*/ ``` The following pattern is considered a warning when `defaultAssignment` is `false`: @@ -54,6 +56,8 @@ var a = x ? x : 1; The following patterns are not considered warnings: ```js +/*eslint no-unneeded-ternary: 2*/ + var a = x === 2 ? "Yes" : "No"; var a = x !== false; diff --git a/docs/rules/no-unreachable.md b/docs/rules/no-unreachable.md index 2a0f5dcff33..53552b5dc02 100644 --- a/docs/rules/no-unreachable.md +++ b/docs/rules/no-unreachable.md @@ -17,28 +17,32 @@ This rule is aimed at detecting unreachable code. It produces an error when a st The following are considered warnings: ```js +/*eslint no-unreachable: 2*/ + function foo() { return true; - console.log("done"); + console.log("done"); /*error Found unexpected statement after a return.*/ } function bar() { - throws new Error("Oops!"); - console.log("done"); + throw new Error("Oops!"); + console.log("done"); /*error Found unexpected statement after a throw.*/ } while(value) { break; - console.log("done"); + console.log("done"); /*error Found unexpected statement after a break.*/ } throw new Error("Oops!"); -console.log("done"); +console.log("done"); /*error Found unexpected statement after a throw.*/ ``` The following patterns are not considered warnings (due to JavaScript function and variable hoisting): ```js +/*eslint no-unreachable: 2*/ + function foo() { return bar(); function bar() { diff --git a/docs/rules/no-unused-expressions.md b/docs/rules/no-unused-expressions.md index 9f172f16dc6..528d1224cea 100644 --- a/docs/rules/no-unused-expressions.md +++ b/docs/rules/no-unused-expressions.md @@ -26,15 +26,15 @@ By default the following patterns are considered warnings: ```js /*eslint no-unused-expressions: 2*/ -0 +0 /*error Expected an assignment or function call and instead saw an expression.*/ -if(0) 0 +if(0) 0 /*error Expected an assignment or function call and instead saw an expression.*/ -{0} +{0} /*error Expected an assignment or function call and instead saw an expression.*/ -f(0), {} +f(0), {} /*error Expected an assignment or function call and instead saw an expression.*/ -a && b() +a && b() /*error Expected an assignment or function call and instead saw an expression.*/ ``` The following patterns are not considered warnings by default: diff --git a/docs/rules/no-unused-vars.md b/docs/rules/no-unused-vars.md index 7e83e2d212f..60a0fc2cfe9 100644 --- a/docs/rules/no-unused-vars.md +++ b/docs/rules/no-unused-vars.md @@ -17,22 +17,24 @@ A variable is *not* considered read if it is only ever assigned to (`var x = 5`) The following patterns are considered warnings: ```js +/*eslint no-unused-vars: 2*/ +/*global some_unsed_var */ /*error "some_unsed_var" is defined but never used*/ //It checks variables you have defined as global -/*global some_used_var */ +some_unsed_var = 42; -var x = 10; +var x; /*error "x" is defined but never used*/ -var y = 10; +var y = 10; /*error "y" is defined but never used*/ y = 5; // By default, unused arguments cause warnings. -(function(foo) { +(function(foo) { /*error "foo" is defined but never used*/ return 5; })(); // Unused recursive functions also cause warnings. -function fact(n) { +function fact(n) { /*error "fact" is defined but never used*/ if (n < 2) return 1; return n * fact(n - 1); } @@ -41,6 +43,8 @@ function fact(n) { The following patterns are not considered warnings: ```js +/*eslint no-unused-vars: 2*/ + var x = 10; alert(x); @@ -86,13 +90,31 @@ This option has three settings: * `after-used` - only the last argument must be used. This allows you, for instance, to have two named parameters to a function and as long as you use the second argument, ESLint will not warn you about the first. This is the default setting. * `none` - do not check arguments. -The following code: +##### with `{ "args": "all" }` + +```js +/*eslint no-unused-vars: [2, { "args": "all" }]*/ + +(function(foo, bar, baz) { /*error "foo" is defined but never used*/ /*error "baz" is defined but never used*/ + return bar; +})(); +``` -* will throw `"baz" is defined but never used` when `args`: `after-used` -* will throw `"foo" is defined but never used` and `"baz" is defined but never used` when `args`: `all` -* will throw nothing when `args`: `none` +##### with `{ "args": "after-used" }` ```js +/*eslint no-unused-vars: [2, { "args": "after-used" }]*/ + +(function(foo, bar, baz) { /*error "baz" is defined but never used*/ + return bar; +})(); +``` + +##### with `{ "args": "none" }` + +```js +/*eslint no-unused-vars: [2, { "args": "none" }]*/ + (function(foo, bar, baz) { return bar; })(); @@ -118,6 +140,8 @@ The following code: With this configuration, this rule will not warn about the following code: ```js + /*eslint no-unused-vars: [2, {"args": "after-used", "argsIgnorePattern": "^_"}]*/ + function foo(x, _y) { return x + 1; } @@ -138,6 +162,8 @@ The following code: With this configuration, this rule will not warn about the following code: ```js + /*eslint no-unused-vars: [2, {"args": "after-used", "varsIgnorePattern": "[iI]gnored"}]*/ + var [ firstItemIgnored, secondItem ] = items; console.log(secondItem); ``` diff --git a/docs/rules/no-use-before-define.md b/docs/rules/no-use-before-define.md index d4076182b09..78c6c191919 100644 --- a/docs/rules/no-use-before-define.md +++ b/docs/rules/no-use-before-define.md @@ -11,20 +11,22 @@ This rule will warn when it encounters a reference to an identifier that has not The following patterns are considered warnings: ```js -alert(a); +/*eslint no-use-before-define: 2*/ + +alert(a); /*error a was used before it was defined*/ var a = 10; -f(); +f(); /*error f was used before it was defined*/ function f() {} function g() { - return b; + return b; /*error b was used before it was defined*/ } var b = 1; // With blockBindings: true { - alert(c); + alert(c); /*error c was used before it was defined*/ let c = 1; } ``` @@ -32,6 +34,8 @@ var b = 1; The following patterns are not considered warnings: ```js +/*eslint no-use-before-define: 2*/ + var a; a = 10; alert(a); @@ -51,13 +55,13 @@ function g() { } ``` - The rule accepts an additional option that can take the value `"nofunc"`. If this option is active, function declarations are exempted from the rule, so it is allowed to use a function name *before* its declaration. The following patterns are not considered warnings when `"nofunc"` is specified: ```js -f(); +/*eslint no-use-before-define: [2, "nofunc"]*/ +f(); function f() {} ``` diff --git a/docs/rules/no-useless-call.md b/docs/rules/no-useless-call.md index 77ce7e21ee6..796f4423cc7 100644 --- a/docs/rules/no-useless-call.md +++ b/docs/rules/no-useless-call.md @@ -10,22 +10,24 @@ This rule is aimed to flag usage of `Function.prototype.call()` and `Function.pr The following patterns are considered warnings: ```js -// Those are same as `foo(1, 2, 3);` -foo.call(undefined, 1, 2, 3); -foo.apply(undefined, [1, 2, 3]); -foo.call(null, 1, 2, 3); -foo.apply(null, [1, 2, 3]); -``` +/*eslint no-useless-call: 2*/ -```js -// Those are same as `obj.foo(1, 2, 3);` -obj.foo.call(obj, 1, 2, 3); -obj.foo.apply(obj, [1, 2, 3]); +// These are same as `foo(1, 2, 3);` +foo.call(undefined, 1, 2, 3); /*error unnecessary ".call()".*/ +foo.apply(undefined, [1, 2, 3]); /*error unnecessary ".apply()".*/ +foo.call(null, 1, 2, 3); /*error unnecessary ".call()".*/ +foo.apply(null, [1, 2, 3]); /*error unnecessary ".apply()".*/ + +// These are same as `obj.foo(1, 2, 3);` +obj.foo.call(obj, 1, 2, 3); /*error unnecessary ".call()".*/ +obj.foo.apply(obj, [1, 2, 3]); /*error unnecessary ".apply()".*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-useless-call: 2*/ + // The `this` binding is different. foo.call(obj, 1, 2, 3); foo.apply(obj, [1, 2, 3]); @@ -33,9 +35,7 @@ obj.foo.call(null, 1, 2, 3); obj.foo.apply(null, [1, 2, 3]); obj.foo.call(otherObj, 1, 2, 3); obj.foo.apply(otherObj, [1, 2, 3]); -``` -```js // The argument list is variadic. foo.apply(undefined, args); foo.apply(null, args); @@ -48,8 +48,10 @@ This rule compares code statically to check whether or not `thisArg` is changed. So if the code about `thisArg` is a dynamic expression, this rule cannot judge correctly. ```js +/*eslint no-useless-call: 2*/ + // This is warned. -a[i++].foo.call(a[i++], 1, 2, 3); +a[i++].foo.call(a[i++], 1, 2, 3); /*error unnecessary ".call()".*/ // This is not warned. a[++i].foo.call(a[i], 1, 2, 3); diff --git a/docs/rules/no-useless-concat.md b/docs/rules/no-useless-concat.md index 6603a055ce5..3f74693c778 100644 --- a/docs/rules/no-useless-concat.md +++ b/docs/rules/no-useless-concat.md @@ -9,17 +9,21 @@ This rule aims to flag the concenation of 2 literals when they could be combined The following patterns are considered warnings: ```js +/*eslint no-useless-concat: 2*/ + // these are the same as "10" -var a = `some` + `string`; -var a = '1' + '0'; -var a = '1' + `0`; -var a = `1` + '0'; -var a = `1` + `0`; +var a = `some` + `string`; /*error Unexpected string concatenation of literals.*/ +var a = '1' + '0'; /*error Unexpected string concatenation of literals.*/ +var a = '1' + `0`; /*error Unexpected string concatenation of literals.*/ +var a = `1` + '0'; /*error Unexpected string concatenation of literals.*/ +var a = `1` + `0`; /*error Unexpected string concatenation of literals.*/ ``` The following patterns are not warnings: ```js +/*eslint no-useless-concat: 2*/ + // when a non string is included var c = a + b; var c = '1' + a; diff --git a/docs/rules/no-var.md b/docs/rules/no-var.md index 841fd3b9add..2b38eaa5784 100644 --- a/docs/rules/no-var.md +++ b/docs/rules/no-var.md @@ -6,15 +6,15 @@ such as: ```js var count = people.length; -var enoughFood = count > sandwhiches.length; +var enoughFood = count > sandwiches.length; if (enoughFood) { - var count = sandwhiches.length; // accidently overriding the count variable - console.log("We have " + count + " sandwhiches for everyone. Plenty for all!"); + var count = sandwiches.length; // accidently overriding the count variable + console.log("We have " + count + " sandwiches for everyone. Plenty for all!"); } // our count variable is no longer accurate -console.log("We have " + count + " people and " + sandwhiches.length + " sandwhiches!"); +console.log("We have " + count + " people and " + sandwiches.length + " sandwiches!"); ``` ## Rule Details @@ -24,13 +24,17 @@ This rule is aimed at discouraging the use of `var` and encouraging the use of ` The following patterns are considered warnings: ```js -var x = "y"; -var CONFIG = {}; +/*eslint no-var: 2*/ + +var x = "y"; /*error Unexpected var, use let or const instead.*/ +var CONFIG = {}; /*error Unexpected var, use let or const instead.*/ ``` The following patterns are not considered warnings: ```js +/*eslint no-var: 2*/ + let x = "y"; const CONFIG = {}; ``` diff --git a/docs/rules/no-void.md b/docs/rules/no-void.md index 427e60cfca9..8b4bbdd81e7 100644 --- a/docs/rules/no-void.md +++ b/docs/rules/no-void.md @@ -14,7 +14,7 @@ The common case of using `void` operator is to get a "pure" `undefined` value as (function(){ undefined = 1; return undefined; -))(); +})(); // will throw TypeError is ES5+ (function(){ @@ -36,6 +36,9 @@ When used with IIFE (immediately-invoked function expression) `void` can be used var foo = 1; void function(){ foo = 1; }() // will assign foo a value of 1 +function(){ foo = 1; }() // same as above +``` + +``` function(){ foo = 1; }() // will throw SyntaxError ``` @@ -48,11 +51,11 @@ This rule aims to eliminate use of void operator. The following patterns are considered warnings: ```js -void foo -``` +/*eslint no-void: 2*/ -```js -var foo = void bar(); +void foo /*error Expected 'undefined' and instead saw 'void'.*/ + +var foo = void bar(); /*error Expected 'undefined' and instead saw 'void'.*/ ``` ## When Not To Use It diff --git a/docs/rules/no-warning-comments.md b/docs/rules/no-warning-comments.md index 899de440f93..6179c38304e 100644 --- a/docs/rules/no-warning-comments.md +++ b/docs/rules/no-warning-comments.md @@ -6,12 +6,10 @@ Often code is marked during development process for later work on it or with add This rule can be used to help finding these `warning-comments`. It can be configured with an array of terms you don't want to exist in your code. It will raise a warning when one or more of the configured `warning-comments` are present in the checked files. -The default configuration has this rule disabled and looks like this: +The default configuration looks like this: -```js -... +```json "no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }] -... ``` This preconfigures @@ -19,33 +17,34 @@ This preconfigures * the rule is disabled because it is set to `0`. Changing this to `1` for warn or `2` for error mode activates it (this works exactly the same as everywhere else in `ESLint`). * the `terms` array is set to `todo`, `fixme` and `xxx` as `warning-comments`. `terms` has to be an array. It can hold any terms you might want to warn about in your comments - they do not have to be single words. E.g. `really bad idea` is as valid as `attention`. * the `terms` are case-insensitive and are matched as whole words. E.g. `fix` would not match `fixing`. -* the `location`-option set to `start` configures the rule to check only the start of comments. E.g. `// TODO` would be matched, `// This is a TODO` not. You can change this to `anywhere` to check your complete comments. +* the `location`-option set to `start` configures the rule to check only the start of comments. E.g. `// TODO` would be matched, `// This is a TODO` would not. You can change this to `anywhere` to check your complete comments. As already seen above, the configuration is quite simple. Example that enables the rule and configures it to check the complete comment, not only the start: -```js -... +```json "no-warning-comments": [2, { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }] -... ``` The following patterns are considered warnings with the example configuration from above: -```js -// TODO: this -// todo: this too -// Even this: TODO -/* +``` +/*eslint no-warning-comments: [2, { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }]*/ + +// TODO: this /*error Unexpected todo comment.*/ +// todo: this too /*error Unexpected todo comment.*/ +// Even this: TODO /*error Unexpected todo comment.*/ +/* /*error Unexpected todo comment.*/ /*error Unexpected fixme comment.*/ /*Unexpected any other term comment.*/ /* * The same goes for this TODO comment * Or a fixme * as well as any other term */ -... ``` These patterns would not be considered warnings with the same example configuration: -```js +``` +/*eslint no-warning-comments: [2, { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }]*/ + // This is to do // even not any other term // any other terminal @@ -54,15 +53,13 @@ These patterns would not be considered warnings with the same example configurat * with any other interesting term * or fix me this */ -... + ``` ## Rule Options -```js -... +```json "no-warning-comments": [, { "terms": , "location": }] -... ``` * `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to `0`. @@ -80,40 +77,30 @@ These patterns would not be considered warnings with the same example configurat 1. Rule configured to warn on matches and search the complete comment, not only the start of it. Note that the `term` configuration is omitted to use the defaults terms. - ```js - ... + ```json "no-warning-comments": [1, { "location": "anywhere" }] - ... ``` 2. Rule configured to warn on matches of the term `bad string` at the start of comments. Note that the `location` configuration is omitted to use the default location. - ```js - ... + ```json "no-warning-comments": [1, { "terms": ["bad string"] }] - ... ``` 3. Rule configured to warn with error on matches of the default terms at the start of comments. Note that the complete configuration object (that normally holds `terms` and/or `location`) can be omitted for simplicity. - ```js - ... + ```json "no-warning-comments": [2] - ... ``` 4. Rule configured to warn on matches of the default terms at the start of comments. Note that the complete configuration object (as already seen in the example above) and even the square brackets can be omitted for simplicity. - ```js - ... + ```json "no-warning-comments": 1 - ... ``` 5. Rule configured to warn on matches of the specified terms at any location in the comments. Note that you can use as many terms as you want. - ```js - ... + ```json "no-warning-comments": [1, { "terms": ["really any", "term", "can be matched"], "location": "anywhere" }] - ... ``` diff --git a/docs/rules/no-with.md b/docs/rules/no-with.md index 52a5cfd312a..6420717529d 100644 --- a/docs/rules/no-with.md +++ b/docs/rules/no-with.md @@ -8,8 +8,9 @@ This rule is aimed at eliminating `with` statements. The following patterns are considered warnings: -```js -with (foo) { +``` +/*eslint no-with: 2*/ +with (foo) { /*error Unexpected use of 'with' statement.*/ // ... } ``` diff --git a/docs/rules/no-wrap-func.md b/docs/rules/no-wrap-func.md index 5d41ae16e97..6ad17a5f6d3 100644 --- a/docs/rules/no-wrap-func.md +++ b/docs/rules/no-wrap-func.md @@ -22,15 +22,15 @@ This rule will raise a warning when it encounters a function expression wrapped The following patterns are considered warnings: ```js -var a = (function() {...}); +var a = (function() {/*...*/}); ``` The following patterns are considered okay and do not cause warnings: ```js -var a = function() {...}; +var a = function() {/*...*/}; -(function() {...})(); +(function() {/*...*/})(); ``` ## Further Reading diff --git a/docs/rules/object-curly-spacing.md b/docs/rules/object-curly-spacing.md index 7f8d9bccbc9..580ab0d5344 100644 --- a/docs/rules/object-curly-spacing.md +++ b/docs/rules/object-curly-spacing.md @@ -44,18 +44,21 @@ Depending on your coding conventions, you can choose either option by specifying When `"never"` is set, the following patterns are considered warnings: ```js -var obj = { 'foo': 'bar' }; -var obj = {'foo': 'bar' }; -var obj = { baz: {'foo': 'qux'}, 'bar'}; -var obj = {baz: { 'foo': 'qux' }, 'bar'}; -var {x } = y; -import { foo } from 'bar'; +/*eslint object-curly-spacing: [2, "never"]*/ + +var obj = { 'foo': 'bar' }; /*error There should be no space after '{'*/ /*error There should be no space before '}'*/ +var obj = {'foo': 'bar' }; /*error There should be no space before '}'*/ +var obj = { baz: {'foo': 'qux'}, bar}; /*error There should be no space after '{'*/ +var obj = {baz: { 'foo': 'qux'}, bar}; /*error There should be no space after '{'*/ +var {x } = y; /*error There should be no space before '}'*/ +import { foo } from 'bar'; /*error There should be no space after '{'*/ /*error There should be no space before '}'*/ ``` The following patterns are not warnings: ```js -// When options are [2, "never"] +/*eslint object-curly-spacing: [2, "never"]*/ + var obj = {'foo': 'bar'}; var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'}; var obj = { @@ -75,21 +78,25 @@ import {foo} from 'bar'; When `"always"` is used, the following patterns are considered warnings: ```js -var obj = {'foo': 'bar'}; -var obj = {'foo': 'bar' }; -var obj = { baz: {'foo': 'qux'}, 'bar'}; -var obj = {baz: { 'foo': 'qux' }, 'bar'}; -var obj = {'foo': 'bar' +/*eslint object-curly-spacing: [2, "always"]*/ + +var obj = {'foo': 'bar'}; /*error A space is required after '{'*/ /*error A space is required before '}'*/ +var obj = {'foo': 'bar' }; /*error A space is required after '{'*/ +var obj = { baz: {'foo': 'qux'}, bar}; /*error A space is required after '{'*/ /*error A space is required before '}'*/ +var obj = {baz: { 'foo': 'qux' }, bar}; /*error A space is required after '{'*/ /*error A space is required before '}'*/ +var obj = {'foo': 'bar' /*error A space is required after '{'*/ }; var obj = { - 'foo':'bar'}; -var {x} = y; -import {foo } from 'bar'; + 'foo':'bar'}; /*error A space is required before '}'*/ +var {x} = y; /*error A space is required after '{'*/ /*error A space is required before '}'*/ +import {foo } from 'bar'; /*error A space is required after '{'*/ ``` The following patterns are not warnings: ```js +/*eslint object-curly-spacing: [2, "always"]*/ + var obj = {}; var obj = { 'foo': 'bar' }; var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' }; diff --git a/docs/rules/object-shorthand.md b/docs/rules/object-shorthand.md index e3e9b69d371..47f06b6b5a4 100644 --- a/docs/rules/object-shorthand.md +++ b/docs/rules/object-shorthand.md @@ -43,16 +43,20 @@ Each of the following properties would warn: ```js +/*eslint object-shorthand: 2*/ + var foo = { - x: function() {}, - y: function *() {}, - z: z + x: function() {}, /*error Expected method shorthand.*/ + y: function *() {}, /*error Expected method shorthand.*/ + z: z /*error Expected property shorthand.*/ }; ``` In that case the expected syntax would have been: ```js +/*eslint object-shorthand: 2*/ + var foo = { x() {}, *y() {}, @@ -64,6 +68,8 @@ This rule does not flag arrow functions inside of object literals. The following will *not* warn: ```js +/*eslint object-shorthand: 2*/ + var foo = { x: (y) => y }; diff --git a/docs/rules/one-var.md b/docs/rules/one-var.md index 5cdf9f15a90..b65b5fa1917 100644 --- a/docs/rules/one-var.md +++ b/docs/rules/one-var.md @@ -47,51 +47,61 @@ and the values are either `"always"` or `"never"`. This allows you to set behavi You can configure the rule as follows: -```javascript -{ - // (default) Exactly one variable declaration per type per function (var) or block (let or const) - "one-var": [2, "always"], - - // Exactly one declarator per declaration per function (var) or block (let or const) - "one-var": [2, "never"], - - // Configure each declaration type individually. Defaults to "always" if key not present. - "one-var": [2, { - "var": "always", // Exactly one var declaration per function - "let": "always", // Exactly one let declaration per block - "const": "never" // Exactly one declarator per const declaration per block - }] - - // Configure uninitialized and initialized seperately. Defaults to "always" if key not present. - "one-var": [2, { - "uninitialized": "always", // Exactly one declaration for uninitialized variables per function (var) or block (let or const) - "initialized": "never" // Exactly one declarator per initialized variable declaration per function (var) or block (let or const) - }] -} +(default) Exactly one variable declaration per type per function (var) or block (let or const) + +```json +"one-var": [2, "always"] +``` + +Exactly one declarator per declaration per function (var) or block (let or const) + +```json +"one-var": [2, "never"] +``` + +Configure each declaration type individually. Defaults to "always" if key not present. + +```json +"one-var": [2, { + "var": "always", // Exactly one var declaration per function + "let": "always", // Exactly one let declaration per block + "const": "never" // Exactly one declarator per const declaration per block +}] +``` + +Configure uninitialized and initialized seperately. Defaults to "always" if key not present. + +```json +"one-var": [2, { + "uninitialized": "always", // Exactly one declaration for uninitialized variables per function (var) or block (let or const) + "initialized": "never" // Exactly one declarator per initialized variable declaration per function (var) or block (let or const) +}] ``` When configured with `"always"` as the first option (the default), the following patterns are considered warnings: ```js +/*eslint one-var: [2, "always"]*/ + function foo() { var bar; - var baz; + var baz; /*error Combine this with the previous 'var' statement.*/ let qux; - let norf; + let norf; /*error Combine this with the previous 'let' statement.*/ } function foo(){ const bar = false; - const baz = true; + const baz = true; /*error Combine this with the previous 'const' statement.*/ let qux; - let norf; + let norf; /*error Combine this with the previous 'let' statement.*/ } function foo() { var bar; if (baz) { - var qux = true; + var qux = true; /*error Combine this with the previous 'var' statement.*/ } } ``` @@ -99,6 +109,8 @@ function foo() { The following patterns are not considered warnings: ```js +/*eslint one-var: [2, "always"]*/ + function foo() { var bar, baz; @@ -134,15 +146,17 @@ function foo(){ When configured with `"never"` as the first option, the following patterns are considered warnings: ```js +/*eslint one-var: [2, "never"]*/ + function foo() { - var bar, + var bar, /*error Split 'var' declarations into multiple statements.*/ baz; - const bar = true, + const bar = true, /*error Split 'const' declarations into multiple statements.*/ baz = false; } function foo() { - var bar, + var bar, /*error Split 'var' declarations into multiple statements.*/ qux; if (baz) { @@ -151,7 +165,7 @@ function foo() { } function foo(){ - let bar = true, + let bar = true, /*error Split 'let' declarations into multiple statements.*/ baz = false; } ``` @@ -159,6 +173,8 @@ function foo(){ The following patterns are not considered warnings: ```js +/*eslint one-var: [2, "never"]*/ + function foo() { var bar; var baz; @@ -186,6 +202,8 @@ When configured with an object as the first option, you can individually control The following patterns are not considered warnings when the first option is `{ var: "always", let: "never", const: "never" }` ```js +/*eslint one-var: [2, { var: "always", let: "never", const: "never" }]*/ + function foo() { var bar, baz; @@ -194,8 +212,8 @@ function foo() { } function foo() { - const bar; - const baz; + const bar = 1; + const baz = 2; let qux; let norf; } @@ -204,6 +222,8 @@ function foo() { The following patterns are not considered warnings when the first option is `{ uninitialized: "always", initialized: "never" }` ```js +/*eslint one-var: [2, { uninitialized: "always", initialized: "never" }]*/ + function foo() { var a, b, c; var foo = true; @@ -214,6 +234,8 @@ function foo() { If you are configuring the rule with an object, by default, if you didn't specify declaration type it will not be checked. So the following patten is not considered warning when options are set to: `{ var: "always", let: "always" }` ```js +/*eslint one-var: [2, { var: "always", let: "always" }]*/ + function foo() { var a, b; const foo = true; diff --git a/docs/rules/operator-assignment.md b/docs/rules/operator-assignment.md index 70cb994ac00..d27870c0fac 100644 --- a/docs/rules/operator-assignment.md +++ b/docs/rules/operator-assignment.md @@ -31,6 +31,8 @@ This mode enforces use of operator assignment shorthand where possible. The following are examples of valid patterns: ```js +/*eslint operator-assignment: [2, "always"]*/ + x = y; x += y; x = y * z; @@ -43,10 +45,12 @@ x = y + x; // `+` is not always commutative (e.g. x = "abc") The following patterns are considered warnings and should be replaced by their shorthand equivalents: ```js -x = x + y; -x = y * x; -x[0] = x[0] / y; -x.y = x.y << z; +/*eslint operator-assignment: [2, "always"]*/ + +x = x + y; /*error Assignment can be replaced with operator assignment.*/ +x = y * x; /*error Assignment can be replaced with operator assignment.*/ +x[0] = x[0] / y; /*error Assignment can be replaced with operator assignment.*/ +x.y = x.y << z; /*error Assignment can be replaced with operator assignment.*/ ``` ### never @@ -58,6 +62,8 @@ This mode warns on any use of operator assignment shorthand. The following are examples of valid patterns: ```js +/*eslint operator-assignment: [2, "never"]*/ + x = x + y; x.y = x.y / a.b; ``` @@ -65,8 +71,10 @@ x.y = x.y / a.b; The following patterns are considered warnings and should be written out fully without the shorthand assignments: ```js -x *= y; -x ^= (y + z) / foo(); +/*eslint operator-assignment: [2, "never"]*/ + +x *= y; /*error Unexpected operator assignment shorthand.*/ +x ^= (y + z) / foo(); /*error Unexpected operator assignment shorthand.*/ ``` ## When Not To Use It diff --git a/docs/rules/operator-linebreak.md b/docs/rules/operator-linebreak.md index 593a8ab6d76..79ad5afbf0d 100644 --- a/docs/rules/operator-linebreak.md +++ b/docs/rules/operator-linebreak.md @@ -39,38 +39,37 @@ This is the default setting for this rule. This option requires the line break t While using this setting, the following patterns are considered warnings: ```js +/*eslint operator-linebreak: [2, "after"]*/ foo = 1 -+ ++ /*error Bad line breaking before and after '+'.*/ 2; foo = 1 - + 2; - + + 2; /*error '+' should be placed at the end of the line.*/ foo - = 5; + = 5; /*error '=' should be placed at the end of the line.*/ if (someCondition - || otherCondition) { + || otherCondition) { /*error '||' should be placed at the end of the line.*/ } answer = everything - ? 42 - : foo; - + ? 42 /*error '?' should be placed at the end of the line.*/ + : foo; /*error ':' should be placed at the end of the line.*/ ``` The following patterns are not warnings: ```js +/*eslint operator-linebreak: [2, "after"]*/ foo = 1 + 2; foo = 1 + 2; - foo = 5; @@ -81,7 +80,6 @@ if (someCondition || answer = everything ? 42 : foo; - ``` #### `"before"` @@ -91,27 +89,27 @@ This option requires the line break to be placed before the operator. While using this setting, the following patterns are considered warnings: ```js +/*eslint operator-linebreak: [2, "before"]*/ -foo = 1 + +foo = 1 + /*error '+' should be placed at the beginning of the line.*/ 2; - -foo = +foo = /*error '=' should be placed at the beginning of the line.*/ 5; -if (someCondition || +if (someCondition || /*error '||' should be placed at the beginning of the line.*/ otherCondition) { } -answer = everything ? - 42 : +answer = everything ? /*error '?' should be placed at the beginning of the line.*/ + 42 : /*error ':' should be placed at the beginning of the line.*/ foo; - ``` The following patterns are not warnings: ```js +/*eslint operator-linebreak: [2, "before"]*/ foo = 1 + 2; @@ -128,7 +126,6 @@ if (someCondition answer = everything ? 42 : foo; - ``` #### `"none"` @@ -138,34 +135,35 @@ This option disallows line breaks on either side of the operator. While using this setting, the following patterns are considered warnings: ```js +/*eslint operator-linebreak: [2, "none"]*/ -foo = 1 + +foo = 1 + /*error There should be no line break before or after '+'*/ 2; foo = 1 - + 2; + + 2; /*error There should be no line break before or after '+'*/ -if (someCondition || +if (someCondition || /*error There should be no line break before or after '||'*/ otherCondition) { } if (someCondition - || otherCondition) { + || otherCondition) { /*error There should be no line break before or after '||'*/ } answer = everything - ? 42 - : foo; + ? 42 /*error There should be no line break before or after '?'*/ + : foo; /*error There should be no line break before or after ':'*/ -answer = everything ? - 42 : +answer = everything ? /*error There should be no line break before or after '?'*/ + 42 : /*error There should be no line break before or after ':'*/ foo; - ``` The following patterns are not warnings: ```js +/*eslint operator-linebreak: [2, "none"]*/ foo = 1 + 2; @@ -175,7 +173,6 @@ if (someCondition || otherCondition) { } answer = everything ? 42 : foo; - ``` #### Fine-grained control diff --git a/docs/rules/padded-blocks.md b/docs/rules/padded-blocks.md index aa064671c10..94d9076f2b7 100644 --- a/docs/rules/padded-blocks.md +++ b/docs/rules/padded-blocks.md @@ -24,28 +24,30 @@ then all blocks should never start **or** end with a blank line. The default is The following patterns are considered warnings when set to `"always"`: ```js -if (a) { +/*eslint padded-blocks: [2, "always"]*/ + +if (a) { /*error Block must be padded by blank lines.*/ b(); -} +} /*error Block must be padded by blank lines.*/ -if (a) { b(); } +if (a) { b(); } /*error Block must be padded by blank lines.*/ if (a) -{ +{ /*error Block must be padded by blank lines.*/ b(); -} +} /*error Block must be padded by blank lines.*/ if (a) { b(); -} +} /*error Block must be padded by blank lines.*/ -if (a) { +if (a) { /*error Block must be padded by blank lines.*/ b(); } -if (a) { +if (a) { /*error Block must be padded by blank lines.*/ // comment b(); @@ -55,6 +57,8 @@ if (a) { The following patterns are not considered warnings when set to `"always"`: ```js +/*eslint padded-blocks: [2, "always"]*/ + if (a) { b(); @@ -79,20 +83,22 @@ if (a) { The following patterns are considered warnings when set to `"never"`: ```js -if (a) { +/*eslint padded-blocks: [2, "never"]*/ + +if (a) { /*error Block must not be padded by blank lines.*/ b(); -} +} /*error Block must not be padded by blank lines.*/ if (a) -{ +{ /*error Block must not be padded by blank lines.*/ b(); -} +} /*error Block must not be padded by blank lines.*/ -if (a) { +if (a) { /*error Block must not be padded by blank lines.*/ b(); } @@ -100,12 +106,14 @@ if (a) { if (a) { b(); -} +} /*error Block must not be padded by blank lines.*/ ``` The following patterns are not considered warnings when set to `"never"`: ```js +/*eslint padded-blocks: [2, "never"]*/ + if (a) { b(); } diff --git a/docs/rules/prefer-arrow-callback.md b/docs/rules/prefer-arrow-callback.md index a6773ad5362..fd5a759216e 100644 --- a/docs/rules/prefer-arrow-callback.md +++ b/docs/rules/prefer-arrow-callback.md @@ -12,13 +12,17 @@ This rule is aimed to flag usage of function expressions in an argument list. The following patterns are considered warnings: ```js -foo(function(a) { return a; }); -foo(function() { return this.a; }.bind(this)); +/*eslint prefer-arrow-callback: 2*/ + +foo(function(a) { return a; }); /*error Unexpected function expression.*/ +foo(function() { return this.a; }.bind(this)); /*error Unexpected function expression.*/ ``` The following patterns are not considered warnings: ```js +/*eslint prefer-arrow-callback: 2*/ + foo(a => a); foo(function*() { yield; }); diff --git a/docs/rules/prefer-const.md b/docs/rules/prefer-const.md index ed3c9814d9b..9c64d85a8f7 100644 --- a/docs/rules/prefer-const.md +++ b/docs/rules/prefer-const.md @@ -11,18 +11,18 @@ This rule is aimed at flagging variables that are declared using `let` keyword, The following patterns are considered warnings: ```js -let a = 3; +/*eslint prefer-const: 2*/ + +let a = 3; /*error `a` is never modified, use `const` instead.*/ console.log(a); -``` -```js -for (let i in [1,2,3]) { // `i` is re-defined (not modified) on each loop step. +// `i` is re-defined (not modified) on each loop step. +for (let i in [1,2,3]) { /*error `i` is never modified, use `const` instead.*/ console.log(i); } -``` -```js -for (let a of [1,2,3]) { // `a` is re-defined (not modified) on each loop step. +// `a` is re-defined (not modified) on each loop step. +for (let a of [1,2,3]) { /*error `a` is never modified, use `const` instead.*/ console.log(a); } ``` @@ -30,19 +30,19 @@ for (let a of [1,2,3]) { // `a` is re-defined (not modified) on each loop step. The following patterns are not considered warnings: ```js +/*eslint prefer-const: 2*/ + let a; // there is no initialization. console.log(a); -``` -```js -for (let i = 0, end = 10; i < end; ++i) { // `end` is never modified, but we cannot separate the declarations without modifying the scope. +// `end` is never modified, but we cannot separate the declarations without modifying the scope. +for (let i = 0, end = 10; i < end; ++i) { console.log(a); } -``` -```js -var a = 3; // suggest to use `no-var` rule. -console.log(a); +// suggest to use `no-var` rule. +var b = 3; +console.log(b); ``` ## When Not to Use It diff --git a/docs/rules/prefer-reflect.md b/docs/rules/prefer-reflect.md index bdd27d088a8..bc6b5e90235 100644 --- a/docs/rules/prefer-reflect.md +++ b/docs/rules/prefer-reflect.md @@ -13,29 +13,45 @@ The prefer-reflect rule will flag usage of any older method, suggesting to inste ## Rule Details +## Options + +### Exceptions + +``` +"prefer-reflect": [, { exceptions: [<...exceptions>] }] +``` + +The `exceptions` option allows you to pass an array of methods names you'd like to continue to use in the old style. + +For example if you wish to use all Reflect methods, except for `Function.prototype.apply` then your config would look like `prefer-reflect: [2, { exceptions: ["apply"] }]`. + +If you want to use Reflect methods, but keep using the `delete` keyword, then your config would look like `prefer-reflect: [2, { exceptions: ["delete"] }]`. + +These can be combined as much as you like. To make all methods exceptions (thereby rendering this rule useless), use `prefer-reflect: [2, { exceptions: ["apply", "call", "defineProperty", "getOwnPropertyDescriptor", "getPrototypeOf", "setPrototypeOf", "isExtensible", "getOwnPropertyNames", "preventExtensions", "delete"] }]` + ### Reflect.apply (Function.prototype.apply/Function.prototype.call) The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -foo.apply(undefined, args); -foo.apply(null, args); -obj.foo.apply(obj, args); -obj.foo.apply(other, args); +/*eslint prefer-reflect: 2*/ -foo.call(undefined, arg); -foo.call(null, arg); -obj.foo.call(obj, arg); -obj.foo.call(other, arg); +foo.apply(undefined, args); /*error Avoid using Function.prototype.apply, instead use Reflect.apply*/ +foo.apply(null, args); /*error Avoid using Function.prototype.apply, instead use Reflect.apply*/ +obj.foo.apply(obj, args); /*error Avoid using Function.prototype.apply, instead use Reflect.apply*/ +obj.foo.apply(other, args); /*error Avoid using Function.prototype.apply, instead use Reflect.apply*/ + +foo.call(undefined, arg); /*error Avoid using Function.prototype.call, instead use Reflect.apply*/ +foo.call(null, arg); /*error Avoid using Function.prototype.call, instead use Reflect.apply*/ +obj.foo.call(obj, arg); /*error Avoid using Function.prototype.call, instead use Reflect.apply*/ +obj.foo.call(other, arg); /*error Avoid using Function.prototype.call, instead use Reflect.apply*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.apply(undefined, args); Reflect.apply(null, args); Reflect.apply(obj.foo, obj, args); @@ -46,9 +62,9 @@ Reflect.apply(obj.foo, obj, [arg]); Reflect.apply(obj.foo, other, [arg]); ``` -__config:__ `prefer-reflect: [2, { exceptions: ["apply"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["apply"] }]*/ + foo.apply(undefined, args); foo.apply(null, args); obj.foo.apply(obj, args); @@ -59,9 +75,9 @@ Reflect.apply(obj.foo, obj, args); Reflect.apply(obj.foo, other, args); ``` -__config:__ `prefer-reflect: [2, { exceptions: ["call"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["call"] }]*/ + foo.call(undefined, arg); foo.call(null, arg); obj.foo.call(obj, arg); @@ -76,23 +92,23 @@ Reflect.apply(obj.foo, other, [arg]); The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.defineProperty({}, 'foo', {value: 1}) +/*eslint prefer-reflect: 2*/ + +Object.defineProperty({}, 'foo', {value: 1}) /*error Avoid using Object.defineProperty, instead use Reflect.defineProperty*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.defineProperty({}, 'foo', {value: 1}) ``` -__config:__ `prefer-reflect: [2, { exceptions: ["defineProperty"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["defineProperty"] }]*/ + Object.defineProperty({}, 'foo', {value: 1}) Reflect.defineProperty({}, 'foo', {value: 1}) ``` @@ -101,23 +117,25 @@ Reflect.defineProperty({}, 'foo', {value: 1}) The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.getOwnPropertyDescriptor({}, 'foo') +/*eslint prefer-reflect: 2*/ + +Object.getOwnPropertyDescriptor({}, 'foo') /*error Avoid using Object.getOwnPropertyDescriptor, instead use Reflect.getOwnPropertyDescriptor*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.getOwnPropertyDescriptor({}, 'foo') ``` __config:__ `prefer-reflect: [2, { exceptions: ["getOwnPropertyDescriptor"] }]` ```js +/*eslint prefer-reflect: [2, { exceptions: ["getOwnPropertyDescriptor"] }]*/ + Object.getOwnPropertyDescriptor({}, 'foo') Reflect.getOwnPropertyDescriptor({}, 'foo') ``` @@ -126,23 +144,23 @@ Reflect.getOwnPropertyDescriptor({}, 'foo') The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.getPrototypeOf({}, 'foo') +/*eslint prefer-reflect: 2*/ + +Object.getPrototypeOf({}, 'foo') /*error Avoid using Object.getPrototypeOf, instead use Reflect.getPrototypeOf*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.getPrototypeOf({}, 'foo') ``` -__config:__ `prefer-reflect: [2, { exceptions: ["getPrototypeOf"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["getPrototypeOf"] }]*/ + Object.getPrototypeOf({}, 'foo') Reflect.getPrototypeOf({}, 'foo') ``` @@ -151,23 +169,25 @@ Reflect.getPrototypeOf({}, 'foo') The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.setPrototypeOf({}, Object.prototype) +/*eslint prefer-reflect: 2*/ + +Object.setPrototypeOf({}, Object.prototype) /*error Avoid using Object.setPrototypeOf, instead use Reflect.setPrototypeOf*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.setPrototypeOf({}, Object.prototype) ``` __config:__ `prefer-reflect: [2, { exceptions: ["setPrototypeOf"] }]` ```js +/*eslint prefer-reflect: [2, { exceptions: ["setPrototypeOf"] }]*/ + Object.setPrototypeOf({}, Object.prototype) Reflect.setPrototypeOf({}, Object.prototype) ``` @@ -176,23 +196,23 @@ Reflect.setPrototypeOf({}, Object.prototype) The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.isExtensible({}) +/*eslint prefer-reflect: 2*/ + +Object.isExtensible({}) /*error Avoid using Object.isExtensible, instead use Reflect.isExtensible*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.isExtensible({}) ``` -__config:__ `prefer-reflect: [2, { exceptions: ["isExtensible"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["isExtensible"] }]*/ + Object.isExtensible({}) Reflect.isExtensible({}) ``` @@ -201,23 +221,23 @@ Reflect.isExtensible({}) The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.getOwnPropertyNames({}) +/*eslint prefer-reflect: 2*/ + +Object.getOwnPropertyNames({}) /*error Avoid using Object.getOwnPropertyNames, instead use Reflect.getOwnPropertyNames*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.getOwnPropertyNames({}) ``` -__config:__ `prefer-reflect: [2, { exceptions: ["getOwnPropertyNames"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["getOwnPropertyNames"] }]*/ + Object.getOwnPropertyNames({}) Reflect.getOwnPropertyNames({}) ``` @@ -226,23 +246,23 @@ Reflect.getOwnPropertyNames({}) The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -Object.preventExtensions({}) +/*eslint prefer-reflect: 2*/ + +Object.preventExtensions({}) /*error Avoid using Object.preventExtensions, instead use Reflect.preventExtensions*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` - ```js +/*eslint prefer-reflect: 2*/ + Reflect.preventExtensions({}) ``` -__config:__ `prefer-reflect: [2, { exceptions: ["preventExtensions"] }]` - ```js +/*eslint prefer-reflect: [2, { exceptions: ["preventExtensions"] }]*/ + Object.preventExtensions({}) Reflect.preventExtensions({}) ``` @@ -251,46 +271,31 @@ Reflect.preventExtensions({}) The following patterns are considered warnings: -__config:__ `prefer-reflect: [2]` - ```js -delete foo.bar; +/*eslint prefer-reflect: 2*/ + +delete foo.bar; /*error Avoid using the delete keyword, instead use Reflect.deleteProperty*/ ``` The following patterns are not considered warnings: -__config:__ `prefer-reflect: [2]` +``` +/*eslint prefer-reflect: 2*/ -```js delete bar; // Does not reference an object, just a var Reflect.deleteProperty(foo, 'bar'); ``` (Note: For a rule preventing deletion of variables, see [no-delete-var instead](no-delete-var.md)) -__config:__ `prefer-reflect: [2, { exceptions: ["delete"] }]` +``` +/*eslint prefer-reflect: [2, { exceptions: ["delete"] }]*/ -```js delete bar delete foo.bar Reflect.deleteProperty(foo, 'bar'); ``` -## Options - -### Exceptions - -```js -"prefer-reflect": [, { exceptions: [<...exceptions>] }] -``` - -The `exceptions` option allows you to pass an array of methods names you'd like to continue to use in the old style. - -For example if you wish to use all Reflect methods, except for `Function.prototype.apply` then your config would look like `prefer-reflect: [2, { exceptions: ["apply"] }]`. - -If you want to use Reflect methods, but keep using the `delete` keyword, then your config would look like `prefer-reflect: [2, { exceptions: ["delete"] }]`. - -These can be combined as much as you like. To make all methods exceptions (thereby rendering this rule useless), use `prefer-reflect: [2, { exceptions: ["apply", "call", "defineProperty", "getOwnPropertyDescriptor", "getPrototypeOf", "setPrototypeOf", "isExtensible", "getOwnPropertyNames", "preventExtensions", "delete"] }]` ## When Not to Use It diff --git a/docs/rules/prefer-spread.md b/docs/rules/prefer-spread.md index 0096c44858d..2bdf2406831 100644 --- a/docs/rules/prefer-spread.md +++ b/docs/rules/prefer-spread.md @@ -21,27 +21,25 @@ This rule is aimed to flag usage of `Function.prototype.apply()` that can be rep The following patterns are considered warnings: ```js -foo.apply(undefined, args); -``` +/*eslint prefer-spread: 2*/ -```js -foo.apply(null, args); -``` +foo.apply(undefined, args); /*error use the spread operator instead of the ".apply()".*/ -```js -obj.foo.apply(obj, args); +foo.apply(null, args); /*error use the spread operator instead of the ".apply()".*/ + +obj.foo.apply(obj, args); /*error use the spread operator instead of the ".apply()".*/ ``` The following patterns are not considered warnings: ```js +/*eslint prefer-spread: 2*/ + // The `this` binding is different. foo.apply(obj, args); obj.foo.apply(null, args); obj.foo.apply(otherObj, args); -``` -```js // The argument list is not variadic. // Those are warned by the `no-useless-call` rule. foo.apply(undefined, [1, 2, 3]); @@ -55,8 +53,10 @@ This rule analyzes code statically to check whether or not the `this` argument i So if the `this` argument is computed in a dynamic expression, this rule cannot detect a violation. ```js +/*eslint prefer-spread: 2*/ + // This warns. -a[i++].foo.apply(a[i++], args); +a[i++].foo.apply(a[i++], args); /*error use the spread operator instead of the ".apply()".*/ // This does not warn. a[++i].foo.apply(a[i], args); diff --git a/docs/rules/prefer-template.md b/docs/rules/prefer-template.md index 8d6cf22d9cd..d663cd432f3 100644 --- a/docs/rules/prefer-template.md +++ b/docs/rules/prefer-template.md @@ -17,13 +17,17 @@ This rule is aimed to flag usage of `+` operators with strings. The following patterns are considered warnings: ```js -var str = "Hello, " + name + "!"; -var str = "Time: " + (12 * 60 * 60 * 1000); +/*eslint prefer-template: 2*/ + +var str = "Hello, " + name + "!"; /*error Unexpected string concatenation.*/ +var str = "Time: " + (12 * 60 * 60 * 1000); /*error Unexpected string concatenation.*/ ``` The following patterns are not considered warnings: ```js +/*eslint prefer-template: 2*/ + var str = "Hello World!"; var str = `Hello, ${name}!`; var str = `Time: ${12 * 60 * 60}`; diff --git a/docs/rules/quote-props.md b/docs/rules/quote-props.md index 39a3934b954..d02a1649801 100644 --- a/docs/rules/quote-props.md +++ b/docs/rules/quote-props.md @@ -4,7 +4,7 @@ Object literal property names can be defined in two ways: using literals or usin ```js var object1 = { - property: true; + property: true }; var object2 = { @@ -79,16 +79,20 @@ var object = { When configured with `"always"` as the first option (the default), quoting for all properties will be enforced. The following patterns are considered warnings: ```js +/*eslint quote-props: [2, "always"]*/ + var object = { - foo: "bar", - baz: 42, + foo: "bar", /*error Unquoted property `foo` found.*/ + baz: 42, /*error Unquoted property `baz` found.*/ "qux-lorem": true }; ``` -The following patterns are considered okay and do not cause warnings: +The following patterns are not considered warnings: ```js +/*eslint quote-props: [2, "always"]*/ + var object1 = { "foo": "bar", "baz": 42, @@ -113,20 +117,24 @@ var object3 = { When configured with `"as-needed"` as the first option, quotes will be enforced when they are strictly required, and unnecessary quotes will cause warnings. The following patterns are considered warnings: ```js +/*eslint quote-props: [2, "as-needed"]*/ + var object = { - "a": 0, - "0": 0, - "true": 0, - "null": 0 + "a": 0, /*error Unnecessarily quoted property `a` found.*/ + "0": 0, /*error Unnecessarily quoted property `0` found.*/ + "true": 0, /*error Unnecessarily quoted property `true` found.*/ + "null": 0 /*error Unnecessarily quoted property `null` found.*/ }; ``` -The following patterns are considered okay and do not cause warnings: +The following patterns are not considered warnings: ```js +/*eslint quote-props: [2, "as-needed"]*/ + var object1 = { "a-b": 0, - "0x0": 0 + "0x0": 0, "1e2": 0 }; @@ -149,16 +157,18 @@ When the `"as-needed"` mode is selected, an additional `keywords` option can be ```json { - "quote-props": [2, "as-needed", {"keywords": true}] + "quote-props": [2, "as-needed", { "keywords": true }] } ``` When `keywords` is set to `true`, the following patterns become warnings: ```js +/*eslint quote-props: [2, "as-needed", { "keywords": true }]*/ + var x = { - while: 1, - volatile: "foo" + while: 1, /*error Unquoted reserved word `while` used as key.*/ + volatile: "foo" /*error Unquoted reserved word `volatile` used as key.*/ }; ``` @@ -166,13 +176,15 @@ Another modifier for this rule is the `unnecessary` option which defaults to `tr ```json { - "quote-props": [2, "as-needed", {"keywords": true, "unnecessary": false}] + "quote-props": [2, "as-needed", { "keywords": true, "unnecessary": false }] } ``` When `unnecessary` is set to `false`, the following patterns _stop_ becoming warnings: ```js +/*eslint quote-props: [2, "as-needed", { "keywords": true, "unnecessary": false }]*/ + var x = { "while": 1, "foo": "bar" // Would normally have caused a warning @@ -189,15 +201,17 @@ A `numbers` flag, with default value `false`, can also be used as a modifier for When `numbers` is set to `true`, the following patterns become warnings: -``` +```js +/*eslint quote-props: [2, "as-needed", { "numbers": true }]*/ + var x = { - 100: 1 + 100: 1 /*error Unquoted number literal `100` used as key.*/ } ``` and the following patterns _stop_ becoming warnings: -``` +```js var x = { "100": 1 } @@ -208,21 +222,25 @@ var x = { When configured with `"consistent"`, the patterns below are considered warnings. Basically `"consistent"` means all or no properties are expected to be quoted, in other words quoting style can't be mixed within an object. Please note the latter situation (no quotation at all) isn't always possible as some property names require quoting. ```js -var object1 = { +/*eslint quote-props: [2, "consistent"]*/ + +var object1 = { /*error Inconsistently quoted property `baz` found.*/ /*error Inconsistently quoted property `qux-lorem` found.*/ foo: "bar", "baz": 42, "qux-lorem": true }; -var object2 = { +var object2 = { /*error Inconsistently quoted property `baz` found.*/ 'foo': 'bar', baz: 42 }; ``` -The following patterns are considered okay and do not cause warnings: +The following patterns are not considered warnings: ```js +/*eslint quote-props: [2, "consistent"]*/ + var object1 = { "foo": "bar", "baz": 42, @@ -245,21 +263,25 @@ var object3 = { When configured with `"consistent-as-needed"`, the behavior is similar to `"consistent"` with one difference. Namely, properties' quoting should be consistent (as in `"consistent"`) but whenever all quotes are redundant a warning is raised. In other words if at least one property name has to be quoted (like `qux-lorem`) then all property names must be quoted, otherwise no properties can be quoted. The following patterns are considered warnings: ```js -var object1 = { +/*eslint quote-props: [2, "consistent-as-needed"]*/ + +var object1 = { /*error Inconsistently quoted property `baz` found.*/ /*error Inconsistently quoted property `qux-lorem` found.*/ foo: "bar", "baz": 42, "qux-lorem": true }; -var object2 = { +var object2 = { /*error Properties shouldn't be quoted as all quotes are redundant.*/ 'foo': 'bar', 'baz': 42 }; ``` -The following patterns are considered okay and do not cause warnings: +The following patterns are not considered warnings: ```js +/*eslint quote-props: [2, "consistent-as-needed"]*/ + var object1 = { "foo": "bar", "baz": 42, @@ -276,14 +298,16 @@ When the `"consistent-as-needed"` mode is selected, an additional `keywords` opt ```json { - "quote-props": [2, "consistent-as-needed", {"keywords": true}] + "quote-props": [2, "consistent-as-needed", { "keywords": true }] } ``` When `keywords` is set to `true`, the following patterns become warnings: ```js -var x = { +/*eslint quote-props: [2, "consistent-as-needed", { "keywords": true }]*/ + +var x = { /*error Properties should be quoted as `while` is a reserved word.*/ /*error Properties should be quoted as `volatile` is a reserved word.*/ while: 1, volatile: "foo" }; diff --git a/docs/rules/quotes.md b/docs/rules/quotes.md index 0002dd40080..b7f4e4f9998 100644 --- a/docs/rules/quotes.md +++ b/docs/rules/quotes.md @@ -10,10 +10,10 @@ var single = 'single'; var backtick = `backtick`; ``` -The third parameter enables an exception to the rule to avoid escaping quotes. For example, when `"single"` is the standard, this option allows the use of double quotes to avoid escaping single quotes. This option can have the value `"avoid-escape"` and is off by default. +The third parameter enables an exception to the rule to avoid escaping quotes. For example, when `"single"` is the standard, this option allows the use of outer double quotes to avoid escaping single quotes. This option can have the value `"avoid-escape"` and is off by default. -```js -[2, "single", "avoid-escape"] +```json +quotes: [2, "single", "avoid-escape"] ``` ## Rule Details @@ -23,51 +23,82 @@ This rule will throw warnings when the wrong type of quote is used. The following patterns are considered warnings: ```js -// When [1, "double"] -var single = 'single'; +/*eslint quotes: [2, "double"]*/ -// When [1, "single"] -var double = "double"; +var single = 'single'; /*error Strings must use doublequote.*/ +var unescaped = 'a string containing "double" quotes'; /*error Strings must use doublequote.*/ +``` -// When [1, "double", "avoid-escape"] -var single = 'single'; +```js +/*eslint quotes: [2, "single"]*/ -// When [1, "single", "avoid-escape"] -var double = "double"; +var double = "double"; /*error Strings must use singlequote.*/ +var unescaped = "a string containing 'single' quotes"; /*error Strings must use singlequote.*/ +``` -// When [1, "backtick"] -var single = 'single'; -var double = "double"; +```js +/*eslint quotes: [2, "double", "avoid-escape"]*/ -// When [1, "backtick", "avoid-escape"] -var single = 'single'; -var double = "double"; +var single = 'single'; /*error Strings must use doublequote.*/ ``` -The follow patterns are not considered warnings: +```js +/*eslint quotes: [2, "single", "avoid-escape"]*/ + +var double = "double"; /*error Strings must use singlequote.*/ +``` ```js -// When [1, "double"] +/*eslint quotes: [2, "backtick"]*/ + +var single = 'single'; /*error Strings must use backtick.*/ +var double = "double"; /*error Strings must use backtick.*/ +var unescaped = 'a string containing `backticks`'; /*error Strings must use backtick.*/ +``` + +```js +/*eslint quotes: [2, "backtick", "avoid-escape"]*/ + +var single = 'single'; /*error Strings must use backtick.*/ +var double = "double"; /*error Strings must use backtick.*/ +``` + +The following patterns are not considered warnings: + +```js +/*eslint quotes: [2, "double"]*/ + var double = "double"; +var backtick = `backtick`; // backticks are allowed +``` + +```js +/*eslint quotes: [2, "single"]*/ -// When [1, "single"] var single = 'single'; +var backtick = `backtick`; // backticks are allowed +``` + +```js +/*eslint quotes: [2, "double", "avoid-escape"]*/ -// When [1, "double", "avoid-escape"] var single = 'a string containing "double" quotes'; +``` -// When [1, "single", "avoid-escape"] -var double = "a string containing 'single' quotes"; +```js +/*eslint quotes: [2, "single", "avoid-escape"]*/ -// When [1, "backtick"] -var backtick = `backtick`; +var double = "a string containing 'single' quotes"; +``` -// When [1, "backtick", "avoid-escape"] -var double = "a string containing `backtick` quotes" +```js +/*eslint quotes: [2, "backtick"]*/ -// When [1, "single"] var backtick = `backtick`; +``` -// When [1, "double"] -var backtick = `backtick`; +```js +/*eslint quotes: [2, "backtick", "avoid-escape"]*/ + +var double = "a string containing `backtick` quotes" ``` diff --git a/docs/rules/radix.md b/docs/rules/radix.md index d1199ebea13..480cf2573e6 100644 --- a/docs/rules/radix.md +++ b/docs/rules/radix.md @@ -23,14 +23,18 @@ This rule is aimed at preventing the unintended conversion of a string to a numb The following patterns are considered warnings: ```js -var num = parseInt("071"); +/*eslint radix: 2*/ -var num = parseInt(someValue); +var num = parseInt("071"); /*error Missing radix parameter.*/ + +var num = parseInt(someValue); /*error Missing radix parameter.*/ ``` The following patterns are not considered warnings: ```js +/*eslint radix: 2*/ + var num = parseInt("071", 10); var num = parseFloat(someValue); diff --git a/docs/rules/require-yield.md b/docs/rules/require-yield.md index 3cc32c9db5b..21bde95486e 100644 --- a/docs/rules/require-yield.md +++ b/docs/rules/require-yield.md @@ -7,7 +7,9 @@ This rule generates warnings for generator functions that do not have the `yield The following patterns are considered warnings: ```js -function* foo() { +/*eslint require-yield: 2*/ + +function* foo() { /*error This generator function does not have `yield`.*/ return 10; } ``` @@ -15,19 +17,17 @@ function* foo() { The following patterns are not considered warnings: ```js +/*eslint require-yield: 2*/ + function* foo() { yield 5; return 10; } -``` -```js function foo() { return 10; } -``` -```js // This rule does not warn on empty generator functions. function* foo() { } ``` diff --git a/docs/rules/semi-spacing.md b/docs/rules/semi-spacing.md index de839d9305b..0e7eee71ccb 100644 --- a/docs/rules/semi-spacing.md +++ b/docs/rules/semi-spacing.md @@ -34,17 +34,21 @@ This is the default option. It enforces spacing after semicolons and disallows s The following patterns are considered warnings: ```js -var foo ; -var foo;var bar; -throw new Error("error") ; -while (a) { break ; } -for (i = 0 ; i < 10 ; i++) {} -for (i = 0;i < 10;i++) {} +/*eslint semi-spacing: 2*/ + +var foo ; /*error Unexpected whitespace before semicolon.*/ +var foo;var bar; /*error Missing whitespace after semicolon.*/ +throw new Error("error") ; /*error Unexpected whitespace before semicolon.*/ +while (a) { break ; } /*error Unexpected whitespace before semicolon.*/ +for (i = 0 ; i < 10 ; i++) {} /*error Unexpected whitespace before semicolon.*/ +for (i = 0;i < 10;i++) {} /*error Missing whitespace after semicolon.*/ ``` The following patterns are not warnings: ```js +/*eslint semi-spacing: 2*/ + var foo; var foo; var bar; throw new Error("error"); @@ -59,19 +63,23 @@ This option enforces spacing before semicolons and disallows spacing after semic The following patterns are considered warnings: ```js -var foo; -var foo ; var bar; -throw new Error("error"); -while (a) { break; } -for (i = 0;i < 10;i++) {} -for (i = 0; i < 10; i++) {} +/*eslint semi-spacing: [2, { "before": true, "after": false }]*/ + +var foo; /*error Missing whitespace before semicolon.*/ +var foo ; var bar; /*error Missing whitespace before semicolon.*/ /*error Unexpected whitespace after semicolon.*/ +throw new Error("error"); /*error Missing whitespace before semicolon.*/ +while (a) { break; } /*error Missing whitespace before semicolon.*/ /*error Unexpected whitespace after semicolon.*/ +for (i = 0;i < 10;i++) {} /*error Missing whitespace before semicolon.*/ +for (i = 0; i < 10; i++) {} /*error Missing whitespace before semicolon.*/ /*error Unexpected whitespace after semicolon.*/ ``` The following patterns are not warnings: ```js +/*eslint semi-spacing: [2, { "before": true, "after": false }]*/ + var foo ; -var foo ;var bar; +var foo ;var bar ; throw new Error("error") ; while (a) {break ;} for (i = 0 ;i < 10 ;i++) {} diff --git a/docs/rules/semi.md b/docs/rules/semi.md index c9ea0e37d77..7cbaa53288d 100644 --- a/docs/rules/semi.md +++ b/docs/rules/semi.md @@ -11,7 +11,7 @@ On the first line, the JavaScript engine will automatically insert a semicolon, In the debate over ASI, there are generally two schools of thought. The first is that we should treat ASI as if it didn't exist and always include semicolons manually. The rationale is that it's easier to always include semicolons than to try to remember when they are or are not required, and thus decreases the possibility of introducing an error. For example, consider this code: -```js +``` return { name: "ESLint" @@ -20,7 +20,7 @@ return This may look like a `return` statement that returns an object literal, however, the JavaScript engine will interpret this code as: -```js +``` return; { name: "ESLint" @@ -51,16 +51,20 @@ semi: [2, "always"] The following patterns are considered warnings: ```js -var name = "ESLint" +/*eslint semi: 2*/ + +var name = "ESLint" /*error Missing semicolon.*/ object.method = function() { // ... -} +} /*error Missing semicolon.*/ ``` The following patterns are not considered warnings: ```js +/*eslint semi: 2*/ + var name = "ESLint"; object.method = function() { @@ -77,16 +81,20 @@ semi: [2, "never"] Then, the following patterns are considered warnings: ```js -var name = "ESLint"; +/*eslint semi: [2, "never"]*/ + +var name = "ESLint"; /*error Extra semicolon.*/ object.method = function() { // ... -}; +}; /*error Extra semicolon.*/ ``` And the following patterns are not considered warnings: ```js +/*eslint semi: [2, "never"]*/ + var name = "ESLint" object.method = function() { @@ -97,6 +105,8 @@ object.method = function() { Even in "never" mode, semicolons are still allowed to disambiguate statements beginning with `[`, `(`, `/`, `+`, or `-`: ```js +/*eslint semi: [2, "never"]*/ + var name = "ESLint" ;(function() { diff --git a/docs/rules/sort-vars.md b/docs/rules/sort-vars.md index e3dc2812359..f7418f576af 100644 --- a/docs/rules/sort-vars.md +++ b/docs/rules/sort-vars.md @@ -10,16 +10,20 @@ The default configuration of the rule is case-sensitive. The following patterns are considered warnings: ```js -var b, a; +/*eslint sort-vars: 2*/ -var a, B, c; +var b, a; /*error Variables within the same declaration block should be sorted alphabetically*/ -var a, A; +var a, B, c; /*error Variables within the same declaration block should be sorted alphabetically*/ + +var a, A; /*error Variables within the same declaration block should be sorted alphabetically*/ ``` The following patterns are considered okay and do not cause warnings: ```js +/*eslint sort-vars: 2*/ + var a, b, c, d; var _a = 10; @@ -33,21 +37,23 @@ var B, a, c; Alphabetical list is maintained starting from the first variable and excluding any that are considered warnings. So the following code will produce two warnings: ```js -var c, d, a, b; +/*eslint sort-vars: 2*/ + +var c, d, a, b; /*error Variables within the same declaration block should be sorted alphabetically*/ ``` But this one, will only produce one: ```js -var c, d, a, e; +/*eslint sort-vars: 2*/ + +var c, d, a, e; /*error Variables within the same declaration block should be sorted alphabetically*/ ``` ## Rule Options -```js -... +``` "sort-vars": [, { "ignoreCase": }] -... ``` ### `ignoreCase` @@ -57,6 +63,8 @@ When `true` the rule ignores the case-sensitivity of the variables order. The following patterns are considered okay and do not cause warnings: ```js +/*eslint sort-vars: [2, { "ignoreCase": true }]*/ + var a, A; var a, B, c; diff --git a/docs/rules/space-after-keywords.md b/docs/rules/space-after-keywords.md index 8fd0f11f117..4fafd011cd8 100644 --- a/docs/rules/space-after-keywords.md +++ b/docs/rules/space-after-keywords.md @@ -26,33 +26,33 @@ then there should be no spaces following. The default is `"always"`. The following patterns are considered warnings: ```js -if(a) {} -``` +/*eslint space-after-keywords: 2*/ -```js -if (a) {} else{} -``` +if(a) {} /*error Keyword "if" must be followed by whitespace.*/ -```js -do{} while (a); +if (a) {} else{} /*error Keyword "else" must be followed by whitespace.*/ + +do{} while (a); /*error Keyword "do" must be followed by whitespace.*/ ``` ```js -// When ["never"] -if (a) {} +/*eslint space-after-keywords: [2, "never"]*/ + +if (a) {} /*error Keyword "if" must not be followed by whitespace.*/ ``` The following patterns are not considered warnings: ```js +/*eslint space-after-keywords: 2*/ + if (a) {} -``` -```js if (a) {} else {} ``` ```js -// When ["never"] +/*eslint space-after-keywords: [2, "never"]*/ + if(a) {} ``` diff --git a/docs/rules/space-before-blocks.md b/docs/rules/space-before-blocks.md index 3b3c5f893ed..c841e1f3822 100644 --- a/docs/rules/space-before-blocks.md +++ b/docs/rules/space-before-blocks.md @@ -15,28 +15,32 @@ then all blocks should never have any preceding space. The default is `"always"` The following patterns are considered warnings when configured `"always"`: ```js -if (a){ +/*eslint space-before-blocks: 2*/ + +if (a){ /*error Missing space before opening brace.*/ b(); } if (a) { b(); -} else{ +} else{ /*error Missing space before opening brace.*/ c(); } -function a(){} +function a(){} /*error Missing space before opening brace.*/ -for (;;){ +for (;;){ /*error Missing space before opening brace.*/ b(); } -try {} catch(a){} +try {} catch(a){} /*error Missing space before opening brace.*/ ``` The following patterns are not considered warnings when configured `"always"`: ```js +/*eslint space-before-blocks: 2*/ + if (a) { b(); } @@ -53,22 +57,26 @@ try {} catch(a) {} The following patterns are considered warnings when configured `"never"`: ```js -if (a) { +/*eslint space-before-blocks: [2, "never"]*/ + +if (a) { /*error Unexpected space before opening brace.*/ b(); } -function a() {} +function a() {} /*error Unexpected space before opening brace.*/ -for (;;) { +for (;;) { /*error Unexpected space before opening brace.*/ b(); } -try {} catch(a) {} +try {} catch(a) {} /*error Unexpected space before opening brace.*/ ``` The following patterns are not considered warnings when configured `"never"`: ```js +/*eslint space-before-blocks: [2, "never"]*/ + if (a){ b(); } diff --git a/docs/rules/space-before-function-paren.md b/docs/rules/space-before-function-paren.md index f8e1136dda7..7a17e2e62a1 100644 --- a/docs/rules/space-before-function-paren.md +++ b/docs/rules/space-before-function-paren.md @@ -29,26 +29,28 @@ The default configuration is `"always"`. The following patterns are considered warnings when configured `"always"`: ```js -function foo() { +/*eslint space-before-function-paren: 2*/ + +function foo() { /*error Missing space before function parentheses.*/ // ... } -var bar = function() { +var bar = function() { /*error Missing space before function parentheses.*/ // ... }; -var bar = function foo() { +var bar = function foo() { /*error Missing space before function parentheses.*/ // ... }; class Foo { - constructor() { + constructor() { /*error Missing space before function parentheses.*/ // ... } } var foo = { - bar() { + bar() { /*error Missing space before function parentheses.*/ // ... } }; @@ -57,6 +59,8 @@ var foo = { The following patterns are not considered warnings when configured `"always"`: ```js +/*eslint space-before-function-paren: 2*/ + function foo () { // ... } @@ -85,26 +89,28 @@ var foo = { The following patterns are considered warnings when configured `"never"`: ```js -function foo () { +/*eslint space-before-function-paren: [2, "never"]*/ + +function foo () { /*error Unexpected space before function parentheses.*/ // ... } -var bar = function () { +var bar = function () { /*error Unexpected space before function parentheses.*/ // ... }; -var bar = function foo () { +var bar = function foo () { /*error Unexpected space before function parentheses.*/ // ... }; class Foo { - constructor () { + constructor () { /*error Unexpected space before function parentheses.*/ // ... } } var foo = { - bar () { + bar () { /*error Unexpected space before function parentheses.*/ // ... } }; @@ -113,6 +119,8 @@ var foo = { The following patterns are not considered warnings when configured `"never"`: ```js +/*eslint space-before-function-paren: [2, "never"]*/ + function foo() { // ... } @@ -141,22 +149,24 @@ var foo = { The following patterns are considered warnings when configured `{"anonymous": "always", "named": "never"}`: ```js -function foo () { +/*eslint space-before-function-paren: [2, { "anonymous": "always", "named": "never" }]*/ + +function foo () { /*error Unexpected space before function parentheses.*/ // ... } -var bar = function() { +var bar = function() { /*error Missing space before function parentheses.*/ // ... }; class Foo { - constructor () { + constructor () { /*error Unexpected space before function parentheses.*/ // ... } } var foo = { - bar () { + bar () { /*error Unexpected space before function parentheses.*/ // ... } }; @@ -165,6 +175,8 @@ var foo = { The following patterns are not considered warnings when configured `{"anonymous": "always", "named": "never"}`: ```js +/*eslint space-before-function-paren: [2, { "anonymous": "always", "named": "never" }]*/ + function foo() { // ... } @@ -189,22 +201,24 @@ var foo = { The following patterns are considered warnings when configured `{"anonymous": "never", "named": "always"}`: ```js -function foo() { +/*eslint space-before-function-paren: [2, { "anonymous": "never", "named": "always" }]*/ + +function foo() { /*error Missing space before function parentheses.*/ // ... } -var bar = function () { +var bar = function () { /*error Unexpected space before function parentheses.*/ // ... }; class Foo { - constructor() { + constructor() { /*error Missing space before function parentheses.*/ // ... } } var foo = { - bar() { + bar() { /*error Missing space before function parentheses.*/ // ... } }; @@ -213,6 +227,8 @@ var foo = { The following patterns are not considered warnings when configured `{"anonymous": "never", "named": "always"}`: ```js +/*eslint space-before-function-paren: [2, { "anonymous": "never", "named": "always" }]*/ + function foo () { // ... } diff --git a/docs/rules/space-before-function-parentheses.md b/docs/rules/space-before-function-parentheses.md index 7e837f127d6..ef5431afe95 100644 --- a/docs/rules/space-before-function-parentheses.md +++ b/docs/rules/space-before-function-parentheses.md @@ -35,7 +35,7 @@ function foo() { // ... } -var bar function() { +var bar = function() { // ... }; @@ -63,7 +63,7 @@ function foo () { // ... } -var bar function () { +var bar = function () { // ... }; @@ -91,7 +91,7 @@ function foo () { // ... } -var bar function () { +var bar = function () { // ... }; @@ -119,7 +119,7 @@ function foo() { // ... } -var bar function() { +var bar = function() { // ... }; diff --git a/docs/rules/space-before-keywords.md b/docs/rules/space-before-keywords.md index f0e1c7a2e64..decc5589ba6 100644 --- a/docs/rules/space-before-keywords.md +++ b/docs/rules/space-before-keywords.md @@ -20,12 +20,12 @@ The following patterns are considered errors when configured `"never"`: if (foo) { // ... -} else {} /*error Unexpected space before keyword "else".*/ +} else {} /*error Unexpected space before keyword "else".*/ do { } -while (foo) /*error Unexpected space before keyword "while".*/ +while (foo) /*error Unexpected space before keyword "while".*/ try {} finally {} /*error Unexpected space before keyword "finally".*/ @@ -55,13 +55,15 @@ The following patterns are considered errors when configured `"always"`: if (foo) { // ... -}else {} /*error Missing space before keyword "else".*/ +}else {} /*error Missing space before keyword "else".*/ const foo = 'bar';let baz = 'qux'; /*error Missing space before keyword "let".*/ -var foo =function bar () {} /*error Missing space before keyword "function".*/ +var foo =function bar () {} /*error Missing space before keyword "function".*/ -if (foo) {return; } /*error Missing space before keyword "return".*/ +function bar() { + if (foo) {return; } /*error Missing space before keyword "return".*/ +} ``` The following patterns are not considered errors when configured `"always"`: diff --git a/docs/rules/space-in-brackets.md b/docs/rules/space-in-brackets.md index 543039bb2a0..8722aba1a45 100644 --- a/docs/rules/space-in-brackets.md +++ b/docs/rules/space-in-brackets.md @@ -49,8 +49,8 @@ var arr = ['foo', var obj = { 'foo': 'bar' }; var obj = {'foo': 'bar' }; -var obj = { baz: {'foo': 'qux'}, 'bar'}; -var obj = {baz: { 'foo': 'qux' }, 'bar'}; +var obj = { baz: {'foo': 'qux'}, bar}; +var obj = {baz: { 'foo': 'qux' }, bar}; ``` The following patterns are not warnings: @@ -115,8 +115,8 @@ var arr = [ var obj = {'foo': 'bar'}; var obj = {'foo': 'bar' }; -var obj = { baz: {'foo': 'qux'}, 'bar'}; -var obj = {baz: { 'foo': 'qux' }, 'bar'}; +var obj = { baz: {'foo': 'qux'}, bar}; +var obj = {baz: { 'foo': 'qux' }, bar}; var obj = {'foo': 'bar' }; diff --git a/docs/rules/space-in-parens.md b/docs/rules/space-in-parens.md index 2c1e4dac977..83f8f6b01ed 100644 --- a/docs/rules/space-in-parens.md +++ b/docs/rules/space-in-parens.md @@ -32,17 +32,21 @@ Depending on your coding conventions, you can choose either option by specifying When `"always"` is set, the following patterns are considered warnings: ```js -foo( 'bar'); -foo('bar' ); -foo('bar'); +/*eslint space-in-parens: [2, "always"]*/ -var foo = (1 + 2) * 3; -(function () { return 'bar'; }()); +foo( 'bar'); /*error There must be a space inside this paren.*/ +foo('bar' ); /*error There must be a space inside this paren.*/ +foo('bar'); /*error There must be a space inside this paren.*/ + +var foo = (1 + 2) * 3; /*error There must be a space inside this paren.*/ +(function () { return 'bar'; }()); /*error There must be a space inside this paren.*/ ``` The following patterns are not warnings: ```js +/*eslint space-in-parens: [2, "always"]*/ + foo(); foo( 'bar' ); @@ -56,17 +60,21 @@ var foo = ( 1 + 2 ) * 3; When `"never"` is used, the following patterns are considered warnings: ```js -foo( 'bar'); -foo('bar' ); -foo( 'bar' ); +/*eslint space-in-parens: [2, "never"]*/ -var foo = ( 1 + 2 ) * 3; -( function () { return 'bar'; }() ); +foo( 'bar'); /*error There should be no spaces inside this paren.*/ +foo('bar' ); /*error There should be no spaces inside this paren.*/ +foo( 'bar' ); /*error There should be no spaces inside this paren.*/ + +var foo = ( 1 + 2 ) * 3; /*error There should be no spaces inside this paren.*/ +( function () { return 'bar'; }() ); /*error There should be no spaces inside this paren.*/ ``` The following patterns are not warnings: ```js +/*eslint space-in-parens: [2, "never"]*/ + foo(); foo('bar'); @@ -84,13 +92,17 @@ The following exceptions are available: `["{}", "[]", "()", "empty"`]. For example, given `"space-in-parens": [2, "always", { "exceptions": ["{}"] }]`, the following would be warnings: ```js -foo( {bar: 'baz'} ); -foo( 1, {bar: 'baz'} ); +/*eslint space-in-parens: [2, "always", { "exceptions": ["{}"] }]*/ + +foo( {bar: 'baz'} ); /*error There should be no spaces inside this paren.*/ +foo( 1, {bar: 'baz'} ); /*error There should be no spaces inside this paren.*/ ``` And the following patterns would *not* be warnings: ```js +/*eslint space-in-parens: [2, "always", { "exceptions": ["{}"] }]*/ + foo({bar: 'baz'}); foo( 1, {bar: 'baz'}); ``` @@ -98,13 +110,17 @@ foo( 1, {bar: 'baz'}); Or, given `"space-in-parens": [2, "never", { "exceptions": ["{}"] }]`, the following would be warnings: ```js -foo({bar: 'baz'}); -foo(1, {bar: 'baz'}); +/*eslint space-in-parens: [2, "never", { "exceptions": ["{}"] }]*/ + +foo({bar: 'baz'}); /*error There must be a space inside this paren.*/ +foo(1, {bar: 'baz'}); /*error There must be a space inside this paren.*/ ``` And the following patterns would *not* be warnings: ```js +/*eslint space-in-parens: [2, "never", { "exceptions": ["{}"] }]*/ + foo( {bar: 'baz'} ); foo(1, {bar: 'baz'} ); ``` @@ -112,13 +128,17 @@ foo(1, {bar: 'baz'} ); Given `"space-in-parens": [2, "always", { "exceptions": ["[]"] }]`, the following would be warnings: ```js -foo( [bar, baz] ); -foo( [bar, baz], 1 ); +/*eslint space-in-parens: [2, "always", { "exceptions": ["[]"] }]*/ + +foo( [bar, baz] ); /*error There should be no spaces inside this paren.*/ +foo( [bar, baz], 1 ); /*error There should be no spaces inside this paren.*/ ``` And the following patterns would *not* be warnings: ```js +/*eslint space-in-parens: [2, "always", { "exceptions": ["[]"] }]*/ + foo([bar, baz]); foo([bar, baz], 1 ); ``` @@ -126,13 +146,17 @@ foo([bar, baz], 1 ); Or, given `"space-in-parens": [2, "never", { "exceptions": ["[]"] }]`, the following would be warnings: ```js -foo([bar, baz]); -foo([bar, baz], 1); +/*eslint space-in-parens: [2, "never", { "exceptions": ["[]"] }]*/ + +foo([bar, baz]); /*error There must be a space inside this paren.*/ +foo([bar, baz], 1); /*error There must be a space inside this paren.*/ ``` And the following patterns would *not* be warnings: ```js +/*eslint space-in-parens: [2, "never", { "exceptions": ["[]"] }]*/ + foo( [bar, baz] ); foo( [bar, baz], 1); ``` @@ -140,13 +164,17 @@ foo( [bar, baz], 1); Given `"space-in-parens": [2, "always", { "exceptions": ["()"] }]`, the following would be warnings: ```js -foo( ( 1 + 2 ) ); -foo( ( 1 + 2 ), 1 ); +/*eslint space-in-parens: [2, "always", { "exceptions": ["()"] }]*/ + +foo( ( 1 + 2 ) ); /*error There should be no spaces inside this paren.*/ +foo( ( 1 + 2 ), 1 ); /*error There should be no spaces inside this paren.*/ ``` And the following patterns would *not* be warnings: ```js +/*eslint space-in-parens: [2, "always", { "exceptions": ["()"] }]*/ + foo(( 1 + 2 )); foo(( 1 + 2 ), 1 ); ``` @@ -154,13 +182,17 @@ foo(( 1 + 2 ), 1 ); Or, given `"space-in-parens": [2, "never", { "exceptions": ["()"] }]`, the following would be warnings: ```js -foo((1 + 2)); -foo((1 + 2), 1); +/*eslint space-in-parens: [2, "never", { "exceptions": ["()"] }]*/ + +foo((1 + 2)); /*error There must be a space inside this paren.*/ +foo((1 + 2), 1); /*error There must be a space inside this paren.*/ ``` And the following patterns would *not* be warnings: ```js +/*eslint space-in-parens: [2, "never", { "exceptions": ["()"] }]*/ + foo( (1 + 2) ); foo( (1 + 2), 1); ``` @@ -170,38 +202,50 @@ The `"empty"` exception concerns empty parentheses, and works the same way as th For example, given `"space-in-parens": [2, "always", { "exceptions": ["empty"] }]`, the following pattern would be a warning: ```js -foo( ); +/*eslint space-in-parens: [2, "always", { "exceptions": ["empty"] }]*/ + +foo( ); /*error There should be no spaces inside this paren.*/ ``` And the following pattern would *not* be a warning: ```js +/*eslint space-in-parens: [2, "always", { "exceptions": ["empty"] }]*/ + foo(); ``` Or, given `"space-in-parens": [2, "never", { "exceptions": ["empty"] }]`, the following pattern would be a warning: ```js -foo(); +/*eslint space-in-parens: [2, "never", { "exceptions": ["empty"] }]*/ + +foo(); /*error There must be a space inside this paren.*/ ``` And the following pattern would *not* be a warning: ```js +/*eslint space-in-parens: [2, "never", { "exceptions": ["empty"] }]*/ + foo( ); ``` You can include multiple entries in the `"exceptions"` array. For example, given `"space-in-parens": [2, "always", { "exceptions": ["{}", "[]"] }]`, the following patterns would be warnings: ```js -bar( {bar:'baz'} ); -baz( 1, [1,2] ); -foo( {bar: 'baz'}, [1, 2] ); +/*eslint space-in-parens: [2, "always", { "exceptions": ["{}", "[]"] }]*/ + +bar( {bar:'baz'} ); /*error There should be no spaces inside this paren.*/ +baz( 1, [1,2] ); /*error There should be no spaces inside this paren.*/ +foo( {bar: 'baz'}, [1, 2] ); /*error There should be no spaces inside this paren.*/ ``` And the following pattern would *not* be warnings: ```js +/*eslint space-in-parens: [2, "always", { "exceptions": ["{}", "[]"] }]*/ + bar({bar:'baz'}); baz( 1, [1,2]); foo({bar: 'baz'}, [1, 2]); diff --git a/docs/rules/space-infix-ops.md b/docs/rules/space-infix-ops.md index 045d8d1ee39..0069d76b21d 100644 --- a/docs/rules/space-infix-ops.md +++ b/docs/rules/space-infix-ops.md @@ -18,74 +18,56 @@ While this is valid JavaScript syntax, it is hard to determine what the author i This rule is aimed at ensuring there are spaces around infix operators. -The following patterns are considered warnings: +### Options -```js -a+b -``` +This rule accepts a single options argument with the following defaults: -```js -a+ b +```json +"space-infix-ops": [2, {"int32Hint": false}] ``` -```js -a +b -``` +### `int32Hint` -```js -a?b:c -``` +Set the `int32Hint` option to `true` (default is `false`) to allow write `a|0` without space. ```js -const a={b:1}; +var foo = bar|0; // `foo` is forced to be signed 32 bit integer ``` -```js -var {a=0}=bar; -``` +The following patterns are considered warnings: ```js -function foo(a=0) { } +/*eslint space-infix-ops: 2*/ + +a+b /*error Infix operators must be spaced.*/ + +a+ b /*error Infix operators must be spaced.*/ + +a +b /*error Infix operators must be spaced.*/ + +a?b:c /*error Infix operators must be spaced.*/ + +const a={b:1}; /*error Infix operators must be spaced.*/ + +var {a=0}=bar; /*error Infix operators must be spaced.*/ + +function foo(a=0) { } /*error Infix operators must be spaced.*/ ``` The following patterns are not considered warnings: ```js +/*eslint space-infix-ops: 2*/ + a + b -``` -```js a + b -``` -```js a ? b : c -``` -```js const a = {b:1}; -``` -```js var {a = 0} = bar; -``` -```js function foo(a = 0) { } ``` - -### Options - -This rule accepts a single options argument with the following defaults: - -```json -"space-infix-ops": [2, {"int32Hint": false}] -``` - -### `int32Hint` - -Set the `int32Hint` option to `true` (default is `false`) to allow write `a|0` without space. - -```js -var foo = bar|0; // `foo` is forced to be signed 32 bit integer -``` diff --git a/docs/rules/space-return-throw-case.md b/docs/rules/space-return-throw-case.md index 79feb9b7298..d1d7865ec81 100644 --- a/docs/rules/space-return-throw-case.md +++ b/docs/rules/space-return-throw-case.md @@ -7,27 +7,23 @@ Require spaces following `return`, `throw`, and `case`. The following patterns are considered warnings: ```js -throw{a:0} -``` +/*eslint space-return-throw-case: 2*/ -```js -function f(){ return-a; } -``` +throw{a:0} /*error Keyword "throw" must be followed by whitespace.*/ -```js -switch(a){ case'a': break; } +function f(){ return-a; } /*error Keyword "return" must be followed by whitespace.*/ + +switch(a){ case'a': break; } /*error Keyword "case" must be followed by whitespace.*/ ``` The following patterns are not considered warnings: ```js +/*eslint space-return-throw-case: 2*/ + throw {a: 0}; -``` -```js function f(){ return -a; } -``` -```js switch(a){ case 'a': break; } ``` diff --git a/docs/rules/space-unary-ops.md b/docs/rules/space-unary-ops.md index 9fa0e806967..899e655b0d5 100644 --- a/docs/rules/space-unary-ops.md +++ b/docs/rules/space-unary-ops.md @@ -6,128 +6,101 @@ Some styleguides require or disallow spaces before or after unary operators. Thi This rule enforces consistency regarding the spaces after `words` unary operators and after/before `nonwords` unary operators. -### Examples - -Given the default values `words`: `true`, `nonwords`: `false`, the following patterns are considered warnings: - -Word unary operators (i.e. "typeof") should be followed by a whitespace. - -```js -typeof!foo -``` +### Options -Word unary operators (i.e. "void") should be followed by a whitespace. +This rule has two options: `words` and `nonwords`: -```js -void{foo:0} -``` +* `words` - applies to unary word operators such as: `new`, `delete`, `typeof`, `void`, `yield` +* `nonwords` - applies to unary operators such as: `-`, `+`, `--`, `++`, `!`, `!!` -Word unary operators (i.e. "new") should be followed by a whitespace. +Default values are: -```js -new[foo][0] +```json +"space-unary-ops": [1, { "words": true, "nonwords": false }] ``` -Word unary operators (i.e. "delete") should be followed by a whitespace. +Examples of unary `words` operators: ```js -delete(foo.bar) -``` - -Unary operator "++" should not be followed by whitespace. +// new +var joe = new Person(); -```js -++ foo -``` +// delete +var obj = { + foo: 'bar' +}; +delete obj.foo; -Unary operator "--" should not be preceeded by whitespace. +// typeof +typeof {} // object -```js -foo -- +// void +void 0 // undefined ``` -Unary operator "-" should not be preceeded by whitespace. +Examples of unary `nonwords` operators: ```js -- 1 +if ([1,2,3].indexOf(1) !== -1) {}; +foo = --foo; +bar = bar++; +baz = !foo; +qux = !!baz; ``` -Given the default values `words`: `true`, `nonwords`: `false`, the following patterns are not considered warnings: +### Examples -Word unary operator "delete" is followed by a whitespace. +Given the default values `words`: `true`, `nonwords`: `false`, the following patterns are considered warnings: ```js -delete foo.bar -``` +/*eslint space-unary-ops: 2*/ -Word unary operator "new" is followed by a whitespace. +typeof!foo; /*error Unary word operator "typeof" must be followed by whitespace.*/ -```js -new Foo -``` +void{foo:0}; /*error Unary word operator "void" must be followed by whitespace.*/ -Word unary operator "void" is followed by a whitespace. - -```js -void 0 -``` +new[foo][0]; /*error Unary word operator "new" must be followed by whitespace.*/ -Unary operator "++" is not followed by whitespace. +delete(foo.bar); /*error Unary word operator "delete" must be followed by whitespace.*/ -```js -++foo -``` +function *foo() { + yield(0) /*error Unary word operator "yield" must be followed by whitespace.*/ +} -Unary operator "--" is not preceeded by whitespace. +++ foo; /*error Unexpected space after unary operator "++".*/ -```js -foo-- -``` +foo --; /*error Unexpected space before unary operator "--".*/ -Unary operator "-" is not followed by whitespace. +- foo; /*error Unexpected space after unary operator "-".*/ -```js --1 ++ "3"; /*error Unexpected space after unary operator "+".*/ ``` -### Options - -This rule have two options: `words` and `nonwords`: +Given the default values `words`: `true`, `nonwords`: `false`, the following patterns are not considered warnings: -* `words` - applies to unary word operators such as: `new`, `delete`, `typeof`, `void` -* `nonwords` - applies to unary operators such as: `-`, `+`, `--`, `++`, `!`, `!!` -Default values are: ```js -"space-unary-ops": [1, { "words": true, "nonwords": false }] -``` +/*eslint space-unary-ops: 2*/ -Examples of unary `words` operators: +// Word unary operator "delete" is followed by a whitespace. +delete foo.bar; -```js -// new -var joe = new Person(); +// Word unary operator "new" is followed by a whitespace. +new Foo; -// delete -var obj = { - foo: 'bar' -}; -delete obj.foo; +// Word unary operator "void" is followed by a whitespace. +void 0; -// typeof -typeof {} // object +// Unary operator "++" is not followed by whitespace. +++foo; -// void -void 0 // undefined -``` +// Unary operator "--" is not preceeded by whitespace. +foo--; -Examples of unary `nonwords` operators: +// Unary operator "-" is not followed by whitespace. +-foo; -```js -if ([1,2,3].indexOf(1) !== -1) {}; -foo = --foo; -bar = bar++; -baz = !foo; -qux = !!baz; +// Unary operator "+" is not followed by whitespace. ++"3"; ``` diff --git a/docs/rules/spaced-comment.md b/docs/rules/spaced-comment.md index 5e69203dc18..1ac128209d2 100644 --- a/docs/rules/spaced-comment.md +++ b/docs/rules/spaced-comment.md @@ -15,7 +15,7 @@ The rule takes two options. The first is a string which be either "always" or "n Here is an example of how to configure the rule with this option: -```js +```json "spaced-comment": [2, "always"] ``` @@ -26,7 +26,7 @@ This rule can also take a 2nd option, an object with either of the following key The `"exceptions"` value is an array of string patterns which are considered exceptions to the rule. Please note that exceptions are ignored if the first argument is `"never"`. -```js +```json "spaced-comment": [2, "always", { "exceptions": ["-", "+"] }] ``` @@ -34,7 +34,7 @@ The `"markers"` value is an array of string patterns which are considered marker such as an additional `/`, used to denote documentation read by doxygen, vsdoc, etc. which must have additional characters. The `"markers"` array will apply regardless of the value of the first argument, e.g. `"always"` or `"never"`. -```js +```json "spaced-comment": [2, "always", { "markers": ["/"] }] ``` @@ -43,7 +43,7 @@ exceptions can occur anywhere in the comment string. You can also define separate exceptions and markers for block and line comments: -```js +```json "spaced-comment": [2, "always", { "line": { "markers": ["/"], @@ -58,144 +58,141 @@ You can also define separate exceptions and markers for block and line comments: #### Examples -The following patterns **are** considered warnings: +The following patterns are considered warnings: ```js -// When [2, "never"] -// This is a comment with a whitespace at the beginning -``` +/*eslint spaced-comment: [2, "never"]*/ -```js -// When [2, "never"] -/* This is a comment with a whitespace at the beginning */ -``` +// This is a comment with a whitespace at the beginning /*error Unexpected space or tab after // in comment.*/ +/* This is a comment with a whitespace at the beginning */ /*error Unexpected space or tab after /* in comment.*/ -```js -// When [2, "never"] -/* \nThis is a comment with a whitespace at the beginning */ +/* \nThis is a comment with a whitespace at the beginning */ /*error Unexpected space or tab after /* in comment.*/ ``` ```js -// When [2, "always"] -//This is a comment with no whitespace at the beginning -var foo = 5; -``` +/*eslint spaced-comment: [2, "always"]*/ /*error Expected space or tab after /* in comment.*/ -```js -// When [2, "always"] -/*This is a comment with no whitespace at the beginning */ -var foo = 5; +//This is a comment with no whitespace at the beginning /*error Expected space or tab after // in comment.*/ + +/*This is a comment with no whitespace at the beginning */ /*error Expected space or tab after /* in comment.*/ ``` ```js -// When [2, "always", { "block": { "exceptions": ["-"] } }] -//-------------- +/* eslint spaced-comment: [2, "always", { "block": { "exceptions": ["-"] } }] */ + +//-------------- /*error Expected space or tab after // in comment.*/ // Comment block -//-------------- +//-------------- /*error Expected space or tab after // in comment.*/ ``` ```js -// When [2, "always", { "exceptions": ["-", "+"] }] -//------++++++++ +/* eslint spaced-comment: [2, "always", { "exceptions": ["-", "+"] }] */ + +//------++++++++ /*error Expected exception block, space or tab after // in comment.*/ // Comment block -//------++++++++ +//------++++++++ /*error Expected exception block, space or tab after // in comment.*/ ``` ```js -// When [2, "always", { "markers": ["/"] }] -///This is a comment with a marker but without whitespace +/* eslint spaced-comment: [2, "always", { "markers": ["/"] }] */ + +///This is a comment with a marker but without whitespace /*error Expected space or tab after // in comment.*/ ``` ```js -// When [2, "always", { "exceptions": ["-", "+"] }] -/*------++++++++*/ +/* eslint spaced-comment: [2, "always", { "exceptions": ["-", "+"] }] */ + +/*------++++++++*/ /*error Expected exception block, space or tab after /* in comment.*/ /* Comment block */ -/*------++++++++*/ +/*------++++++++*/ /*error Expected exception block, space or tab after /* in comment.*/ ``` ```js -// When [2, "always", { "line": { "exceptions": ["-+"] } }] -/*-+-+-+-+-+-+-+*/ +/* eslint spaced-comment: [2, "always", { "line": { "exceptions": ["-+"] } }] */ + +/*-+-+-+-+-+-+-+*/ /*error Expected space or tab after /* in comment.*/ // Comment block -/*-+-+-+-+-+-+-+*/ +/*-+-+-+-+-+-+-+*/ /*error Expected space or tab after /* in comment.*/ ``` -The following patterns **are not** warnings: +The following patterns are not considered warnings: ```js -// When [2, "always"] +/* eslint spaced-comment: [2, "always"] */ + // This is a comment with a whitespace at the beginning -var foo = 5; -``` -```js -// When [2, "always"] /* This is a comment with a whitespace at the beginning */ -var foo = 5; -``` -```js -// When [2, "always"] -/*\n * This is a comment with a whitespace at the beginning */ -var foo = 5; +/* + * This is a comment with a whitespace at the beginning + */ + +/* +This comment has a newline +*/ ``` ```js -// When [2, "never"] +/*eslint spaced-comment: [2, "never"]*/ + /*This is a comment with no whitespace at the beginning */ -var foo = 5; ``` ```js -// When [2, "always", { "exceptions": ["-"] }] +/* eslint spaced-comment: [2, "always", { "exceptions": ["-"] }] */ + //-------------- // Comment block //-------------- ``` ```js -// When [2, "always", { "line": "exceptions": ["-"] } }] +/* eslint spaced-comment: [2, "always", { "line": { "exceptions": ["-"] } }] */ + //-------------- // Comment block //-------------- ``` ```js -// When [2, "always", { "exceptions": ["-+"] }] +/* eslint spaced-comment: [2, "always", { "exceptions": ["-+"] }] */ + //-+-+-+-+-+-+-+ // Comment block //-+-+-+-+-+-+-+ -``` -```js -// When [2, "always", { "exceptions": ["-+"] }] /*-+-+-+-+-+-+-+*/ // Comment block /*-+-+-+-+-+-+-+*/ ``` ```js -// When [2, "always", { "block": { "exceptions": ["-+"] } }] +/* eslint spaced-comment: [2, "always", { "block": { "exceptions": ["-+"] } }] */ + /*-+-+-+-+-+-+-+*/ // Comment block /*-+-+-+-+-+-+-+*/ ``` ```js -// When [2, "always", { "exceptions": ["*"] }] +/* eslint spaced-comment: [2, "always", { "exceptions": ["*"] }] */ + /**************** * Comment block ****************/ ``` ```js -// When [2, "always", { "markers": ["/"] }] +/* eslint spaced-comment: [2, "always", { "markers": ["/"] }] */ + /// This is a comment with a marker ``` ```js -// When [2, "never", { "markers": ["!<"] }] +/*eslint spaced-comment: [2, "never", { "markers": ["!<"] }]*/ + //! count) { +if (5 > count) { /*error Expected literal to be on the right side of >.*/ // ... } -``` -```js -if (-1 < str.indexOf(substr)) { +if (-1 < str.indexOf(substr)) { /*error Expected literal to be on the right side of <.*/ // ... } -``` -```js -// When ["always"] -if (color == "blue") { +if (0 <= x && x < 1) { /*error Expected literal to be on the right side of <=.*/ // ... } ``` ```js -if (0 <= x && x < 1) { +/*eslint yoda: [2, "always"]*/ + +if (color == "blue") { /*error Expected literal to be on the left side of ==.*/ // ... } ``` + The following patterns are not considered warnings: ```js +/*eslint yoda: 2*/ + if (5 & value) { // ... } -``` -```js if (value === "red") { // ... } ``` ```js -// When ["always"] +/*eslint yoda: [2, "always"]*/ + if ("blue" == value) { // ... } -``` -```js -// When ["always"] if (-1 < str.indexOf(substr)) { // ... } @@ -117,24 +111,20 @@ The `onlyEquality` option is a superset of `exceptRange`, thus both options are With the `exceptRange` option enabled, the following patterns become valid: ```js +/*eslint yoda: [2, "never", { "exceptRange": true }]*/ + function isReddish(color) { return (color.hue < 60 || 300 < color.hue); } -``` -```js if (x < -1 || 1 < x) { // ... } -``` -```js if (count < 10 && (0 <= rand && rand < 1)) { // ... } -``` -```js function howLong(arr) { return (0 <= arr.length && arr.length < 10) ? "short" : "long"; } @@ -145,11 +135,11 @@ function howLong(arr) { Some developers might prefer to only enforce the rule for the equality operators `==` and `===`, and not showing any warnings for any code around other operators. With `onlyEquality` option, these patterns will not be considered warnings: ```js +/*eslint yoda: [2, "never", { "onlyEquality": true }]*/ + if (x < -1 || 9 < x) { } -``` -```js if (x !== 'foo' && 'bar' != x) { } ```