Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: fix doc for no-unneeded-ternary rule (fixes #12098) #12410

Merged
merged 4 commits into from Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}]
}
]
});