Skip to content

Commit

Permalink
Docs: fix doc for no-unneeded-ternary rule (fixes #12098) (#12410)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
samrae7 authored and platinumazure committed Oct 22, 2019
1 parent b1dc58f commit e15e1f9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
10 changes: 6 additions & 4 deletions docs/rules/no-unneeded-ternary.md
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:

```js
/*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/

var a = x ? x : 1;

f(x ? x : 1);
```

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.
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-unneeded-ternary.js
Expand Up @@ -27,13 +27,23 @@ ruleTester.run("no-unneeded-ternary", rule, {
"var a = x === 2 ? 'true' : 'false';",
"var a = foo ? foo : bar;",
"var value = 'a';var canSet = true;var result = value || (canSet ? 'unset' : 'can not set')",
"var a = foo ? bar : foo;",
"foo ? bar : foo;",
"var a = f(x ? x : 1)",
"f(x ? x : 1);",
"foo ? foo : bar;",
"var a = foo ? 'Yes' : foo;",
{
code: "var a = foo ? 'Yes' : foo;",
options: [{ defaultAssignment: false }]
},
{
code: "var a = foo ? bar : foo;",
options: [{ defaultAssignment: false }]
},
{
code: "foo ? bar : foo;",
options: [{ defaultAssignment: false }]
}
],
invalid: [
Expand Down Expand Up @@ -314,6 +324,39 @@ ruleTester.run("no-unneeded-ternary", rule, {
line: 1,
column: 13
}]
},
{
code: "f(x ? x : 1);",
output: "f(x || 1);",
options: [{ defaultAssignment: false }],
errors: [{
message: "Unnecessary use of conditional expression for default assignment.",
type: "ConditionalExpression",
line: 1,
column: 7
}]
},
{
code: "x ? x : 1;",
output: "x || 1;",
options: [{ defaultAssignment: false }],
errors: [{
message: "Unnecessary use of conditional expression for default assignment.",
type: "ConditionalExpression",
line: 1,
column: 5
}]
},
{
code: "var a = foo ? foo : bar;",
output: "var a = foo || bar;",
options: [{ defaultAssignment: false }],
errors: [{
message: "Unnecessary use of conditional expression for default assignment.",
type: "ConditionalExpression",
line: 1,
column: 15
}]
}
]
});

0 comments on commit e15e1f9

Please sign in to comment.