-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: add guide for building ESLint plugins #9684
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
Merged
JoshuaKGoldberg
merged 12 commits into
typescript-eslint:main
from
JoshuaKGoldberg:plugin-docs
Aug 5, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e33d784
docs: add guide for building ESLint plugins
JoshuaKGoldberg b498a37
Merge branch 'main' into plugin-docs
JoshuaKGoldberg bf9ce80
Merge branch 'main' into plugin-docs
JoshuaKGoldberg 946a266
Merge branch 'main'
JoshuaKGoldberg 5f5f0a9
Corrected languageOptions placement
JoshuaKGoldberg d7b4d7e
Corrected languageOptions placement a bit more
JoshuaKGoldberg da7f4fc
Merge branch 'main' into plugin-docs
JoshuaKGoldberg 01e72ab
nit on custom rules: four
JoshuaKGoldberg 659ad36
Notes from Josh
JoshuaKGoldberg 6e286d1
yarn format --write
JoshuaKGoldberg 07036ff
One last string parser reference
JoshuaKGoldberg 0dae980
remove requiresTypeChecking
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,91 @@ | ||
--- | ||
id: eslint-plugins | ||
sidebar_label: Building ESLint Plugins | ||
title: ESLint Plugins | ||
--- | ||
|
||
:::important | ||
This page describes how to write your own custom ESLint plugins using typescript-eslint. | ||
You should be familiar with [ESLint's plugins guide](https://eslint.org/docs/latest/extend/plugins) and [typescript-eslint Custom Rules](./Custom_Rules.mdx) before writing custom plugins. | ||
::: | ||
|
||
Custom plugins that support TypeScript code and typed linting look very similar to any other ESLint plugin. | ||
Follow the same general steps as [ESLint's plugins guide > _Creating a plugin_](https://eslint.org/docs/latest/extend/plugins#creating-a-plugin) to set up your plugin. | ||
The required differences are noted on this page. | ||
|
||
:::tip | ||
See [**`eslint-plugin-example-typed-linting`**](https://github.com/typescript-eslint/examples/tree/main/packages/eslint-plugin-example-typed-linting) for an example plugin that supports typed linting. | ||
::: | ||
|
||
## Package Dependencies | ||
|
||
Your plugin should have the following `package.json` entries. | ||
|
||
For all `@typescript-eslint` and `typescript-eslint` packages, keep them at the same semver versions. | ||
As an example, you might set each of them to `^8.1.2` or `^7.12.0 || ^8.0.0`. | ||
|
||
### `dependencies` | ||
|
||
[`@typescript-eslint/utils`](../packages/Utils.mdx) is required for the [`RuleCreator` factory to create rules](#rulecreator-usage). | ||
|
||
### `devDependencies` | ||
|
||
[`@typescript-eslint/rule-tester`](../packages/Rule_Tester.mdx) is strongly recommended to be able to [test rules with our `RuleTester`](./Custom_Rules.mdx). | ||
|
||
### `peerDependencies` | ||
|
||
Include the following to enforce the version range allowed without making users' package managers install them: | ||
|
||
- `@typescript-eslint/parser` and any other parsers users are expected to be using | ||
- `eslint` | ||
- `typescript` | ||
|
||
Those are all packages consumers are expected to be using already. | ||
|
||
## `RuleCreator` Usage | ||
|
||
We recommend including at least the following three properties in your plugin's [`RuleCreator` extra rule docs types](./Custom_Rules.mdx#extra-rule-docs-types): | ||
|
||
- `description: string`: a succinct description of what the rule does | ||
- `recommended?: boolean`: whether the rule exists in your plugin's shared _"`recommended`"_ config | ||
- `requiresTypeChecking?: boolean`: whether the rule will use type information, for documentation generators such as [`eslint-doc-generator`](https://github.com/bmish/eslint-doc-generator) | ||
|
||
For example, from [`eslint-plugin-example-typed-linting`'s `utils.ts`](https://github.com/typescript-eslint/examples/blob/main/packages/eslint-plugin-example-typed-linting/src/utils.ts): | ||
|
||
```ts | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
|
||
export interface ExamplePluginDocs { | ||
description: string; | ||
recommended?: boolean; | ||
requiresTypeChecking?: boolean; | ||
} | ||
|
||
export const createRule = ESLintUtils.RuleCreator<ExamplePluginDocs>( | ||
name => | ||
`https://github.com/your/eslint-plugin-example/tree/main/docs/${name}.md`, | ||
); | ||
``` | ||
|
||
## Type Checking and Configs | ||
|
||
Most ESLint plugins export a _"`recommended`"_ [ESLint shared config](https://eslint.org/docs/latest/extend/shareable-configs). | ||
Many ESLint users assume enabling a plugin's `recommended` config is enough to enable all its relevant rules. | ||
|
||
However, at the same time, not all users want to or are able to enabled typed linting. | ||
If your plugin's rules heavily use type information, it might be difficult to enable those in a `recommended` config. | ||
|
||
You have roughly two options: | ||
|
||
- Have your plugin's `recommended` config require enabling type information | ||
- Have a separate config with a name like `recommendedTypeChecked` | ||
|
||
Either way, explicitly mention the strategy taken in your docs. | ||
|
||
:::info | ||
Per [_Custom Rules_ > _Conditional Type Information_](./Custom_Rules.mdx#conditional-type-information), we recommend not changing rule logic based on whether type information is available. | ||
::: | ||
|
||
:::tip | ||
See [**`eslint-plugin-example-typed-linting`**](https://github.com/typescript-eslint/examples/tree/main/packages/eslint-plugin-example-typed-linting) for an example plugin that supports typed linting. | ||
::: |
This file contains hidden or 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
This file contains hidden or 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
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.
There's really no need to ask users to load in a full third-party-package for a single enum helper function. Especially not for a package outside of the org's ownership.