linters/eslint-rules/prefer-logical-operator-over-ternary
ESLint rule: prefer-logical-operator-over-ternary
unicorn/prefer-logical-operator-over-ternary, with the fix it withholds in a TypeScript file.
Unicorn reports a ternary with one boolean branch (a ? false : b) and fixes it to a logical expression (!a && b), but only outside TypeScript. unicorn/prefer-ternary fixes if (a) { return false; } return b; into exactly that ternary, so in a TypeScript file lint:fix produced an error it then could not fix, and every site had to be rewritten by hand. This rule reports what unicorn reports and adds the fix where one branch is a boolean literal. The rewrite is exact at runtime: with a negated test (a ? false : b → !a && b, a ? b : true → !a || b) for any test, and otherwise (a ? true : b → a || b, a ? b : false → a && b) because unicorn reports that shape only for a test it has proven boolean.
Variables
Section titled “Variables”| Variable | Description |
|---|---|
| MESSAGE_ID | The message id unicorn reports a ternary under when a logical operator could replace it. |
| preferLogicalOperatorOverTernary | ESLint rule reporting what unicorn/prefer-logical-operator-over-ternary reports, fixing a ternary with a boolean literal branch in a TypeScript file too. |
| UNICORN_RULE_NAME | The id of the unicorn rule this rule delegates to, inside the unicorn namespace. |