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.
Common errors
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'.
Warnings
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'.
Install
npm install eslint-plugin-ternaries yarn add eslint-plugin-ternaries pnpm add eslint-plugin-ternaries Imports
- Plugin wrong
// npm install eslint-plugin-ternaries --save-dev (forgetting to add to plugins)correct// .eslintrc: { "plugins": ["ternaries"] } - no-null-ternary wrong
{ "rules": { "no-null-ternary": "error" } }correct// .eslintrc: { "rules": { "ternaries/no-null-ternary": "error" } } - no-empty-ternary wrong
{ "rules": { "ternaries/no-empty-ternary": "error", "ternaries/no-null-ternary": 2 } }correct// .eslintrc: { "rules": { "ternaries/no-empty-ternary": "warn" } }
Quickstart
// 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