1.0.1 • Published 4 years ago

eslint-config-standard-vincent v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

An ESLint Shareable Config for JavaScript Standard Style

该规则包基于eslint-config-standard定制

除了局部引用插件的,规则都出自于eslint内置规则,文档详情请访问ESLint Rules

安装

npm install --save-dev @h3/eslint-config-standard

规则

accessor-pairs

规则

'accessor-pairs': 'error'

介绍

get允许独立存在,若是set必须成对存在,不允许单独set

Code Demo

// Bad
var o = {
    set a(value) {
        this.val = value;
    }
};

// Good
var o = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};
// or
var o = {
    get a() {
        return this.val;
    }
};

camelcase

规则

camelcase: ['error', { properties: 'never' }]

介绍

除属性外,变量命名都需使用驼峰法

Code Demo

// Bad
var myfavoritecolor = '#FFF';
var my_love = '#EEE';


// Good
var myFavoriteColor   = "#112C85";
var _myFavoriteColor  = "#112C85";
var myFavoriteColor_  = "#112C85";
var MY_FAVORITE_COLOR = "#112C85";
var foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };

// Good , 属性名可以不驼峰法
var obj = {
    my_pref: 1
};

comma-dangle

规则:

'comma-dangle': ['error', {
    arrays: 'always-multiline',
    objects: 'always-multiline',
    imports: 'always-multiline',
    exports: 'always-multiline',
    functions: 'always-multiline'
 }]

介绍

多行语句后面须跟随逗号

Code Demo

// Bad
var foo = {
    bar: "baz",
    qux: "quux"
};

// Good
var foo = {
    bar: "baz",
    qux: "quux",
};

comma-spacing

规则

'comma-spacing': ['error', { before: false, after: true }]

介绍

禁止再逗号前使用空格,要求再逗号后使用一个或者多个空格

Code Demo

// Bad
var obj = {"foo": "bar" ,"baz": "qur"};

// Good
var obj = {"foo": "bar", "baz": "qur"};

comma-style

规则

'comma-style': ['error', 'last']

介绍

要求逗号放在数组元素、对象属性或变量声明之后,且在同一行

Code Demo

// Bad
var foo = 1
,
bar = 2;

var foo = 1
  , bar = 2;

// Good
var foo = 1, bar = 2;

var foo = 1,
    bar = 2;

dot-location

规则

'dot-location': ['error', 'property']

介绍

点换行后跟随属性

Code Demo

// Bad
var foo = object.
property;

// Good
var foo = object
    .property;

var bar = object.property;

eqeqeq

规则

eqeqeq: ['error', 'always', { null: 'ignore' }]

介绍

只允许===!== , 不允许==(除了null

Code Demo

// Bad
a == b
false == 0

// Good
a === b
false === 0

generator-star-spacing

规则

'generator-star-spacing': ['error', { before: true, after: false }]

介绍

generator的*前面有空格,后面没有

Code Demo

// Bad
function * generator() {}

// Good
function *generator() {}

handle-callback-err

规则

'handle-callback-err': ['error', '^(err|error)$']

介绍

要对名字为errerror的参数做异常处理

Code Demo

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

// Good
function loadData (error, data) {
    if (error) {
       console.log(error.stack);
    }
    doSomething();
}

key-spacing

规则

'key-spacing': ['error', {
  beforeColon: false,
  afterColon: true,
}],

介绍

紧凑型格式,冒号前没有空格,冒号后有一个空格

Code Demo

// Bad
var obj = {
    myObjectFunction: function() {
      // Do something
    },
    one             : 1,
    seven           : 7
}

// Good
var myObj = {
  myObjectFunction: function() {
    // Do something
  },
  one: 1,
  seven: 7,
}

lines-between-class-members

规则

'lines-between-class-members': ['error', 'always']

介绍

类的成员之间有一行空行

Code Demo

// Bad
class MyClass {
  foo() {
    //...
  }
  bar() {
    //...
  }
}

// Good
class MyClass {
  foo() {
    //...
  }

  bar() {
    //...
  }
}

new-cap

规则

'new-cap': ['error', { newIsCap: true, capIsNew: false, properties: true }]

介绍

强制要求构造函数名必须首字母大写, 但是不要求大写字母一定是构造函数;

Code Demo

// Bad
var friend = new person();
var colleague = Person(); // capIsNew: true
var friend = new person.acquaintance(); // properties: true


// Good
var colleague = new Person();
var friend = new person.Acquaintance();

no-async-promise-executor

规则

'no-async-promise-executor': 'error'

介绍

此规则旨在禁止使用异步的 Promise executor 函数。

Code Demo

// Bad
const foo = new Promise(async (resolve, reject) => {
  readFile('foo.txt', function(err, result) {
    if (err) {
      reject(err);
    } else {
      resolve(result);
    }
  });
});

const result = new Promise(async (resolve, reject) => {
  resolve(await foo);
});

// Good
const foo = new Promise((resolve, reject) => {
  readFile('foo.txt', function(err, result) {
    if (err) {
      reject(err);
    } else {
      resolve(result);
    }
  });
});

const result = Promise.resolve(foo);

no-case-declarations

规则

'no-case-declarations': 'off'

介绍

允许在 case 或 default 子句中出现词法声明

Code Demo

switch (foo) {
    // The following case clauses are wrapped into blocks using brackets
    case 1: {
        let x = 1;
        break;
    }
    case 2: {
        const y = 2;
        break;
    }
    case 3: {
        function f() {}
        break;
    }
    case 4:
        var z = 4;
        break;
    default: {
        class C {}
    }
}

no-constant-condition

规则

'no-constant-condition': ['error', { checkLoops: false }]

介绍

禁止条件判断使用常量,但循环语句除外

Code Demo

// Bad
if (false) {
    doSomethingUnfinished();
}

if (void x) {
    doSomethingUnfinished();
}

for (;-2;) {
    doSomethingForever();
}


// Good
while (true) {
    doSomething();
    if (condition()) {
        break;
    }
};

no-eval

规则

'no-eval': ['error', { allowIndirect: true }]

介绍

不允许直接调用eval,可以间接调用

Code Demo

// Bad
var obj = { x: "foo" },
    key = "x",
    value = eval("obj." + key);

// Good
(0, eval)("var a = 0");

var foo = eval;
foo("var a = 0");

this.eval("var a = 0");

no-inner-declarations

规则

'no-inner-declarations': ['error', 'both']

介绍

禁止在块级作用域内声明变量和函数

Code Demo

// Bad
if (test) {
    function doSomethingElse () { }
}

function anotherThing() {
    var fn;

    if (test) {

        // Good
        fn = function expression() { };

        // Bad
        function declaration() { }
    }
}

while (test) {
    var bar2;
}

// Good
function doSomething() { }

no-multi-spaces

规则

'no-multi-spaces': ['error', { exceptions: { Property: true } }]

介绍

禁止多个空格,对象属性除外,防止与key-spacing冲突

Code Demo

// Bad

var a =  1;

if(foo   === "bar") {}

a <<  b

var arr = [1,  2];

a ?  b: c


// Good
var a = 1;

if(foo === "bar") {}

a << b

var arr = [1, 2];

a ? b: c

var obj = {
    first:  "first",
    second: "second"
};

no-redeclare

规则

'no-redeclare': ['error', { builtinGlobals: true }]

介绍

禁止重复声明,禁止复写全局变量

Code Demo

// Bad
var a = 3;
var a = 10;
var Object = 0;
var top = 0;


// Good
var a = 3;
a = 10;

no-unneeded-ternary

规则

'no-unneeded-ternary': ['error', { defaultAssignment: false }]

介绍

禁止无意义的三元运算符,禁止exp ? exp : value的格式

Code Demo

// Bad
var isYes = answer === 1 ? true : false;
foo(bar ? bar : 1);

// Good
var isYes = answer === 1;
foo(bar || 1);

no-unused-vars

规则

'no-unused-vars': ['error', { vars: 'all', args: 'after-used', ignoreRestSiblings: true }]

介绍

禁止未使用过的变量,函数内只检查最后一个使用之后的参数。 若是最后一个参数给使用,可以忽略对前面几个参数的检测。 若是把rest来聚合没有使用的参数也可以忽略聚合部分。

Code Demo

// Bad
(function(foo, bar, baz, qux) {
    return bar;
})();

// Good
(function(foo, bar, baz, qux) {
    return qux;
})();

var { type, ...coords } = data;

object-curly-newline

规则

'object-curly-newline': ['error', {
  ObjectExpression: { multiline: true, consistent: true },
  ObjectPattern: { multiline: true, minProperties: 4 },
  ImportDeclaration: { multiline: true, minProperties: 4 },
  ExportDeclaration: 'always'
}],

介绍

强制大括号内换行符的一致性

Code Demo

// Bad
import { a,b,c,d } from 'test';
export { a,b,c,d };

// Good
import { a,b,c } from 'test';
import {
  a,
  b,
  c
} from 'test';

const { a,b } = { a:'1' , b:'2' , c ='3' }
const {
  a,b
} = { a:'1' , b:'2' , c ='3' }

object-property-newline

规则

'object-property-newline': ['error', { allowMultiplePropertiesPerLine: true }]

介绍

强制将对象的属性放在不同的行上,开启allowMultiplePropertiesPerLine也允许同一行存在

Code Demo

// Bad
const newObject = {a: 1, b: [2, {a: 3, b: 4}]};


// Good
const obj = { foo: "foo", bar: "bar", baz: "baz" };

const obj2 = {
    foo: "foo", bar: "bar", baz: "baz"
};
const user = process.argv[2];
const obj3 = {
    user, [process.argv[3] ? "foo" : "bar"]: 0, baz: [1, 2, 4, 8]
};

prefer-promise-reject-errors

规则

'prefer-promise-reject-errors': ['error', { allowEmptyReject: false }]

介绍

要求Promise reject 传入一个Error对象作为参数

Code Demo

// Bad
Promise.reject("something bad happened");

Promise.reject(5);

Promise.reject();

// Good
Promise.reject(new Error("something bad happened"));

Promise.reject(new TypeError("something bad happened"));

new Promise(function(resolve, reject) {
  reject(new Error("something bad happened"));
});

var foo = getUnknownValue();
Promise.reject(foo);

semi

规则

semi: ['error', 'always']

介绍

要求行尾有分号

Code Demo

// Bad
var name = "ESLint"

object.method = function() {
    // ...
}

// Good
var name = "ESLint";

object.method = function() {
    // ...
};

wrap-iife

规则

'wrap-iife': ['error', 'inside', { functionPrototypeMethods: true }]

介绍

自执行函数使用.call.apply 调用时,强制要求包裹函数表达式

Code Demo

// Bad
var x = function(){ foo(); }()
var x = (function(){ foo(); }())
var x = function(){ foo(); }.call(bar)
var x = (function(){ foo(); }.call(bar))

// Good
var x = (function(){ foo(); })()
var x = (function(){ foo(); }).call(bar)

linebreak-style

规则

'linebreak-style': ['error', 'unix']

介绍

强制使用一致的换行符风格

Code Demo

// Bad , throw error
var a = 'a'; // \r\n

// Good
var a = 'a', // \n
    b = 'b'; // \n
// \n
function foo(params) { // \n
    // do stuff \n
}// \n

no-shadow

规则

'no-shadow': ['error', { builtinGlobals: false, hoist: 'functions',allow:["self"] }]

介绍

禁止变量声明覆盖外层作用域的变量,但是允许复写self,忽略全局对象的检测

Code Demo

// Bad
var a = 3;
function b() {
    var a = 10;
}

var b = function () {
    var a = 10;
}

function foo() {
    var Object = 0;
}

// Good
if (true) {
    let a = 3;
}

let a = 5;

no-underscore-dangle

规则

'no-underscore-dangle': 'off',

介绍

该规则关闭,也就是允许下划线作为标识符的一部分

Code Demo

// OK
var foo_;
var __proto__ = {};
foo._bar();

no-param-reassign

规则

'no-param-reassign': ['error', { props: false }]

介绍

参数不能给替代,属性可以更改

Code Demo

// Good
function foo(bar) {
    bar.prop = "value";
}

function foo(bar) {
    delete bar.aaa;
}

function foo(bar) {
    bar.aaa++;
}

规则

'max-len': ['error', { code: 120, comments: 140 }]

介绍

代码字符单行最长允许120个字符,注释140个字符

Code Demo

// Bad
// 自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补
let a = "自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补自行脑补"

// Good
// @derc - 我是注释,简洁又明了
let a = "我只想说。。。。"