This ESLint plugin includes several opinionated rules aimed at enhancing the readability, performance, and maintainability of conditionals in JavaScript.
These rules focus on writing clear and efficient conditional statements, so while they encourage consistency and simplicity, they may not suit every coding style. Feel free to adjust the rules based on your team's preferences.
Install the npm package
# If eslint is installed globally
npm install -g @andreasnicolaou/eslint-plugin-conditional-best-practices
# If eslint is installed locally
npm install -D @andreasnicolaou/eslint-plugin-conditional-best-practices
To use this plugin in your project, add it to your ESLint configuration like this:
{
"plugins": ["@andreasnicolaou/conditional-best-practices"],
"extends": [
"plugin:@andreasnicolaou/conditional-best-practices/recommended"
]
}
- Alternatively, you can extend individual rules if you prefer more granular control.
This plugin enforces the following opinionated best practices:
Name | Description |
---|---|
no-constant-conditionals | This rule disallows conditionals that always evaluate to true or false. |
no-duplicated-conditions | This rule disallows duplicate conditions in if-else chains. |
no-excessive-nested-conditionals | This rule disallows excessive nesting of conditionals in order to improve code readability. |
no-long-else-if-chains | This rule limits the number of consecutive else-if statements. |
no-nested-ternary-operators | This rule disallows nested ternary operators. |
no-useless-ternary | This rule disallows unnecessary ternary expressions. |
prefer-early-return | This rule encourages the use of early returns instead of deeply nested if-else blocks. |
require-default-in-switch | This rule ensures that switch statements include a default case and/or its not empty. |
no-eval | This rule disallows the use of eval() due to security risks. |
Currently, this plugin does not support automatic fixes for code violations. It only detects incorrect usage and warns you, helping to maintain consistency and best practices in your codebase.