Skip to content

ESLint 扁平配置 ​

ESLint 9+ 开始默认使用扁平配置,废弃以往 .eslintrc 的配置方式,改用单一文件 eslint.config.* 来提供更简洁直观易于维护的配置。

命令配置 ​

命令行安装和配置 ESLint,将会以问卷形式来进行配置。

bash
pnpm create @eslint/config@latest -D

# 若使用共享配置,可使用 --config 选项
pnpm create @eslint/config@latest -- --config eslint-config-xo -D
安装和配置ESlint
  • 将 eslint 用于检查哪些文件:JavaScript / JSON / JSON with comments / JSON5 / Markdown / CSS。
  • 如何使用 eslint:仅检查语法 / 检查语法和修复问题。
  • 项目使用什么类型的模块:ESM / CJS / None of these。
  • 使用的库框:React / Vue / None of these。
  • 项目是否使用 TypeScript:No / Yes。
  • 代码在哪运行:Browser / Node。
  • 配置文件使用哪种语言:JavaScript / TypeScript。
  • 要检查什么风格的 Markdown 文件:CommonMark / GitHub Flavored Markdown。
  • 是否将 Jiti 添加为开发依赖:No / Yes。

TIP

CommonMark(CM) 为 markdown 官方标准规范,而 GitHub Flavored Markdown(GFM) 为 github 的扩展规范,在包含 CM 基础上还支持更多特性,比如表格、脚注等。

TIP

Jiti 的作用是可用 required 语法来导入 ESM,该依赖主要是兼容一些工具内部使用 required 加载 ESM 时报错的问题。

命令执行完后自动生成 eslint.config.mts 文件:

typescript
import js from '@eslint/js'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'
import prettierPlugin from 'eslint-plugin-prettier'
import vuePrettierConfig from '@vue/eslint-config-prettier'
import json from '@eslint/json'
import markdown from '@eslint/markdown'
import css from '@eslint/css'
import { defineConfig } from 'eslint/config'

export default defineConfig([
  {
    files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
    plugins: { js },
    extends: ['js/recommended'],
    languageOptions: { globals: globals.browser }
  },
  tseslint.configs.recommended,
  pluginVue.configs['flat/recommended'],
  { files: ['**/*.vue'], languageOptions: { parserOptions: { parser: tseslint.parser } } },
  { files: ['**/*.json'], plugins: { json }, language: 'json/json', extends: ['json/recommended'] },
  { files: ['**/*.jsonc'], plugins: { json }, language: 'json/jsonc', extends: ['json/recommended'] },
  { files: ['**/*.md'], plugins: { markdown }, language: 'markdown/gfm', extends: ['markdown/recommended'] },
  { files: ['**/*.css'], plugins: { css }, language: 'css/css', extends: ['css/recommended'] }
])

脚本命令 ​

将 eslint 命令添加到 package.json 的脚本命令中,--ext 选项用于指定需要检查的文件扩展名,点号不可忽略,--fix 选项用于自动修复检查出的问题。

json
{
  "scripts": {
    "lint": "eslint . --ext .vue,.js,.ts",
    "lint:fix": "eslint . --ext .vue,.js,.ts --fix"
  }
}

故障排除 ​

执行 pnpm run lint 时出现以下错误。

TypeError: Error while loading rule 'vue/multi-word-component-names': Cannot read properties of undefined (reading 'getDocumentFragment')
Occurred while linting D:\apo-monorepo\lerna.json`

vue/multi-word-component-names 是 vue 相关的规则,而 vue 单文件由 eslint-plugin-vue 插件来解析,所以问题应该出自该插件,检查 json 文件时报错, 但按理会自动排除 json 文件,在该库的 issues eslint-plugin-vue#2603 中找到解决办法,将 pluginVue.configs['flat/recommended'] 配置对象展开,指定要匹配的文件。

typescript
export default defineConfig([
  pluginVue.configs['flat/recommended'],  
  ...pluginVue.configs['flat/recommended'].map(config => ({ 
    ...config, 
    files: ['**/*.vue'], 
  })), 
    
  // 其他配置
]);

至此问题解决。

不同项目不同场景下可能会有不同问题,只能是多看 eslint 官方文档,在熟悉了各种配置项后才可能有解决问题的思路。