Extending ESLint
ESLint is designed to be completely extensible, allowing you to create custom rules, parsers, processors, formatters, and plugins to meet your project’s unique needs.Prerequisites
To extend ESLint, you should have:- Knowledge of JavaScript, since ESLint is written in JavaScript
- Familiarity with Node.js, since ESLint runs on it
- Comfort with command-line programs
- Understanding of Abstract Syntax Trees (ASTs)
Ways to Extend ESLint
ESLint provides multiple extension points, each serving different purposes:Custom Rules
Create rules to enforce code standards specific to your project or organization.
Custom Parsers
Enable ESLint to understand non-standard JavaScript syntax or new language features.
Custom Processors
Process non-JavaScript files to extract and lint JavaScript code.
Custom Formatters
Control how ESLint displays linting results.
Plugins
Bundle rules, processors, and configs into reusable packages.
Shareable Configs
Share ESLint configurations across multiple projects.
Extension Architecture
Understanding how these extensions work together:Parser
Transforms source code into an Abstract Syntax Tree (AST) that ESLint can analyze. Custom parsers enable support for TypeScript, JSX, or experimental syntax.Rules
Analyze the AST and report problems. Rules are the core of ESLint’s functionality.Processor
Extracts JavaScript from non-JavaScript files (like Markdown or HTML) before parsing.Formatter
Formats linting results for display or integration with other tools.Plugin
Packages multiple extensions together for distribution.Common Use Cases
Enforcing Team-Specific Patterns
Enforcing Team-Specific Patterns
Create custom rules to enforce patterns unique to your codebase:
- Specific naming conventions
- Custom API usage patterns
- Architecture constraints
- Security requirements
Supporting Custom Syntax
Supporting Custom Syntax
Build custom parsers for:
- Domain-specific languages
- Experimental JavaScript features
- TypeScript or Flow type annotations
- Template languages
Linting Non-JavaScript Files
Linting Non-JavaScript Files
Use processors to lint:
- Markdown code blocks
- HTML script tags
- Vue single-file components
- Svelte components
Integrating with Tools
Integrating with Tools
Create formatters for:
- CI/CD pipelines
- IDE integrations
- Code review systems
- Custom reporting dashboards
Development Workflow
Understand the AST
Use the ESLint Code Explorer to visualize how your code is represented as an AST.
Implement Your Extension
Create your rule, parser, processor, or formatter following the appropriate guide.
Performance Considerations
Profiling Rules
Measure rule performance using theTIMING environment variable:
Core APIs
Key APIs you’ll use when extending ESLint:SourceCode
Access tokens, comments, and AST navigation methods
RuleTester
Test framework for validating rule behavior
context
Provides rule metadata, reporting, and configuration
Scope
Track variable declarations and references
fixer
Apply automatic fixes to code
CodePath
Analyze code execution paths
Best Practices
Documentation
- Provide clear descriptions of what your extension does
- Include examples of valid and invalid code patterns
- Document all configuration options
- Link to relevant resources
Testing
- Test edge cases and error conditions
- Include tests for all options and configurations
- Verify fixes don’t introduce new problems
- Test performance with large files
Versioning
- Follow semantic versioning (semver)
- Document breaking changes clearly
- Maintain changelog
- Test against multiple ESLint versions
Distribution
- Use clear naming conventions (e.g.,
eslint-plugin-*) - Add appropriate npm keywords
- Include comprehensive README
- Specify peer dependencies correctly
Next Steps
Create Custom Rules
Start with the most common extension type
Build a Plugin
Package your extensions for distribution