Custom Rules
Custom rules allow you to enforce code standards specific to your project. This guide shows you how to create, test, and distribute custom rules.Rule Structure
Every rule is a JavaScript module that exports an object with two main properties:The Meta Object
Themeta object contains metadata about your rule:
Rule type:
"problem", "suggestion", or "layout""problem": Code that will cause errors or confusing behavior"suggestion": Code that could be improved but won’t cause errors"layout": Whitespace, semicolons, and code formatting
Either
"code" or "whitespace" if the rule supports automatic fixesSet to
true if the rule provides suggestionsJSON Schema defining the rule’s options. Mandatory when the rule has options.
Object mapping message IDs to message templates
The Create Function
Thecreate() function returns an object with visitor methods that ESLint calls while traversing the AST:
Real Example: no-console
Here’s a simplified version of ESLint’sno-console rule:
The Context Object
Thecontext parameter provides essential information and methods:
The rule ID
The filename being linted
Object to interact with the source code
Configured options for the rule (excludes severity)
Shared settings from configuration
Language configuration (sourceType, ecmaVersion, parser, etc.)
Reports a problem in the code
Reporting Problems
Usecontext.report() to report violations:
Using Message IDs
Message IDs are the recommended way to define messages:Applying Fixes
Provide automatic fixes using thefix function:
Fixer Methods
Insert text after a node or token
Insert text before a node or token
Remove a node or token
Replace the text of a node or token
Insert text after a range
Insert text before a range
Remove text in a range
Replace text in a range
Fix Best Practices
Providing Suggestions
Suggestions are alternatives to automatic fixes that require user approval:Accessing Source Code
TheSourceCode object provides methods to work with the code:
Common SourceCode Methods
getText(node)
getText(node)
Returns the source code for a node
getFirstToken(node)
getFirstToken(node)
Returns the first token of a node
getTokensBefore(node, count)
getTokensBefore(node, count)
Returns tokens before a node
getCommentsBefore(node)
getCommentsBefore(node)
Returns comments before a node
Working with Options
Define a schema to validate rule options:Accessing Variable Scopes
Track variables and their usage:Creating Your First Rule
1
Define the Rule
Create a new file for your rule:
lib/rules/no-var.js
2
Test Your Rule
Create tests using RuleTester:
tests/rules/no-var.js
3
Use the Rule
Add to your ESLint configuration:
eslint.config.js
Testing Rules
UseRuleTester to validate your rule:
Runtime Rules
Test rules locally without publishing:1
Create a rules directory
2
Add your rule file
3
Configure ESLint
eslint.config.js
4
Run with --rulesdir
Performance Tips
Profile Your Rule
Real-World Examples
Learn from ESLint’s built-in rules:no-unused-vars
Complex scope analysis and variable tracking
semi
Automatic fixing and option handling
no-shadow
Scope traversal and variable shadowing
array-callback-return
Code path analysis
Next Steps
Create a Plugin
Package your rule for distribution
Learn About Selectors
Use advanced AST selectors
Scope Manager
Deep dive into scope analysis
Code Path Analysis
Analyze execution paths