安装:npm install eslint -D,安装后就会在node_modules/.bin/下有一个CLI程序。
先配置,再检查
使用eslint --init生成配置文件,可以是JavaScript,也可以是JSON文件。
使用npx eslint 对file进行eslint检查。
使用npx eslint 来自动纠正检查中的问题。
standard风格下,默认的配置文件位于node_modules/eslint-config-standard/eslintrc.json。
"env": {
"es6": true,
"node": true
}
其中,定义了如下的全局变量
"globals": {
"document": "readonly",
"navigator": "readonly",
"window": "readonly"
},
因此,即使是browser环境设置为false,以上的全局变量也是可读的,在代码中使用不会报错。
下图给出了所有能够运行的环境和对应的说明。
这些环境并不是互斥的,可以同时开启。

比如,定义一个公共的配置文件,可以让其他配置文件继承。
可以是数组,用来继承多个配置。
可以在 npm官方 搜索 “eslint-config” 使用别人创建好的配置
parserOptions: {
// 启用es语法版本
ecmaVersion: 5,
// JS文件是script还是module的形式
sourceType: 'script'
}
env: {
browser: true,
es6: false
},
extends: [
'standard'
],
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module'
},
将配置通过注释写在要Lint的代码中。
通常用于针对特定的代码,采用特定的配置规则,这些特定规则与配置文件中的已定规则不同。
参考
const str = '${name} is a coder' // eslint-disable-line no-template-curly-in-string
console.log(str)
module.exports = {
globals: {
MyGlobal: true
},
rules: {
semi: [2, "always"]
}
};
可分享配置被设计和 .eslintrc 文件的 extends 特性一起使用。使用模块名称作为 extends 取值而不是文件路径
{
"extends": "eslint-config-myconfig"
}
可以省略eslint-config-,ESLint 会自动找到
{
"extends": "myconfig"
}
在配置文件的plugins字段中注册插件,在extends字段中扩展插件的规则(或配置)
{
"plugins": [
"react"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"rules": {
"no-set-state": "off"
}
}
gulp-eslint包const {src, task} = require('gulp');
const eslint = require('gulp-eslint');
task('default', () => {
return src(['scripts/*.js'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
eslint-loader {
test: /\.js$/,
exclude: /node_modules/,
use: 'eslint-loader',
// 表示前置loader,表示对于.js文件类型,eslint-loader优先处理
// enforce用来强制调整loader的顺序
enforce: 'pre'
},
1:8 error 'React' is defined but never used no-unused-vars
4:8 error 'App' is defined but never used no-unused-vars
针对上述报错,实际上React是需要加载React的,而App同样在JSX编译为js中使用了,因此要配置eslint使得这种情况可以允许。
eslint-plugin-react可以配置插件和对应的规则
rules: {
'react/jsx-uses-react': 2,
'react/jsx-uses-vars': 2
},
plugins: [
'react'
]
也可以直接配置继承
extends: [
'standard',
'plugin:react/recommended'
],
现代项目中使用的CLI程序已经将ESLint都集成到工作流了,如vue-cli,create-react-app等。
在使用npx eslint --init生成配置文件时,选择目标为TypeScript。
module.exports = {
// 以下两个选项是针对TypeScript,parser是TS语法的解析器
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint'
],
}
ESLint并不是重点做代码风格校验的工具,而代码风格校验正是Prettier的强项,因此两者一起使用来强制规范代码风格。
npm i -D prettier
npm i -D eslint-plugin-prettier
eslint-plugin-prettier插件会调用prettier对你的代码风格进行检查,其原理是先使用prettier对你的代码进行格式化,然后与格式化之前的代码进行对比,如果过出现了不一致,这个地方就会被prettier进行标记。
//.eslintrc.js
{
"plugins": ["prettier"],
"rules": {
// 表示被prettier标记的地方抛出错误信息
"prettier/prettier": "error"
}
}
npm i -D eslint-config-prettier
能够关闭一些与格式相关的ESlint规则。
使用的时候需要确保,这个配置在extends的最后一项。
//.eslintrc.js
{
"extends": ["plugin:prettier/recommended"]
}
配置做了三件事
npm install --save-dev stylelint.stylelintrc.js,也可以是json文件stylelint-config-standard:检查CSS代码的标准语法stylelint-config-sass-guidelines:检查Sass代码的语法// .stylelintrc.js
module.exports = {
extends: [
'stylelint-config-standard',
'stylelint-config-sass-guidelines'
]
}
前端代码格式化工具
npm install prettier -D,同样会生成一个CLI程序npx prettier --write 将的代码格式化后写回到源文件。支持通配符。在不编写shell脚本的情况下,也能使用git hooks。
安装husky:npm install husky -D,安装之后,就会在.git/hooks生成很多hooks脚本文件。
配置husky: 在package.json中配置husky字段
"husky": {
"hooks": {
"pre-commit": "npm run test"
}
}
这里配置了"pre-commit",也就是在git commit之前运行npm run test。
如果每次提交都去Lint很多代码,无疑是很麻烦,且很多代码可能并不需要Lint,毕竟代码需要渐进式地更新,而非整体推倒重来。因此,为了只Lint准备提交的代码,可以使用lint-staged来做。
使用git add 后,file就被git记录到了staged(索引区)。我们只要将这些文件Lint即可。
安装:npm install lint-staged -D
在package.json中配置lint-staged字段
"lint-staged": {
// 键为目标文件,可以是glob
// 值为在命令行中对键定义的目标进行的任务,可以是一个数组,按数组元素的顺序进行操作,流式操作
"*.js": [
"eslint",
// 实际上,这里的git add 不再需要,默认就会将操作后的更改更新到索引区
"git add"
]
}
在git commit 之前启动lint-staged。
"scripts": {
"precommit": "lint-staged"
},
"husky": {
"hooks": {
"pre-commit": "npm run precommit"
}
},
"lint-staged": {
"*.js": [
"eslint"
]
}