Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
no-unneeded-ternary incorrect documentation for defaultAssignment option #12098
This is a "documentation bug".
The version of ESLint you are using.
The problem you want to solve.
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:
/*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,
/*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.
The option (when
Your take on the correct solution to problem.
Fix the documentation.
Are you willing to submit a pull request to implement this change?
Yes, I would be glad to do it.
Marking as accepted based on this Online Demo
The code is copy & paste from the 'incorrect' example in the docs. In this example, the rule doesn't report error for the third statement.
To avoid confusion, I removed the part about changing the option's name from the original post.
This is now just an issue to fix the docs to match the current behavior.