代码检查与格式化
ESLint
npm i eslint -D
npx eslint --init # 创建配置文件
npx eslint index.js # 只输出正确格式的文件内容,不修改源文件
npx eslint indexjs --fix # 自动修复语法或格式错误
配置文件
eslint
社区存在诸多第三方配置文件(以eslint-config-
开头)和插件(以eslint-plugin-
开头)以供我们使用,我们可以在extends
和plugins
字段中引用这些类库,此时我们可以省略对应的前缀。
对于作用域模块,规则如下
{
"extends": ["@akara/test", "prettier"], // 等于@akara/eslint-config-test eslint-config-prettier
"plugins": ["@akara"] // 等于@akara/eslint-plugin
}
extends
有几种常见的配置文件可以提供给我们继承。
eslint
核心规则eslint:recommended
和eslint:all
eslint-config-*
导出的配置eslint-plugin-*
导出的配置
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended", // @typescript-eslint/eslint-plugin
"prettier" // eslint-config-prettier
]
}
parser
如同我在编译器
一节所述,eslint
默认解析器espree
并不支持对TS
代码的解析,我们可以使用@typescript-eslint/parser
进行替代。
npm i --save-dev typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
module.exports = {
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
};
parserOptions
解析器的选项,默认为espree
的选项。当我们使用其他解析器时,解析器选项的属性可能有一些出入。
{
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module",
"ecmaFeature": {
"jsx": true
}
}
}
ecmaVersion
set to 3, 5 (default), 6, 7, 8, 9, 10, 11, or 12 to specify the version of ECMAScript syntax you want to use. You can also set to 2015 (same as 6), 2016 (same as 7), 2017 (same as 8), 2018 (same as 9), 2019 (same as 10), 2020 (same as 11), or 2021 (same as 12) to use the year-based naming. You can also set "latest" to use the most recently supported version.
sourceType
set to
"script"
(default) or"module"
if your code is in ECMAScript modules.
代码存在import
或export
时需要设置为module
ecmaFeature
an object indicating which additional language features you'd like to use:
globalReturn
- allowreturn
statements in the global scopeimpliedStrict
- enable global strict mode (ifecmaVersion
is 5 or greater)jsx
- enable JSX
我们需要先安装eslint-plugin-react
,再将JSX
设置为true
{
"parserOptions": {
"ecmaFeature": {
"jsx": true
}
},
"extends": [
"plugin:react/recommended"
],
}
If you do not use a preset you will need to specify individual rules and add extra configuration.
{
"parserOptions": {
"ecmaFeatures": {
"jsx": true
}
},
"plugins": [
"react"
],
"rules": {
// 定制规则
}
}
env
支持不同环境的内置变量
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true
}
}
browser
支持window
等浏览器环境变量
node
支持process
等node环境变量
commonjs
CommonJS global variables and CommonJS scoping (use this for browser-only code that uses Browserify/WebPack).
支持module
变量
es2021
{
"env": {
"es2021": true
}
}
supporting ES6 syntax is not the same as supporting new ES6 globals (e.g., new types such as
Set
). For ES6 syntax, use{ "parserOptions": { "ecmaVersion": 6 } }
; for new ES6 global variables, use{ "env": { "es6": true } }
.{ "env": { "es6": true } }
enables ES6 syntax automatically, but{ "parserOptions": { "ecmaVersion": 6 } }
does not enable ES6 globals automatically.
globals
指定全局变量
{
"globals": {
"Promise": 'off'
},
}
plugins
列出几个常见插件。
@typescript-eslint/eslint-plugin
eslint-plugin-react
通过ecmaFeature.JSX
,支持react
代码提示
eslint-plugin-react-hooks
支持hook
提示,比如提示useCallback
函数依赖项
eslint-plugin-prettier
把prettier
作为eslint
的格式化工具