eslint.config.js VS .eslintrc.*

ESLint 生态中存在两种配置文件格式:

  • .eslintrc.*,在 Eslint 9 之前的配置文件
  • eslint.config.js ,eslint 9 之后推出的新的扁平化 的配置文件(flat-config)

两种配置文件 在配置规则、配置处理器(processor) 的语法方面相同,但在很多方面有一些不同。

两者的核心区别在于:

配置文件的位置和格式不同:

eslintrc 允许在多个位置使用多个配置文件、使用多种配置文件格式,而新的 flat config 限制一个位置、一种格式(eslint.config.js),减少混乱。

两者的一个重要的区别还在于**eslintrc**是配置一个对象,而 **eslint.config.js**是配置一个对象数组。

新的配置使用对象数组可以带来更灵活、更精细的处理方式,比如我们想对不同的文件采用不同的规则配置:

javascript 复制代码
export default [
  {
    files: ["**/*.js", "**/*.cjs"],
    rules: {
      "semi": "error",
      "no-unused-vars": "error"
    }  
  },
  {
    files: ["**/*.js"],
    rules: {
      "no-undef": "error",
      "semi": "warn"
    }  
  }
];

此配置有两个具有重叠files模式的配置对象。第一个配置对象适用于所有.js和.cjs文件,而第二个配置对象仅适用于.js文件。

导入插件、解析器、处理器的方式不同

对于导入插件、解析器、处理器,在eslintrc采用的是基于字符串的导入解析系统,而在eslint.config.js中这些就是一个 js 对象,所以可以用 js 模块导入语法加载(esm 或 commonjs)。

以下是一个导入 jsdoc插件的示例:

javascript 复制代码
// .eslintrc.js
module.exports = {
  // ...other config
  plugins: ["jsdoc"],
  rules: {
    "jsdoc/require-description": "error",
    "jsdoc/check-values": "error"
  }
  // ...other config
};
// eslint.config.js
import jsdoc from "eslint-plugin-jsdoc";
export default [
  {
    files: ["**/*.js"],
    plugins: {
      jsdoc: jsdoc
    },
    rules: {
      "jsdoc/require-description": "error",
      "jsdoc/check-values": "error"
    }
  }
];

配置语言选项

在 eslintrc 文件中,您可以通过env、globals和parserOptions属性配置各种语言选项。

在eslint.config.js中,globals和parserOptions合并在languageOptions键下,env属性不存在。

示例:

javascript 复制代码
// .eslintrc.js
module.exports = {
    env: {
        browser: true,
    },
    globals: {
        myCustomGlobal: "readonly",
    },
    parserOptions: {
        ecmaVersion: 2022,
        sourceType: "module"
    }
    // ...other config
}
// eslint.config.js
import globals from "globals";
export default [
    {
        languageOptions: {
            ecmaVersion: 2022,
            sourceType: "module",
            globals: {
                ...globals.browser,
                myCustomGlobal: "readonly"
            }
        }
        // ...other config
    }
];

使用预定义配置的不同

在 eslintrc 中,使用extends属性来使用预定义的配置。ESLint 附带两个可以作为字符串访问的预定义配置:

  • "eslint:recommended":ESLint 推荐的规则
  • "eslint:all":ESLint 附带的所有规则

在eslint.config.js中,预定义的配置以模块的存在,需导入使用。

javascript 复制代码
// .eslintrc.js
module.exports = {
  extends: "eslint:recommended",
  rules: {
    semi: ["warn", "always"]
  }
}
// eslint.config.js
import js from "@eslint/js";
import customConfig from "./custom-config.js";//本地的预定义配置
export default [
  js.configs.recommended,
  customConfig,
  {
    rules: {
      semi: ["warn", "always"]
    }
  }
];

配置忽略检查文件的方式不同

eslintrc使用单独的.eslintignore文件或在ignorePatterns中配置。
eslint.config.js配置不再支持.eslintignore文件,而使用ignores属性配置

package.json 支持

使用 eslintrc,可以使用package.json文件通过配置 ESLint 。

使用eslint.config.js后,不再可能使用文件package.json来配置 ESLint,需要将配置移至单独的文件中。

相关推荐
沙漠之主34 分钟前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
hasty38 分钟前
不上传新包,也能改变用户拿到的版本:npm dist-tag 的 OIDC 权限治理
前端·npm·node.js
Csvn1 小时前
diff 算法(虚拟 DOM Reconciliation)
前端
前端snow2 小时前
ai agent --- 文件存储
前端
Frag0ut2 小时前
Chrome四大版本获取及共存指南:Stable/Beta/Dev/Canary
前端·chrome·浏览器·dev·beta·canary·共存版
IT_陈寒4 小时前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
广州华水科技4 小时前
单北斗GNSS变形监测在大坝安全监测中的应用与优势
前端
zeng不错4 小时前
B站 173 个投稿活动看到眼瞎?我写了个扩展,主打一个精准打击
前端
Latchh4 小时前
前端导出的JPEG红字发糊,质量要拉到100才清楚
前端·图像处理·人工智能·计算机视觉
默_笙5 小时前
🚦 请三个 AI 考官给 RAG 打分:量化评估实战(下)
前端·javascript