eslint-plugin-ternaries

raw JSON →
1.1.0 verified Sat Apr 25 auth: no javascript

An ESLint plugin that provides rules specifically for controlling the use of ternary expressions in JavaScript. The current stable version is 1.1.0, which adds the no-empty-ternary rule to complement the existing no-null-ternary rule. This plugin is designed for teams that want to enforce coding standards around ternaries, such as disallowing null or empty string values in ternary branches. It is a lightweight plugin with no additional dependencies besides ESLint itself.

error Error: Failed to load plugin 'ternaries': Cannot find module 'eslint-plugin-ternaries'
cause Plugin not installed in node_modules.
fix
Run 'npm install eslint-plugin-ternaries --save-dev'.
error Error: Failed to load rule 'no-null-ternary'
cause Rule missing 'ternaries/' prefix in ESLint config.
fix
Use 'ternaries/no-null-ternary' instead of 'no-null-ternary'.
gotcha Rules must be prefixed with 'ternaries/' in ESLint configuration.
fix Use 'ternaries/no-null-ternary' instead of just 'no-null-ternary'.
breaking ESLint required as peer dependency. Plugin will not work without ESLint.
fix Ensure ESLint is installed in your project.
gotcha Severity values: 0 (off), 1 (warn), 2 (error) or strings 'off', 'warn', 'error'. Do not use other numbers.
fix Use correct severity: 'error', 'warn', or 'off'.
npm install eslint-plugin-ternaries
yarn add eslint-plugin-ternaries
pnpm add eslint-plugin-ternaries

Shows installation, configuration in .eslintrc, and example violations.

// Step 1: Install ESLint and the plugin
npm install eslint --save-dev
npm install eslint-plugin-ternaries --save-dev

// Step 2: Create .eslintrc.json
{
  "plugins": ["ternaries"],
  "rules": {
    "ternaries/no-null-ternary": "error",
    "ternaries/no-empty-ternary": "warn"
  }
}

// Step 3: Test with a file
const x = condition ? null : 1; // Error: ternaries/no-null-ternary
const y = condition ? '' : 2;   // Warning: ternaries/no-empty-ternary