ESLint javascript格式要求

來源:互聯網
上載者:User
首行縮排2個空格 eslint:  indent
functionhello (name) {  console.log('hi', name)}
字串使用單引號(除了避免轉義) eslint:  quotes
console.log('hello there')$("<div class='box'>")
禁止出現未使用的變數 eslint:  no-unused-vars
functionmyFunction () {  var result =something()   // ✗ avoid}
關鍵字後增加一個空格 eslint:  keyword-spacing
if (condition) { ... }   // ✓ ok
if(condition) { ... }    // ✗ avoid
函式宣告的圓括弧前增加一個空格 eslint:  space-before-function-paren
functionname (arg) { ... }   // ✓ ok
functionname(arg) { ... }    // ✗ avoid
run(function () { ... })      // ✓ ok
run(function() { ... })       // ✗ avoid
使用嚴格運算子===替代相等運算子== 除了obj == null 允許檢查 null || undefined eslint:  eqeqeq
if (name === 'John')   // ✓ okif (name == 'John')    // ✗ avoid
if (name !== 'John')   // ✓ okif (name != 'John')    // ✗ avoid
中綴運算子必須使用一個空格分開 eslint:  space-infix-ops
// ✓ okvar x = 2var message = 'hello, ' + name + '!'
// ✗ avoidvar x=2var message = 'hello, '+name+'!'
逗號後面緊跟一個空格 eslint:  comma-spacing
// ✓ okvar list = [1, 2, 3, 4]function greet (name, options) { ... }
// ✗ avoidvar list = [1,2,3,4]function greet (name,options) { ... }
else和後括弧保持在同一行 eslint:  brace-style
// ✓ okif (condition) {  // ...} else {  // ...}
// ✗ avoidif (condition) {  // ...}else {  // ...}
多行if聲明,使用花括弧 eslint:  curly
// ✓ okif (options.quiet !== true) console.log('done')
// ✓ okif (options.quiet !== true) {  console.log('done')}
// ✗ avoidif (options.quiet !== true)  console.log('done')
函數參數err必須處理 eslint:  handle-callback-err
// ✓ okrun(function (err) {  if (err) throw err  window.alert('done')})
// ✗ avoidrun(function (err) {  window.alert('done')})
必須使用瀏覽器全域參數window.首碼,除了document,console,navigator eslint:  no-undef
window.alert('hi')   // ✓ ok
多個空白行是不允許的 eslint:  no-multiple-empty-lines
// ✓ okvar value = 'hello world'console.log(value)
// ✗ avoidvar value = 'hello world'console.log(value)
使用多行書寫三元運算式時,?和:放在所屬行 eslint: 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.