@babel/eslint-plugin
@babel/eslint-parser 的配套规则。@babel/eslint-parser 在将 eslint 适配到 Babel 上做得非常好,但它无法修改内置规则以支持实验性功能。@babel/eslint-plugin 重新实现了有问题的规则,让它们不会产生误报或漏报。
🌐 Companion rules for @babel/eslint-parser. @babel/eslint-parser does a great job at adapting eslint
for use with Babel, but it can't change the built-in rules to support experimental features.
@babel/eslint-plugin re-implements problematic rules so they do not give false positives or negatives.
需要 Node.js 10.13 或更高版本
安装
🌐 Install
- npm
- Yarn
- pnpm
- Bun
npm install @babel/eslint-plugin --save-dev
yarn add @babel/eslint-plugin --dev
pnpm add @babel/eslint-plugin --save-dev
bun add @babel/eslint-plugin --dev
在你的 ESLint 配置中加载该插件并启用所有你想要使用的规则(记住也禁用原始规则!)。
🌐 Load the plugin in your ESLint config and enable all the rules you would like to use (remember to disable the original ones as well!).
import babelParser from "@babel/eslint-parser";
import babelPlugin from "@babel/eslint-plugin";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.js", "**/*.cjs", "**/*.mjs"],
languageOptions: {
parser: babelParser,
},
plugins: {
babel: babelPlugin
},
rules: {
"new-cap": "off",
"no-undef": "off",
"no-unused-expressions": "off",
"babel/new-cap": "error",
"babel/no-undef": "error",
"babel/no-unused-expressions": "error",
}
},
]);
规则
🌐 Rules
每条规则对应一个核心 eslint 规则,并且具有相同的选项。
🌐 Each rule corresponds to a core eslint rule and has the same options.
🛠:表示可以用 --fix 自动修复。
@babel/new-cap:处理装饰器(@Decorator)@babel/no-undef:处理类访问器属性(class A { accessor x = 2 })@babel/no-unused-expressions:处理do表达式
TypeScript
虽然 @babel/eslint-parser 可以解析 TypeScript,但我们目前不支持使用 @babel/eslint-plugin 的规则来对 TypeScript 进行 lint。这是因为 TypeScript 社区主要围绕 @typescript-eslint 展开,我们也想避免重复工作。另外,由于 @typescript-eslint 在底层使用了 TypeScript,它的规则可以具备类型感知能力,这是 Babel 做不到的。
🌐 While @babel/eslint-parser can parse TypeScript, we don't currently support linting TypeScript using the rules in @babel/eslint-plugin. This is because the TypeScript community has centered around @typescript-eslint and we want to avoid duplicate work. Additionally, since @typescript-eslint uses TypeScript under the hood, its rules can be made type-aware, which is something Babel doesn't have the ability to do.