eslint-config-codesupport v1.0.2
CodeSupport's ESLint Config
Table of Contents
- Variables
- Possible Errors
- Disallow Duplicate Arguments In Function Definitions
- Disallow Duplicate Keys In Object Literals
- Rule To Disallow A Duplicate Case Label
- Disallow Empty Block Statements
- Disallow Reassigning Exceptions In Catch Clauses
- Disallow Unnecessary Parentheses
- Disallow Unnecessary Semicolons
- Disallow Reassigning
function
Declarations - Disallow Variable or
function
Declarations In Nested Blocks - Disallow Invalid Regular Expression Strings In RegExp Constructors
- Disallow Calling Global Object Properties As Functions
- Disallow Template Literal Placeholder Syntax In Regular Strings
- Disallow Confusing Multiline Expressions
- Disallow Unreachable Code After
return
,throw
,continue
andbreak
Statements - Require Calls To
isNaN()
When Checking ForNaN
Enforce Comparing
typeof
Expressions Against Valid Strings#enforce-comparing-typeof-expressions-against-valid-strings
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;
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));
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);
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");
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;
}
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"
};
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;
}
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!");
}
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;
}
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;
Disallow Unnecessary Semicolons
This rule disallows unnecessary semicolons.
Incorrect Usage
let x = 5;;
Correct Usage
let x = 5;
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."
};
}
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;
}
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(".");
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"
});
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}!`);
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();
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";
}
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();
}
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"