ESLint 介绍
ESLint 安装步骤
ESLint 快速上手
初始化执行 ESLint 配置文件 yarn eslint --init
How would you like to use ESLint ?
What type of modules does your project use ? (您的项目使用什么类型的模块)
Which framework does your project use ? (您的项目使用哪种框架)
Does your project use TypeScript ? (Y/N) (你的项目使用TypeScript吗)
Where does your code run ? (你的代码在哪里运行)
How would you like to define a style for your project ? (您希望如何为您的代码风格)
What format do you want your config file to be in ? (您希望配置文件的格式是什么) javaScript (推荐) YAML JSON
之后会下载Standard风格依赖 YES 就可以了
执行代码检查
yarn eslint xxx.js 这样就会提示代码问题和编码风格问题
也可以是用 --fix 修正绝大多数代码风格上的问题
yarn eslint xxx.js --fix
ESLint 配置
module.exports = { env: { //标记当前代码的运行环境 browser: true, es2021: true }, extends: [ //继承共享配置 ‘standard‘ ], parserOptions: { //设置语法解析器配置 ecmaVersion: 12 //版本 }, rules: { //配置检验规则的开始或者关闭 } }
ENV可以是可以设置哪些运行环境
ESLint 配置注释
不修改 ESLint 配置的基础上需要违反了配置规则的代码,
可以在你的文件中使用以下格式的块注释来临时禁止规则出现警告
const str1 = ‘${name} is a coder‘ // eslint-disable-line no-template-curly-in-string console.log(str1)
为文件的某部分禁用警告的注释,告诉 ESLint 不要对禁用的代码报告规则的冲突。ESLint 仍解析整个文件,然而,禁用的代码仍需要是有效的 JavaScript 语法。
ESLint 结合自动化工具
结合Gulp使用 需要安装 eslint gulp-eslint 注意版本控制
const script = () => { return src(‘src/assets/scripts/*.js‘, { base: ‘src‘ }) .pipe(plugins.eslint()) //进行代码检查 .pipe(plugins.eslint.format()) //格式化 .pipe(plugins.eslint.failAfterError()) //发现异常抛出,中断执行 .pipe(plugins.babel({ presets: [‘@babel/preset-env‘] })) .pipe(dest(‘temp‘)) .pipe(bs.reload({ stream: true })) }
结合webpack使用 需要安装 eslint gulp-eslint 注意版本控制
rules: [ { test: /\.js$/, exclude: /node_modules/, use: [ ‘babel-loader‘, ‘eslint-loader‘ ] } ]
结合typescript使用
配置文件 parser 制定一个语法解析器
原文:https://www.cnblogs.com/faint33/p/14921325.html