Craft an ESLint Plugin for Your Team
Introduction
When your style guide expands or your team decides to make changes, having those rules in your style lint is the way to go. This way, team members can see the recommended approach in their editor when adding or modifying code. The CI will catch code that doesn't fit the guidelines in every PR, saving time on commenting and requesting changes from the PR owner. Newcomers to your team will also feel more confident in their early contributions. Frontend developers typically use ESLint for linting coding styles, as it offers an excellent interface for adding custom rules. In this post, I'll guide you through creating your own ESLint plugin and explain how to test and document it for streamlined communication and consistent code.
Crafting an ESLint Plugin
To kickstart your plugin creation, use the Yeoman generator, which helps set up the plugin's basic structure.
After creating a plugin, add your first rule with yo eslint:rule
. A rule consists of three components: source file, test file, and documentation.
Rule - Source File
Understanding AST (Abstract Syntax Tree) and the Visitor Pattern is essential for developing an ESLint plugin (Babel and Codemods plugins also use these tools).
Consider the limit-continuous-import-declarations
rule as an example:
Additionally, enhance your rule with a meta object containing properties like type
, docs
, fixable
, schema
, and more.
Note: Use Program:exit
when the rule needs to wait until the entire file is parsed.
Rule - Test File
Don't forget to configure the correct parser for your ruleTester
. For instance, set the parser to @babel/eslint-parser
if you want to use ES6+ features.
Here's a portion of the unit tests in the limit-continuous-import-declarations
rule:
Use the ESLint Plugin
To use the plugin, configure your ESLint configuration file as follows:
Alternatively, if the plugin offers configurations like recommended
, you can use it like this:
Complete Example
You can find the complete source code in this GitHub repository: https://github.com/wtlin1228/eslint-plugin-wtlin.