一、安装

npm install -g eslint 安装eslint

编辑器安装插件eslint(具体安装方法根据不同编辑器而不同)

二、使用

使用方法一:

eslint --init npm中用命令新建eslintrc.js文件

eslint yourfile.js npm中用命令检查自己文件中的错误

使用方法二:

手动在项目的根目录下新建eslintrc.*文件(.js、.json、.yaml、.yml等),进行配置(具体配置规则详见下文),即可在安装好eslint的编辑器中查看到出现错误的位置。

三、配置:http://eslint.org/docs/rules/ (以下规则文件配置一个即可,置于项目根目录下)

(1).eslintrc文件中进行配置 http://eslint.org/docs/user-guide/configuring

{
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "double"]
}
}

四、其他

1.在样式之前标注“/* eslint-disable */”,可忽略配置的规则;标注“/* esint-enable */“,开启配置的规则

2.“/* eslint-enable */“必须在“/* eslint-disable */”之后使用

3.忽略指定的内容:新建一个.eslineignore文件,例如:

# /node_modules/* and /bower_components/* ignored by default

# Ignore built files except build/index.js

build/*

!build/index.js

通过命令 eslint --ignore-path .eslintignore file.js可检测是否被忽视

4.enlint 有很多的rules,为了改变rule的设置,可以设置rule ID等同于一些规则属性:如

"off" or 0 关闭规则

"warn" or 1 打开规则,出现警告提示

"error" or 2 打开规则,出现错误提示

规则可如下定义:

"rules": {
"camel_case": 2
}

五、配置文件

{
"rules": {
"array-callback-return": "error",
"indent": ["error", 4, {"SwitchCase": 1}],
"block-spacing": "error",
"brace-style": ["error", "1tbs"],
"camelcase": ["error", { "properties": "never" }],
"callback-return": ["error", ["cb", "callback", "next"]],
"comma-spacing": "error",
"comma-style": ["error", "last"],
"consistent-return": "error",
"curly": ["error", "all"],
"default-case": "error",
"dot-notation": ["error", { "allowKeywords": false }],
"eol-last": "error",
"eqeqeq": "error",
"guard-for-in": "error",
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
"keyword-spacing": "error",
"lines-around-comment": ["error", {
"beforeBlockComment": true,
"afterBlockComment": false,
"beforeLineComment": true,
"afterLineComment": false
}],
"new-cap": "error",
"newline-after-var": ["error", "never"],
"new-parens": "error",
"no-array-constructor": "error",
"no-invalid-this": "error",
"no-multi-spaces": "error",
"no-redeclare": "error",
"no-return-assign": "error",
"no-spaced-func": "error",
"no-trailing-spaces": "error",
"semi": "error",
"semi-spacing": "error",
"quotes":["error","double"],
"space-before-function-paren": ["error", "never"],
"space-in-parens": "error",
"space-infix-ops": "error",
"space-unary-ops": ["error", {"words": true, "nonwords": false}],
"spaced-comment": "error",
"yoda": ["error", "never"],
"no-mixed-requires": "error",
"handle-callback-err": ["error", "err"]
}
}

六、配置文件说明

属性名 属性值 描述
[array-callback-return](http://eslint.org/docs/rules/array-callback-return) "error" Array执行回调函数返回语句
[indent](http://eslint.org/docs/rules/indent) ["error", 4, {"SwitchCase": 1}] 缩写格式的一致性
[block-spacing](http://eslint.org/docs/rules/block-spacing) "error" 禁止执行空间内出现'-'
[brace-style](http://eslint.org/docs/rules/brace-style) ["error","1tbs"] 代码书写格式验证
[camelcase](http://eslint.org/docs/rules/camelcase) ["error", { "properties": "never" }] 属性命名规则可以不使用驼峰命名法
[callback-return](http://eslint.org/docs/rules/callback-return) ["error", ["cb", "callback", "next"]] 回调函数需要return进行返回
[comma-spacing](http://eslint.org/docs/rules/comma-spacing) "error" 不允许在逗号前面出现空格
[comma-style](http://eslint.org/docs/rules/comma-style) ["error", "last"] 方数组元素、变量声明等直接需要逗号隔开
[consistent-return](http://eslint.org/docs/rules/consistent-return) "error" 保持return返回的一致性
[curly](http://eslint.org/docs/rules/curly) ["error", "all"] 函数或者条件判断时需要统一使用大括号
[default-case](http://eslint.org/docs/rules/default-case) "error" switch语句中必须有default条件
[dot-notation](http://eslint.org/docs/rules/dot-notation) ["error", { "allowKeywords": false }] 不允许关键字出现在变量中
[eol-last](http://eslint.org/docs/rules/eol-last) "error" 代码间间隔出现一行
[eqeqeq](http://eslint.org/docs/rules/eqeqeq) "error" 消除不安全类型的全等操作
[guard-for-in](http://eslint.org/docs/rules/guard-for-in) "error" for循环中过滤掉一下不被需要的行为
[key-spacing](http://eslint.org/docs/rules/key-spacing) ["error", { "beforeColon": false, "afterColon": true }] 键和值前保留一个空格
[keyword-spacing](http://eslint.org/docs/rules/keyword-spacing) "error" 确保字符前后空格的一致性
[lines-around-comment](http://eslint.org/docs/rules/lines-around-comment) ["error", {
"beforeBlockComment": true,
"afterBlockComment": false,
"beforeLineComment": true,
"afterLineComment": false
}]
注释前需要空行,注释后不需要空行
[new-cap](http://eslint.org/docs/rules/new-cap) "error" 构造函数首字母需要大写
[newline-after-var](http://eslint.org/docs/rules/newline-after-var) ["error", "never"] var定义后不空行
[new-parens](http://eslint.org/docs/rules/new-parens) "error" 没有参数时,构造函数也需要添加括号
[no-invalid-this](http://eslint.org/docs/rules/no-invalid-this) "error" 不允许关键字this在函数或者类的外面
[no-multi-spaces](http://eslint.org/docs/rules/no-multi-spaces) "error" 不允许键和值之间存在多个空格
[no-redeclare](http://eslint.org/docs/rules/no-redeclare) "error" 不允许重复声明
[no-return-assign](http://eslint.org/docs/rules/no-return-assign) "error" 不允许在return语句中任务
[no-spaced-func](http://eslint.org/docs/rules/no-spaced-func) "error" 调用函数时,函数名和括号之间不能有空格。
[no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces) "error" 不允许在语句后存在多余的空格
[semi](http://eslint.org/docs/rules/semi) "error" 语句以分号结尾
[semi-spacing](http://eslint.org/docs/rules/semi-spacing) "error" 分号前后不能有空格
[quotes](http://eslint.org/docs/rules/quotes) ["error","double"] 使用双引号
[space-before-function-paren](http://eslint.org/docs/rules/space-before-function-paren) "space-before-function-paren": ["error", "never"] 不允许函数括号之间存在空格
[space-in-parens](http://eslint.org/docs/rules/space-in-parens) "error" 不允许在括号里面存在空格
[space-infix-ops](http://eslint.org/docs/rules/space-infix-ops) "error" 插入符合变量之间需要添加一个空格
[space-unary-ops](http://eslint.org/docs/rules/space-unary-ops) ["error", {"words": true, "nonwords": false}] 允许一元运算符操作
[spaced-comment](http://eslint.org/docs/rules/spaced-comment) "error" 注释前需要一个空格
[yoda](http://eslint.org/docs/rules/yoda) ["error", "never"] 条件语句中,变量在赋值语句的前面
[no-mixed-requires](http://eslint.org/docs/rules/no-mixed-requires) "error" 不允许混合requires文件
[no-new-require](http://eslint.org/docs/rules/no-new-require) "error" 不允许new require出现
[no-path-concat](http://eslint.org/docs/rules/no-path-concat) "error" 不允许路径以_链接
[handle-callback-err](http://eslint.org/docs/rules/handle-callback-err) ["error", "err"] 处理错误的回调函数

eslint — js书写规范的更多相关文章

  1. 前端js书写规范和维护方案

    在网上看到一篇文章,写的是怎样来维护自己写的js.感觉挺不错的,感觉代码很漂亮,转之,代码如下: /** * Created by gerry.zhong on 2016/10/11. */ var ...

  2. js 书写规范

    1.字符串用单引号 2.运算符号和变量之间用空格建立间距 3.书写插件时使用 'use strict'; 开头 4.方法如果是获取或者设置数据集合则使用动词如  getData,setData,eac ...

  3. JS书写规范

    1.js代码是由语句组成的,每一条语句以分号结尾: 语句是有关键字,元素符,表达式组成的:2.js代码严格区分大小写3.所有的标点符号都是英文的4.//表示单行注释,/* */表示多行注释

  4. 前端js的书写规范和高效维护的方案_自我总结使用的方案

    作为程序员,人生最值得幸福的事有几件: 解决困扰了很长时间的问题 升职加薪 找个漂亮又靠谱的对象 深得领导的喜欢 带领团队冲锋陷阵 ... 哈哈,这些都是梦想,暂时想想就好了.这肯定和我说的东西不符合 ...

  5. 前端规范之JS代码规范(ESLint + Prettier)

    代码规范是软件开发领域经久不衰的话题,几乎所有工程师在开发过程中都会遇到或思考过这一问题.而随着前端应用的大型化和复杂化,越来越多的前端团队也开始重视代码规范.同样,前段时间,笔者所在的团队也开展了一 ...

  6. html和css书写规范

    HTML 规范 分离的标记.样式和脚本 结构.表现.行为分离 在可能情况下验证你的标记 使用编辑器验证你的标记是否正确,一般编辑器都自带有这个功能. 技术不支持的时候使用备胎,如canvas 编码格式 ...

  7. CSS书写规范

    一.CSS书写顺序 1.位置属性(position,top,right,z-index,display,float等) 2.大小(width,height,padding,margin) 3.文字系列 ...

  8. 推荐大家使用的CSS书写规范、顺序

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...

  9. 分享给大家的CSS书写规范、顺序

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...

随机推荐

  1. [BZOJ4530]大融合

    LCT维护子树信息 维护两个子树信息,$vinf_x$表示节点$x$的所有轻儿子子树信息,$inf_x$表示以$x$为根的LCT子树(包含虚边)的信息 对$vinf$: access时,断开$x$的原 ...

  2. Exercise03_01

    import javax.swing.JOptionPane; public class TheDirection { public static void main(String[] args){ ...

  3. Mysql5.6.x版本半同步主从复制的开启方法

    介绍 先了解一下mysql的主从复制是什么回事,我们都知道,mysql主从复制是基于binlog的复制方式,而mysql默认的主从复制方式,其实是异步复制. 主库实际上并不关心从库是否把数据拉完没有, ...

  4. vmware三种网络连接模式区别

    vmware有三种网络连接模式分别是 桥接模式 相当于给虚拟机分配了一个和主机同一个子网下的ip,此时该虚拟机相当于同一子网中一台主机,可以访问子网中任意一台主机,也可以访问外网. NAT模式 虚拟机 ...

  5. 好未来AI Lab 思考下面的问题

    好未来AI Lab和科赛联合举办的TAIL CAMP——AI实战训练营 图像识别: 卷积层是所有CNN网络中必不可少的模块,请解释为什么3X3的卷积是最为常用的卷积核大小?小尺寸卷积核(1x1)和大尺 ...

  6. Servlet执行时一般实现哪几个方法?

    public void init(ServletConfig config) public ServletConfig getServletConfig() public String getServ ...

  7. python对solr进行查询、插入操作(GET\POST)

    循环查询一个列表中词的个数: #coding=utf-8 import requests ls = [u'我', u'是', u'一个', u'天才', u'你', u'也是'] for item i ...

  8. Android实现批量照片上传至server,拍照或者从相冊选择

    近期因为项目需求,须要完毕批量照片上传,折腾了一段时间,最终完毕了,达到了例如以下效果 主界面主要有GridView组成和button组成,当按下一个格点时,会调用相机或者相冊,拍照或者选择相冊照片, ...

  9. EffectiveJava(23)为什么不能在新生代码中使用原生态类型

    泛型类和泛型接口 声明一个或者多个类型参数的类或者接口. 为什么不要在新代码中使用原生态类型 原生态类型,即泛型不带参数的类型 如List的list,list就是其原生态类型 1.使用原生态类型,插入 ...

  10. Win7如何修改开机动画

    1 使用魔方美化大师可以替换WIN7的开机画面,我们用该软件打开一个开机动画,居然是一张BMP的图片.   2 在PS中可以发现,这张有21000像素长,大小12MB,可见所谓的动画其实是一个一个动作 ...