ESLint
来自康健生活
作者
ESLint 是由 Nicholas C. Zakas 在2013年开始开发的,它的初衷就是为了能让开发者能自定义自己的 linting rules,而且它提供了一套相当完善的插件机制,可以自由的扩展,动态加载配置规则,同时可以方便的根据报错定位到具体的规则配置。
.editorconfig
# http://editorconfig.org
root = true
[*]
#缩进风格:空格
indent_style = space
#缩进大小4
indent_size = 4
#换行符lf
end_of_line = lf
#字符集utf-8
charset = utf-8
#是否删除行尾的空格
trim_trailing_whitespace = true
#是否在文件的最后插入一个空行
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
[Makefile]
indent_style = tab
特征
- 可扩展、规则独立、可配置。
- lint工具
- QA工具
用来作为静态代码检查,避免低级错误和统一代码的风格。
安装
npm install - g eslint
npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise
eslint --init
命令
初始化和生成.eslintrc.*文件
eslint --init
执行检查命令
eslint add.js
指定配置文件
eslint - c config.json add.js
打印出eslint所使用的配置和规则
eslint --print-config xx.js > 1
配置
- 使用.eslintrc.*文件( 支持JSON和YAML两种语法)
- 在package.json中添加eslintConfig配置块
- 使用JavaScript注释直接把配置嵌入到文件中
规则
"quotes": "error"
第一部分:规则名 第二部分:规则的严重性(rule severity)
- "off"
- or 0 - turn the rule off 不验证 "warn"
- or 1 - turn the rule on as a warning(doesn’ t affect exit code) 警告 "error"
- or 2 - turn the rule on as an error(exit code is 1 when triggered) 错误
规则的选项(additional options)
"quotes": [2, "double"]
☟
| 规则 |
示例
技巧
No categories