-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Description
This is a "documentation bug".
The version of ESLint you are using.
6.1.0
The problem you want to solve.
Documentation for defaultAssignment
option of the no-unneeded-ternary
rule is incorrect.
defaultAssignment = true
means don't report expressions such as x ? x : y
.
defaultAssignment = false
means report expressions such as x ? x : y
.
Default is true
.
The rule doesn't actually check if the expression is in an assignment context. That's by design from the start (#3232 and #3260). It simply searches for all ternary expressions where test and consequent are same identifier.
Incorrect parts of the documentation are:
- The
incorrect
example:
/*eslint no-unneeded-ternary: "error"*/
var a = x === 2 ? true : false;
var a = x ? true : false;
var a = f(x ? x : 1);
The example is wrong, var a = f(x ? x : 1);
is not a warning because of the default option value.
- The
correct
example:
/*eslint no-unneeded-ternary: "error"*/
var a = x === 2 ? "Yes" : "No";
var a = x !== false;
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.
The comment is wrong.
- defaultAssignment section
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).
The option (when true
) allows such expressions everywhere.
Your take on the correct solution to problem.
Fix the documentation.
Perhaps also consider changing the name of the option for two reasons:
TheAssignment
part is confusing.When the option name doesn't have an explicit prefix,true
ususally means "enforce on", rather than "allow".
Maybe allowSameConsequent
or allowSameIfTrue
.
Are you willing to submit a pull request to implement this change?
Yes, I would be glad to do it.