require trailing commas in multiline object literals
✅ Enabled (error)
// Bad
/*
var bad = {
bar: 'baz',
qux: 'quux'
};
*/
// Good
var good = {
bar: 'baz',
qux: 'quux',
};
disallow assignment in conditional expressions
✅ Enabled (error)
// Bad
/*
var x;
if (x = 0) {
var b = 1;
}
*/
disallow use of console
✅ Enabled (error)
// Bad
/*
console.log('hello world');
*/
disallow use of constant expressions in conditions
✅ Enabled (error)
// Bad
/*
if (false) {
doSomethingUnfinished();
}
*/
disallow control characters in regular expressions
✅ Enabled (error)
// Bad
/*
var pattern1 = /\x1f/;
var pattern2 = new RegExp('\x1f');
*/
// Good
var pattern1 = /\x20/;
var pattern2 = new RegExp('\x20');
disallow use of debugger
✅ Enabled (error)
// Bad
/*
function isTruthy(x) {
debugger;
return Boolean(x);
}
*/
disallow duplicate arguments in functions
✅ Enabled (error)
// Bad
/*
function foo(a, b, a) {
console.log('value of the second a:', a);
}
*/
disallow duplicate keys when creating object literals
✅ Enabled (error)
// Bad
/*
var foo = {
bar: 'baz',
bar: 'qux'
};
*/
disallow a duplicate case label.
✅ Enabled (error)
// Bad
/*
var a = 1;
switch (a) {
case 1:
break;
case 2:
break;
case 1:
break;
default:
break;
}
*/
disallow the use of empty character classes in regular expressions
✅ Enabled (error)
// Bad
/*
(/^abc[]/).test('abcdefg');
'abcdefg'.match(/^abc[]/);
*/
// Good
(/^abc/).test('abcdefg');
'abcdefg'.match(/^abc/);
disallow empty statements
✅ Enabled (error)
// Bad
/*
var foo = true;
if (foo) {
}
*/
disallow assigning to the exception in a catch block
✅ Enabled (error)
// Bad
/*
try {
// code
} catch (e) {
e = 10;
}
*/
disallow double-negation boolean casts in a boolean context
❌ Disabled
disallow unnecessary parentheses
❌ Disabled
// Bad
var b = 1;
var c = 2;
var a = (b * c);
var d = (a * b) + c;
disallow unnecessary semicolons
✅ Enabled (error)
// Bad
/*
var x = 5;;
*/
disallow overwriting functions written as function declarations
✅ Enabled (error)
// Bad
/*
function foo() {}
foo = bar;
*/
disallow function or variable declarations in nested blocks
✅ Enabled (error)
// Bad
/*
if (test) {
function doSomethingElse() { }
}
*/
// Good
function doSomething() { }
disallow invalid regular expression strings in the RegExp constructor
✅ Enabled (error)
// Bad
/*
RegExp('[');
RegExp('.', 'z');
new RegExp('\\');
*/
disallow irregular whitespace outside of strings and comments
✅ Enabled (error)
disallow negation of the left operand of an in expression
✅ Enabled (error)
// Bad
/*
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
}
*/
disallow the use of object properties of the global object (Math and JSON) as functions
✅ Enabled (error)
// Bad
/*
var math = Math();
var json = JSON();
*/
disallow use of Object.prototypes builtins directly
✅ Enabled (error)
// Bad
/*
var hasBarProperty = foo.hasOwnProperty('bar');
var isPrototypeOfBar = foo.isPrototypeOf(bar);
var barIsEnumerable = foo.propertyIsEnumerable('bar');
*/
// Good
var hasBarProperty = {}.hasOwnProperty.call(foo, 'bar');
var isPrototypeOfBar = {}.isPrototypeOf.call(foo, bar);
var barIsEnumerable = {}.propertyIsEnumerable.call(foo, 'bar');
disallow multiple spaces in a regular expression literal
✅ Enabled (error)
// Bad
/*
var re = /foo bar/;
var re = new RegExp('foo bar');
*/
// Good
var re = /foo {3}bar/;
var re = new RegExp('foo {3}bar');
disallow sparse arrays
✅ Enabled (error)
// Bad
/*
var items = [, ];
var colors = ['red',, 'blue'];
*/
// Good
var items = [];
var items = new Array(23);
var colors = ['red', 'blue'];
Avoid code that looks like two expressions but is actually one
❌ Disabled
// Bad
let a = function () {}
`hello`
// Good
let b = function () {};
`hello`
disallow unreachable statements after a return, throw, continue, or break statement
✅ Enabled (error)
// Bad
/*
function foo() {
return true;
console.log('done');
}
*/
disallow return/throw/break/continue inside finally blocks
✅ Enabled (error)
// Bad
/*
let foo = function () {
try {
return 1;
} catch (err) {
return 2;
} finally {
return 3;
}
};
*/
disallow comparisons with the value NaN
✅ Enabled (error)
// Bad
/*
if (foo === NaN) {
// ...
}
*/
// Good
if (isNaN(foo)) {
// ...
}
ensure JSDoc comments are valid
✅ Enabled (error)
ensure that the results of typeof are compared against a valid string
✅ Enabled (error)
// Bad
/*
typeof foo === 'strnig';
typeof foo === 'undefimed';
typeof bar !== 'nunber';
typeof bar !== 'fucntion';
*/
// Good
typeof foo === 'string';
typeof foo === 'undefined';
typeof bar !== 'number';
typeof bar !== 'function';