- 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
These rules relate to variable declarations.
This rule disallows the use of the delete operator on variables.
let x;
delete x;
This rule is aimed at eliminating unused variables, functions, and function parameters.
let x;
function doSomething(y) {
return 5;
}
let x = 5;
function doSomething(y) {
return y * 2;
}
console.log(doSomething(x));
This rule will warn when it encounters a reference to an identifier that has not yet been declared.
console.log(a);
let a = 5;
let a = 5;
console.log(a);
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.
console.log("Starting application...");
const fs = require("fs");
fs.readFile("example.txt");
const fs = require("fs");
console.log("Starting application...");
fs.readFile("example.txt");
These rules relate to possible syntax or logic errors in JavaScript code.
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.
function addNumbers(number, number) {
return number + number;
}
function addNumbers(a, b) {
return a + b;
}
This rule disallows duplicate keys in object literals.
const data = {
name: "Jack",
name: "Jill"
};
const data = {
name_one: "Jack",
name_two: "Jill"
};
This rule disallows duplicate test expressions in case
clauses of switch
statements.
switch (x) {
case 1:
break;
case 1:
break;
}
switch (x) {
case 1:
break;
case 2:
break;
}
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).
if (condition) {
}
if (condition) {
console.log("Hello!");
}
This rule disallows reassigning exceptions in catch
clauses.
try {
getData();
} catch (error) {
error = 5;
}
try {
getData();
} catch (error) {
const number = 5;
}
This rule restricts the use of parentheses to only where they are necessary.
const a = (5 * 2);
const a = 5 * 2;
This rule disallows unnecessary semicolons.
let x = 5;;
let x = 5;
This rule disallows reassigning function
declarations.
function getData() {
return {
name: "John"
};
}
function getData() {
return {
title: "This is an article."
};
}
function getUserData() {
return {
name: "John"
};
}
function getArticleData() {
return {
title: "This is an article."
};
}
This rule requires that function declarations and, optionally, variable declarations be in the root of a program or the body of a function.
if (condition) {
function doSomething() {}
}
function doSomething() {}
if (condition) {
let x = 5;
}
This rule disallows invalid regular expression strings in RegExp constructors.
new RegExp("[");
new RegExp(".");
This rule disallows calling the Math
, JSON
, Reflect
and Atomics
objects as functions.
const json = JSON();
JSON.parse({
"some": "data"
});
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.
console.log("Hello ${name}!");
console.log(`Hello ${name}!`);
This rule disallows confusing multiline expressions where a newline looks like it is ending a statement, but is not.
const foo = bar
(1 || 2).baz();
const foo = bar;
(1 || 2).baz();
This rule disallows unreachable code after return
, throw
, continue
, and break
statements.
function foo() {
return "bar";
console.log("Hello!");
}
function foo() {
console.log("Hello!");
return "bar";
}
This rule disallows comparisons to NaN
.
if (foo == NaN) {
doSomething();
}
if (isNaN(foo)) {
doSomething();
}
This rule enforces comparing typeof
expressions to valid string literals.
typeof foo === "strnig"
typeof foo === "string"