Skip to content

Latest commit

 

History

History
424 lines (327 loc) · 9.88 KB

README.md

File metadata and controls

424 lines (327 loc) · 9.88 KB

CodeSupport's ESLint Config

Table of Contents

Style Guide

Variables

These rules relate to variable declarations.

Disallow Deleting Variables

This rule disallows the use of the delete operator on variables.

Incorrect Code

let x;
delete x;

View rule on ESLint.org

Disallow Unused Variables

This rule is aimed at eliminating unused variables, functions, and function parameters.

Incorrect Usage

let x;

function doSomething(y) {
    return 5;
}

Correct Usage

let x = 5;

function doSomething(y) {
    return y * 2;
}

console.log(doSomething(x));

View rule on ESLint.org

Disallow Early Use

This rule will warn when it encounters a reference to an identifier that has not yet been declared.

Incorrect Usage

console.log(a);

let a = 5;

Correct Usage

let a = 5;

console.log(a);

View rule on ESLint.org

Enforce require() on the top-level module scope

This rule requires all calls to require() to be at the top level of the module, similar to ES6 import and export statements, which also can occur only at the top level.

Incorrect Usage

console.log("Starting application...");

const fs = require("fs");

fs.readFile("example.txt");

Correct Usage

const fs = require("fs");

console.log("Starting application...");

fs.readFile("example.txt");

View rule on ESLint.org

Possible Errors

These rules relate to possible syntax or logic errors in JavaScript code.

Disallow Duplicate Arguments In Function Definitions

This rule disallows duplicate parameter names in function declarations or expressions. It does not apply to arrow functions or class methods, because the parser reports the error.

Incorrect Usage

function addNumbers(number, number) {
    return number + number;
}

Correct Usage

function addNumbers(a, b) {
    return a + b;
}

View rule on ESLint.org

Disallow Duplicate Keys In Object Literals

This rule disallows duplicate keys in object literals.

Incorrect Usage

const data = {
    name: "Jack",
    name: "Jill"
};

Correct Usage

const data = {
    name_one: "Jack",
    name_two: "Jill"
};

View rule on ESLint.org

Rule To Disallow A Duplicate Case Label

This rule disallows duplicate test expressions in case clauses of switch statements.

Incorrect Usage

switch (x) {
    case 1:
        break;
    case 1:
        break;
}

Correct Usage

switch (x) {
    case 1:
        break;
    case 2:
        break;
}

View rule on ESLint.org

Disallow Empty Block Statements

This rule disallows empty block statements. This rule ignores block statements which contain a comment (for example, in an empty catch or finally block of a try statement to indicate that execution should continue regardless of errors).

Incorrect Usage

if (condition) {
}

Correct Usage

if (condition) {
    console.log("Hello!");
}

View rule on ESLint.org

Disallow Reassigning Exceptions In Catch Clauses

This rule disallows reassigning exceptions in catch clauses.

Incorrect Usage

try {
    getData();
} catch (error) {
    error = 5;
}

Correct Usage

try {
    getData();
} catch (error) {
    const number = 5;
}

View rule on ESLint.org

Disallow Unnecessary Parentheses

This rule restricts the use of parentheses to only where they are necessary.

Incorrect Usage

const a = (5 * 2);

Correct Usage

const a = 5 * 2;

View rule on ESLint.org

Disallow Unnecessary Semicolons

This rule disallows unnecessary semicolons.

Incorrect Usage

let x = 5;;

Correct Usage

let x = 5;

View rule on ESLint.org

Disallow Reassigning function Declarations

This rule disallows reassigning function declarations.

Incorrect Usage

function getData() {
    return {
        name: "John"
    };
}

function getData() {
    return {
        title: "This is an article."
    };
}

Correct Usage

function getUserData() {
    return {
        name: "John"
    };
}

function getArticleData() {
    return {
        title: "This is an article."
    };
}

View rule on ESLint.org

Disallow Variable or function Declarations In Nested Blocks

This rule requires that function declarations and, optionally, variable declarations be in the root of a program or the body of a function.

Incorrect Usage

if (condition) {
    function doSomething() {}
}

Correct Usage

function doSomething() {}

if (condition) {
    let x = 5;
}

View rule on ESLint.org

Disallow Invalid Regular Expression Strings In RegExp Constructors

This rule disallows invalid regular expression strings in RegExp constructors.

Incorrect Usage

new RegExp("[");

Correct Usage

new RegExp(".");

View rule on ESLint.org

Disallow Calling Global Object Properties As Functions

This rule disallows calling the Math, JSON, Reflect and Atomics objects as functions.

Incorrect Usage

const json = JSON();

Correct Usage

JSON.parse({
    "some": "data"
});

View rule on ESLint.org

Disallow Template Literal Placeholder Syntax In Regular Strings

This rule aims to warn when a regular string contains what looks like a template literal placeholder. It will warn when it finds a string containing the template literal placeholder (${something}) that uses either " or ' for the quotes.

Incorrect Usage

console.log("Hello ${name}!");

Correct Usage

console.log(`Hello ${name}!`);

View rule on ESLint.org

Disallow Confusing Multiline Expressions

This rule disallows confusing multiline expressions where a newline looks like it is ending a statement, but is not.

Incorrect Usage

const foo = bar
(1 || 2).baz();

Correct Usage

const foo = bar;
(1 || 2).baz();

View rule on ESLint.org

Disallow Unreachable Code After return, throw, continue and break Statements

This rule disallows unreachable code after return, throw, continue, and break statements.

Incorrect Usage

function foo() {
    return "bar";

    console.log("Hello!");
}

Correct Usage

function foo() {
    console.log("Hello!");

    return "bar";
}

View rule on ESLint.org

Require Calls To isNaN() When Checking For NaN

This rule disallows comparisons to NaN.

Incorrect Usage

if (foo == NaN) {
    doSomething();
}

Correct Usage

if (isNaN(foo)) {
    doSomething();
}

View rule on ESLint.org

Enforce Comparing typeof Expressions Against Valid Strings

This rule enforces comparing typeof expressions to valid string literals.

Incorrect Usage

typeof foo === "strnig"

Correct Usage

typeof foo === "string"

View rule on ESLint.org