- 縮進
使用兩個空格進行縮進。
eslint:indent
function() {
let a = '測試字符串';
console.log('測試字符串’);
}
- 引號
除需要轉(zhuǎn)義的情況或json文件外腋妙,字符串統(tǒng)一使用單引號
eslint:quotes
let a = '測試字符串';
console.log('測試字符串');
-
變量
3.1 變量/常量聲明聲明變量請使用
let
聲明常量或函數(shù)表達式請使用const
禁止使用var
let a = '測試字符串'; // ? ok const b = '常量'; // ? ok var c = 'test'; // ? avoid
3.2 變量和函數(shù)命名規(guī)則
一律采用駝峰式命名
eslint:camelcase
let testName = '測試字符串'; function openLink() {} const jumpToUrl = () => {};
3.3 變量使用規(guī)則
不要定義未使用的變量
eslint: no-unused-vars
let a = 'test'; b = 1; // ? avoid
3.4 使用字面量而不是構(gòu)造器
eslint:
no-array-constructor
let arr = [1, 2, 3]; // ? ok let arr = new Array(1, 2, 3); // ? avoid let obj = {a: 1, b: 2}; // ? ok let obj = new Object(); // ? avoid
3.5 禁止修改
const
創(chuàng)建的常量eslint:
no-const-assign
-
空格規(guī)則
4.1 關(guān)鍵字后面加空格eslint:
keyword-spacing
let a = 'test'; // ? ok if (condition) { ... } // ? ok if(condition) { ... } // ? avoid function (arguments) { // do something }
4.2 函數(shù)聲明時括號與函數(shù)名間加空格
eslint:
space-before-function-paren
function (arguments) { // do something }
4.3 字符串拼接操作符 (Infix operators) 之間要留空格场躯,盡量使用es6模板字符串
eslint:
space-infix-ops
let a = '我的姓名是' + name; let b = `我的姓名是${name}`; // recommend
4.4 類似
+-*/=?
這類符號兩邊需要加空格let a = 1 + 2 * 3 / 4; let b = true ? 1 : 2;
4.5 逗號后面加空格
eslint:
comma-spacing
let arr = [1, 2, 3, 4];
4.6 單行代碼塊兩邊加空格
eslint:
block-spacing
function foo () {return true;} // ? avoid function foo () { return true; } // ? ok let a = { b: 1 }; // ? ok let a = {b: 1}; // ? avoid
等號規(guī)則
盡量使用
===
代替==
eslint:eqeqeq
-
if else
語句
6.1 else 關(guān)鍵字要與花括號保持在同一行eslint:
brace-style
// ? ok if (condition) { // ... } else { // ... } // ? avoid if (condition) { // ... } else { // ... }
6.2 多行 if 語句的的括號不能省
eslint:
curly
if (options.quiet !== true) console.log('done'); // ? ok if (options.quiet !== true) { // ? ok console.log('done'); } if (options.quiet !== true) // ? avoid console.log('done');
-
標點使用規(guī)則
7.1 不允許有多余的行末逗號eslint:
comma-dangle
let obj = { message: 'hello', // ? avoid }
7.2 始終將逗號置于行末
eslint:
comma-style
var obj = { foo: 'foo' ,bar: 'bar' // ? avoid } var obj = { foo: 'foo', bar: 'bar' // ? ok }
7.3 點號操作符須與屬性需在同一行
eslint:
dot-location
new Promise(). then() // ? avoid new Promise() .then() // ? ok .catch()
7.4 鍵值對當中冒號與值之間要留空白
eslint:
key-spacing
let obj = { 'key' : 'value' } // ? avoid let obj = { 'key' :'value' } // ? avoid let obj = { 'key':'value' } // ? avoid let obj = { 'key': 'value' } // ? ok
7.5 表達式末尾需要添加逗號的必須添加逗號
let a = 1; const test = () => {};
-
構(gòu)造函數(shù)
8.1 無參的構(gòu)造函數(shù)調(diào)用時要帶上括號eslint:
new-parens
function Animal () {} const dog = new Animal // ? avoid const dog = new Animal() // ? ok
8.2 構(gòu)造函數(shù)要以大寫字母開頭
eslint:new-cap
function animal () {} let dog = new animal(); // ? avoid function Animal () {} let dog = new Animal(); // ? ok class MyComponent extends Component() {} // ? ok
其他注意事項
- 不要使用
debugger
eslint:
no-debugger
function setName() {
debugger
// do something
}
- 不要使用
eval()
- 注意隱式的
eval()
eslint:no-implied-eval
setTimeout("alert('Hello world')") // ? avoid
setTimeout(function () { alert('Hello world') }) // ? ok
- 不要擴展原生對象
eslint:no-extend-native
Object.prototype.age = 21 // ? avoid
-
switch
一定要使用break
來將條件分支正常中斷
eslint:no-fallthrough
switch (filter) {
case 1:
doSomething() // ? avoid
case 2:
doSomethingElse()
}
switch (filter) {
case 1:
doSomething()
break // ? ok
case 2:
doSomethingElse()
}
switch (filter) {
case 1:
doSomething()
// fallthrough // ? ok
case 2:
doSomethingElse()
}
- 如果有更好的實現(xiàn)套么,盡量不要使用三元表達式
eslint:no-unneeded-ternary
let score = val ? val : 0 // ? avoid
let score = val || 0 // ? ok
-
return狼纬,throw抖甘,continue
和break
后不要再跟代碼爷绘。
eslint:no-unreachable
function doSomething () {
return true
console.log('never called') // ? avoid
}
- 避免不必要的
.call()
和.apply()
eslint:no-useless-call
sum.call(null, 1, 2, 3) // ? avoid
- ajax返回的數(shù)據(jù)需要明確字段類型鲤屡,需要進行空值判斷
let arr = response.data || [];
let obj = response.data || {};
注釋
//
與注釋內(nèi)容之前需要空格請盡量使用
es6孝治、es7
語法
const func = (arguments) => {
// do someting
}
let {a = 0, b = {}} = obj;
async / await
...
未完待續(xù)...