Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 56 additions & 18 deletions docs/eslint-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ Companion rules for [`@babel/eslint-parser`](./eslint-parser.md). `@babel/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.

> Requires Node.js 10.13 or greater

## Install

```shell npm2yarn
npm install @babel/eslint-plugin --save-dev
```

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!).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is moved to Rules/Configure Rules.

## Predefined configurations

`@babel/eslint-plugin` has two predefined configurations:

- `recommended`: Enables all companion rules when the reimplemented built-in rules are in the [`js/recommended` predefined configuration](https://eslint.org/docs/latest/use/configure/configuration-files#use-predefined-configurations).

```js title=eslint.config.js
- `all`: Enables all companion rules.

```js title="eslint.config.js"
import babelParser from "@babel/eslint-parser";
import babelPlugin from "@babel/eslint-plugin";
import js from "@eslint/js";
import { defineConfig } from "eslint/config";

export default defineConfig([
js.configs.recommended,
{
files: ["**/*.js", "**/*.cjs", "**/*.mjs"],
languageOptions: {
Expand All @@ -34,33 +40,65 @@ export default defineConfig([
plugins: {
babel: babelPlugin
},
...babelPlugin.configs.recommended,
},
]);
```

## Rules

Each rule corresponds to a core `eslint` rule and has the same options.

✅ means the [`recommended`](#predefined-configurations) config from `@babel/eslint-plugin` enables this rule.

🔧 means it's autofixable with `--fix`.


| Name | Description | R | F |
| --- | --- | --- | --- |
| new-cap | handles decorators (`@Decorator`) | | |
| no-empty | handles `do` expressions | ✅ | 🔧 |
| no-undef | handles class accessor properties (`class A { accessor x = 2 }`) | | |
| no-unused-expressions | handles `do` expressions | | |

### Configure Rules

Load the plugin in your ESLint config and enable all the rules you would like to use (remember to disable the built-in ones as well!).

```js title="eslint.config.js"
import js from "@eslint/js";
import babelParser from "@babel/eslint-parser";
import babelPlugin from "@babel/eslint-plugin";
import { defineConfig } from "eslint/config";

export default defineConfig([
js.configs.recommended,
{
files: ["**/*.js", "**/*.cjs", "**/*.mjs"],
languageOptions: {
parser: babelParser,
},
plugins: {
js,
babel: babelPlugin
},
rules: {
// Disable built-in rules in @eslint/js
"new-cap": "off",
"no-empty": "off",
"no-undef": "off",
"no-unused-expressions": "off",

// Enable Babel rules
"babel/new-cap": "error",
"babel/no-empty": "error",
"babel/no-undef": "error",
"babel/no-unused-expressions": "error",
}
},
]);
```



## Rules

Each rule corresponds to a core `eslint` rule and has the same options.

🛠: means it's autofixable with `--fix`.

- `@babel/new-cap`: handles decorators (`@Decorator`)
- `@babel/no-undef`: handles class accessor properties (`class A { accessor x = 2 }`)
- `@babel/no-unused-expressions`: handles `do` expressions



## TypeScript

While [`@babel/eslint-parser`](https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser) can parse TypeScript, we don't currently support linting TypeScript using the rules in [`@babel/eslint-plugin`](https://github.com/babel/babel/tree/main/eslint/babel-eslint-plugin). This is because the TypeScript community has centered around [`@typescript-eslint`](https://github.com/typescript-eslint/typescript-eslint) and we want to avoid duplicate work. Additionally, since [`@typescript-eslint`](https://github.com/typescript-eslint/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.
2 changes: 1 addition & 1 deletion docs/v8-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ The following syntax plugins are no longer needed, you can safely remove them fr

**Migration**: Specify the minor version of core-js 3 that you use to ensure latest features will be polyfilled if required. For example,

```js title=babel.config.mjs
```js title="babel.config.mjs"
import corejsPackage from "core-js/package.json" with { type: "json" }
export default {
"presets": [[
Expand Down