一、安装

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. [Contest20180405]抑制「超我」

    古明地恋(koishi)和计算器(calculator)是好朋友.恋恋有一个神奇的计算器,可以进行两个数在模$2^n$意义下的加法运算.计算器上有一个寄存器,一开始寄存器中的数为$0$,每当恋恋输入一 ...

  2. 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons

    从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...

  3. 【博弈论】【SG函数】bzoj1777 [Usaco2010 Hol]rocks 石头木头

    仅有距根节点为奇数距离的节点的石子被移走对答案有贡献,∵即使偶数的石子被移走,迟早会被再移到奇数,而奇数被移走后,不一定能够在移到偶数(到根了). 最多移L个:石子数模(L+1),比较显然,也可以自己 ...

  4. 【权值分块】bzoj3570 DZY Loves Physics I

    以下部分来自:http://www.cnblogs.com/zhuohan123/p/3726306.html 此证明有误. DZY系列. 这题首先是几个性质: 1.所有球质量相同,碰撞直接交换速度, ...

  5. 1.4(Spring学习笔记)Spring-JDBC基础

    一.Spring JDBC相关类 1.1 DriverManagerDataSource DriverManagerDataSource主要包含数据库连接地址,用户名,密码. 属性及含义如下配置所示: ...

  6. ZooKeeper服务器是用Java创建的,它在JVM上运行。

    ZooKeeper服务器是用Java创建的,它在JVM上运行. 创建配置文件 使用命令 vi conf/zoo.cfg 和所有以下参数设置为起点,打开名为 conf/zoo.cfg 的配置文件. $ ...

  7. activemq 5.13.2 jdbc 数据库持久化 异常 找不到驱动程序

    原文:https://my.oschina.net/u/2284972/blog/662033 摘要: activemq jdbc 数据库持久化 异常 找不到驱动程序 Caused by: java. ...

  8. 微信 JS SDK 的 chooseImage 接口在部分安卓机上容易造成页面刷新

    该问题的症状是,当调用 chooseImage 接口并选择拍照,选择照片确定之后,然后从相机返回后,当前web页面就刷新了一次,导致拍照的图片无法选择上传:但是如果直接从相册中选择图片,则不会出现这个 ...

  9. 纯C实现面向对象

    #include <stdio.h> #include <stdlib.h> //接口 #ifndef Interface #define Interface struct # ...

  10. Concise: Compressed ’n’ Composable Integer Set

    Word Aligned Hybrid (WAH) bitmap compression 下面是:Concise: Compressed ’n’ Composable Integer Set Figu ...