ESLint Plugins
This page describes how to write your own custom ESLint plugins using typescript-eslint.You should be familiar withESLint's plugins guide andtypescript-eslint Custom Rules 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 asESLint's plugins guide >Creating a plugin to set up your plugin.The required differences are noted on this page.
Seeeslint-plugin-example-typed-linting for an example plugin that supports typed linting.
Package Dependencies
Your plugin should have the followingpackage.json entries.
For all@typescript-eslint andtypescript-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 is required for theRuleCreator factory to create rules.
devDependencies
@typescript-eslint/rule-tester is strongly recommended to be able totest rules with ourRuleTester.
peerDependencies
Include the following to enforce the version range allowed without making users' package managers install them:
@typescript-eslint/parserand any other parsers users are expected to be usingeslinttypescript
Those are all packages consumers are expected to be using already.
You don't need to declare any dependencies ontypescript-eslint or@typescript-eslint/eslint-plugin.Our setup guide ensures that the parser is automatically registered when configuring ESLint.
RuleCreator Usage
We recommend including at least the following three properties in your plugin'sRuleCreator extra rule docs types:
description: string: a succinct description of what the rule doesrecommended?: boolean: whether the rule exists in your plugin's shared"recommended" configrequiresTypeChecking?: boolean: whether the rule will use type information, for documentation generators such aseslint-doc-generator
For example, fromeslint-plugin-example-typed-linting'sutils.ts:
import{ ESLintUtils}from'@typescript-eslint/utils';
exportinterfaceExamplePluginDocs{
description:string;
recommended?:boolean;
requiresTypeChecking?:boolean;
}
exportconst 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.Many ESLint users assume enabling a plugin'srecommended 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 arecommended config.
You have roughly two options:
- Have your plugin's
recommendedconfig require enabling type information - Have a separate config with a name like
recommendedTypeChecked
Either way, explicitly mention the strategy taken in your docs.
PerCustom Rules >Conditional Type Information, we recommend not changing rule logic based on whether type information is available.
Seeeslint-plugin-example-typed-linting for an example plugin that supports typed linting.