|
| 1 | +# ESLint Project Knowledge |
| 2 | + |
| 3 | +## Project Structure |
| 4 | + |
| 5 | +- The ESLint project is organized into several directories: |
| 6 | + - `bin`: Contains the command-line interface (CLI) files |
| 7 | + - `conf`: Contains configuration data for ESLint |
| 8 | + - `docs`: Contains the documentation website for ESLint |
| 9 | + - `lib`: Contains the main source code for ESLint |
| 10 | + - `messages`: Contains verbose error messages for certain errors in ESLint |
| 11 | + - `packages`: Contains additional packages that are published separately |
| 12 | + - `eslint-config-eslint`: Contains the ESLint configuration package |
| 13 | + - `js`: Contains the `@eslint/js` package |
| 14 | + - `templates`: Contains templates tools that generate files |
| 15 | + - `tests`: Contains the test files for ESLint |
| 16 | + - `tools`: Contains scripts for building, testing, and other tasks |
| 17 | +- Test files mirror the structure of the source code files they're testing |
| 18 | +- Configuration files are in the root directory, including `eslint.config.js`. |
| 19 | + |
| 20 | +## Source File Conventions |
| 21 | + |
| 22 | +- Source files follow a standard structure: |
| 23 | + - Header with `@fileoverview` and `@author` |
| 24 | + - Requirements section with necessary imports |
| 25 | + - Optionally a type definitions section for importing types |
| 26 | + - Optionally a helpers section with utility functions and constants |
| 27 | + - Optionally an exports section where the file has its classes, functions, and constants to export |
| 28 | + - For tools and scripts, a main section that executes code |
| 29 | + |
| 30 | +## Testing Conventions |
| 31 | + |
| 32 | +- Tests use Mocha for the testing framework |
| 33 | +- Chai is used for assertions with `const assert = require("chai").assert` |
| 34 | +- Test files follow a standard structure: |
| 35 | + - Header with `@fileoverview` and `@author` |
| 36 | + - Requirements section with necessary imports |
| 37 | + - Optionally a helpers section with utility functions and constants |
| 38 | + - Tests section with describe/it blocks |
| 39 | +- Tests are organized in describe blocks for classes and methods |
| 40 | +- Before running tests, objects like mock contexts/configs are set up |
| 41 | +- Testing patterns include: |
| 42 | + - Verifying object properties are set correctly |
| 43 | + - Testing for expected behaviors and edge cases |
| 44 | + - Validating error handling scenarios |
| 45 | + - Testing deprecated methods for backward compatibility |
| 46 | +- All new exported functions and public class members require writing new tests |
| 47 | +- All bug fixes must have corresponding tests |
| 48 | +- Never delete existing tests even if they are failing |
| 49 | +- `npm test` command runs all tests in the `tests` directory |
| 50 | +- `npx mocha <filename>` command runs a specific test file |
| 51 | + |
| 52 | +## Rules |
| 53 | + |
| 54 | +- Rules are located in the `lib/rules` directory |
| 55 | +- Documentation for rules is located in the `docs/src/rules` directory |
| 56 | +- Each rule module exports an object with the following structure: |
| 57 | + - `meta`: Contains metadata about the rule, including: |
| 58 | + - `type`: The type of rule (e.g., "suggestion", "problem", "layout") |
| 59 | + - `docs`: Documentation properties including description, recommended status, and URL |
| 60 | + - `schema`: JSON Schema for rule configuration options |
| 61 | + - `fixable`: Whether the rule provides auto-fixes (e.g., "code", "whitespace") |
| 62 | + - `messages`: Message IDs and text templates for reporting |
| 63 | + - `create`: A function that accepts a context object and returns an object with AST visitor methods |
| 64 | + |
| 65 | +## Rule Implementation Patterns |
| 66 | + |
| 67 | +- Rules use the visitor pattern to analyze JavaScript AST nodes |
| 68 | +- Helper functions should be defined outside the `create` function to avoid recreating them on each execution |
| 69 | +- Common utilities for working with ASTs are available in `./utils/ast-utils` |
| 70 | +- Rules that need to fix code should implement a fixer function that returns corrections to apply |
| 71 | +- Rules should avoid duplicate computations by factoring out common checks into helper functions |
| 72 | +- The rule tester configuration now uses flat configuration format (`languageOptions` instead of `parserOptions`) |
| 73 | + |
| 74 | +## Rule Documentation |
| 75 | + |
| 76 | +- Documentation files use frontmatter with `title` and `rule_type` fields |
| 77 | +- Rule documentation should include: |
| 78 | + - A description of what the rule checks |
| 79 | + - The rule details section explaining when the rule reports issues |
| 80 | + - Examples of incorrect and correct code wrapped in ::: incorrect and ::: correct blocks |
| 81 | + - A "When Not To Use It" section explaining when the rule might not be appropriate |
| 82 | + - Optional version information and additional resources |
| 83 | +- Code examples in the documentation should include the enabling comment (e.g., `/*eslint rule-name: "error"*/`) |
| 84 | + |
| 85 | +## Rules Registration |
| 86 | + |
| 87 | +- New rules must be added to the `lib/rules/index.js` file to be available in ESLint |
| 88 | +- Rules are registered in an alphabetically sorted object using the `LazyLoadingRuleMap` for efficient loading |
0 commit comments