Code Style & Linting
Relevant source files
The following files were used as context for generating this wiki page:
Cheerio maintains a strict code quality and formatting standard to ensure the library remains fast, flexible, and elegant. The project utilizes a multi-layered approach to linting and formatting, primarily relying on ESLint for complex logic and Biome for high-performance formatting and recommended linting rules.
The code style is enforced through automated git commit hooks, ensuring that every contribution adheres to the project's standards before being merged. Developers are expected to follow specific conventions for private methods and documentation to maintain consistency with the jQuery-like API structure.
Sources: CONTRIBUTING.md:10-15, package.json:106-118
Tooling Architecture
Cheerio integrates multiple tools to manage code quality. This architecture ensures that different types of files (JavaScript, TypeScript, JSON) are handled by the most appropriate tool.
Integrated Linting Flow
The following diagram illustrates how various tools interact during the development process to enforce standards.
The linting process is triggered both locally via Husky hooks and in the CI environment to ensure consistency across the development lifecycle.
Sources: package.json:115-125, eslint.config.js:10-25
Formatting Standards
Formatting is primarily handled by Biome, which is configured to use specific indentation and quote styles. Biome replaces traditional tools like Prettier for faster execution while maintaining similar formatting guarantees.
| Configuration | Value | Description |
|---|---|---|
indentStyle | space | Uses spaces for indentation instead of tabs. |
indentWidth | 2 | Standard 2-space indentation. |
quoteStyle | single | Enforces single quotes for JavaScript strings. |
trailingComma | all | Implied by recommended presets for better diffs. |
Sources: biome.json:11-19
ESLint & Biome Interoperability
To prevent conflicts between the two tools, the project uses eslint-config-biome. This configuration disables ESLint rules that might conflict with Biome's formatting output, such as the handling of global number constants.
Sources: eslint.config.js:203-205, eslint.config.js:88-95
Coding Conventions
Private Functionality
When implementing functionality that is not part of the public jQuery-like API, developers must follow these conventions:
- Static Methods: If the function does not require a Cheerio instance reference (
this), it is defined as a named function within the module. - Instance Methods: If the function requires a Cheerio instance, it is informally marked as private:
- Defined on the
Cheerio.prototype. - Prefixed with an underscore (e.g.,
_root,_parse). - Documented with the
@api privateJSDoc tag.
- Defined on the
Sources: CONTRIBUTING.md:27-39, eslint.config.js:63-66
Naming and Complexity Rules
The project enforces specific naming conventions and logic simplifications through Biome's linter rules:
- Logic: Encourages the use of
whileloops, simplified logic expressions, and removal of uselessundefinedassignments. - Style: Enforces
as constassertions, consistent array types, and specific file naming conventions. - Renaming (Pending): The project has identified a need for more descriptive names (e.g.,
itoindex,eltoelement), though some of these rules are currently disabled to manage large-scale refactoring.
Sources: biome.json:23-58, eslint.config.js:101-110
Configuration Summary
The following table summarizes the primary linting commands used in the repository:
| Command | Tool | Purpose |
|---|---|---|
npm run lint | ESLint, TS, Biome | Full project-wide linting check. |
npm run lint:es | ESLint | Checks JavaScript/TypeScript logic and JSDoc rules. |
npm run lint:ts | TSC | Performs static type checking without emitting files. |
npm run lint:biome | Biome | Fast formatting and recommended linting checks. |
npm run format | Biome & ESLint | Automatically fixes formatting and linting errors. |
Sources: package.json:106-112
Vitest & Test Styling
For test files (*.spec.ts), specific rules apply to support the Vitest environment. ESLint is configured with the vitest plugin to recognize globals like describe, expect, and it. Rules against http:// URLs are relaxed in tests because they are often used as fixture data for attributes or XML namespaces rather than actual network links.
Sources: eslint.config.js:159-176, biome.json:96-108
Conclusion
Code style and linting in Cheerio are designed to balance high-performance parsing with a developer-friendly API. By combining the speed of Biome with the deep logic analysis of ESLint and TypeScript, the project ensures that code remains robust and maintainable while strictly following the jQuery-compatible design pattern.
Sources: Readme.md:46-52, CONTRIBUTING.md:1-5