Docs: fix doc for no-unneeded-ternary rule (fixes#12098) (#12410)
* Docs: fix docs for no-unneeded-ternary (fixes#12098)
* Update: add test cases
- default assignment on right hand side
- default assignment not on right side
- assignment that is not default e.g. x ? 1 : x
* Update doc for clarity
@@ -41,8 +41,6 @@ Examples of **incorrect** code for this rule:
var a = x ===2?true:false;
var a = x ?true:false;
var a =f(x ? x :1);
```
Examples of **correct** code for this rule:
@@ -58,7 +56,7 @@ var a = x ? "Yes" : "No";
var a = x ? y : x;
var a =x ? x :1; //Note that this is only allowed as it on the right hand side of an assignment; this type of ternary is disallowed everywhere else. See defaultAssignment option below for more details.
f(x ? x :1); //default assignment - would be disallowed if defaultAssignment option set to false. See option details below.
```
##Options
@@ -70,16 +68,20 @@ This rule has an object option:
###defaultAssignment
The defaultAssignment option allows expressions of the form `x ? x : expr` (where `x` is any identifier and `expr` is any expression) as the right hand side of assignments (but nowhere else).
When set to `true`, which it is by default, The defaultAssignment option allows expressions of the form `x ? x : expr` (where `x` is any identifier and `expr` is any expression).
Examples of additional **incorrect** code for this rule with the `{ "defaultAssignment": false }` option:
Note that `defaultAssignment: false` still allows expressions of the form `x ? expr : x` (where the identifier is on the right hand side of the ternary).
##When Not To Use It
You can turn this rule off if you are not concerned with unnecessary complexity in conditional expressions.