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 属性规定一 ...
随机推荐
- UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)
题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...
- 51nod 算法马拉松3 A:序列分解
序列分解 System Message (命题人) 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 小刀和大刀是双胞胎兄弟.今天他们玩一个有意思的游戏. 大刀给小刀准备了一个长度为n ...
- 二、在SAP中创建一个程序
一.我们来到SE38 二.添加一个程序的名字,需要以Y或者Z开头,点击创建就可以了 三.我们输入hello Sap,然后选择可执行程序,然后保存 四.创建对象目录时,可以选择把这个加入到包中,或者选择 ...
- JS高级学习笔记(6)- 事件循环
参考文章:深入理解JS引擎的执行机制 JavaScript 异步.栈.事件循环.任务队列 我的笔记:ES系列之Promise async 和 await Event Loop 前提 js ...
- vmware桥接模式-无法内网通-克隆机要删除的文件-ssl
网卡太多自动模式有时候无法正常通信需要绑定外部网卡 rm /etc/udev/rules.d/-persistent-ipoib.rules vmware报错 Microsoft Runtime DL ...
- (排序)P1781 宇宙总统
题解: 此题的关键不在排序,而在于大数字 我们可以用字符串进行存储,比较他们的长度,长度一样时比较他们的大小即可 #include<iostream>using namespace std ...
- 2020/2/5 php编程学习
一事无成的上午..就安了框架解决了一些报错信息 下面简单了解一下 安装一下框架: 什么是路由: 系统从URI(唯一资源定位器)参数中分析出当前请求的分组(平台),控制器和操作方法的过程就是路由. UR ...
- part11 Vue项目接口联调//真机测试
何为项目接口联调? 前端代码编译好了 后端接口写好了 我们就需要去掉前端模拟数据干掉 用后端提供的数据.进行前后端的一个调试 如何联调? config目录下面 index.js 文件 dev 中pr ...
- Codeforces 1299B/1300D - Aerodynamic
题目大意: 给定一个图形S,让这个图形任意平移,但是要保证原点(0,0)一直在它的内部或者边上 最后把它能移动到的所有位置进行拼合可以得到一个图形T 问图形S与图形T是否相似 点会按照逆时针顺序给出 ...
- AT1983 BBQ Hard 解题报告
题意 求\(\sum_{i=1}^{n} \sum_{j=i+1}^{n} \dbinom{a_i+a_j}{a_i+b_i+a_j+b_j}\) 解法 考虑\(\dbinom{a_i+a_j}{a_ ...