eslint-config-sickle

0.0.1 • Public • Published

eslint

万方数据前端团队eslint配置检测规范

Vscode 自动修复配置

  1. 在vscode中安装eslint插件
  2. 在settings.json添加如下配置
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
},

规则

  • 不使用console

no-console

console.log(123); // ✗ avoid
  • 避免修改const声明的变量

no-const-assign

const a = 0;
a = 1;           // ✗ avoid
  • 不使用 debugger

no-debugger

function sum(a, b) {
  debugger;     // ✗ avoid
  return a + b;
}
  • 对象字面量只呢个不定义重复的属性

no-dupe-keys

var foo = {
  bar: 'baz',
  bar: 'qux'    // ✗ avoid
}
  • 不定义冗余的函数参数

no-dupe-args

function foo(a, b, a) {  // ✗ avoid
  const data = a;
}
  • switch语句中不定义重复的case分支

no-duplicate-case

switch(id) {
  case 1:
    // ...
  case 1:     // ✗ avoid
}
  • 不允许空块语句,该规则忽略包含注释的块语句,但允许空catch语句

no-empty

if (foo) {  // ✗ avoid
}

if (foo) {  // ✓ ok
  // empty
}

try {
  // doSomething
} catch (error) {  // ✓ ok

}
  • 正则表达式中不使用空字符

no-empty-character-class

/^abc[]/.test('abcdefg');  // ✗ avoid
'abcdefg'.match(/^abc[]/); // ✗ avoid

/^abc/.test('abcdefg');  // ✓ ok
'abcdefg'.match(/^abc[a-z]/); // ✓ ok
  • 不使用 == 或 != 操作符与null进行比较

no-eq-null

if(foo == null) { // ✗ avoid
  // doSomething
}

if(foo === null) { // ✓ ok
  // doSomething
}
  • 不使用eval

no-eval

window.eval('console.log(123)') // ✗ avoid
  • catch 中不要对错误重新赋值

no-ex-assign

try {
  // doSomething
} catch (error) {
  error = 123 // ✗ avoid
}
  • 不使用多余的括号包裹函数

no-extra-parens

a = (b * c) // ✗ avoid
  • 不使用不必要的分号

no-extra-semi

const test = 123;;  // ✗ avoid
  • 不省略小数点前面的0

no-floating-decimal

const num = .5; // ✗ avoid
  • 避免对声明过的函数重新赋值

no-func-assign

function foo() {}
foo = bar; // ✗ avoid
  • 嵌套代码块中禁止定义函数或使用var声明变量

no-inner-declarations

if(test) {
  const foo = 30; // ✗ avoid
}
if(test) {
  function f(){} // ✗ avoid
}
  • 使用let或者const 代替 var

no-var

const a = 123; // ✗ avoid
  • 不要向RegExp构造器传入非法的正则表达式

no-invalid-regexp

RegExp('['); // ✗ avoid
  • 禁止this关键字在类或类对象之外出现,只在严格模式下生效

no-invalid-this

"use strict";

this.a = 0;
baz(() => this); // ✗ avoid
  • 不使用非法空白符

no-irregular-whitespace

function thing() {
  return `template <NBSP>string`;  // ✗ avoid
}
  • 不书写不必要的嵌套代码块

no-lone-blocks

{
  const foo = bar(); // ✗ avoid
}
  • 不允许将 if 语句作为 else 块中的唯一语句

no-lonely-if

if (foo) {
  // ...
} else { // ✗ avoid
  if (bar) {
    // ...
  }
}

if (foo) {
  // ...
} else if (bar) { // ✓ ok
  // ...
}
  • 禁止在循环中使用函数

no-loop-func

for (let i=10; i; i--) {
    (function() { return i; })(); // ✗ avoid
}

const a = function() {};

for (let i=10; i<0; i--) { 
    a(); // ✓ ok
}
  • 不要混合使用空格与制表符作为缩进

no-mixed-spaces-and-tabs

function add(x, y) {
// --->..return x + y;  
        return x + y;
}
  • 除了缩进,不要使用多个空格

no-multi-spaces

let abc =  1; // ✗ avoid
let abc1 = 1; // ✓ ok
  • 允许最多连续两行空格

no-multiple-empty-lines

let test1 = 5; // ✗ avoid



let test2 = 10; // ✓ ok


let test3 = 11;
  • 关系运算符的左值不要做取反操作

no-unsafe-negation

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创造对象实例后需要赋值给变量

no-new

new Character()                     // ✗ avoid
const character = new Character()   // ✓ ok
  • 禁止重复生命变量

no-redeclare

let name = 'John'
let name = 'Jane'     // ✗ avoid
 
let name = 'John'
name = 'Jane'         // ✓ ok
  • 行末不留空格

no-trailing-spaces

const foo = 0;//•••••
const baz = 5;//••
  • 使用 this 前请确保 super() 已调用

no-this-before-super

class A extends B {
  constructor() {
    this.a = 0;
    super();           // ✗ avoid
  }
}

class A extends B {
  constructor() {
    super();           // ✗ avoid
    this.a = 0;
  }
}
  • 使用throw抛错时,抛出Error对象而不是字符串

no-throw-literal

throw 'error'               // ✗ avoid
throw new Error('error')    // ✓ ok
  • 对于未声明的变量引用会导致警告

no-undef

let testUndef = testUndef1 + 1// ✗ avoid
  • 禁止使用undefined初始化变量

no-undef-init

let name = undefined    // ✗ avoid
let name
name = 'value'          // ✓ ok
  • 不要使用 (, [, or ` 等作为一行的开始。在没有分号的情况下代码压缩后会导致报错,而坚持这一规范则可避免出错。

no-unexpected-multiline

let hello = 'world'
[1, 2, 3].forEach(addNumber);  // ✗ avoid
  • return,throw,continue 和 break 后不要再跟代码。

no-unreachable

function doSomething () {
  return true
  console.log('never called')     // ✗ avoid
}
  • 禁止无用的表达式

no-unused-expressions

if(0) 0 // ✗ avoid
  • 不要定义未使用的变量

no-unused-vars

let a;
  • 在使用变量或函数之前定义

no-use-before-define

abcdef(); // ✗ avoid

function abcdef() {}
  • 避免不必要的.call()和.apply()

no-useless-call

sum.call(null, 1, 2, 3)   // ✗ avoid
  • 禁止使用void操作符

no-void

(function(){
    return void 0; // ✗ avoid
})();
  • 禁止使用with

no-with

with (val) {...}    // ✗ avoid
  • 数组元素之间以空格隔开,[ 之后和 ] 之前 不能加空格

array-bracket-spacing

const [ x, y ] = z; // ✗ avoid
const [x, y] = z;   // ✓ ok
  • else 关键字要与花括号保持在同一行。

brace-style

// ✓ ok
if (condition) {
  // ...
} else {
  // ...
}

// ✗ avoid
if (condition)
{
  // ...
}
else
{
  // ...
}
  • 对于变量和函数名统一使用驼峰命名法

camelcase

function my_function () { }    // ✗ avoid
function myFunction () { }     // ✓ ok
 
const my_var = 'hello'         // ✗ avoid
const myVar = 'hello'          // ✓ ok
  • 不允许有多余的行末逗号。

comma-dangle

const obj = {
  message: 'hello',   // ✗ avoid
}
  • 逗号后面加空格

comma-spacing

// ✓ ok
const list = [1, 2, 3, 4]
function greet (name, options) { ... }
// ✗ avoid
let list = [1,2,3,4]
function greet (name,options) { ... }
  • 始终将逗号置于行末

comma-style

let obj = {
  foo: 'foo'
  ,bar: 'bar'   // ✗ avoid
}

let obj = {
  foo: 'foo',
  bar: 'bar'   // ✓ ok
}
  • return 语句必须有返回值

consistent-return

// ✗ avoid
function doSomething(condition) {
  if (condition) {
    return true;
  } else {
    return;
  }
}
// ✓ ok
function doSomething(condition) {
  if (condition) {
    return true;
  } else {
    return false;
  }
}
  • 派生类的构造器中一定要调用 super, 非派生类的构造器不能调用super

constructor-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 语句的的括号不能省略

curly

if (foo) foo++; // ✗ avoid

// ✓ ok
if (foo) {
  foo++;
}
  • switch语句最后必须有default

default-case

switch (foo) {
  case 1:
    doSomething();
    break;

  case 2:
    doSomething();
    break;
  // no default ✗ avoid
}

// ✓ ok
switch (a) {
  case 1:
    /* code */
    break;

  default:
    /* code */
    break;
}
  • 使用 === 代替 ==

eqeqeq

if (x == 42) { } // ✗ avoid
if(x === 41) { } // ✓ ok
  • 不要丢掉异常处理中err参数。

handle-callback-err

// ✗ avoid
function loadData (err, data) {
  doSomething();
}

// ✓ ok
function loadData (err, data) {
  if (err) {
    console.log(err.stack);
  }
  doSomething();
}
  • 使用两个空格进行缩进。

indent

// ✗ avoid
if (a) {
    b=c;
    function foo(d) {
        e=f;
    }
}

// ✓ ok
if (a) {
  b=c;
  function foo(d) {
    e=f;
  }
}
  • 键值对当中冒号与值之间要留空白

key-spacing

const obj = { 'key' : 'value' }    // ✗ avoid
const obj = { 'key' :'value' }     // ✗ avoid
const obj = { 'key':'value' }      // ✗ avoid
const obj = { 'key': 'value' }     // ✓ ok
  • 除需要转义的情况外,字符串统一使用单引号

quotes

const double = "double"; // ✗ avoid
consg single = 'single'; // ✓ ok
  • 使用分号结尾

semi

const value = 1 // ✗ avoid
const value = 1; // ✓ ok
  • 遇到分号时空格要后留前不留

semi-spacing

for (let i = 0 ;i < items.length ;i++) {...}    // ✗ avoid
for (let i = 0; i < items.length; i++) {...}    // ✓ ok
if (condition) { ... }   // ✓ ok
if(condition) { ... }    // ✗ avoid
  • 判断类型是不是NAN使用isNaN()

use-isnan

// ✗ avoid
if (foo == NaN) {
  // ...
}

// ✓ ok
if (isNaN(foo)) {
  // ...
}
  • 用合法的字符串跟 typeof 进行比较操作

valid-typeof

typeof foo === "strnig"; // ✗ avoid
typeof foo === "string"; // ✓ ok

*自调用匿名函数 (IIFEs) 使用括号包裹

wrap-iife

const getName = function () { }()     // ✗ avoid
 
const getName = (function () { }())   // ✓ ok

Readme

Keywords

Package Sidebar

Install

npm i eslint-config-sickle

Weekly Downloads

11

Version

0.0.1

License

ISC

Unpacked Size

50.8 kB

Total Files

7

Last publish

Collaborators

  • tarosunn