Skip to content

Enforces opinionated rules to improve the readability, performance, and maintainability of conditionals in JavaScript, encouraging cleaner and more efficient code.

License

Notifications You must be signed in to change notification settings

andreasnicolaou/eslint-plugin-conditional-best-practices

Repository files navigation

@andreasnicolaou/eslint-plugin-conditional-best-practices

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.

Installation

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.

Usage

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.

Autofixing

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.

About

Enforces opinionated rules to improve the readability, performance, and maintainability of conditionals in JavaScript, encouraging cleaner and more efficient code.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •