eslint: indent正則表達式
function hello (name) { console.log('hi', name) }
eslint: quotesapi
console.log('hello there') $("<div class='box'>")
eslint: no-unused-vars數組
function myFunction () { var result = something() // ✗ avoid }
eslint: keyword-spacing瀏覽器
if (condition) { ... } // ✓ ok if(condition) { ... } // ✗ avoid
eslint: space-before-function-parenapp
function name (arg) { ... } // ✓ ok function name(arg) { ... } // ✗ avoid run(function () { ... }) // ✓ ok run(function() { ... }) // ✗ avoid
例外: obj == null 能夠用來檢查 null || undefined。less
eslint: eqeqeqcurl
if (name === 'John') // ✓ ok if (name == 'John') // ✗ avoid if (name !== 'John') // ✓ ok if (name != 'John') // ✗ avoid
eslint: space-infix-ops函數
// ✓ ok var x = 2 var message = 'hello, ' + name + '!' // ✗ avoid var x=2 var message = 'hello, '+name+'!'
eslint: comma-spacingoop
// ✓ ok var list = [1, 2, 3, 4] function greet (name, options) { ... } // ✗ avoid var list = [1,2,3,4] function greet (name,options) { ... }
eslint: brace-styleui
// ✓ ok if (condition) { // ... } else { // ... } // ✗ avoid if (condition) { // ... } else { // ... }
。
eslint: curly
// ✓ ok if (options.quiet !== true) console.log('done') // ✓ ok if (options.quiet !== true) { console.log('done') } // ✗ avoid if (options.quiet !== true) console.log('done')
eslint: handle-callback-err
// ✓ ok run(function (err) { if (err) throw err window.alert('done') }) // ✗ avoid run(function (err) { window.alert('done') })
Exceptions are: document, console and navigator.
eslint: no-undef
window.alert('hi') // ✓ ok
eslint: no-multiple-empty-lines
// ✓ ok var value = 'hello world' console.log(value) // ✗ avoid var value = 'hello world' console.log(value)
eslint: operator-linebreak
// ✓ ok var location = env.development ? 'localhost' : 'www.api.com' // ✓ ok var location = env.development ? 'localhost' : 'www.api.com' // ✗ avoid var location = env.development ? 'localhost' : 'www.api.com'
eslint: one-var
// ✓ ok var silent = true var verbose = true // ✗ avoid var silent = true, verbose = true // ✗ avoid var silent = true, verbose = true
這樣使得代碼更加清晰可讀,而不會認爲是將條件判斷語句的全等號(===)錯寫成了等號(=)。
eslint: no-cond-assign
// ✓ ok while ((m = text.match(expr))) { // ... } // ✗ avoid while (m = text.match(expr)) { // ... }
eslint: block-spacing
function foo () {return true} // ✗ avoid function foo () { return true } // ✓ ok
eslint: camelcase
function my_function () { } // ✗ avoid function myFunction () { } // ✓ ok var my_var = 'hello' // ✗ avoid var myVar = 'hello' // ✓ ok
eslint: comma-dangle
var obj = { message: 'hello', // ✗ avoid }
eslint: comma-style
var obj = { foo: 'foo' ,bar: 'bar' // ✗ avoid } var obj = { foo: 'foo', bar: 'bar' // ✓ ok }
eslint: dot-location
console. log('hello') // ✗ avoid console .log('hello') // ✓ ok
eslint: eol-last
eslint: func-call-spacing
console.log ('hello') // ✗ avoid console.log('hello') // ✓ ok
eslint: key-spacing
var obj = { 'key' : 'value' } // ✗ avoid var obj = { 'key' :'value' } // ✗ avoid var obj = { 'key':'value' } // ✗ avoid var obj = { 'key': 'value' } // ✓ ok
eslint: new-cap
function animal () {} var dog = new animal() // ✗ avoid function Animal () {} var dog = new Animal() // ✓ ok
eslint: new-parens
function Animal () {} var dog = new Animal // ✗ avoid var dog = new Animal() // ✓ ok
eslint: accessor-pairs
var person = { set name (value) { // ✗ avoid this._name = value } } var person = { set name (value) { this._name = value }, get name () { // ✓ ok return this._name } }
eslint: constructor-super
class Dog { constructor () { super() // ✗ avoid } } class Dog extends Mammal { constructor () { super() // ✓ ok } }
eslint: no-array-constructor
var nums = new Array(1, 2, 3) // ✗ avoid var nums = [1, 2, 3] // ✓ ok
eslint: no-caller
function foo (n) { if (n <= 0) return arguments.callee(n - 1) // ✗ avoid } function foo (n) { if (n <= 0) return foo(n - 1) }
eslint: no-class-assign
class Dog {} Dog = 'Fido' // ✗ avoid
eslint: no-const-assign
const score = 100 score = 125 // ✗ avoid
eslint: no-constant-condition
if (false) { // ✗ avoid // ... } if (x === 0) { // ✓ ok // ... } while (true) { // ✓ ok // ... }
eslint: no-control-regex
var pattern = /\x1f/ // ✗ avoid var pattern = /\x20/ // ✓ ok
eslint: no-debugger
function sum (a, b) { debugger // ✗ avoid return a + b }
。
eslint: no-delete-var
var name delete name // ✗ avoid
eslint: no-dupe-args
function sum (a, b, a) { // ✗ avoid // ... } function sum (a, b, c) { // ✓ ok // ... }
eslint: no-dupe-class-members
class Dog { bark () {} bark () {} // ✗ avoid }
eslint: no-dupe-keys
var user = { name: 'Jane Doe', name: 'John Doe' // ✗ avoid }
eslint: no-duplicate-case
switch (id) { case 1: // ... case 1: // ✗ avoid }
eslint: no-duplicate-imports
import { myFunc1 } from 'module' import { myFunc2 } from 'module' // ✗ avoid import { myFunc1, myFunc2 } from 'module' // ✓ ok
eslint: no-empty-character-class
const myRegex = /^abc[]/ // ✗ avoid const myRegex = /^abc[a-z]/ // ✓ ok
eslint: no-empty-pattern
const { a: {} } = foo // ✗ avoid const { a: { b } } = foo // ✓ ok
eslint: no-eval
eval( "var result = user." + propName ) // ✗ avoid var result = user[propName] // ✓ ok
eslint: no-ex-assign
try { // ... } catch (e) { e = 'new value' // ✗ avoid } try { // ... } catch (e) { const newVal = 'new value' // ✓ ok }
eslint: no-extend-native
Object.prototype.age = 21 // ✗ avoid
eslint: no-extra-bind
const name = function () { getName() }.bind(user) // ✗ avoid const name = function () { this.getName() }.bind(user) // ✓ ok
eslint: no-extra-boolean-cast
const result = true if (!!result) { // ✗ avoid // ... } const result = true if (result) { // ✓ ok // ... }
eslint: no-extra-parens
const myFunc = (function () { }) // ✗ avoid const myFunc = function () { } // ✓ ok
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() }
eslint: no-floating-decimal
const discount = .5 // ✗ avoid const discount = 0.5 // ✓ ok
eslint: no-func-assign
function myFunc () { } myFunc = myOtherFunc // ✗ avoid
eslint: no-global-assign
window = {} // ✗ avoid
eslint: no-implied-eval
setTimeout("alert('Hello world')") // ✗ avoid setTimeout(function () { alert('Hello world') }) // ✓ ok
eslint: no-inner-declarations
if (authenticated) { function setAuthUser () {} // ✗ avoid }
eslint: no-invalid-regexp
RegExp('[a-z') // ✗ avoid RegExp('[a-z]') // ✓ ok
eslint: no-irregular-whitespace
function myFunc () /*<NBSP>*/{} // ✗ avoid
eslint: no-iterator
Foo.prototype.__iterator__ = function () {} // ✗ avoid
eslint: no-label-var
var score = 100 function game () { score: while (true) { // ✗ avoid score -= 10 if (score > 0) continue score break } }
eslint: no-labels
label: while (true) { break label // ✗ avoid }
eslint: no-lone-blocks
function myFunc () { { // ✗ avoid myOtherFunc() } } function myFunc () { myOtherFunc() // ✓ ok }
eslint: no-mixed-spaces-and-tabs
eslint: no-multi-spaces
const id = 1234 // ✗ avoid const id = 1234 // ✓ ok
eslint: no-multi-str
const message = 'Hello \ world' // ✗ avoid
eslint: no-new
new Character() // ✗ avoid const character = new Character() // ✓ ok
eslint: no-new-func
var sum = new Function('a', 'b', 'return a + b') // ✗ avoid
eslint: no-new-object
let config = new Object() // ✗ avoid
eslint: no-new-require
const myModule = new require('my-module') // ✗ avoid
eslint: no-new-symbol
const foo = new Symbol('foo') // ✗ avoid
eslint: no-new-wrappers
const message = new String('hello') // ✗ avoid
eslint: no-obj-calls
const math = Math() // ✗ avoid
eslint: no-octal
const num = 042 // ✗ avoid const num = '042' // ✓ ok
eslint: no-octal-escape
const copyright = 'Copyright \251' // ✗ avoid
eslint: no-path-concat
const pathToFile = __dirname + '/app.js' // ✗ avoid const pathToFile = path.join(__dirname, 'app.js') // ✓ ok
eslint: no-proto
const foo = obj.__proto__ // ✗ avoid const foo = Object.getPrototypeOf(obj) // ✓ ok
eslint: no-redeclare
let name = 'John' let name = 'Jane' // ✗ avoid let name = 'John' name = 'Jane' // ✓ ok
eslint: no-regex-spaces
const regexp = /test value/ // ✗ avoid const regexp = /test {3}value/ // ✓ ok const regexp = /test value/ // ✓ ok
eslint: no-return-assign
function sum (a, b) { return result = a + b // ✗ avoid } function sum (a, b) { return (result = a + b) // ✓ ok }
eslint: no-self-assign
name = name // ✗ avoid
esint: no-self-compare
if (score === score) {} // ✗ avoid
eslint: no-sequences
if (doSomething(), !!test) {} // ✗ avoid
eslint: no-shadow-restricted-names
let undefined = 'value' // ✗ avoid
eslint: no-sparse-arrays
let fruits = ['apple',, 'orange'] // ✗ avoid
eslint: no-tabs
正確使用 ES6 中的字符串模板。
eslint: no-template-curly-in-string
const message = 'Hello ${name}' // ✗ avoid const message = `Hello ${name}` // ✓ ok
eslint: no-this-before-super
class Dog extends Animal { constructor () { this.legs = 4 // ✗ avoid super() } }
eslint: no-throw-literal
throw 'error' // ✗ avoid throw new Error('error') // ✓ ok
eslint: no-trailing-spaces
eslint: no-undef-init
let name = undefined // ✗ avoid let name name = 'value' // ✓ ok
eslint: no-unmodified-loop-condition
for (let i = 0; i < items.length; j++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok
eslint: no-unneeded-ternary
let score = val ? val : 0 // ✗ avoid let score = val || 0 // ✓ ok
eslint: no-unreachable
function doSomething () { return true console.log('never called') // ✗ avoid }
eslint: no-unsafe-finally
try { // ... } catch (e) { // ... } finally { return 42 // ✗ avoid }
eslint: no-unsafe-negation
if (!key in obj) {} // ✗ avoid
eslint: no-useless-call
sum.call(null, 1, 2, 3) // ✗ avoid
eslint: no-useless-computed-key
const user = { ['name']: 'John Doe' } // ✗ avoid const user = { name: 'John Doe' } // ✓ ok
eslint: no-useless-constructor
class Car { constructor () { // ✗ avoid } }
eslint: no-useless-escape
let message = 'Hell\o' // ✗ avoid
eslint: no-useless-rename
import { config as config } from './config' // ✗ avoid import { config } from './config' // ✓ ok
eslint: no-whitespace-before-property
user .name // ✗ avoid user.name // ✓ ok
eslint: no-with
with (val) {...} // ✗ avoid
eslint: object-property-newline
const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' // ✗ avoid } const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok
eslint: padded-blocks
if (user) { // ✗ avoid const name = getName() } if (user) { const name = getName() // ✓ ok }
eslint: rest-spread-spacing
fn(... args) // ✗ avoid fn(...args) // ✓ ok
eslint: semi-spacing
for (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok
eslint: space-before-blocks
if (admin){...} // ✗ avoid if (admin) {...} // ✓ ok
eslint: space-in-parens
getName( name ) // ✗ avoid getName(name) // ✓ ok
eslint: space-unary-ops
typeof!admin // ✗ avoid typeof !admin // ✓ ok
eslint: spaced-comment
//comment // ✗ avoid // comment // ✓ ok /*comment*/ // ✗ avoid /* comment */ // ✓ ok
eslint: template-curly-spacing
const message = `Hello, ${ name }` // ✗ avoid const message = `Hello, ${name}` // ✓ ok
eslint: use-isnan
if (price === NaN) { } // ✗ avoid if (isNaN(price)) { } // ✓ ok
eslint: valid-typeof
typeof name === 'undefimed' // ✗ avoid typeof name === 'undefined' // ✓ ok
eslint: wrap-iife
const getName = function () { }() // ✗ avoid const getName = (function () { }()) // ✓ ok const getName = (function () { })() // ✓ ok
eslint: yield-star-spacing
yield* increment() // ✗ avoid yield * increment() // ✓ ok
eslint: yoda
if (42 === age) { } // ✗ avoid if (age === 42) { } // ✓ ok
關於分號
。 (參見:1,2,3)
eslint: semi
window.alert('hi') // ✓ ok window.alert('hi'); // ✗ avoid
eslint: no-unexpected-multiline
// ✓ ok ;(function () { window.alert('ok') }()) // ✗ avoid (function () { window.alert('ok') }()) // ✓ ok ;[1, 2, 3].forEach(bar) // ✗ avoid [1, 2, 3].forEach(bar) // ✓ ok ;`hello`.indexOf('o') // ✗ avoid `hello`.indexOf('o') 備註:上面的寫法只能說聰明過頭了。
譬如:
;[1, 2, 3].forEach(bar) 建議的寫法是: var nums = [1, 2, 3] nums.forEach(bar)