Quick Start
Use thejust new-rule command to generate a new rule:
Plugin-Specific Commands
For convenience, plugin-specific aliases are available:Step-by-Step Guide
Generate the rule scaffold
crates/oxc_linter/src/rules/eslint/no_console_log.rs.Understand the generated structure
The generated file includes:
- Rule metadata (name, category, documentation)
- Configuration struct (if the rule accepts options)
- Diagnostic functions for error messages
- Rule implementation with the visitor pattern
- Test scaffolding
Implement the rule logic
Edit the generated file to implement your rule’s logic using the visitor pattern.
Rule Structure
A typical rule file follows this structure:1. Imports and Declarations
2. Diagnostic Functions
Define functions that return diagnostic messages:3. Rule Declaration
Use thedeclare_oxc_lint! macro to define the rule:
4. Rule Implementation
Implement theRule trait:
5. Test Cases
Add comprehensive test cases:Visitor Pattern
Oxc uses the visitor pattern for AST traversal. Your rule’srun method is called for each AST node.
Common AST Kinds
Using Semantic Information
Access semantic information via the context:Rule Categories
Choose the appropriate category for your rule:correctness- Code that is definitely wrongsuspicious- Code that is likely wrongpedantic- Opinionated style choicesrestriction- Banned patterns (opt-in)style- Code style improvementsperf- Performance improvements
Rule Configuration
If your rule accepts configuration options:Testing Your Rule
Running Tests
Test Best Practices
- Cover all branches - Test every code path in your rule
- Include edge cases - Test unusual or complex scenarios
- Test with and without config - If your rule has options
- Use real-world examples - Base tests on actual code patterns
- Test false positives - Ensure valid code passes
Snapshot Testing
Thetest_and_snapshot() method creates snapshots of diagnostics:
Debugging Your Rule
Using the Linter Example
Test your rule on actual files:Adding Debug Output
Using AST Explorer
Use the Oxc Playground to explore the AST structure of JavaScript code.Updating Rule Tests from Upstream
If you’re implementing an ESLint rule, you can pull test cases from upstream:Rule Documentation
Include comprehensive documentation in thedeclare_oxc_lint! macro:
Submitting Your Rule
Create a pull request
Follow the Getting Started guide for creating a PR.
Resources
Next Steps
- Review the Testing Guide
- Explore the Architecture
- Check out existing rules for inspiration