-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ESLint compat utilities post #564
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/content/blog/2024-05-09-eslint-compatibility-utilities.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
layout: post | ||
title: Introducing ESLint Compatibility Utilities | ||
teaser: "Do you rely on a plugin that doesn't yet work with ESLint v9? This package will help." | ||
tags: | ||
- Compatibility | ||
- Tools | ||
authors: | ||
- nzakas | ||
categories: | ||
- Announcements | ||
--- | ||
|
||
The release of ESLint v9.0.0 brought with it the rollout of the [new configuration system](/blog/2023/10/flat-config-rollout-plans/), but also a series of changes to the [rules API](/blog/2023/09/preparing-custom-rules-eslint-v9/). These changes are necessary in order to prepare ESLint for implementing [language plugins](https://github.com/eslint/rfcs/blob/main/designs/2022-languages/README.md), which will give ESLint the ability to natively lint languages other than JavaScript. As a result, plugin authors needed to update their rules to work with v9.0.0, and unfortunately, that means some of the plugins you rely on may not have been updated yet. That's why we're releasing the compatibility utilities. | ||
|
||
## How to know if the compatibility utilities will help | ||
|
||
These utilities may help if you encounter any of the following errors while running ESLint: | ||
|
||
``` | ||
TypeError: context.getScope is not a function | ||
TypeError: context.getAncestors is not a function | ||
TypeError: context.markVariableAsUsed is not a function | ||
TypeError: context.getDeclaredVariables is not a function | ||
``` | ||
|
||
These errors mean that the plugin rules have not been updated to the latest ESLint rule API. | ||
|
||
## Using the compatibility utilities | ||
|
||
First, install the [`@eslint/compat`](https://npmjs.com/package/@eslint/compat) package using npm or any npm-compatible CLI: | ||
|
||
```shell | ||
npm install @eslint/compat -D | ||
# or | ||
yarn add @eslint/compat -D | ||
# or | ||
pnpm install @eslint/compat -D | ||
# or | ||
bun install @eslint/compat -D | ||
``` | ||
|
||
Then, use the `fixupPluginRules()` function in your `eslint.config.js` file to wrap the plugin in a compatibility layer: | ||
|
||
```js | ||
// eslint.config.js | ||
import { fixupPluginRules } from "@eslint/compat"; | ||
import example from "eslint-plugin-example"; | ||
|
||
export default [ | ||
{ | ||
plugins: { | ||
example: fixupPluginRules(example) | ||
} | ||
}, | ||
|
||
// other config | ||
]; | ||
``` | ||
|
||
After that, the plugin should work as expected. | ||
|
||
## Fixing an imported configuration | ||
|
||
If you are importing a flat-style configuration from another package that references plugins, you can use the `fixupConfigRules()` function to wrap all of the plugins found, like this: | ||
|
||
```js | ||
// eslint.config.js | ||
import { fixupConfigRules } from "@eslint/compat"; | ||
import recommended from "eslint-plugin-example/configs/recommended.js"; | ||
|
||
const flatCompat = new FlatCompat(); | ||
|
||
export default [ | ||
|
||
...fixupConfigRules(recommended) | ||
|
||
// other config | ||
]; | ||
``` | ||
|
||
The `fixupConfigRules()` function accepts both a single object and an array of objects to easily update any configuration you're using. | ||
|
||
## Using with `FlatCompat` | ||
|
||
If you are using `FlatCompat` from the [`@eslint/eslintrc`](https://npmjs.com/package/@eslint/eslintrc) package, you may not be able to access each of the plugins that are referenced inside of an eslintrc-style configuration. In that case, you can use the `fixupConfigRules()` function to wrap all plugins, as in this example: | ||
|
||
```js | ||
// eslint.config.js | ||
import { fixupConfigRules } from "@eslint/compat"; | ||
import { FlatCompat } from "@eslint/eslintrc"; | ||
|
||
const flatCompat = new FlatCompat(); | ||
|
||
export default [ | ||
|
||
...fixupConfigRules( | ||
flatCompat.extends("my-config") | ||
) | ||
|
||
// other config | ||
]; | ||
``` | ||
|
||
While this example shows how to use `fixupConfigRules()` with the `extends()` method, any method on `FlatCompat` works. | ||
|
||
## Conclusion | ||
|
||
One of the benefits of the new configuration system is that it allows us the ability to patch plugins and configs that haven't yet been updated by their maintainers. We know it can be frustrating to migrate to ESLint v9.0.0 and have your configuration not work, and that's why we're committed to providing packages like `@eslint/eslintrc` and `@eslint/compat` to help aid the transition. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to also provide an example with a plugin that exports a flat config?
For example, eslint-plugin-react exports flat configs:
https://github.com/jsx-eslint/eslint-plugin-react?tab=readme-ov-file#configuration-new-eslintconfigjs
Rules aren't compatible with the new API yet, but the config can be used like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.