一、安装

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. 【后缀数组】【二分答案】poj3261

    注意:对整型数组求sa时,s[n]请置成-1. 请离散化. 可重叠的 k 次最长重复子串(pku3261)给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠.算法分析:先二分答案 ...

  2. Exercise02_05

    import java.util.Scanner; public class Rate { public static void main(String[] args){ Scanner input ...

  3. 通过映射缓存文件夹加速WPF的响应速度

    到了Visual Studio 2012时,WPF的设计器已经非常好用了,大多数的时候基本上可以抛弃Blend了.但是,仍然存在一些不令人满意的问题,那就是当项目非常大的时候,很多时候页面切换变得比较 ...

  4. Linux下使用GDB进行调试

    Linux下使用GDB进行调试的常用命令记于此. $ sudo su # g++ -g test.cpp -o test -pthread # gdb test         <------- ...

  5. Javascript高级程序设计-问答模式

    1.谈谈javascript数组排序方法sort()的使用,重点介绍sort()参数的使用及其内部机制 sort的实现的功能类似JAVA的比较器,数据排序从多维数组的第一维开始排序可以自己定义排序方法 ...

  6. springMVC中Restful支持

    RESTFul支持 http://localhost:8090/user/doAdd.action?username=tony&age=8 http://localhost:8090/user ...

  7. Docker核心技术

    Docker核心技术 1.cgroup 即controller group,其重要概念是子系统,首先挂载子系统,然后才有control group.例如cpu子系统,挂载至系统之后,创建一个cgrou ...

  8. hadoop2.2.0伪分布式安装

    修改主机名和IP的映射关系 vi /etc/hosts 192.168.61.134 hadoop 关闭防火墙 #查看防火墙状态 service iptables status #关闭防火墙 serv ...

  9. Mycat探索之旅(3)----Mycat的全局序列号

    一.本地文件方式 原理:此方式MyCAT将sequence配置到文件中,当使用到sequence中的配置后,MyCAT会更下classpath中的sequence_conf.properties文件中 ...

  10. FBXImport

    using UnityEditor; public class MyEditor : AssetPostprocessor{ public void OnPreprocessModel() { Mod ...