eslint
万方数据前端团队eslint配置检测规范
Vscode 自动修复配置
- 在vscode中安装eslint插件
- 在settings.json添加如下配置
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
规则
- 不使用console
console.log(123); // ✗ avoid
- 避免修改const声明的变量
const a = 0;
a = 1; // ✗ avoid
- 不使用 debugger
function sum(a, b) {
debugger; // ✗ avoid
return a + b;
}
- 对象字面量只呢个不定义重复的属性
var foo = {
bar: 'baz',
bar: 'qux' // ✗ avoid
}
- 不定义冗余的函数参数
function foo(a, b, a) { // ✗ avoid
const data = a;
}
- switch语句中不定义重复的case分支
switch(id) {
case 1:
// ...
case 1: // ✗ avoid
}
- 不允许空块语句,该规则忽略包含注释的块语句,但允许空catch语句
if (foo) { // ✗ avoid
}
if (foo) { // ✓ ok
// empty
}
try {
// doSomething
} catch (error) { // ✓ ok
}
- 正则表达式中不使用空字符
/^abc[]/.test('abcdefg'); // ✗ avoid
'abcdefg'.match(/^abc[]/); // ✗ avoid
/^abc/.test('abcdefg'); // ✓ ok
'abcdefg'.match(/^abc[a-z]/); // ✓ ok
- 不使用 == 或 != 操作符与null进行比较
if(foo == null) { // ✗ avoid
// doSomething
}
if(foo === null) { // ✓ ok
// doSomething
}
- 不使用eval
window.eval('console.log(123)') // ✗ avoid
- catch 中不要对错误重新赋值
try {
// doSomething
} catch (error) {
error = 123 // ✗ avoid
}
- 不使用多余的括号包裹函数
a = (b * c) // ✗ avoid
- 不使用不必要的分号
const test = 123;; // ✗ avoid
- 不省略小数点前面的0
const num = .5; // ✗ avoid
- 避免对声明过的函数重新赋值
function foo() {}
foo = bar; // ✗ avoid
- 嵌套代码块中禁止定义函数或使用var声明变量
if(test) {
const foo = 30; // ✗ avoid
}
if(test) {
function f(){} // ✗ avoid
}
- 使用let或者const 代替 var
const a = 123; // ✗ avoid
- 不要向RegExp构造器传入非法的正则表达式
RegExp('['); // ✗ avoid
- 禁止this关键字在类或类对象之外出现,只在严格模式下生效
"use strict";
this.a = 0;
baz(() => this); // ✗ avoid
- 不使用非法空白符
function thing() {
return `template <NBSP>string`; // ✗ avoid
}
- 不书写不必要的嵌套代码块
{
const foo = bar(); // ✗ avoid
}
- 不允许将 if 语句作为 else 块中的唯一语句
if (foo) {
// ...
} else { // ✗ avoid
if (bar) {
// ...
}
}
if (foo) {
// ...
} else if (bar) { // ✓ ok
// ...
}
- 禁止在循环中使用函数
for (let i=10; i; i--) {
(function() { return i; })(); // ✗ avoid
}
const a = function() {};
for (let i=10; i<0; i--) {
a(); // ✓ ok
}
- 不要混合使用空格与制表符作为缩进
function add(x, y) {
// --->..return x + y;
return x + y;
}
- 除了缩进,不要使用多个空格
let abc = 1; // ✗ avoid
let abc1 = 1; // ✓ ok
- 允许最多连续两行空格
let test1 = 5; // ✗ avoid
let test2 = 10; // ✓ ok
let test3 = 11;
- 关系运算符的左值不要做取反操作
if (!key in object) { // ✗ avoid
// operator precedence makes it equivalent to (!key) in object
// and type conversion makes it equivalent to (key ? "false" : "true") in object
}
if (!(key in object)) { // ✓ ok
// key is not in object
}
- new创造对象实例后需要赋值给变量
new Character() // ✗ avoid
const character = new Character() // ✓ ok
- 禁止重复生命变量
let name = 'John'
let name = 'Jane' // ✗ avoid
let name = 'John'
name = 'Jane' // ✓ ok
- 行末不留空格
const foo = 0;//•••••
const baz = 5;//••
- 使用 this 前请确保 super() 已调用
class A extends B {
constructor() {
this.a = 0;
super(); // ✗ avoid
}
}
class A extends B {
constructor() {
super(); // ✗ avoid
this.a = 0;
}
}
- 使用throw抛错时,抛出Error对象而不是字符串
throw 'error' // ✗ avoid
throw new Error('error') // ✓ ok
- 对于未声明的变量引用会导致警告
let testUndef = testUndef1 + 1// ✗ avoid
- 禁止使用undefined初始化变量
let name = undefined // ✗ avoid
let name
name = 'value' // ✓ ok
- 不要使用 (, [, or ` 等作为一行的开始。在没有分号的情况下代码压缩后会导致报错,而坚持这一规范则可避免出错。
let hello = 'world'
[1, 2, 3].forEach(addNumber); // ✗ avoid
- return,throw,continue 和 break 后不要再跟代码。
function doSomething () {
return true
console.log('never called') // ✗ avoid
}
- 禁止无用的表达式
if(0) 0 // ✗ avoid
- 不要定义未使用的变量
let a;
- 在使用变量或函数之前定义
abcdef(); // ✗ avoid
function abcdef() {}
- 避免不必要的.call()和.apply()
sum.call(null, 1, 2, 3) // ✗ avoid
- 禁止使用void操作符
(function(){
return void 0; // ✗ avoid
})();
- 禁止使用with
with (val) {...} // ✗ avoid
- 数组元素之间以空格隔开,[ 之后和 ] 之前 不能加空格
const [ x, y ] = z; // ✗ avoid
const [x, y] = z; // ✓ ok
- else 关键字要与花括号保持在同一行。
// ✓ ok
if (condition) {
// ...
} else {
// ...
}
// ✗ avoid
if (condition)
{
// ...
}
else
{
// ...
}
- 对于变量和函数名统一使用驼峰命名法
function my_function () { } // ✗ avoid
function myFunction () { } // ✓ ok
const my_var = 'hello' // ✗ avoid
const myVar = 'hello' // ✓ ok
- 不允许有多余的行末逗号。
const obj = {
message: 'hello', // ✗ avoid
}
- 逗号后面加空格
// ✓ ok
const list = [1, 2, 3, 4]
function greet (name, options) { ... }
// ✗ avoid
let list = [1,2,3,4]
function greet (name,options) { ... }
- 始终将逗号置于行末
let obj = {
foo: 'foo'
,bar: 'bar' // ✗ avoid
}
let obj = {
foo: 'foo',
bar: 'bar' // ✓ ok
}
- return 语句必须有返回值
// ✗ avoid
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
// ✓ ok
function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
- 派生类的构造器中一定要调用 super, 非派生类的构造器不能调用super
class A {
constructor() {
super(); // This is a SyntaxError.
}
}
class A extends B {
constructor() { } // Would throw a ReferenceError.
}
// ✓ ok
class A {
constructor() { }
}
class A extends B {
constructor() {
super();
}
}
- 多行 if 语句的的括号不能省略
if (foo) foo++; // ✗ avoid
// ✓ ok
if (foo) {
foo++;
}
- switch语句最后必须有default
switch (foo) {
case 1:
doSomething();
break;
case 2:
doSomething();
break;
// no default ✗ avoid
}
// ✓ ok
switch (a) {
case 1:
/* code */
break;
default:
/* code */
break;
}
- 使用 === 代替 ==
if (x == 42) { } // ✗ avoid
if(x === 41) { } // ✓ ok
- 不要丢掉异常处理中err参数。
// ✗ avoid
function loadData (err, data) {
doSomething();
}
// ✓ ok
function loadData (err, data) {
if (err) {
console.log(err.stack);
}
doSomething();
}
- 使用两个空格进行缩进。
// ✗ avoid
if (a) {
b=c;
function foo(d) {
e=f;
}
}
// ✓ ok
if (a) {
b=c;
function foo(d) {
e=f;
}
}
- 键值对当中冒号与值之间要留空白
const obj = { 'key' : 'value' } // ✗ avoid
const obj = { 'key' :'value' } // ✗ avoid
const obj = { 'key':'value' } // ✗ avoid
const obj = { 'key': 'value' } // ✓ ok
- 除需要转义的情况外,字符串统一使用单引号
const double = "double"; // ✗ avoid
consg single = 'single'; // ✓ ok
- 使用分号结尾
const value = 1 // ✗ avoid
const value = 1; // ✓ ok
- 遇到分号时空格要后留前不留
for (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid
for (let i = 0; i < items.length; i++) {...} // ✓ ok
- 关键字后面加空格 keyword-spacing
if (condition) { ... } // ✓ ok
if(condition) { ... } // ✗ avoid
- 判断类型是不是NAN使用isNaN()
// ✗ avoid
if (foo == NaN) {
// ...
}
// ✓ ok
if (isNaN(foo)) {
// ...
}
- 用合法的字符串跟 typeof 进行比较操作
typeof foo === "strnig"; // ✗ avoid
typeof foo === "string"; // ✓ ok
*自调用匿名函数 (IIFEs) 使用括号包裹
const getName = function () { }() // ✗ avoid
const getName = (function () { }()) // ✓ ok