Individual configurable linting checks that enforce code quality standards
Core Idea: ESLint rules are atomic code quality checks that can be individually enabled, disabled, or configured to enforce specific patterns and practices in JavaScript and TypeScript code.
Key Elements
-
Rule Structure:
- Each rule has a unique identifier (e.g.,
no-unused-vars
) - Rules can be set to "off" (0), "warn" (1), or "error" (2)
- Many rules accept configuration options via array syntax
- Rules can be disabled for specific lines using inline comments
- Rules can target syntax, potential bugs, or style conventions
- Each rule has a unique identifier (e.g.,
-
Rule Categories:
- Possible Errors: Detect code likely to contain bugs
- Best Practices: Enforce better ways of doing things
- Variables: Ensure proper variable declaration and usage
- Stylistic Issues: Enforce consistent code formatting
- ECMAScript 6: Handle ES6/ES2015+ features
- Plugin-specific: Extended functionality from plugins
-
Configuration Methods:
// Different ways to configure rules { rules: { // Basic on/off "no-console": "error", // With severity level "no-unused-vars": 2, // With options "quotes": ["error", "single", { "avoidEscape": true }], // Turning off a rule from an extended config "react/prop-types": "off" } }
- **Common Core Rules**:
- `no-unused-vars`: Prevent unused variable declarations
- `no-undef`: Disallow undeclared variables
- `eqeqeq`: Require === and !== over == and !=
- `no-console`: Disallow console.log statements
- `quotes`: Enforce consistent quote style
- `semi`: Require or disallow semicolons
- `max-len`: Enforce maximum line length
- `indent`: Enforce consistent indentation
- **Rule Customization Example**:
```js
// Advanced rule configuration
{
"rules": {
"complexity": ["warn", 10],
"max-depth": ["error", 4],
"max-lines": ["warn", {"max": 300, "skipBlankLines": true}],
"no-magic-numbers": ["error", {
"ignore": [-1, 0, 1, 2],
"ignoreArrayIndexes": true
}],
"no-restricted-syntax": [
"error",
{
"selector": "ForInStatement",
"message": "For..in loops are discouraged, use Object.keys instead."
}
]
}
}
```
## Connections
- **Related Concepts**: ESLint (the tool that applies these rules), ESLint Configurations (collections of rule settings)
- **Broader Context**: Code Style Guides (formalized as rule configurations)
- **Applications**: Static Code Analysis (mechanism for automated code review)
- **Components**: ESLint Plugins (provide additional rules beyond core)
## References
1. ESLint Rules Documentation: https://eslint.org/docs/latest/rules/
2. "Understanding ESLint Rules" by Nicholas C. Zakas
3. "JavaScript Linting Essentials" by Cory House
#eslint #javascript #typescript #code-quality #linting-rules
---
**Connections:**
-
---
**Sources:**
- From: Syntax - Lint como un desarrollador senior con eslint + husky + lint staged + acciones de github