@tango-io/eslint-config-tango-node
v1.0.1
Published
ESLint guidelines package for NODE project by Tango.io
Downloads
530
Readme
tango node eslint config
Tango ESLint rules
Installation
You'll first need to install ESLint:
npm install eslint --save-dev
Next, install eslint-plugin-json-files:
npm i @tango-io/eslint-config-tango-node --save-dev
NOTE: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-json-files globally.
Usage
You'll see several dependencies were installed. Now, create (or update) a .eslintrc
file with the following content:
{
"extends": [
"@tango-io/tango-node"
]
}
Possible Problems Rules
for-direction
Examples of incorrect code for this rule:
for (var i = 0; i < 10; i--) {}
for (var i = 10; i >= 0; i++) {}
for (var i = 0; i > 10; i++) {}
Examples of correct code for this rule:
for (var i = 0; i < 10; i++) {}
getter-return
Examples of incorrect code for this rule:
p = {
get name() {
// no returns.
},
};
Object.defineProperty(p, 'age', {
get: function () {
// no returns.
},
});
class P {
get name() {
// no returns.
}
}
Examples of correct code for this rule:
p = {
get name() {
return 'nicholas';
},
};
Object.defineProperty(p, 'age', {
get: function () {
return 18;
},
});
class P {
get name() {
return 'nicholas';
}
}
no-await-in-loop
Examples of incorrect code for this rule:
async function foo(things) {
const results = [];
for (const thing of things) {
// Bad: each loop iteration is delayed until the entire asynchronous operation completes
results.push(await bar(thing));
}
return baz(results);
}
Examples of correct code for this rule:
async function foo(things) {
const results = [];
for (const thing of things) {
// Good: all asynchronous operations are immediately started.
results.push(bar(thing));
}
// Now that all the asynchronous operations are running, here we wait until they all complete.
return baz(await Promise.all(results));
}
no-compare-neg-zero
Examples of incorrect code for this rule:
if (x === -0) {
// doSomething()...
}
Examples of correct code for this rule:
if (x === 0) {
// doSomething()...
}
/* eslint no-compare-neg-zero: 'error' */
if (Object.is(x, -0)) {
// doSomething()...
}
no-cond-assign
Examples of incorrect code for this rule:
// Unintentional assignment
var x;
if ((x = 0)) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
'use strict';
do {
someNode.height = '100px';
} while ((someNode = someNode.parentNode));
}
Examples of correct code for this rule:
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
'use strict';
do {
someNode.height = '100px';
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
'use strict';
do {
someNode.height = '100px';
} while ((someNode = someNode.parentNode) !== null);
}
no-console
Examples of incorrect code for this rule:
console.log('Log a debug level message.');
console.log = foo();
Examples of correct code for this rule:
console.warn('Log a warn level message.');
console.error('Log an error level message.');
no-constant-condition
Examples of incorrect code for this rule:
if (false) {
doSomethingUnfinished();
}
if (void x) {
doSomethingUnfinished();
}
if ((x &&= false)) {
doSomethingNever();
}
if (class {}) {
doSomethingAlways();
}
if (new Boolean(x)) {
doSomethingAlways();
}
if ((x ||= true)) {
doSomethingAlways();
}
for (; -2; ) {
doSomethingForever();
}
while (typeof x) {
doSomethingForever();
}
do {
doSomethingForever();
} while ((x = -1));
var result = 0 ? a : b;
Examples of correct code for this rule:
if (x === 0) {
doSomething();
}
for (;;) {
doSomethingForever();
}
while (typeof x === 'undefined') {
doSomething();
}
do {
doSomething();
} while (x);
var result = x !== 0 ? a : b;
no-debugger
Examples of incorrect code for this rule:
function isTruthy(x) {
debugger;
return Boolean(x);
}
Examples of correct code for this rule:
function isTruthy(x) {
return Boolean(x); // set a breakpoint at this line
}
no-dupe-args
Examples of incorrect code for this rule:
function foo(a, b, a) {
console.log('value of the second a:', a);
}
var bar = function (a, b, a) {
console.log('value of the second a:', a);
};
Examples of correct code for this rule:
function foo(a, b, c) {
console.log(a, b, c);
}
var bar = function (a, b, c) {
console.log(a, b, c);
};
no-dupe-else-if
Examples of incorrect code for this rule:
if (isSomething(x)) {
foo();
} else if (isSomething(x)) {
bar();
}
if (a) {
foo();
} else if (b) {
bar();
} else if (c && d) {
baz();
} else if (c && d) {
quux();
} else {
quuux();
}
if (n === 1) {
foo();
} else if (n === 2) {
bar();
} else if (n === 3) {
baz();
} else if (n === 2) {
quux();
} else if (n === 5) {
quuux();
}
Examples of correct code for this rule:
if (isSomething(x)) {
foo();
} else if (isSomethingElse(x)) {
bar();
}
if (a) {
foo();
} else if (b) {
bar();
} else if (c && d) {
baz();
} else if (c && e) {
quux();
} else {
quuux();
}
if (n === 1) {
foo();
} else if (n === 2) {
bar();
} else if (n === 3) {
baz();
} else if (n === 4) {
quux();
} else if (n === 5) {
quuux();
}
no-dupe-keys
Examples of incorrect code for this rule:
var foo = {
bar: 'baz',
bar: 'qux',
};
var foo = {
bar: 'baz',
bar: 'qux',
};
var foo = {
0x1: 'baz',
1: 'qux',
};
Examples of correct code for this rule:
var foo = {
bar: 'baz',
quxx: 'qux',
};
no-duplicate-case
Examples of incorrect code for this rule:
switch (a) {
case '1':
break;
case '2':
break;
case '1': // duplicate test expression
break;
default:
break;
}
Examples of correct code for this rule:
switch (a) {
case '1':
break;
case '2':
break;
case '3':
break;
default:
break;
}
no-empty
Examples of incorrect code for this rule:
if (foo) {
}
while (foo) {}
switch (foo) {
}
try {
doSomething();
} catch (ex) {
} finally {
}
Examples of correct code for this rule:
if (foo) {
// empty
}
while (foo) {
/* empty */
}
try {
doSomething();
} catch (ex) {
// continue regardless of error
}
try {
doSomething();
} finally {
/* continue regardless of error */
}
no-ex-assign
Examples of incorrect code for this rule:
try {
// code
} catch (e) {
e = 10;
}
Examples of correct code for this rule:
try {
// code
} catch (e) {
var foo = 10;
}
no-extra-boolean-cast
Examples of incorrect code for this rule:
var foo = !!!bar;
var foo = !!bar ? baz : bat;
var foo = Boolean(!!bar);
var foo = new Boolean(!!bar);
if (!!foo) {
// ...
}
if (Boolean(foo)) {
// ...
}
while (!!foo) {
// ...
}
do {
// ...
} while (Boolean(foo));
for (; !!foo; ) {
// ...
}
Examples of correct code for this rule:
var foo = !!bar;
var foo = Boolean(bar);
function foo() {
return !!bar;
}
var foo = bar ? !!baz : !!bat;
no-extra-parens
Examples of incorrect code for this rule:
a = b * c;
a * b + c;
for (a in (b, c));
for (a in b);
for (a of b);
typeof a;
(function () {} ? a() : b());
class A {
[x] = 1;
}
class B {
x = y + z;
}
Examples of correct code for this rule:
(0).toString();
Object.prototype.toString.call();
({}.toString.call());
(function () {} ? a() : b());
/^a$/.test(x);
for (a of (b, c));
for (a of b);
for (a in (b, c));
for (a in b);
class A {
[x] = 1;
}
class B {
x = y + z;
}
no-extra-semi
Examples of incorrect code for this rule:
var x = 5;
function foo() {
// code
}
class C {
field;
method() {
// code
}
static {
// code
}
}
Examples of correct code for this rule:
var x = 5;
function foo() {
// code
}
class C {
field;
method() {
// code
}
static {
// code
}
}
no-func-assign
Examples of incorrect code for this rule:
function foo() {}
foo = bar;
function foo() {
foo = bar;
}
var a = function hello() {
hello = 123;
};
Examples of correct code for this rule:
var foo = function () {};
foo = bar;
function foo(foo) {
foo = bar; // `foo` is shadowed.
}
function foo() {
var foo = bar; // `foo` is shadowed.
}
no-import-assign
Examples of incorrect code for this rule:
import mod, { named } from './mod.mjs';
import * as mod_ns from './mod.mjs';
mod = 1; // ERROR: 'mod' is readonly.
named = 2; // ERROR: 'named' is readonly.
mod_ns.named = 3; // ERROR: The members of 'mod_ns' are readonly.
mod_ns = {}; // ERROR: 'mod_ns' is readonly.
// Can't extend 'mod_ns'
Object.assign(mod_ns, { foo: 'foo' }); // ERROR: The members of 'mod_ns' are readonly.
Examples of correct code for this rule:
import mod, { named } from './mod.mjs';
import * as mod_ns from './mod.mjs';
mod.prop = 1;
named.prop = 2;
mod_ns.named.prop = 3;
// Known Limitation
function test(obj) {
obj.named = 4; // Not errored because 'obj' is not namespace objects.
}
test(mod_ns); // Not errored because it doesn't know that 'test' updates the member of the argument.
no-inner-declarations
Examples of incorrect code for this rule:
if (test) {
function doSomething() {}
}
function doSomethingElse() {
if (test) {
function doAnotherThing() {}
}
}
if (foo) function f() {}
class C {
static {
if (test) {
function doSomething() {}
}
}
}
Examples of correct code for this rule:
function doSomething() {}
function doSomethingElse() {
function doAnotherThing() {}
}
class C {
static {
function doSomething() {}
}
}
if (test) {
asyncCall(id, function (err, data) {});
}
var fn;
if (test) {
fn = function fnExpression() {};
}
if (foo) var a;
no-irregular-whitespace
Examples of incorrect code for this rule:
function thing() /*<NBSP>*/{
return 'test';
}
function thing( /*<NBSP>*/){
return 'test';
}
function thing /*<NBSP>*/(){
return 'test';
}
function thing/*<MVS>*/(){
return 'test';
}
function thing() {
return 'test'; /*<ENSP>*/
}
function thing() {
return 'test'; /*<NBSP>*/
}
function thing() {
// Description <NBSP>: some descriptive text
}
/*
Description <NBSP>: some descriptive text
*/
function thing() {
return / <NBSP>regexp/;
}
/*eslint-env es6*/
function thing() {
return `template <NBSP>string`;
}
Examples of correct code for this rule:
function thing() {
return ' <NBSP>thing';
}
function thing() {
return '<ZWSP>thing';
}
function thing() {
return 'th <NBSP>ing';
}
no-loss-of-precision
Examples of incorrect code for this rule:
const x = 9007199254740993;
const x = 5123000000000000000000000000001;
const x = 1230000000000000000000000.0;
const x = 0.123;
const x = 0x20000000000001;
const x = 0x2_000000000_0001;
Examples of correct code for this rule:
const x = 12345;
const x = 123.456;
const x = 123e34;
const x = 12300000000000000000000000;
const x = 0x1fffffffffffff;
const x = 9007199254740991;
const x = 9007_1992547409_91;
no-misleading-character-class
Examples of incorrect code for this rule:
/^[Á]$/u
/^[❇️]$/u
/^[👶🏻]$/u
/^[🇯🇵]$/u
/^[👨👩👦]$/u
/^[👍]$/
Examples of correct code for this rule:
/^[abc]$/
/^[👍]$/u
no-obj-calls
Examples of incorrect code for this rule:
var math = Math();
var newMath = new Math();
var json = JSON();
var newJSON = new JSON();
var reflect = Reflect();
var newReflect = new Reflect();
var atomics = Atomics();
var newAtomics = new Atomics();
Examples of correct code for this rule:
function area(r) {
return Math.PI * r * r;
}
var object = JSON.parse('{}');
var value = Reflect.get({ x: 1, y: 2 }, 'x');
var first = Atomics.load(foo, 0);
no-promise-executor-return
Examples of incorrect code for this rule:
new Promise((resolve, reject) => {
if (someCondition) {
return defaultResult;
}
getSomething((err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
new Promise((resolve, reject) =>
getSomething((err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
})
);
new Promise(() => {
return 1;
});
Examples of correct code for this rule:
new Promise((resolve, reject) => {
if (someCondition) {
resolve(defaultResult);
return;
}
getSomething((err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
new Promise((resolve, reject) => {
getSomething((err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
Promise.resolve(1);
no-prototype-builtins
Examples of incorrect code for this rule:
var hasBarProperty = foo.hasOwnProperty('bar');
var isPrototypeOfBar = foo.isPrototypeOf(bar);
var barIsEnumerable = foo.propertyIsEnumerable('bar');
Examples of correct code for this rule:
var hasBarProperty = Object.prototype.hasOwnProperty.call(foo, 'bar');
var isPrototypeOfBar = Object.prototype.isPrototypeOf.call(foo, bar);
var barIsEnumerable = {}.propertyIsEnumerable.call(foo, 'bar');
no-regex-spaces
Examples of incorrect code for this rule:
var re = /foo bar/;
var re = new RegExp('foo bar');
Examples of correct code for this rule:
var re = /foo {3}bar/;
var re = new RegExp('foo {3}bar');
no-setter-return
Examples of incorrect code for this rule:
var foo = {
set a(value) {
this.val = value;
return value;
},
};
class Foo {
set a(value) {
this.val = value * 2;
return this.val;
}
}
const Bar = class {
static set a(value) {
if (value < 0) {
this.val = 0;
return 0;
}
this.val = value;
}
};
Object.defineProperty(foo, 'bar', {
set(value) {
if (value < 0) {
return false;
}
this.val = value;
},
});
Examples of correct code for this rule:
var foo = {
set a(value) {
this.val = value;
},
};
class Foo {
set a(value) {
this.val = value * 2;
}
}
const Bar = class {
static set a(value) {
if (value < 0) {
this.val = 0;
return;
}
this.val = value;
}
};
Object.defineProperty(foo, 'bar', {
set(value) {
if (value < 0) {
throw new Error('Negative value.');
}
this.val = value;
},
});
no-unexpected-multiline
Examples of incorrect code for this rule:
var foo = bar(1 || 2).baz();
var hello = 'world'[(1, 2, 3)].forEach(addNumber);
let x = (function () {})`hello`;
let x = function () {};
x`hello`;
let x = foo / regex / g.test(bar);
Examples of correct code for this rule:
var foo = bar;
(1 || 2).baz();
var foo = bar;
(1 || 2).baz();
var hello = 'world';
[1, 2, 3].forEach(addNumber);
var hello = 'world';
void [1, 2, 3].forEach(addNumber);
let x = function () {};
`hello`;
let tag = function () {};
tag`hello`;
no-unreachable
Examples of incorrect code for this rule:
function foo() {
return true;
console.log('done');
}
function bar() {
throw new Error('Oops!');
console.log('done');
}
while (value) {
break;
console.log('done');
}
throw new Error('Oops!');
console.log('done');
function baz() {
if (Math.random() < 0.5) {
return;
} else {
throw new Error();
}
console.log('done');
}
for (;;) {}
console.log('done');
Examples of correct code for this rule:
function foo() {
return bar();
function bar() {
return 1;
}
}
function bar() {
return x;
var x;
}
switch (foo) {
case 1:
break;
var x;
}
no-unreachable-loop
Examples of incorrect code for this rule:
while (foo) {
doSomething(foo);
foo = foo.parent;
break;
}
function verifyList(head) {
let item = head;
do {
if (verify(item)) {
return true;
} else {
return false;
}
} while (item);
}
function findSomething(arr) {
for (var i = 0; i < arr.length; i++) {
if (isSomething(arr[i])) {
return arr[i];
} else {
throw new Error('Doesn't exist.');
}
}
}
for (key in obj) {
if (key.startsWith('_')) {
break;
}
firstKey = key;
firstValue = obj[key];
break;
}
for (foo of bar) {
if (foo.id === id) {
doSomething(foo);
}
break;
}
Examples of correct code for this rule:
while (foo) {
doSomething(foo);
foo = foo.parent;
}
function verifyList(head) {
let item = head;
do {
if (verify(item)) {
item = item.next;
} else {
return false;
}
} while (item);
return true;
}
function findSomething(arr) {
for (var i = 0; i < arr.length; i++) {
if (isSomething(arr[i])) {
return arr[i];
}
}
throw new Error('Doesn't exist.');
}
for (key in obj) {
if (key.startsWith('_')) {
continue;
}
firstKey = key;
firstValue = obj[key];
break;
}
for (foo of bar) {
if (foo.id === id) {
doSomething(foo);
break;
}
}
no-unsafe-finally
Examples of incorrect code for this rule:
let foo = function () {
try {
return 1;
} catch (err) {
return 2;
} finally {
return 3;
}
};
Examples of correct code for this rule:
let foo = function () {
try {
return 1;
} catch (err) {
return 2;
} finally {
console.log('hola!');
}
};
no-unsafe-negation
Examples of incorrect code for this rule:
if (!key in object) {
// operator precedence makes it equivalent to (!key) in object
// and type conversion makes it equivalent to (key ? 'false' : 'true') in object
}
if (!obj instanceof Ctor) {
// operator precedence makes it equivalent to (!obj) instanceof Ctor
// and it equivalent to always false since boolean values are not objects.
}
Examples of correct code for this rule:
if (!(key in object)) {
// key is not in object
}
if (!(obj instanceof Ctor)) {
// obj is not an instance of Ctor
}
no-unsafe-optional-chaining
Examples of incorrect code for this rule:
(obj?.foo)();
(obj?.foo).bar;
(foo?.()).bar;
(foo?.()).bar();
(obj?.foo ?? obj?.bar)();
(foo || obj?.foo)();
(obj?.foo && foo)();
(foo ? obj?.foo : bar)();
(foo, obj?.bar).baz;
obj?.foo`template`;
new (obj?.foo)();
[...obj?.foo];
bar(...obj?.foo);
1 in obj?.foo;
bar instanceof obj?.foo;
for (bar of obj?.foo);
const { bar } = obj?.foo;
[{ bar } = obj?.foo] = [];
with (obj?.foo);
class A extends obj?.foo {}
var a = class A extends obj?.foo {};
async function foo() {
const { bar } = await obj?.foo;
(await obj?.foo)();
(await obj?.foo).bar;
}
Examples of correct code for this rule:
obj?.foo?.();
obj?.foo();
(obj?.foo ?? bar)();
obj?.foo.bar;
obj.foo?.bar;
foo?.()?.bar;
(obj?.foo ?? bar)`template`;
new (obj?.foo ?? bar)();
var baz = { ...obj?.foo };
const { bar } = obj?.foo || baz;
async function foo() {
const { bar } = (await obj?.foo) || baz;
(await obj?.foo)?.();
(await obj?.foo)?.bar;
}
require-atomic-updates
Examples of incorrect code for this rule:
let result;
async function foo() {
result += await something;
}
async function bar() {
result = result + (await something);
}
async function baz() {
result = result + doSomething(await somethingElse);
}
async function qux() {
if (!result) {
result = await initialize();
}
}
function* generator() {
result += yield;
}
Examples of correct code for this rule:
let result;
async function foobar() {
result = (await something) + result;
}
async function baz() {
const tmp = doSomething(await somethingElse);
result += tmp;
}
async function qux() {
if (!result) {
const tmp = await initialize();
if (!result) {
result = tmp;
}
}
}
async function quux() {
let localVariable = 0;
localVariable += await something;
}
function* generator() {
result = (yield) + result;
}
use-isnan
Examples of incorrect code for this rule:
if (foo == NaN) {
// ...
}
if (foo != NaN) {
// ...
}
if (foo == Number.NaN) {
// ...
}
if (foo != Number.NaN) {
// ...
}
Examples of correct code for this rule:
if (isNaN(foo)) {
// ...
}
if (!isNaN(foo)) {
// ...
}
valid-typeof
examples of incorrect code for this rule:
typeof foo === 'strnig';
typeof foo == 'undefimed';
typeof bar != 'nunber';
typeof bar !== 'fucntion';
examples of correct code for this rule:
typeof foo === 'string';
typeof bar == 'undefined';
typeof foo === baz;
typeof bar === typeof qux;
Best Practices Rules
block-scoped-var
examples of incorrect code for this rule:
/*eslint block-scoped-var: 'error'*/
function doIf() {
if (true) {
var build = true;
}
console.log(build);
}
function doIfElse() {
if (true) {
var build = true;
} else {
var build = false;
}
}
function doTryCatch() {
try {
var build = 1;
} catch (e) {
var f = build;
}
}
function doFor() {
for (var x = 1; x < 10; x++) {
var y = f(x);
}
console.log(y);
}
class C {
static {
if (something) {
var build = true;
}
build = false;
}
}
examples of correct code for this rule:
/*eslint block-scoped-var: 'error'*/
function doIf() {
var build;
if (true) {
build = true;
}
console.log(build);
}
function doIfElse() {
var build;
if (true) {
build = true;
} else {
build = false;
}
}
function doTryCatch() {
var build;
var f;
try {
build = 1;
} catch (e) {
f = build;
}
}
function doFor() {
for (var x = 1; x < 10; x++) {
var y = f(x);
console.log(y);
}
}
class C {
static {
var build = false;
if (something) {
build = true;
}
}
}
complexity
examples of incorrect code for this rule:
/*eslint complexity: ['error', 2]*/
function a(x) {
if (true) {
return x;
} else if (false) {
return x + 1;
} else {
return 4; // 3rd path
}
}
function b() {
foo ||= 1;
bar &&= 1;
}
examples of correct code for this rule:
/*eslint complexity: ['error', 2]*/
function a(x) {
if (true) {
return x;
} else {
return 4;
}
}
function b() {
foo ||= 1;
}
consistent-return
examples of incorrect code for this rule:
/*eslint consistent-return: 'error'*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomething(condition) {
if (condition) {
return true;
}
}
examples of correct code for this rule:
/*eslint consistent-return: 'error'*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
function Foo() {
if (!(this instanceof Foo)) {
return new Foo();
}
this.a = 0;
}
curly
examples of incorrect code for this rule:
/*eslint curly: 'error'*/
if (foo) foo++;
while (bar) baz();
if (foo) {
baz();
} else qux();
examples of correct code for this rule:
/*eslint curly: 'error'*/
if (foo) {
foo++;
}
while (bar) {
baz();
}
if (foo) {
baz();
} else {
qux();
}
default-case
examples of incorrect code for this rule:
/*eslint default-case: 'error'*/
switch (a) {
case 1:
/* code */
break;
}
examples of correct code for this rule:
/*eslint default-case: 'error'*/
switch (a) {
case 1:
/* code */
break;
default:
/* code */
break;
}
switch (a) {
case 1:
/* code */
break;
// no default
}
switch (a) {
case 1:
/* code */
break;
// No Default
}
default-case-last
examples of incorrect code for this rule:
/*eslint default-case-last: 'error'*/
switch (foo) {
default:
bar();
break;
case 'a':
baz();
break;
}
switch (foo) {
case 1:
bar();
break;
default:
baz();
break;
case 2:
quux();
break;
}
switch (foo) {
case 'x':
bar();
break;
default:
case 'y':
baz();
break;
}
switch (foo) {
default:
break;
case -1:
bar();
break;
}
switch (foo) {
default:
doSomethingIfNotZero();
case 0:
doSomethingAnyway();
}
examples of correct code for this rule:
/*eslint default-case-last: 'error'*/
switch (foo) {
case 'a':
baz();
break;
default:
bar();
break;
}
switch (foo) {
case 1:
bar();
break;
case 2:
quux();
break;
default:
baz();
break;
}
switch (foo) {
case 'x':
bar();
break;
case 'y':
default:
baz();
break;
}
switch (foo) {
case -1:
bar();
break;
}
if (foo !== 0) {
doSomethingIfNotZero();
}
doSomethingAnyway();
default-param-last
examples of incorrect code for this rule:
/* eslint default-param-last: ['error'] */
function f(a = 0, b) {}
function f(a, b = 0, c) {}
examples of correct code for this rule:
/* eslint default-param-last: ['error'] */
function f(a, b = 0) {}
dot-notation
examples of incorrect code for this rule:
/*eslint dot-notation: 'error'*/
var x = foo['bar'];
examples of correct code for this rule:
/*eslint dot-notation: 'error'*/
var x = foo.bar;
var x = foo[bar]; // Property name is a variable, square-bracket notation required
eqeqeq
examples of incorrect code for this rule:
/*eslint eqeqeq: 'error'*/
if (x == 42) {
}
if ('' == text) {
}
if (obj.getStuff() != undefined) {
}
examples of correct code for this rule:
/*eslint eqeqeq: ['error', 'always']*/
a == b;
foo == true;
bananas != 1;
value == undefined;
typeof foo == 'undefined';
'hello' != 'world';
0 == 0;
true == true;
foo == null;
guard-for-in
examples of incorrect code for this rule:
/*eslint guard-for-in: 'error'*/
for (key in foo) {
doSomething(key);
}
examples of correct code for this rule:
/*eslint guard-for-in: 'error'*/
for (key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
for (key in foo) {
if ({}.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
max-classes-per-file
examples of incorrect code for this rule:
/*eslint max-classes-per-file: 'error'*/
class Foo {}
class Bar {}
examples of correct code for this rule:
/*eslint max-classes-per-file: 'error'*/
class Foo {}
no-alert
examples of incorrect code for this rule:
/*eslint no-alert: 'error'*/
alert('here!');
confirm('Are you sure?');
prompt('What's your name?', 'John Doe');
examples of correct code for this rule:
/*eslint no-alert: 'error'*/
customAlert('Something happened!');
customConfirm('Are you sure?');
customPrompt('Who are you?');
function foo() {
var alert = myCustomLib.customAlert;
alert();
}
no-caller
examples of incorrect code for this rule:
/*eslint no-caller: 'error'*/
function foo(n) {
if (n <= 0) {
return;
}
arguments.callee(n - 1);
}
[1, 2, 3, 4, 5].map(function (n) {
return !(n > 1) ? 1 : arguments.callee(n - 1) * n;
});
examples of correct code for this rule:
/*eslint no-caller: 'error'*/
function foo(n) {
if (n <= 0) {
return;
}
foo(n - 1);
}
[1, 2, 3, 4, 5].map(function factorial(n) {
return !(n > 1) ? 1 : factorial(n - 1) * n;
});
no-constructor-return
examples of incorrect code for this rule:
/*eslint no-constructor-return: 'error'*/
class A {
constructor(a) {
this.a = a;
return a;
}
}
class B {
constructor(f) {
if (!f) {
return 'falsy';
}
}
}
examples of correct code for this rule:
/*eslint no-constructor-return: 'error'*/
class C {
constructor(c) {
this.c = c;
}
}
class D {
constructor(f) {
if (!f) {
return; // Flow control.
}
f();
}
}
no-else-return
examples of incorrect code for this rule:
/*eslint no-else-return: 'error'*/
function foo() {
if (x) {
return y;
} else {
return z;
}
}
function foo() {
if (x) {
return y;
} else if (z) {
return w;
} else {
return t;
}
}
function foo() {
if (x) {
return y;
} else {
var t = 'foo';
}
return t;
}
function foo() {
if (error) {
return 'It failed';
} else {
if (loading) {
return 'It's still loading';
}
}
}
// Two warnings for nested occurrences
function foo() {
if (x) {
if (y) {
return y;
} else {
return x;
}
} else {
return z;
}
}
examples of correct code for this rule:
/*eslint no-else-return: 'error'*/
function foo() {
if (x) {
return y;
}
return z;
}
function foo() {
if (x) {
return y;
} else if (z) {
var t = 'foo';
} else {
return w;
}
}
function foo() {
if (x) {
if (z) {
return y;
}
} else {
return z;
}
}
function foo() {
if (error) {
return 'It failed';
} else if (loading) {
return 'It's still loading';
}
}
no-empty-pattern
examples of incorrect code for this rule:
/*eslint no-empty-pattern: 'error'*/
var {} = foo;
var [] = foo;
var {
a: {},
} = foo;
var {
a: [],
} = foo;
function foo({}) {}
function foo([]) {}
function foo({ a: {} }) {}
function foo({ a: [] }) {}
examples of correct code for this rule:
/*eslint no-empty-pattern: 'error'*/
var { a = {} } = foo;
var { a = [] } = foo;
function foo({ a = {} }) {}
function foo({ a = [] }) {}
no-eq-null
examples of incorrect code for this rule:
/*eslint no-eq-null: 'error'*/
if (foo == null) {
bar();
}
while (qux != null) {
baz();
}
examples of correct code for this rule:
/*eslint no-eq-null: 'error'*/
if (foo === null) {
bar();
}
while (qux !== null) {
baz();
}
no-eval
examples of incorrect code for this rule:
/*eslint no-eval: 'error'*/
var obj = { x: 'foo' },
key = 'x',
value = eval('obj.' + key);
(0, eval)('var a = 0');
var foo = eval;
foo('var a = 0');
// This `this` is the global object.
this.eval('var a = 0');
examples of correct code for this rule:
/*eslint no-eval: 'error'*/
/*eslint-env browser*/
window.eval('var a = 0');
no-extend-native
examples of incorrect code for this rule:
/*eslint no-extend-native: 'error'*/
Object.prototype.a = 'a';
Object.defineProperty(Array.prototype, 'times', { value: 999 });
examples of correct code for this rule:
/*eslint no-extend-native: ['error', { 'exceptions': ['Object'] }]*/
Object.prototype.a = 'a';
no-extra-bind
examples of incorrect code for this rule:
/*eslint no-extra-bind: 'error'*/
/*eslint-env es6*/
var x = function () {
foo();
}.bind(bar);
var x = (() => {
foo();
}).bind(bar);
var x = (() => {
this.foo();
}).bind(bar);
var x = function () {
(function () {
this.foo();
})();
}.bind(bar);
var x = function () {
function foo() {
this.bar();
}
}.bind(baz);
examples of correct code for this rule:
/*eslint no-extra-bind: 'error'*/
var x = function () {
this.foo();
}.bind(bar);
var x = function (a) {
return a + 1;
}.bind(foo, bar);
no-extra-label
examples of incorrect code for this rule:
/*eslint no-extra-label: 'error'*/
A: while (a) {
break A;
}
B: for (let i = 0; i < 10; ++i) {
break B;
}
C: switch (a) {
case 0:
break C;
}
examples of correct code for this rule:
/*eslint no-extra-label: 'error'*/
while (a) {
break;
}
for (let i = 0; i < 10; ++i) {
break;
}
switch (a) {
case 0:
break;
}
A: {
break A;
}
B: while (a) {
while (b) {
break B;
}
}
C: switch (a) {
case 0:
while (b) {
break C;
}
break;
}
no-fallthrough
examples of incorrect code for this rule:
/*eslint no-fallthrough: 'error'*/
switch (foo) {
case 1:
doSomething();
case 2:
doSomething();
}
examples of correct code for this rule:
/*eslint no-fallthrough: 'error'*/
switch (foo) {
case 1:
doSomething();
break;
case 2:
doSomething();
}
function bar(foo) {
switch (foo) {
case 1:
doSomething();
return;
case 2:
doSomething();
}
}
switch (foo) {
case 1:
doSomething();
throw new Error('Boo!');
case 2:
doSomething();
}
switch (foo) {
case 1:
case 2:
doSomething();
}
switch (foo) {
case 1:
doSomething();
// falls through
case 2:
doSomething();
}
switch (foo) {
case 1: {
doSomething();
// falls through
}
case 2: {
doSomethingElse();
}
}
no-floating-decimal
examples of incorrect code for this rule:
/*eslint no-floating-decimal: 'error'*/
var num = 0.5;
var num = 2;
var num = -0.7;
examples of correct code for this rule:
/*eslint no-floating-decimal: 'error'*/
var num = 0.5;
var num = 2.0;
var num = -0.7;
no-global-assign
examples of incorrect code for this rule:
/*eslint no-global-assign: 'error'*/
Object = null;
undefined = 1;
examples of correct code for this rule:
/*eslint no-global-assign: 'error'*/
/*eslint-env browser*/
window = {};
length = 1;
top = 1;
no-implicit-coercion
examples of incorrect code for this rule:
/*eslint no-implicit-coercion: 'error'*/
var b = !!foo;
var b = ~foo.indexOf('.');
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.
examples of correct code for this rule:
/*eslint no-implicit-coercion: 'error'*/
var b = Boolean(foo);
var b = foo.indexOf('.') !== -1;
var n = ~foo; // This is a just bitwise not.
no-implicit-globals:
no-implied-eval
examples of incorrect code for this rule:
/*eslint no-implied-eval: 'error'*/
setTimeout('alert('Hi!');', 100);
setInterval('alert('Hi!');', 100);
execScript('alert('Hi!')');
window.setTimeout('count = 5', 10);
window.setInterval('foo = bar', 10);
examples of correct code for this rule:
/*eslint no-implied-eval: 'error'*/
setTimeout(function () {
alert('Hi!');
}, 100);
setInterval(function () {
alert('Hi!');
}, 100);
no-invalid-this
examples of incorrect code for this rule:
/*eslint no-invalid-this: 'error'*/
/*eslint-env es6*/
'use strict';
this.a = 0;
baz(() => this);
(function () {
this.a = 0;
baz(() => this);
})();
function foo() {
this.a = 0;
baz(() => this);
}
var foo = function () {
this.a = 0;
baz(() => this);
};
foo(function () {
this.a = 0;
baz(() => this);
});
obj.foo = () => {
// `this` of arrow functions is the outer scope's.
this.a = 0;
};
var obj = {
aaa: function () {
return function foo() {
// There is in a method `aaa`, but `foo` is not a method.
this.a = 0;
baz(() => this);
};
},
};
foo.forEach(function () {
this.a = 0;
baz(() => this);
});
examples of correct code for this rule:
/*eslint no-invalid-this: 'error'*/
/*eslint-env es6*/
'use strict';
function Foo() {
// OK, this is in a legacy style constructor.
this.a = 0;
baz(() => this);
}
class Foo {
constructor() {
// OK, this is in a constructor.
this.a = 0;
baz(() => this);
}
}
var obj = {
foo: function foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
},
};
var obj = {
foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
},
};
var obj = {
get foo() {
// OK, this is in a method (this function is on object literal).
return this.a;
},
};
var obj = Object.create(null, {
foo: {
value: function foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
},
},
});
Object.defineProperty(obj, 'foo', {
value: function foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
},
});
Object.defineProperties(obj, {
foo: {
value: function foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
},
},
});
function Foo() {
this.foo = function foo() {
// OK, this is in a method (this function assigns to a property).
this.a = 0;
baz(() => this);
};
}
obj.foo = function foo() {
// OK, this is in a method (this function assigns to a property).
this.a = 0;
};
Foo.prototype.foo = function foo() {
// OK, this is in a method (this function assigns to a property).
this.a = 0;
};
class Foo {
// OK, this is in a class field initializer.
a = this.b;
// OK, static initializers also have valid this.
static a = this.b;
foo() {
// OK, this is in a method.
this.a = 0;
baz(() => this);
}
static foo() {
// OK, this is in a method (static methods also have valid this).
this.a = 0;
baz(() => this);
}
static {
// OK, static blocks also have valid this.
this.a = 0;
baz(() => this);
}
}
var foo = function foo() {
// OK, the `bind` method of this function is called directly.
this.a = 0;
}.bind(obj);
foo.forEach(function () {
// OK, `thisArg` of `.forEach()` is given.
this.a = 0;
baz(() => this);
}, thisArg);
/** @this Foo */
function foo() {
// OK, this function has a `@this` tag in its JSDoc comment.
this.a = 0;
}
no-empty-function
examples of incorrect code for this rule:
/*eslint no-empty-function: 'error'*/
/*eslint-env es6*/
function foo() {}
var foo = function () {};
var foo = () => {};
function* foo() {}
var foo = function* () {};
var obj = {
foo: function () {},
foo: function* () {},
foo() {},
*foo() {},
get foo() {},
set foo(value) {},
};
class A {
constructor() {}
foo() {}
*foo() {}
get foo() {}
set foo(value) {}
static foo() {}
static *foo() {}
static get foo() {}
static set foo(value) {}
}
examples of correct code for this rule:
/*eslint no-empty-function: 'error'*/
/*eslint-env es6*/
function foo() {
// do nothing.
}
var foo = function () {
// any clear comments.
};
var foo = () => {
bar();
};
function* foo() {
// do nothing.
}
var foo = function* () {
// do nothing.
};
var obj = {
foo: function () {
// do nothing.
},
foo: function* () {
// do nothing.
},
foo() {
// do nothing.
},
*foo() {
// do nothing.
},
get foo() {
// do nothing.
},
set foo(value) {
// do nothing.
},
};
class A {
constructor() {
// do nothing.
}
foo() {
// do nothing.
}
*foo() {
// do nothing.
}
get foo() {
// do nothing.
}
set foo(value) {
// do nothing.
}
static foo() {
// do nothing.
}
static *foo() {
// do nothing.
}
static get foo() {
// do nothing.
}
static set foo(value) {
// do nothing.
}
}
no-iterator
examples of incorrect code for this rule:
/*eslint no-iterator: 'error'*/
Foo.prototype.__iterator__ = function () {
return new FooIterator(this);
};
foo.__iterator__ = function () {};
foo['__iterator__'] = function () {};
examples of correct code for this rule:
/*eslint no-iterator: 'error'*/
var __iterator__ = foo; // Not using the `__iterator__` property.
no-lone-blocks
examples of incorrect code for this rule:
/*eslint no-lone-blocks: 'error'*/
{
}
if (foo) {
bar();
{
baz();
}
}
function bar() {
{
baz();
}
}
{
function foo() {}
}
{
aLabel: {
}
}
class C {
static {
{
foo();
}
}
}
examples of correct code for this rule:
/*eslint no-lone-blocks: 'error'*/
/*eslint-env es6*/
while (foo) {
bar();
}
if (foo) {
if (bar) {
baz();
}
}
function bar() {
baz();
}
{
let x = 1;
}
{
const y = 1;
}
{
class Foo {}
}
aLabel: {
}
class C {
static {
lbl: {
if (something) {
break lbl;
}
foo();
}
}
}
no-loop-func
examples of incorrect code for this rule:
/*eslint no-loop-func: 'error'*/
/*eslint-env es6*/
for (var i = 10; i; i--) {
(function () {
return i;
})();
}
while (i) {
var a = function () {
return i;
};
a();
}
do {
function a() {
return i;
}
a();
} while (i);
let foo = 0;
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
foo += 1;
}
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
}
foo = 100;
examples of correct code for this rule:
/*eslint no-loop-func: 'error'*/
/*eslint-env es6*/
var a = function () {};
for (var i = 10; i; i--) {
a();
}
for (var i = 10; i; i--) {
var a = function () {}; // OK, no references to variables in the outer scopes.
a();
}
for (let i = 10; i; i--) {
var a = function () {
return i;
}; // OK, all references are referring to block scoped variables in the loop.
a();
}
var foo = 100;
for (let i = 10; i; i--) {
var a = function () {
return foo;
}; // OK, all references are referring to never modified variables.
a();
}
//... no modifications of foo after this loop ...
no-magic-numbers
examples of incorrect code for this rule:
/*eslint no-magic-numbers: 'error'*/
var dutyFreePrice = 100,
finalPrice = dutyFreePrice + dutyFreePrice * 0.25;
examples of correct code for this rule:
/*eslint no-magic-numbers: 'error'*/
var data = ['foo', 'bar', 'baz'];
var dataLast = data[2];
no-multi-spaces
examples of incorrect code for this rule:
/*eslint no-multi-spaces: 'error'*/
var a = 1;
if (foo === 'bar') {
}
a << b;
var arr = [1, 2];
a ? b : c;
examples of correct code for this rule:
/*eslint no-multi-spaces: 'error'*/
var a = 1;
if (foo === 'bar') {
}
a << b;
var arr = [1, 2];
a ? b : c;
no-new
examples of incorrect code for this rule:
/*eslint no-new: 'error'*/
new Thing();
examples of correct code for this rule:
/*eslint no-new: 'error'*/
var thing = new Thing();
Thing();
no-new-func
examples of incorrect code for this rule:
/*eslint no-new-func: 'error'*/
var x = new Function('a', 'b', 'return a + b');
var x = Function('a', 'b', 'return a + b');
var x = Function.call(null, 'a', 'b', 'return a + b');
var x = Function.apply(null, ['a', 'b', 'return a + b']);
var x = Function.bind(null, 'a', 'b', 'return a + b')();
var f = Function.bind(null, 'a', 'b', 'return a + b'); // assuming that the result of Function.bind(...) will be eventually called.
examples of correct code for this rule:
/*eslint no-new-func: 'error'*/
var x = function (a, b) {
return a + b;
};
no-new-wrappers
examples of incorrect code for this rule:
/*eslint no-new-wrappers: 'error'*/
var stringObject = new String('Hello world');
var numberObject = new Number(33);
var booleanObject = new Boolean(false);
var stringObject = new String();
var numberObject = new Number();
var booleanObject = new Boolean();
examples of correct code for this rule:
/*eslint no-new-wrappers: 'error'*/
var text = String(someValue);
var num = Number(someValue);
var object = new MyString();
no-param-reassign
examples of incorrect code for this rule:
/*eslint no-param-reassign: 'error'*/
function foo(bar) {
bar = 13;
}
function foo(bar) {
bar++;
}
function foo(bar) {
for (bar in baz) {
}
}
function foo(bar) {
for (bar of baz) {
}
}
examples of correct code for this rule:
/*eslint no-param-reassign: 'error'*/
function foo(bar) {
var baz = bar;
}
no-proto
examples of incorrect code for this rule:
/*eslint no-proto: 'error'*/
var a = obj.__proto__;
var a = obj['__proto__'];
obj.__proto__ = b;
obj['__proto__'] = b;
examples of correct code for this rule:
/*eslint no-proto: 'error'*/
var a = Object.getPrototypeOf(obj);
Object.setPrototypeOf(obj, b);
var c = { __proto__: a };
no-redeclare
examples of incorrect code for this rule:
/*eslint no-redeclare: 'error'*/
var a = 3;
var a = 10;
class C {
foo() {
var b = 3;
var b = 10;
}
static {
var c = 3;
var c = 10;
}
}
examples of correct code for this rule:
/*eslint no-redeclare: 'error'*/
var a = 3;
a = 10;
class C {
foo() {
var b = 3;
b = 10;
}
static {
var c = 3;
c = 10;
}
}
no-return-assign
examples of incorrect code for this rule:
/*eslint no-return-assign: 'error'*/
function doSomething() {
return (foo = bar + 2);
}
function doSomething() {
return (foo += 2);
}
const foo = (a, b) => (a = b);
const bar = (a, b, c) => ((a = b), c == b);
function doSomething() {
return (foo = bar && foo > 0);
}
examples of correct code for this rule:
/*eslint no-return-assign: 'error'*/
function doSomething() {
return foo == bar + 2;
}
function doSomething() {
return foo === bar + 2;
}
function doSomething() {
return (foo = bar + 2);
}
const foo = (a, b) => (a = b);
const bar = (a, b, c) => ((a = b), c == b);
function doSomething() {
return (foo = bar) && foo > 0;
}
no-return-await
examples of incorrect code for this rule:
/*eslint no-return-await: 'error'*/
async function foo() {
return await bar();
}
examples of correct code for this rule:
/*eslint no-return-await: 'error'*/
async function foo() {
return bar();
}
async function foo() {
await bar();
return;
}
// This is essentially the same as `return await bar();`, but the rule checks only `await` in `return` statements
async function foo() {
const x = await bar();
return x;
}
// In this example the `await` is necessary to be able to catch errors thrown from `bar()`
async function foo() {
try {
return await bar();
} catch (error) {}
}
no-self-assign
examples of incorrect code for this rule:
/*eslint no-self-assign: 'error'*/
foo = foo;
[a, b] = [a, b];
[a, ...b] = [x, ...b];
({ a, b } = { a, x });
foo &&= foo;
foo ||= foo;
foo ??= foo;
examples of correct code for this rule:
/*eslint no-self-assign: 'error'*/
foo = bar;
[a, b] = [b, a];
// This pattern is warned by the `no-use-before-define` rule.
let foo = foo;
// The default values have an effect.
[foo = 1] = [foo];
// non-self-assignments with properties.
obj.a = obj.b;
obj.a.b = obj.c.b;
obj.a.b = obj.a.c;
obj[a] = obj['a'];
// This ignores if there is a function call.
obj.a().b = obj.a().b;
a().b = a().b;
// `&=` and `|=` have an effect on non-integers.
foo &= foo;
foo |= foo;
// Known limitation: this does not support computed properties except single literal or single identifier.
obj[a + b] = obj[a + b];
obj['a' + 'b'] = obj['a' + 'b'];
no-self-compare
examples of incorrect code for this rule:
/*eslint no-self-compare: 'error'*/
var x = 10;
if (x === x) {
x = 20;
}
no-throw-literal
examples of incorrect code for this rule:
/*eslint no-throw-literal: 'error'*/
/*eslint-env es6*/
throw 'error';
throw 0;
throw undefined;
throw null;
var err = new Error();
throw 'an ' + err;
// err is recast to a string literal
var err = new Error();
throw `${err}`;
examples of correct code for this rule:
/*eslint no-throw-literal: 'error'*/
throw new Error();
throw new Error('error');
var e = new Error('error');
throw e;
try {
throw new Error('error');
} catch (e) {
throw e;
}
no-unused-expressions:
no-useless-call
examples of incorrect code for this rule:
/*eslint no-useless-call: 'error'*/
// These 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]);
// These are same as `obj.foo(1, 2, 3);`
obj.foo.call(obj, 1, 2, 3);
obj.foo.apply(obj, [1, 2, 3]);
examples of correct code for this rule:
/*eslint no-useless-call: 'error'*/
// The `this` binding is different.
foo.call(obj, 1, 2, 3);
foo.apply(obj, [1, 2, 3]);
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]);
// The argument list is variadic.
// Those are warned by the `prefer-spread` rule.
foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
no-useless-concat
examples of incorrect code for this rule:
/*eslint no-useless-concat: 'error'*/
/*eslint-env es6*/
var a = `some` + `string`;
// these are the same as '10'
var a = '1' + '0';
var a = '1' + `0`;
var a = `1` + '0';
var a = `1` + `0`;
examples of correct code for this rule:
/*eslint no-useless-concat: 'error'*/
// when a non string is included
var c = a + b;
var c = '1' + a;
var a = 1 + '1';
var c = 1 - 2;
// when the string concatenation is multiline
var c = 'foo' + 'bar';
no-useless-return
examples of incorrect code for this rule:
/* eslint no-useless-return: 'error' */
function foo() {
return;
}
function foo() {
doSomething();
return;
}
function foo() {
if (condition) {
bar();
return;
} else {
baz();
}
}
function foo() {
switch (bar) {
case 1:
doSomething();
default:
doSomethingElse();
return;
}
}
examples of correct code for this rule:
/* eslint no-useless-return: 'error' */
function foo() {
return 5;
}
function foo() {
return doSomething();
}
function foo() {
if (condition) {
bar();
return;
} else {
baz();
}
qux();
}
function foo() {
switch (bar) {
case 1:
doSomething();
return;
default:
doSomethingElse();
}
}
function foo() {
for (const foo of bar) {
return;
}
}
no-void
examples of incorrect code for this rule:
/*eslint no-void: 'error'*/
void foo;
void someFunction();
var foo = void bar();
function baz() {
return void 0;
}
examples of correct code for this rule:
/*eslint no-void: ['error', { 'allowAsStatement': true }]*/
var foo = void bar();
function baz() {
return void 0;
}
no-with
examples of incorrect code for this rule:
/*eslint no-with: 'error'*/
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
examples of correct code for this rule:
/*eslint no-with: 'error'*/
/*eslint-env es6*/
const r = ({ x, y }) => Math.sqrt(x * x + y * y);
wrap-iife
examples of incorrect code for this rule:
/*eslint wrap-iife: ['error', 'outside']*/
var x = (function () {
return { y: 1 };
})(); // unwrapped
var x = (function () {
return { y: 1 };
})(); // wrapped function expression
examples of correct code for this rule:
/*eslint wrap-iife: ['error', 'outside']*/
var x = (function () {
return { y: 1 };
})(); // wrapped call expression
require-await
examples of incorrect code for this rule:
/*eslint require-await: 'error'*/
async function foo() {
doSomething();
}
bar(async () => {
doSomething();
});
examples of correct code for this rule:
/*eslint require-await: 'error'*/
async function foo() {
await doSomething();
}
bar(async () => {
await doSomething();
});
function foo() {
doSomething();
}
bar(() => {
doSomething();
});
// Allow empty functions.
async function noop() {}
yoda
examples of incorrect code for this rule:
/*eslint yoda: 'error'*/
if ('red' === color) {
// ...
}
if (`red` === color) {
// ...
}
if (`red` === `${color}`) {
// ...
}
if (true == flag) {
// ...
}
if (5 > count) {
// ...
}
if (-1 < str.indexOf(substr)) {
// ...
}
if (0 <= x && x < 1) {
// ...
}
examples of correct code for this rule:
/*eslint yoda: 'error'*/
if (5 & value) {
// ...
}
if (value === 'red') {
// ...
}
if (value === `red`) {
// ...
}
if (`${value}` === `red`) {
}
Variables Rules
no-delete-var
examples of incorrect code for this rule:
/*eslint no-delete-var: 'error'*/
var x;
delete x;
no-shadow
examples of incorrect code for this rule:
/*eslint no-shadow: 'error'*/
/*eslint-env es6*/
var a = 3;
function b() {
var a = 10;
}
var b = function () {
var a = 10;
};
function b(a) {
a = 10;
}
b(a);
if (true) {
let a = 5;
}
examples of correct code for this rule:
{
'no-shadow': ['error', { 'builtinGlobals': false, 'hoist': 'functions', 'allow': [] }]
}
no-unused-vars
examples of incorrect code for this rule:
/*eslint no-unused-vars: 'error'*/
/*global some_unused_var*/
// It checks variables you have defined as global
some_unused_var = 42;
var x;
// Write-only variables are not considered as used.
var y = 10;
y = 5;
// A read for a modification of itself is not considered as used.
var z = 0;
z = z + 1;
// By default, unused arguments cause warnings.
(function (foo) {
return 5;
})();
// Unused recursive functions also cause warnings.
function fact(n) {
if (n < 2) return 1;
return n * fact(n - 1);
}
// When a function definition destructures an array, unused entries from the array also cause warnings.
function getY([x, y]) {
return y;
}
examples of correct code for this rule:
/*eslint no-unused-vars: 'error'*/
var x = 10;
alert(x);
// foo is considered used here
myFunc(
function foo() {
// ...
}.bind(this)
);
(function (foo) {
return foo;
})();
var myFunc;
myFunc = setTimeout(function () {
// myFunc is considered used
myFunc();
}, 50);
// Only the second argument from the destructured array is used.
function getY([, y]) {
return y;
}
no-use-before-define
examples of incorrect code for this rule:
/*eslint no-use-before-define: 'error'*/
alert(a);
var a = 10;
f();
function f() {}
function g() {
return b;
}
var b = 1;
{
alert(c);
let c = 1;
}
{
class C extends C {}
}
{
class C {
static x = 'foo';
[C.x]() {}
}
}
{
const C = class {
static x = C;
};
}
{
const C = class {
static {
C.x = 'foo';
}
};
}
examples of correct code for this rule:
/*eslint no-use-before-define: 'error'*/
var a;
a = 10;
alert(a);
function f() {}
f(1);
var b = 1;
function g() {
return b;
}
{
let c;
c++;
}
{
class C {
static x = C;
}
}
{
const C = class C {
static x = C;
};
}
{
const C = class {
x = C;
};
}
{
const C = class C {
static {
C.x = 'foo';
}
};
}
Stylistic Issues Rules
array-bracket-newline
examples of incorrect code for this rule:
/*eslint array-bracket-newline: ['error', 'always']*/
var a = [];
var b = [1];
var c = [1, 2];
var d = [1, 2];
var e = [
function foo() {
dosomething();
},
];
examples of correct code for this rule:
/*eslint array-bracket-newline: ['error', 'always']*/
var a = [];
var b = [1];
var c = [1, 2];
var d = [1, 2];
var e = [
function foo() {
dosomething();
},
];
array-bracket-spacing
examples of incorrect code for this rule:
/*eslint array-bracket-spacing: ['error', 'never']*/
/*eslint-env es6*/
var arr = ['foo', 'bar'];
var arr = ['foo', 'bar'];
var arr = [['foo'], 'bar'];
var arr = [['foo'], 'bar'];
var arr = ['foo', 'bar'];
var [x, y] = z;
var [x, y] = z;
var [x, ...y] = z;
var [, , x] = z;
examples of correct code for this rule:
/*eslint array-bracket-spacing: ['error', 'never']*/
/*eslint-env es6*/
var arr = [];
var arr = ['foo', 'bar', 'baz'];
var arr = [['foo'], 'bar', 'baz'];
var arr = ['foo', 'bar', 'baz'];
var arr = ['foo', 'bar'];
var arr = ['foo', 'bar'];
var [x, y] = z;
var [x, y] = z;
var [x, ...y] = z;
var [, , x] = z;
array-element-newline
examples of incorrect code for this rule:
{
'array-element-newline': ['error', {
'ArrayExpression': 'consistent',
'ArrayPattern': { 'minItems': 3 },
}]
}
examples of correct code for this rule:
/*eslint array-element-newline: ['error', 'always']*/
var c = [1, 2];
var d = [1, 2, 3];
var e = [1, 2, 3];
var f = [1, 2, 3];
var g = [
function foo() {
dosomething();
},
function bar() {
dosomething();
},
];
block-spacing
examples of incorrect code for this rule:
/*eslint block-spacing: 'error'*/
function foo() {
return true;
}
if (foo) {
bar = 0;
}
function baz() {
let i = 0;
return i;
}
class C {
static {
this.bar = 0;
}
}
examples of correct code for this rule:
/*eslint block-spacing: 'error'*/
function foo() {
return true;
}
if (foo) {
bar = 0;
}
class C {
static {
this.bar = 0;
}
}
brace-style
examples of incorrect code for this rule:
/*eslint brace-style: 'error'*/
function foo() {
return true;
}
if (foo) {
bar();
}
try {
somethingRisky();
} catch (e) {
handleError();
}
if (foo) {
bar();
} else {
baz();
}
class C {
static {
foo();
}
}
examples of correct code for this rule:
/*eslint brace-style: 'error'*/
function foo() {
return true;
}
if (foo) {
bar();
}
if (foo) {
bar();
} else {
baz();
}
try {
somethingRisky();
} catch (e) {
handleError();
}
class C {
static {
foo();
}
}
// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
camelcase
examples of incorrect code for this rule:
/*eslint camelcase: 'error'*/
import { no_camelcased } from 'external-module';
var my_favorite_color = '#112C85';
function do_something() {
// ...
}
obj.do_something = function () {
// ...
};
function foo({ no_camelcased }) {
// ...
}
function foo({ isCamelcased: no_camelcased }) {
// ...
}
function foo({ no_camelcased = 'default value' }) {
// ...
}
var obj = {
my_pref: 1,
};
var { category_id = 1 } = query;
var { foo: no_camelcased } = bar;
var { foo: bar_baz = 1 } = quz;
examples of correct code for this rule:
/*eslint camelcase: 'error'*/
import { no_camelcased as camelCased } from 'external-module';
var myFavoriteColor = '#112C85';
var _myFavoriteColor = '#112C85';
var myFavoriteColor_ = '#112C85';
var MY_FAVORITE_COLOR = '#112C85';
var foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };
obj.do_something();
do_something();
new do_something();
var { category_id: category } = query;
function foo({ isCamelCased }) {
// ...
}
function foo({ isCamelCased: isAlsoCamelCased }) {
// ...
}
function foo({ isCamelCased = 'default value' }) {
// ...
}
var { categoryId = 1 } = query;
var { foo: isCamelCased } = bar;
var { foo: isCamelCased = 1 } = quz;
comma-dangle
examples of incorrect code for this rule:
{
'comma-dangle': ['error', 'never'],
// or
'comma-dangle': ['error', {
'arrays': 'never',
'objects': 'never',
'imports': 'never',
'exports': 'never',
'functions': 'never'
}]
}
examples of correct code for this rule:
/*eslint comma-dangle: ['error', 'never']*/
var foo = {
bar: 'baz',
qux: 'quux',
};
var arr = [1, 2];
foo({
bar: 'baz',
qux: 'quux',
});
comma-style
examples of incorrect code for this rule:
/*eslint comma-style: ['error', 'last']*/
var foo = 1,
bar = 2;
var foo = 1,
bar = 2;
var foo = ['apples', 'oranges'];
function bar() {
return {
a: 1,
'b:': 2,
};
}
examples of correct code for this rule:
/*eslint comma-style: ['error', 'last']*/
var foo = 1,
bar = 2;
var foo = 1,
bar = 2;
var foo = ['apples', 'oranges'];
function bar() {
return {
a: 1,
'b:': 2,
};
}
computed-property-spacing
examples of incorrect code for this rule:
/*eslint computed-property-spacing: ['error', 'never']*/
/*eslint-env es6*/
obj[foo];
obj['foo'];
var x = { [b]: a };
obj[foo[bar]];
const { [a]: someProp } = obj;
({ [b]: anotherProp } = anotherObj);
examples of correct code for this rule:
/*eslint computed-property-spacing: ['error', 'never']*/
/*eslint-env es6*/
obj[foo];
obj['foo'];
var x = { [b]: a };
obj[foo[bar]];
const { [a]: someProp } = obj;
({ [b]: anotherProp } = anotherObj);
func-call-spacing
examples of incorrect code for this rule:
/*eslint func-call-spacing: ['error', 'never']*/
fn();
fn();
examples of correct code for this rule:
/*eslint func-call-spacing: ['error', 'never']*/
fn();
func-names
examples of incorrect code for this rule:
/*eslint func-names: ['error', 'always']*/
Foo.prototype.bar = function () {};
const cat = {
meow: function () {},
}(
(function () {
// ...
})()
);
export default function () {}
examples of correct code for this rule:
/*eslint func-names: ['error', 'always']*/
Foo.prototype.bar = function bar() {};
const cat = {
meow() {},
}(
(function bar() {
// ...
})()
);
export default function foo() {}
func-style
examples of incorrect code for this rule:
/*eslint func-style: ['error', 'expression']*/
function foo() {
// ...
}
examples of correct code for this rule:
/*eslint func-style: ['error', 'expression']*/
var foo = function () {
// ...
};
var foo = () => {};
// allowed as allowArrowFunctions : false is applied only for declaration
implicit-arrow-linebreak
examples of incorrect code for this rule:
/* eslint implicit-arrow-linebreak: ['error', 'beside'] */
(foo) => bar;
(foo) => bar;
(foo) => (bar) => baz;
(foo) => bar();
examples of correct code for this rule:
/* eslint implicit-arrow-linebreak: ['error', 'beside'] */
(foo) => bar;
(foo) => bar;
(foo) => (bar) => baz;
(foo) => bar();
// functions with block bodies allowed with this rule using any style
// to enforce a consistent location for this case, see the rule: `brace-style`
(foo) => {
return bar();
};
(foo) => {
return bar();
};
jsx-quotes
examples of incorrect code for this rule:
/*eslint jsx-quotes: ['error', 'prefer-double']*/
<a b='c' />
examples of correct code for this rule:
/*eslint jsx-quotes: ['error', 'prefer-double']*/
<a b='c' />
<a b=''' />
key-spacing
examples of incorrect code for this rule:
/*eslint key-spacing: ['error', { 'beforeColon': false }]*/
var obj = { foo: 42 };
examples of correct code for this rule:
/*eslint key-spacing: ['error', { 'beforeColon': false }]*/
var obj = { foo: 42 };
lines-between-class-members
examples of incorrect code for this rule:
/* eslint lines-between-class-members: ['error', 'always']*/
class MyClass {
x;
foo() {
//...
}
bar() {
//...
}
}
examples of correct code for this rule:
/* eslint lines-between-class-members: ['error', 'always']*/
class MyClass {
x;
foo() {
//...
}
bar() {
//...
}
}
linebreak-style
examples of incorrect code for this rule:
/*eslint linebreak-style: ['error', 'unix']*/
var a = 'a'; // \r\n
examples of correct code for this rule:
/*eslint linebreak-style: ['error', 'unix']*/
var a = 'a', // \n
b = 'b'; // \n
// \n
function foo(params) {
// \n
// do stuff \n
} // \n
indent
examples of incorrect code for this rule:
funct