ESLint javascript格式要求
indentfunctionhello (name) {
console.log('hi', name)
}
字符串使用单引号(除了避免转义)
quotesconsole.log('hello there')
$("<div class='box'>")
禁止出现未使用的变量
no-unused-varsfunctionmyFunction () {
var result =something() // ✗ avoid
}
关键字后增加一个空格
keyword-spacingif (condition) { ... } // ✓ ok
if(condition) { ... } // ✗ avoid
函数声明的圆括号前增加一个空格
space-before-function-parenfunctionname (arg) { ... } // ✓ ok
functionname(arg) { ... } // ✗ avoid
run(function () { ... }) // ✓ ok
run(function() { ... }) // ✗ avoid
eqeqeqif (name === 'John') // ✓ ok
if (name == 'John') // ✗ avoid
if (name !== 'John') // ✓ ok
if (name != 'John') // ✗ avoid
中缀运算符必须使用一个空格分开
space-infix-ops// ✓ ok
var x = 2
var message = 'hello, ' + name + '!'
// ✗ avoid
var x=2
var message = 'hello, '+name+'!'
逗号后面紧跟一个空格
comma-spacing// ✓ ok
var list = [1, 2, 3, 4]
function greet (name, options) { ... }
// ✗ avoid
var list = [1,2,3,4]
function greet (name,options) { ... }
else和后括号保持在同一行
brace-style// ✓ ok
if (condition) {
// ...
} else {
// ...
}
// ✗ avoid
if (condition) {
// ...
}
else {
// ...
}
多行if声明,使用花括号
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')
函数参数err必须处理
handle-callback-err// ✓ ok
run(function (err) {
if (err) throw err
window.alert('done')
})
// ✗ avoid
run(function (err) {
window.alert('done')
})
必须使用浏览器全局参数window.前缀,除了document,console,navigator
no-undefwindow.alert('hi') // ✓ ok
多个空白行是不允许的
no-multiple-empty-lines// ✓ ok
var value = 'hello world'
console.log(value)
// ✗ avoid
var value = 'hello world' console.log(value)
使用多行书写三元表达式时,?和:放在所属行
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'
使用var声明时,一个声明使用一个表达式
one-var// ✓ ok
var silent = true
var verbose = true // ✗ avoid
var silent = true, verbose = true // ✗ avoid
var silent = true,
verbose = true
使用额外的括号包裹额外的赋值
no-cond-assign// ✓ ok
while ((m = text.match(expr))) {
// ...
} // ✗ avoid
while (m = text.match(expr)) {
// ...
}
单行块里首尾增加空格
block-spacing function foo () {return true} // ✗ avoid
function foo () { return true } // ✓ ok
变量和函数命名使用驼峰法
camelcase function my_function () { } // ✗ avoid
function myFunction () { } // ✓ ok
var my_var = 'hello' // ✗ avoid
var myVar = 'hello' // ✓ ok
禁止尾部加逗号
comma-dangle var obj = {
message: 'hello', // ✗ avoid
}
逗号放在当前行的末尾
comma-style var obj = {
foo: 'foo'
,bar: 'bar' // ✗ avoid
}
var obj = {
foo: 'foo',
bar: 'bar' // ✓ ok
}
点号和属性保持同一行
dot-locationconsole.
log('hello') // ✗ avoid console
.log('hello') // ✓ ok
文件以换行符结束
eol-lastfunc-call-spacingconsole.log ('hello') // ✗ avoid
console.log('hello') // ✓ ok
键值对表达式的冒号和值之间要有空格
key-spacingvar obj = { 'key' : 'value' } // ✗ avoid
var obj = { 'key' :'value' } // ✗ avoid
var obj = { 'key':'value' } // ✗ avoid
var obj = { 'key': 'value' } // ✓ ok
构造方法的名字首字母大写
new-capfunction animal () {}
var dog = new animal() // ✗ avoid
function Animal () {}
var dog = new Animal() // ✓ ok
无参构造方法调用时必须有圆括号
new-parensfunction Animal () {}
var dog = new Animal // ✗ avoid
var dog = new Animal() // ✓ ok
一个对象有setter方法的前提必须先订单getter方法
accessor-pairsvar person = {
set name (value) { // ✗ avoid
this.name = value
}
}
var person = {
set name (value) {
this.name = value
},
get name () { // ✓ ok
return this.name
}
}
派生类(继承类)的构造才能调用super
constructor-superclass Dog {
constructor () {
super() // ✗ avoid
}
}
class Dog extends Mammal {
constructor () {
super() // ✓ ok
}
}
使用数组字面量代替数组构造函数
no-array-constructorvar nums = new Array(1, 2, 3) // ✗ avoid
var nums = [1, 2, 3] // ✓ ok
避免使用arguments.callee和arguments.caller
no-callerfunction foo (n) {
if (n <= 0) return
arguments.callee(n - 1) // ✗ avoid
}
function foo (n) {
if (n <= 0) return
foo(n - 1)
}
避免修改类声明变量
no-class-assignclass Dog {}
Dog = 'Fido' // ✗ avoid
避免修改const声明的变量
no-const-assignconst score = 100
score = 125 // ✗ avoid
避免在条件中使用常量表达式(循环例外)
no-constant-conditionif (false) { // ✗ avoid
// ...
}
if (x === 0) { // ✓ ok
// ...
}
while (true) { // ✓ ok
// ...
}
正则表达式中禁止使用控制字符
no-control-regexvar pattern = /\x1f/ // ✗ avoid
var pattern = /\x20/ // ✓ ok
不要使用debugger表达式
no-debuggerfunction sum (a, b) {
debugger // ✗ avoid
return a + b
}
不要在变量上使用delete运算符
no-delete-varvar name
delete name // ✗ avoid
函数定义时不要使用重复的参数
no-dupe-argsfunction sum (a, b, a) { // ✗ avoid
// ...
}
function sum (a, b, c) { // ✓ ok
// ...
}
no-dupe-class-membersclass Dog {
bark () {}
bark () {} // ✗ avoid
}
对象字面量中不能出现重复键值
no-dupe-keysvar user = {
name: 'Jane Doe',
name: 'John Doe' // ✗ avoid
}
switch表达式中不能出现重复的case值
no-duplicate-caseswitch (id) {
case 1:
// ...
case 1: // ✗ avoid
}
每一个模块只能有一个import表达式
no-duplicate-importsimport { myFunc1 } from 'module'
import { myFunc2 } from 'module' // ✗ avoid
import { myFunc1, myFunc2 } from 'module' // ✓ ok
正则表达式中不能有空的字母类
no-empty-character-classconst myRegex = /^abc[]/ // ✗ avoid
const myRegex = /^abc[a-z]/ // ✓ ok
不能有空的结构模式
no-empty-patternconst { a: {} } = foo // ✗ avoid
const { a: { b } } = foo // ✓ ok
禁止使用eval()
no-evaleval( "var result = user." + propName ) // ✗ avoid
var result = user[propName] // ✓ ok
catch子句中不能重新分配异常
no-ex-assigntry {
// ...
} catch (e) {
e = 'new value' // ✗ avoid
}
try {
// ...
} catch (e) {
const newVal = 'new value' // ✓ ok
}
不能扩展本地对象
no-extend-nativeObject.prototype.age = 21 // ✗ avoid
避免不必要的函数绑定
no-extra-bindconst name = function () {
getName()
}.bind(user) // ✗ avoid
const name = function () {
this.getName()
}.bind(user) // ✓ ok
避免不必要的布尔操作
no-extra-boolean-castconst result = true
if (!!result) { // ✗ avoid
// ...
} const result = true
if (result) { // ✓ ok
// ...
}
避免不必要的括号包裹函数表达式
no-extra-parensconst myFunc = (function () { }) // ✗ avoid
const myFunc = function () { } // ✓ ok
switch的case表达式处理中使用break阻止继续下沉
no-fallthroughswitch (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()
}
不能有浮点小数
no-floating-decimalconst discount = .5 // ✗ avoid
const discount = 0.5 // ✓ ok
避免重新分配函数声明
no-func-assignfunction myFunc () { }
myFunc = myOtherFunc // ✗ avoid
避免重新分配只读全局变量
no-global-assignwindow = {} // ✗ avoid
不使用隐式的函数
no-implied-evalsetTimeout("alert('Hello world')") // ✗ avoid
setTimeout(function () { alert('Hello world') }) // ✓ ok
嵌套块中不能有函数声明
no-inner-declarationsif (authenticated) {
function setAuthUser () {} // ✗ avoid
}
正则构造方法中不能出现无效的正则表达式
no-invalid-regexpRegExp('[a-z') // ✗ avoid
RegExp('[a-z]') // ✓ ok
不能出现不规则的空白
no-irregular-whitespacefunction myFunc () /*<NBSP>*/{} // ✗ avoid
不能使用_iterator_
no-iteratorFoo.prototype.__iterator__ = function () {} // ✗ avoid
标签名不能和局部变量重名
no-label-varvar score = 100
function game () {
score: 50 // ✗ avoid
}
不能有标签声明
no-labelslabel:
while (true) {
break label // ✗ avoid
}
不能有无用的嵌套块
no-lone-blocksfunction myFunc () {
{ // ✗ avoid
myOtherFunc()
}
}
function myFunc () {
myOtherFunc() // ✓ ok
}
缩进时避免混合空格和tabs
no-mixed-spaces-and-tabsno-multi-spacesconst id = 1234 // ✗ avoid
const id = 1234 // ✓ ok
禁止多行的字符串
no-multi-strconst message = 'Hello \
world' // ✗ avoid
没有给变量赋值对象时不要使用new
no-newnew Character() // ✗ avoid
const character = new Character() // ✓ ok
不要使用Function构造方法
no-new-funcvar sum = new Function('a', 'b', 'return a + b') // ✗ avoid
不要使用Object构造方法
no-new-objectlet config = new Object() // ✗ avoid
不要使用new require
no-new-requireconst myModule = new require('my-module') // ✗ avoid
不要使用Symbol构造方法
no-new-symbolconst foo = new Symbol('foo') // ✗ avoid
不要使用原始包装器实例
no-new-wrappersconst message = new String('hello') // ✗ avoid
不要调用全局对象属性作为函数
no-obj-callsconst math = Math() // ✗ avoid
不要使用八进制字符
no-octalconst num = 042 // ✗ avoid
const num = '042' // ✓ ok
字符串中不要使用八进制转义序列
no-octal-escapeconst copyright = 'Copyright \251' // ✗ avoid
字符串拼接时避免使用__dirname和__filename
no-path-concatconst pathToFile = __dirname + '/app.js' // ✗ avoid
const pathToFile = path.join(__dirname, 'app.js') // ✓ ok
避免使用__proto__,使用getPrototypeOf来代替
no-protoconst foo = obj.__proto__ // ✗ avoid
const foo = Object.getPrototypeOf(obj) // ✓ ok
不要重复声明变量
no-redeclarelet name = 'John'
let name = 'Jane' // ✗ avoid let name = 'John'
name = 'Jane' // ✓ ok
正则表达式中避免出现多个空格
no-regex-spacesconst regexp = /test value/ // ✗ avoid
const regexp = /test {3}value/ // ✓ ok
const regexp = /test value/ // ✓ ok
return表达式中赋值时使用括号包裹
no-return-assignfunction sum (a, b) {
return result = a + b // ✗ avoid
}
function sum (a, b) {
return (result = a + b) // ✓ ok
}
避免给变量本身赋值
no-self-assignname = name // ✗ avoid
避免变量本身比较
no-self-compareif (score === score) {} // ✗ avoid
避免使用逗号表达式
no-sequencesif (doSomething(), !!test) {} // ✗ avoid
受限关键字不能使用
no-shadow-restricted-nameslet undefined = 'value' // ✗ avoid
不允许稀疏数组出现
no-sparse-arrayslet fruits = ['apple',, 'orange'] // ✗ avoid
Tabs禁止使用
no-tabsno-template-curly-in-stringconst message = 'Hello ${name}' // ✗ avoid
const message = `Hello ${name}` // ✓ ok
super()必须在this之前调用
no-this-before-superclass Dog extends Animal {
constructor () {
this.legs = 4 // ✗ avoid
super()
}
}
只能抛出Error对象
no-throw-literalthrow 'error' // ✗ avoid
throw new Error('error') // ✓ ok
行尾不允许有空格
no-trailing-spacesno-undef-initlet name = undefined // ✗ avoid let name
name = 'value' // ✓ ok
循环语句中循环条件要变更
no-unmodified-loop-conditionfor (let i = 0; i < items.length; j++) {...} // ✗ avoid
for (let i = 0; i < items.length; i++) {...} // ✓ ok
有更简单的形式存在时,不要使用三元表达式
no-unneeded-ternarylet score = val ? val : 0 // ✗ avoid
let score = val || 0 // ✓ ok
return, throw, continue, break后不要有代码
no-unreachablefunction doSomething () {
return true
console.log('never called') // ✗ avoid
}
finally语句块中不要有控制流语句
no-unsafe-finallytry {
// ...
} catch (e) {
// ...
} finally {
return 42 // ✗ avoid
}
关系运算符的左操作数不能否定
no-unsafe-negationif (!key in obj) {} // ✗ avoid
避免不必要的使用.call()和.apply()
no-useless-callsum.call(null, 1, 2, 3) // ✗ avoid
在对象中避免使用不必要的计算属性键
no-useless-computed-keyconst user = { ['name']: 'John Doe' } // ✗ avoid
const user = { name: 'John Doe' } // ✓ ok
不必要的构造方法
no-useless-constructorclass Car {
constructor () { // ✗ avoid
}
}
避免不必要的转义
no-useless-escapelet message = 'Hell\o' // ✗ avoid
导入、导出和解构赋值,不要赋相同的名字
no-useless-renameimport { config as config } from './config' // ✗ avoid
import { config } from './config' // ✓ ok
属性前不要有空格
no-whitespace-before-propertyuser .name // ✗ avoid
user.name // ✓ ok
no-withwith (val) {...} // ✗ avoid
对象属性的行分配要保持一致
object-property-newlineconst 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'
}
语句块中不要有空隙
padded-blocksif (user) {
// ✗ avoid
const name = getName()
}
if (user) {
const name = getName() // ✓ ok
}
传播运算符的表达式不能有空格
rest-spread-spacingfn(... args) // ✗ avoid
fn(...args) // ✓ ok
分号前不能有空格,后必须有空格
semi-spacingfor (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid
for (let i = 0; i < items.length; i++) {...} // ✓ ok
块语句前要有一个空格
space-before-blocksif (admin){...} // ✗ avoid
if (admin) {...} // ✓ ok
圆括号内不能有空格
space-in-parensgetName( name ) // ✗ avoid
getName(name) // ✓ ok
一元运算符后要有空格
space-unary-opstypeof!admin // ✗ avoid
typeof !admin // ✓ ok
注释内要有空格
spaced-comment//comment // ✗ avoid
// comment // ✓ ok /*comment*/ // ✗ avoid
/* comment */ // ✓ ok
模板字符串中不能有空格
template-curly-spacingconst message = `Hello, ${ name }` // ✗ avoid
const message = `Hello, ${name}` // ✓ ok
检查NaN时使用isNaN()
use-isnanif (price === NaN) { } // ✗ avoid
if (isNaN(price)) { } // ✓ ok
typeof必须和有效的字符串比较
valid-typeoftypeof name === 'undefimed' // ✗ avoid
typeof name === 'undefined' // ✓ ok
直接调用函数表达式必要要被包装
wrap-iifeconst getName = function () { }() // ✗ avoid
const getName = (function () { }()) // ✓ ok
const getName = (function () { })() // ✓ ok
yield*表达式中的*前后都应有一个空格
yield-star-spacingyield* increment() // ✗ avoid
yield * increment() // ✓ ok
避免使用尤达条件
yodaif (42 === age) { } // ✗ avoid
if (age === 42) { } // ✓ ok
分号
semiwindow.alert('hi') // ✓ ok
window.alert('hi'); // ✗ avoid
开始行不要以(,[,`开头,否则前面要加分号
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')
ESLint javascript格式要求的更多相关文章
- 屏蔽eslint代码格式报错
1.在文件中找到node_modules 2.node_modules文件夹下的eslint-config-standard 3.打开eslint-config-standard文件夹下的eslint ...
- JavaScript 日期格式
有四种 JavaScript 日期输入格式: 类型 实例 ISO 日期 "2018-02-19" (国际标准) 短日期 "02/19/2018" 或者 &quo ...
- 一统江湖的大前端(5)editorconfig + eslint——你的代码里藏着你的优雅
<一统江湖的大前端>系列是自己的前端学习笔记,旨在介绍javascript在非网页开发领域的应用案例和发现各类好玩的js库,不定期更新.如果你对前端的理解还是写写页面绑绑事件,那你真的是有 ...
- 轻量高效的开源JavaScript插件和库 【转】
图片 布局 轮播图 弹出层 音频视频 编辑器 字符串 表单 存储 动画 时间 其它 加载器 构建工具 测试 包管理器 CDN 图片 baguetteBox.js - 是一个简单易用的响应式图像灯箱效果 ...
- VSCode常用插件之ESLint使用
更多VSCode插件使用请访问:VSCode常用插件汇总 ESLint这是VS Code ESLint扩展,将ESLint JavaScript集成到VS Code中. 首先简单说一下使用流程: 1. ...
- Json时间格式转换问题
很多时候在数据库中取出数据,需要用Json来接收,但是接受出来的数据竟然是:/Date(1386040883000+0800)/ 这种格式. 这个时候就需要将Json格式,转换成Javascript格 ...
- 文本输入框和下拉菜单特效-用正则表达式验证E-mail格式
———————————————————————————— <script type="text/javascript"> ...
- 用 React 整合 LogEntries JavaScript 库
[编者按]本文作者为 David Posin,主要介绍 React 与 LogEntries 间的相互操作.本文系国内 ITOM 管理平台 OneAPM 编译呈现. 众所周知,React.js已经被证 ...
- 原生javascript实现异步的7种方式
1.$(document).ready 点评: 需要引用jquery :兼容所有浏览器. 2.标签的async=”async”属性 async的定义和用法(是HTML5的属性) async 属性规定一 ...
随机推荐
- P 1024 科学计数法
转跳点:
- iPhone系统固若金汤,为何还有人偷苹果手机去卖?
2016年初,美国FBI希望苹果开发软件用以解锁一部嫌疑人的iPhone.苹果虽然帮助FBI从疑犯iCloud账户中提取数据,但有些数据仍然不可获取.由于不知道手机密码,FBI也无法打开手机.虽然在最 ...
- ORACLE时间相关问题
一.遇到一个情况数据库存入的时间格式为20180831154546,需要将其转化为日期时分秒格式的. to_date('20180831154546', 'yyyy-mm-dd hh24:mi:ss' ...
- java.sql.Date转换
---恢复内容开始--- JAVA 处理时间 - java.sql.Date.java.util.Date与数据库中的Date字段的转换方法,以及util包下的Date类与字符串的相互转换 在java ...
- HDU——Monkey and Banana 动态规划
Monkey and Banana Time Limit:2000 ...
- HZNU-ACM寒假集训Day2小结 二分答案
Day2 ---二分 这里直接给出模板 两种对应不同的情况 可以借助数轴理解 int bsearch_1(int l, int r) { while (l < r) { ; if (check( ...
- LeJOS下eclipse通过USB连接EV3
适用情况:在eclipse安装好LeJOS插件后,再通过USB连接EV3. 第一:更新驱动 在<设备管理器-网络适配器>下更新RNDIS/Ethernet Gadget,电脑需要先连接EV ...
- POJ 1401:Factorial 求一个数阶乘的末尾0的个数
Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 9349 Descri ...
- C++交换两个变量值的方法
简单地列一下交换两个变量值地几种方法. 1.通过第三方实现,这一种也是最最最常见普通的方法: void swap(int *a, int *b) { int tmp = *a; *a = *b; *b ...
- 自己组装一台1U服务器
视频资料链接 自己组装一台1U服务器 执行思路: 1.评估访问了,根据需求确定服务器要求 2.根据要求选择硬件:冗余.稳定等 3.搜索主流服务器参数进行对比,及对比价格 4.咨询IDC机房价格 DIY ...