Custom Parsers
Custom parsers enable ESLint to understand non-standard JavaScript syntax, experimental features, or entirely different languages. A parser transforms source code into an Abstract Syntax Tree (AST) that ESLint can analyze.When to Create a Parser
Create a custom parser when you need to:- Support TypeScript, Flow, or other type systems
- Enable experimental JavaScript features
- Lint domain-specific languages (DSLs)
- Handle custom syntax extensions
- Process template languages
Popular Custom Parsers:
@typescript-eslint/parser- TypeScript support@babel/eslint-parser- Babel syntax supportvue-eslint-parser- Vue.js single-file components
Parser Interface
A parser must export either aparse() or parseForESLint() method:
Parser Methods
Simple parsing method that returns only the ASTParameters:
code(string): Source code to parseoptions(object): Parser options from configuration
Advanced parsing method that returns AST and additional dataParameters:
code(string): Source code to parseoptions(object): Parser options from configuration
ast(required): The AST objectservices(optional): Parser-dependent servicesscopeManager(optional): Custom scope managervisitorKeys(optional): Custom visitor keys
Return Object Structure
When usingparseForESLint(), return an object with these properties:
ast (required)
The Abstract Syntax Tree based on ESTree specification:services (optional)
Provide custom services to rules. For example, TypeScript parser provides type checking:scopeManager (optional)
Custom ScopeManager for non-standard scoping:visitorKeys (optional)
Define custom AST traversal for non-standard nodes:AST Specification
Your parser must generate an AST compatible with ESLint requirements:All Nodes
Every AST node must have:Node type (e.g., “Identifier”, “FunctionDeclaration”)
Character indices [start, end] in source code
Line and column location information
The
parent property will be set by ESLint during traversal. Ensure it’s writable.Program Node
The root node must include:Array of tokens affecting program behavior
Array of comment tokens
Literal Nodes
Literal nodes must include:The original source code text
Parser Metadata
Include metadata for better debugging and caching:Should match your npm package name
Should match your npm package version
Example: Simple Custom Parser
Here’s a basic parser that adds logging:Example: Parser with Services
Provide custom services to rules:Creating Your Parser
1
Set Up Project
Create a new npm package:
2
Install Dependencies
3
Implement Parser
Create
index.js:4
Test Parser
Create a test file:
test.js
Packaging and Publishing
1
Update package.json
package.json
2
Publish to npm
3
Use in Projects
Install:Configure:
eslint.config.js
Using a Custom Parser
Configure ESLint to use your parser:Parser Options
Parsers receive options from the configuration:eslint.config.js
Real-World Example: TypeScript Parser
Study@typescript-eslint/parser for a complete implementation:
Testing Strategies
Unit Tests
Integration Tests
Test with ESLint:Performance Considerations
Benchmarking
Common Patterns
Wrapping Existing Parser
Adding Custom Nodes
Troubleshooting
Parser not found
Parser not found
Ensure the parser is installed and correctly imported:
Invalid AST structure
Invalid AST structure
Verify all nodes have required properties:
typerangeloc
Program has tokens and comments.Scope analysis errors
Scope analysis errors
If providing custom
scopeManager, ensure it:- Implements required methods
- Correctly tracks variable declarations
- Resolves references properly
Performance issues
Performance issues
Profile your parser:
Resources
@typescript-eslint/parser
Reference implementation for TypeScript
ESTree Specification
Official AST specification
Espree
ESLint’s default JavaScript parser
Configure a Parser
User guide for parser configuration
Next Steps
Create Custom Rules
Build rules that use parser services
Create a Plugin
Package parser with rules and configs