transform (t ? c : expr)
to (t && c)
if expr
is a variant of false
the "false variant" is defined in the rule config
note: this is a lossy transform.
if it works for your code depends on
how the return value of r=(t ? c : expr)
is tested
for example, when r is tested with a 'bool test' like
if (r) {c} else {a}
or (r && c)
or (r ? c : a)
then you can use the testVariant 'bool'
and the transform is lossless for your code
First install ESLint:
$ npm i -g eslint
Second install the plugin:
$ npm i -g https://github.com/milahu/eslint-plugin-ternary-to-binary-conditional
If you have no .eslintrc.js
config file,
run eslint --init
to create a new one
Add plugin and rule to your .eslintrc.js
:
module.exports = {
// ...
"plugins": [
"ternary-to-binary-conditional",
],
"rules": {
"ternary-to-binary-conditional/ternary-to-binary-conditional": [
"error", {
testVariant: "strict",
//testExpression: "exprSrc === 'void 0'",
}
],
},
};
There are (at least) three types of false in javascript:
- strict false:
expr === false
- abstract false:
expr == false
- bool false:
Boolean(expr) === false
Sample "false expressions" are
- strict:
[ false ]
- abstract:
[ false, "", 0, -0, +0, [0], [], [[]], {[]}, {[[]]}, .... ]
- bool:
[ false, "", 0, -0, +0, NaN, void 0, undefined, null, {}, {{}}, .... ]
bool false is used in
if (expr) {cond}
(if branch)if (expr) {cond} else {alt}
(if else branch)(expr && cond)
(binary conditional expression)(expr ? cond : alt)
(ternary conditional expression)
The option testVariant
or testExpression
decides,
how expr
is tested in the ternary conditional (t ? c : expr)
The default config is {testVariant: "strict"}
Values for testVariant
are defined in testExpressionPresets
in the file lib/rules/ternary-to-binary-conditional.js
Each testVariant sets a testExpression:
- strict:
expr === false
(default) - abstract:
expr == false
- bool:
!expr
- bool-or-abstract:
!expr || expr == false
- react:
expr === false || expr === null || expr === undefined
- coffeescript:
exprSrc === 'void 0'
If you need a custom test expression,
you can set testExpression
to something like
expr === null || expr === undefined
exprSrc === 'void 0'
(()=>{return exprSrc === 'false';})()
testExpression can test expr
or exprSrc
if testExpression
is set, testVariant
is ignored
the coffeescript compiler coffee -c
translates binary conditionals like res = if test then cond
to ternary conditionals like res = test ? cond : void 0
for example,
when transforming React CJSX (coffee-JSX) to JSX code:
# cjsx
f = () ->
<Component arg={if test then "cond"} />
// jsx
f = () => (
<Component arg={test ? "cond" : void 0} />
);
but the expected result is
// jsx
f = () => (
<Component arg={test && "cond"} />
);
such ternary conditionals are unnecessary in some situations
more precise:
when res
is tested with a 'bool test'
this plugin was made to remove such unneeded ternary conditionals
and transform them to res = test && cond
the rule ternary-to-binary-conditional
is similar
to the eslint rule no-unneeded-ternary (source)
license is CC-0 aka "creative commons zero"