在多人协作研发的时候,每个人都需要在代码上传的时候做好编码规范检查,随着前端技术的发展,前端领域也明显了出现了这样的需求,ESLint 由此应运而生
什么是 ESLint
ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. Its goal is to provide a pluggable linting utility for JavaScript.
ESLint requires Node.js and works on Windows, Mac and Linux
ESLint 的特点
非常灵活的目录支持结构:
your-project
├── package.json
├── lib
│ └── source.js
└─┬ tests
├── .eslintrc
└── test.js
home
└── user
├── .eslintrc <- Always skipped if other configs present
└── projectA
├── .eslintrc <- Not used
└── lib
├── .eslintrc <- { "root": true }
└── main.js
所有报错情况的查询列表:
http://eslint.org/docs/rules/
Vue 如何使用 ESLint 的配合
Vue 有两个 ESLint 插件 eslint-config-vue 和 eslint-plugin-vue,因为前者依赖后者,所以直接安装 eslint-config-vue 并完成配置就可以:
npm install --save-dev eslint-config-vue eslint-plugin-vue
在项目根目录下面建立 .eslintrc 文件,然后
{
"extends": "vue"
// Your overrides...
}
使用
eslint ./resources/assets/js/store.js
eslint ./resources/assets/js/store.js --fix // 检查出来以后自动修复
eslint ./resources/assets/js/ --ext .js,.vue // 检查所有.js和.vue扩展名的文件
(这里会后续补充)
网友评论