ESLint javascript格式要求
indent
- functionhello (name) {
- console.log('hi', name)
- }
字符串使用单引号(除了避免转义)
quotes
- console.log('hello there')
- $("<div class='box'>")
禁止出现未使用的变量
no-unused-vars
- functionmyFunction () {
- var result =something() // ✗ avoid
- }
关键字后增加一个空格
keyword-spacing
- if (condition) { ... } // ✓ ok
- if(condition) { ... } // ✗ avoid
函数声明的圆括号前增加一个空格
space-before-function-paren
- functionname (arg) { ... } // ✓ ok
- functionname(arg) { ... } // ✗ avoid
- run(function () { ... }) // ✓ ok
- run(function() { ... }) // ✗ avoid
eqeqeq
- if (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-undef
- window.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-location
- console.
- log('hello') // ✗ avoid
- console
- .log('hello') // ✓ ok
文件以换行符结束
eol-last
func-call-spacing
- console.log ('hello') // ✗ avoid
- console.log('hello') // ✓ ok
键值对表达式的冒号和值之间要有空格
key-spacing
- var obj = { 'key' : 'value' } // ✗ avoid
- var obj = { 'key' :'value' } // ✗ avoid
- var obj = { 'key':'value' } // ✗ avoid
- var obj = { 'key': 'value' } // ✓ ok
构造方法的名字首字母大写
new-cap
- function animal () {}
- var dog = new animal() // ✗ avoid
- function Animal () {}
- var dog = new Animal() // ✓ ok
无参构造方法调用时必须有圆括号
new-parens
- function Animal () {}
- var dog = new Animal // ✗ avoid
- var dog = new Animal() // ✓ ok
一个对象有setter方法的前提必须先订单getter方法
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
- }
- }
派生类(继承类)的构造才能调用super
constructor-super
- class Dog {
- constructor () {
- super() // ✗ avoid
- }
- }
- class Dog extends Mammal {
- constructor () {
- super() // ✓ ok
- }
- }
使用数组字面量代替数组构造函数
no-array-constructor
- var nums = new Array(1, 2, 3) // ✗ avoid
- var nums = [1, 2, 3] // ✓ ok
避免使用arguments.callee和arguments.caller
no-caller
- function foo (n) {
- if (n <= 0) return
- arguments.callee(n - 1) // ✗ avoid
- }
- function foo (n) {
- if (n <= 0) return
- foo(n - 1)
- }
避免修改类声明变量
no-class-assign
- class Dog {}
- Dog = 'Fido' // ✗ avoid
避免修改const声明的变量
no-const-assign
- const score = 100
- score = 125 // ✗ avoid
避免在条件中使用常量表达式(循环例外)
no-constant-condition
- if (false) { // ✗ avoid
- // ...
- }
- if (x === 0) { // ✓ ok
- // ...
- }
- while (true) { // ✓ ok
- // ...
- }
正则表达式中禁止使用控制字符
no-control-regex
- var pattern = /\x1f/ // ✗ avoid
- var pattern = /\x20/ // ✓ ok
不要使用debugger表达式
no-debugger
- function sum (a, b) {
- debugger // ✗ avoid
- return a + b
- }
不要在变量上使用delete运算符
no-delete-var
- var name
- delete name // ✗ avoid
函数定义时不要使用重复的参数
no-dupe-args
- function sum (a, b, a) { // ✗ avoid
- // ...
- }
- function sum (a, b, c) { // ✓ ok
- // ...
- }
no-dupe-class-members
- class Dog {
- bark () {}
- bark () {} // ✗ avoid
- }
对象字面量中不能出现重复键值
no-dupe-keys
- var user = {
- name: 'Jane Doe',
- name: 'John Doe' // ✗ avoid
- }
switch表达式中不能出现重复的case值
no-duplicate-case
- switch (id) {
- case 1:
- // ...
- case 1: // ✗ avoid
- }
每一个模块只能有一个import表达式
no-duplicate-imports
- import { myFunc1 } from 'module'
- import { myFunc2 } from 'module' // ✗ avoid
- import { myFunc1, myFunc2 } from 'module' // ✓ ok
正则表达式中不能有空的字母类
no-empty-character-class
- const myRegex = /^abc[]/ // ✗ avoid
- const myRegex = /^abc[a-z]/ // ✓ ok
不能有空的结构模式
no-empty-pattern
- const { a: {} } = foo // ✗ avoid
- const { a: { b } } = foo // ✓ ok
禁止使用eval()
no-eval
- eval( "var result = user." + propName ) // ✗ avoid
- var result = user[propName] // ✓ ok
catch子句中不能重新分配异常
no-ex-assign
- try {
- // ...
- } catch (e) {
- e = 'new value' // ✗ avoid
- }
- try {
- // ...
- } catch (e) {
- const newVal = 'new value' // ✓ ok
- }
不能扩展本地对象
no-extend-native
- Object.prototype.age = 21 // ✗ avoid
避免不必要的函数绑定
no-extra-bind
- const name = function () {
- getName()
- }.bind(user) // ✗ avoid
- const name = function () {
- this.getName()
- }.bind(user) // ✓ ok
避免不必要的布尔操作
no-extra-boolean-cast
- const result = true
- if (!!result) { // ✗ avoid
- // ...
- }
- const result = true
- if (result) { // ✓ ok
- // ...
- }
避免不必要的括号包裹函数表达式
no-extra-parens
- const myFunc = (function () { }) // ✗ avoid
- const myFunc = function () { } // ✓ ok
switch的case表达式处理中使用break阻止继续下沉
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()
- }
不能有浮点小数
no-floating-decimal
- const discount = .5 // ✗ avoid
- const discount = 0.5 // ✓ ok
避免重新分配函数声明
no-func-assign
- function myFunc () { }
- myFunc = myOtherFunc // ✗ avoid
避免重新分配只读全局变量
no-global-assign
- window = {} // ✗ avoid
不使用隐式的函数
no-implied-eval
- setTimeout("alert('Hello world')") // ✗ avoid
- setTimeout(function () { alert('Hello world') }) // ✓ ok
嵌套块中不能有函数声明
no-inner-declarations
- if (authenticated) {
- function setAuthUser () {} // ✗ avoid
- }
正则构造方法中不能出现无效的正则表达式
no-invalid-regexp
- RegExp('[a-z') // ✗ avoid
- RegExp('[a-z]') // ✓ ok
不能出现不规则的空白
no-irregular-whitespace
- function myFunc () /*<NBSP>*/{} // ✗ avoid
不能使用_iterator_
no-iterator
- Foo.prototype.__iterator__ = function () {} // ✗ avoid
标签名不能和局部变量重名
no-label-var
- var score = 100
- function game () {
- score: 50 // ✗ avoid
- }
不能有标签声明
no-labels
- label:
- while (true) {
- break label // ✗ avoid
- }
不能有无用的嵌套块
no-lone-blocks
- function myFunc () {
- { // ✗ avoid
- myOtherFunc()
- }
- }
- function myFunc () {
- myOtherFunc() // ✓ ok
- }
缩进时避免混合空格和tabs
no-mixed-spaces-and-tabs
no-multi-spaces
- const id = 1234 // ✗ avoid
- const id = 1234 // ✓ ok
禁止多行的字符串
no-multi-str
- const message = 'Hello \
- world' // ✗ avoid
没有给变量赋值对象时不要使用new
no-new
- new Character() // ✗ avoid
- const character = new Character() // ✓ ok
不要使用Function构造方法
no-new-func
- var sum = new Function('a', 'b', 'return a + b') // ✗ avoid
不要使用Object构造方法
no-new-object
- let config = new Object() // ✗ avoid
不要使用new require
no-new-require
- const myModule = new require('my-module') // ✗ avoid
不要使用Symbol构造方法
no-new-symbol
- const foo = new Symbol('foo') // ✗ avoid
不要使用原始包装器实例
no-new-wrappers
- const message = new String('hello') // ✗ avoid
不要调用全局对象属性作为函数
no-obj-calls
- const math = Math() // ✗ avoid
不要使用八进制字符
no-octal
- const num = 042 // ✗ avoid
- const num = '042' // ✓ ok
字符串中不要使用八进制转义序列
no-octal-escape
- const copyright = 'Copyright \251' // ✗ avoid
字符串拼接时避免使用__dirname和__filename
no-path-concat
- const pathToFile = __dirname + '/app.js' // ✗ avoid
- const pathToFile = path.join(__dirname, 'app.js') // ✓ ok
避免使用__proto__,使用getPrototypeOf来代替
no-proto
- const foo = obj.__proto__ // ✗ avoid
- const foo = Object.getPrototypeOf(obj) // ✓ ok
不要重复声明变量
no-redeclare
- let name = 'John'
- let name = 'Jane' // ✗ avoid
- let name = 'John'
- name = 'Jane' // ✓ ok
正则表达式中避免出现多个空格
no-regex-spaces
- const regexp = /test value/ // ✗ avoid
- const regexp = /test {3}value/ // ✓ ok
- const regexp = /test value/ // ✓ ok
return表达式中赋值时使用括号包裹
no-return-assign
- function sum (a, b) {
- return result = a + b // ✗ avoid
- }
- function sum (a, b) {
- return (result = a + b) // ✓ ok
- }
避免给变量本身赋值
no-self-assign
- name = name // ✗ avoid
避免变量本身比较
no-self-compare
- if (score === score) {} // ✗ avoid
避免使用逗号表达式
no-sequences
- if (doSomething(), !!test) {} // ✗ avoid
受限关键字不能使用
no-shadow-restricted-names
- let undefined = 'value' // ✗ avoid
不允许稀疏数组出现
no-sparse-arrays
- let fruits = ['apple',, 'orange'] // ✗ avoid
Tabs禁止使用
no-tabs
no-template-curly-in-string
- const message = 'Hello ${name}' // ✗ avoid
- const message = `Hello ${name}` // ✓ ok
super()必须在this之前调用
no-this-before-super
- class Dog extends Animal {
- constructor () {
- this.legs = 4 // ✗ avoid
- super()
- }
- }
只能抛出Error对象
no-throw-literal
- throw 'error' // ✗ avoid
- throw new Error('error') // ✓ ok
行尾不允许有空格
no-trailing-spaces
no-undef-init
- let name = undefined // ✗ avoid
- let name
- name = 'value' // ✓ ok
循环语句中循环条件要变更
no-unmodified-loop-condition
- for (let i = 0; i < items.length; j++) {...} // ✗ avoid
- for (let i = 0; i < items.length; i++) {...} // ✓ ok
有更简单的形式存在时,不要使用三元表达式
no-unneeded-ternary
- let score = val ? val : 0 // ✗ avoid
- let score = val || 0 // ✓ ok
return, throw, continue, break后不要有代码
no-unreachable
- function doSomething () {
- return true
- console.log('never called') // ✗ avoid
- }
finally语句块中不要有控制流语句
no-unsafe-finally
- try {
- // ...
- } catch (e) {
- // ...
- } finally {
- return 42 // ✗ avoid
- }
关系运算符的左操作数不能否定
no-unsafe-negation
- if (!key in obj) {} // ✗ avoid
避免不必要的使用.call()和.apply()
no-useless-call
- sum.call(null, 1, 2, 3) // ✗ avoid
在对象中避免使用不必要的计算属性键
no-useless-computed-key
- const user = { ['name']: 'John Doe' } // ✗ avoid
- const user = { name: 'John Doe' } // ✓ ok
不必要的构造方法
no-useless-constructor
- class Car {
- constructor () { // ✗ avoid
- }
- }
避免不必要的转义
no-useless-escape
- let message = 'Hell\o' // ✗ avoid
导入、导出和解构赋值,不要赋相同的名字
no-useless-rename
- import { config as config } from './config' // ✗ avoid
- import { config } from './config' // ✓ ok
属性前不要有空格
no-whitespace-before-property
- user .name // ✗ avoid
- user.name // ✓ ok
no-with
- with (val) {...} // ✗ avoid
对象属性的行分配要保持一致
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'
- }
语句块中不要有空隙
padded-blocks
- if (user) {
- // ✗ avoid
- const name = getName()
- }
- if (user) {
- const name = getName() // ✓ ok
- }
传播运算符的表达式不能有空格
rest-spread-spacing
- fn(... args) // ✗ avoid
- fn(...args) // ✓ ok
分号前不能有空格,后必须有空格
semi-spacing
- for (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid
- for (let i = 0; i < items.length; i++) {...} // ✓ ok
块语句前要有一个空格
space-before-blocks
- if (admin){...} // ✗ avoid
- if (admin) {...} // ✓ ok
圆括号内不能有空格
space-in-parens
- getName( name ) // ✗ avoid
- getName(name) // ✓ ok
一元运算符后要有空格
space-unary-ops
- typeof!admin // ✗ avoid
- typeof !admin // ✓ ok
注释内要有空格
spaced-comment
- //comment // ✗ avoid
- // comment // ✓ ok
- /*comment*/ // ✗ avoid
- /* comment */ // ✓ ok
模板字符串中不能有空格
template-curly-spacing
- const message = `Hello, ${ name }` // ✗ avoid
- const message = `Hello, ${name}` // ✓ ok
检查NaN时使用isNaN()
use-isnan
- if (price === NaN) { } // ✗ avoid
- if (isNaN(price)) { } // ✓ ok
typeof必须和有效的字符串比较
valid-typeof
- typeof name === 'undefimed' // ✗ avoid
- typeof name === 'undefined' // ✓ ok
直接调用函数表达式必要要被包装
wrap-iife
- const getName = function () { }() // ✗ avoid
- const getName = (function () { }()) // ✓ ok
- const getName = (function () { })() // ✓ ok
yield*表达式中的*前后都应有一个空格
yield-star-spacing
- yield* increment() // ✗ avoid
- yield * increment() // ✓ ok
避免使用尤达条件
yoda
- if (42 === age) { } // ✗ avoid
- if (age === 42) { } // ✓ ok
分号
semi
- window.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 属性规定一 ...
随机推荐
- python -- 犯过的错之变量作用域
1.写代码时发现取得变量值,会被覆盖,改为图二的写法后case_id则不会覆盖. 原因:可以理解为变量是内存中一个对象的“引用”.在函数参数传值时,变量也是内存对象的引用. 当对象为可更改对象时,是引 ...
- 从GitLab上拉到本地仓库的项目导入到eclipse中
拉项目 在本地仓库中右键git clone,填写地址 OK, 然后在拉下来的项目上面右键检出创建dev分支. 要将新分支导入到eclipse中, 如果是没有导入过就选第三个,导入过就选第一个. 然后O ...
- MyBatis 关联查询的实现:一对一
有2个实体:用户.会员卡,一个用户只能办理一张会员卡,即一对一. user_tb : 需要在一方引入另一方的主键作为外键. card_tb: 使用扩展类 (1)在pojo包下新建User类: pack ...
- 使用DOM4J生成XML文档
package xml; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; imp ...
- 使用navicat连接mysql8.0.12版本 出现client does not support。。。解决办法
navicat版本的问题 出现连接失败的原因:mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password ...
- request和response的setCharacterEncoding()方法
1.pageEncoding=”UTF-8”的作用是设置JSP编译成Servlet时使用的编码.2.contentType=”text/html;charset=UTF-8”的作用是指定服务器响应给浏 ...
- 吴裕雄--天生自然TensorFlow2教程:合并与分割
import tensorflow as tf # 6个班级的学生分数情况 a = tf.ones([4, 35, 8]) b = tf.ones([2, 35, 8]) c = tf.concat( ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:输出
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- LinkedList源码阅读笔记
LinkedList LinkedList是双向链表,不循环(1.6之前循环),继承AbstractSequentialList类,实现了List, Deque, Cloneable接口. 链表的特点 ...
- 动手动脑 4 String 类
动手动脑1: 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 在Java中,内容相同的字串常量(“Hello”)只保存一份以 ...