Skip to content

Latest commit

 

History

History
800 lines (796 loc) · 14.3 KB

bestPractices.md

File metadata and controls

800 lines (796 loc) · 14.3 KB

Best Practices

Rule Description Snippet ESLint Default Link
accessor-pairs Enforces getter/setter pairs in objects
// Good
var o = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};
Off [Link](http://eslint.org/docs/rules/accessor-pairs)
block-scoped-var Treat `var` statements as if they were block scoped
function doSomething() {
    if (true) {
        var build = true;
    }
    console.log(build);
}
Off [Link](http://eslint.org/docs/rules/block-scoped-var)
complexity Specify the maximum cyclomatic complexity allowed in a program
// complexity: [1, 2]
function a(x) {
    if (true) {
        return x;
    } else if (false) {
        return x+1;
    } else {
        return 4; // 3rd path
    }
}
Allowed between 0 and 11 levels of complexity [Link](http://eslint.org/docs/rules/complexity)
consistent-return Require `return` statements to either always or never specify values
function doSomething(condition) {
    if (condition) {
        return true;
    } else {
        return;
    }
}
Off [Link](http://eslint.org/docs/rules/consistent-return)
curly Specific curly brace conventions for all control statements
// Bad
if (foo) return;
// Good
if (foo) {
    return;
}
Off [Link](http://eslint.org/docs/rules/curly)
default-case Require `default` case in `switch` statements
switch (foo) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
    default:
        // do nothing
}
Off [Link](http://eslint.org/docs/rules/default-case)
dot-notation Encourages use of dot notation whenever possible
// Bad
foo["bar"];
// Good
var x = foo.bar;
Off [Link](http://eslint.org/docs/rules/dot-notation)
dot-location Enforces consistent newlines before or after dots
var a = universe.
        galaxy;

var b = universe .galaxy;

Off [Link](http://eslint.org/docs/rules/dot-location)
eqeqeq Require the use of `===` and `!==`
// Bad
if (x == 42) { ... }
if ("" == text) { ... }
if (obj.getStuff() != undefined) { ... }
Off [Link](http://eslint.org/docs/rules/eqeqeq)
guard-for-in Make sure `for-in` loops have an `if` statement
// Bad
for (key in foo) {
    doSomething(key);
}
// Good
for (key in foo) {
    if ({}.hasOwnProperty.call(foo, key)) {
        doSomething(key);
    }
}
Off [Link](http://eslint.org/docs/rules/guard-for-in)
no-alert Disallow the use of `alert`, `confirm`, and `prompt`
// Bad
alert("here!");
confirm("Are you sure?");
prompt("What's your name?", "John Doe");
Off [Link](http://eslint.org/docs/rules/no-alert)
no-caller Disallow the use of `arguments.caller` or `arguments.callee`
function foo() {
    var callee = arguments.callee;
}
Off [Link](http://eslint.org/docs/rules/no-caller)
no-div-regex Disallow division operators explicitly at beginning of regular expression
// Bad
function() { return /=foo/; }
// Good
function() { return /\=foo/; }
Off [Link](http://eslint.org/docs/rules/no-div-regex)
no-else-return Disallow `else` after a `return` in an `if`
function foo() {
    if (x) {
        return y;
    } else {
        return z;
    }
}
Off [Link](http://eslint.org/docs/rules/no-else-return)
no-empty-label Disallow use of labels for anything other than loops and switches
labeled: //Label for the following var statement
    var x = 10;
};
Off [Link](http://eslint.org/docs/rules/no-empty-label)
no-eq-null Disallow comparisons to null without a type-checking operator
if (foo == null) {
  bar();
}
Off [Link](http://eslint.org/docs/rules/no-eq-null)
no-eval Disallow use of `eval()`
// Bad
var obj = { x: "foo" },
    key = "x",
    value = eval("obj." + key);
// Good
var obj = { x: "foo" },
    key = "x",
    value = obj[key];
Off [Link](http://eslint.org/docs/rules/no-eval)
no-extend-native Disallow adding to native types
// seems harmless
Object.prototype.extra = 55;

// loop through some userIds var users = { "123": "Stan", "456": "David" };

// not what you'd expect for (var id in users) { console.log(id); // "123", "456", "extra" }

Off [Link](http://eslint.org/docs/rules/no-extend-native)
no-extra-bind Disallow unnecessary function binding
var foo = function() { // function doesn't use this
  do(stuff);
}.bind(bar)
Off [Link](http://eslint.org/docs/rules/no-extra-bind)
no-fallthrough Disallow fallthrough of `case` statements
// Bad
switch(foo) {
    case 1:
        doSomething();
    case 2:
        doSomethingElse();
}
// Good
switch(foo) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomethingElse();
}
Error [Link](http://eslint.org/docs/rules/no-fallthrough)
no-floating-decimal Disallow the use of leading or trailing decimal points in numeric literals
var num = .5;
var num = 2.;
var num = -.7;
Off [Link](http://eslint.org/docs/rules/no-floating-decimal)
no-implicit-coercion Disallow the type conversions with shorter notations
// Bad
var b = !!foo;
var n = 1 * foo;
var s = "" + foo;
// Good
var b = Boolean(foo);
var n = Number(foo);
var s = String(foo);
Off [Link](http://eslint.org/docs/rules/no-implicit-coercion)
no-implied-eval Disallow use of `eval()`-like methods
setTimeout("alert('Hi!');", 100);
Off [Link](http://eslint.org/docs/rules/no-implied-eval)
no-invalid-this Disallow `this` keywords outside of classes or class-like objects
this.a = 0;
baz(() => this);
Off [Link](http://eslint.org/docs/rules/no-invalid-this)
no-iterator Disallow usage of `__iterator__` property
Foo.prototype.__iterator__ = function() {
    return new FooIterator(this);
}
Off [Link](http://eslint.org/docs/rules/no-iterator)
no-labels Disallow use of labeled statements
outer:
    while (true) {
        while (true) {
            break outer;
        }
    }
Off [Link](http://eslint.org/docs/rules/no-labels)
no-lone-blocks Disallow unnecessary nested blocks
{
    var foo = bar();
}
Off [Link](http://eslint.org/docs/rules/no-lone-blocks)
no-loop-func Disallow creation of functions within loops
for (var i = 0; i < 10; i++) {
    funcs[i] = function() {
        return i;
    };
}
Off [Link](http://eslint.org/docs/rules/no-loop-func)
no-multi-spaces Disallow use of multiple spaces
if(foo     === "bar") {}
Off [Link](http://eslint.org/docs/rules/no-multi-spaces)
no-multi-str Disallow use of multiline strings
var x = "Line 1 \
         Line 2";
Off [Link](http://eslint.org/docs/rules/no-multi-str)
no-native-reassign Disallow reassignments of native objects
String = "hello world";
Off [Link](http://eslint.org/docs/rules/no-native-reassign)
no-new-func Disallow use of new operator for `Function` object
var x = new Function("a", "b", "return a + b");
Off [Link](http://eslint.org/docs/rules/no-new-func)
no-new-wrappers Disallows creating new instances of `String`, `Number`, and `Boolean`
var stringObject = new String("Hello world");
var numberObject = new Number(33);
var booleanObject = new Boolean(false);
Off [Link](http://eslint.org/docs/rules/no-new-wrappers)
no-new Disallow use of the `new` operator when not part of an assignment or comparison
// Bad
new Person();
// Good
var person = new Person();
Off [Link](http://eslint.org/docs/rules/no-new)
no-octal-escape Disallow use of octal escape sequences in string literals
var foo = "Copyright \251";
Off [Link](http://eslint.org/docs/rules/no-octal-escape)
no-octal Disallow use of octal literals
var num = 071;      // 57
Error [Link](http://eslint.org/docs/rules/no-octal)
no-param-reassign Disallow reassignment of function parameters
function foo(bar) {
    bar = 13;
}
Off [Link](http://eslint.org/docs/rules/no-param-reassign)
no-process-env Disallow use of `process.env`
if(process.env.NODE_ENV === "development") {
    //...
}
Off [Link](http://eslint.org/docs/rules/no-process-env)
no-proto Disallow usage of `__proto__` property
// Bad
var a = obj.__proto__;
// Good
var a = Object.getPrototypeOf(obj);
Off [Link](http://eslint.org/docs/rules/no-proto)
no-redeclare Disallow declaring the same variable more than once
var a = 3;
var a = 10; // redeclared
Error [Link](http://eslint.org/docs/rules/no-redeclare)
no-return-assign Disallow use of assignment in `return` statement
function doSomething() {
    return foo = bar + 2;
}
Off [Link](http://eslint.org/docs/rules/no-return-assign)
no-script-url Disallow use of `javascript:` urls
location.href = "javascript:void(0)";
Off [Link](http://eslint.org/docs/rules/no-script-url)
no-self-compare Disallow comparisons where both sides are exactly the same
var x = 10;
if (x === x) {
    x = 20;
}
Off [Link](http://eslint.org/docs/rules/no-self-compare)
no-sequences Disallow use of the comma operator
var a = (3, 5); // a = 5
a = b += 5, a + b;
while (a = next(), a && a.length);
(0,eval)("doSomething();");
Off [Link](http://eslint.org/docs/rules/no-sequences)
no-throw-literal Restrict what can be thrown as an exception
// Bad
throw "error";
throw 0;
throw undefined;
throw null;
// Good
throw new Error();
Off [Link](http://eslint.org/docs/rules/no-throw-literal)
no-unused-expressions Disallow usage of expressions in statement position
"Hello world";
Off [Link](http://eslint.org/docs/rules/no-unused-expressions)
no-useless-call Disallow unnecessary `.call()` and `.apply()`
// 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]);
Off [Link](http://eslint.org/docs/rules/no-useless-call)
no-void Disallow use of the `void` operator
// will always return undefined
(function(){
    return void 0;
})();
Off [Link](http://eslint.org/docs/rules/no-void)
no-warning-comments Disallow usage of configurable warning terms in comments
// with "TODO" set as warning term

// TODO: this // todo: this too // Even this: TODO

Off [Link](http://eslint.org/docs/rules/no-warning-comments)
no-with Disallow use of the `with` statement
with (foo) {
    // ...
}
Off [Link](http://eslint.org/docs/rules/no-with)
radix Require use of the second argument for `parseInt()`
// Bad
var num = parseInt("071");      // 57
// Good
var num = parseInt("071", 10);  // 71
Off [Link](http://eslint.org/docs/rules/radix)
vars-on-top Require declarations of all vars at the top of their containing scope
function doSomething() {
    var first;
    if (true) {
        first = true;
    }
    var second; //not declared at the top
}
Off [Link](http://eslint.org/docs/rules/vars-on-top)
wrap-iife Require immediate function invocation to be wrapped in parentheses
// Bad
var x = function () { return { y: 1 };}();
// Good
var x = (function () { return { y: 1 };})();
Off [Link](http://eslint.org/docs/rules/wrap-iife)
yoda Require or disallow Yoda conditions
if ("red" === color) {
    // ...
}
Off [Link](http://eslint.org/docs/rules/yoda)