-
-
Notifications
You must be signed in to change notification settings - Fork 260
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
Enhance SyntaxLogicalInverter to handle CoalesceExpressions and add corresponding tests #1069
Enhance SyntaxLogicalInverter to handle CoalesceExpressions and add corresponding tests #1069
Conversation
case SyntaxKind.CoalesceExpression: | ||
{ | ||
var binaryExpression = (BinaryExpressionSyntax)expression; | ||
if (binaryExpression.Right.Kind() == SyntaxKind.FalseLiteralExpression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use IsKind
instead of Kind
. It is preferred in the codebase. It has also slightly better perf.
if (binaryExpression.Right.Kind() == SyntaxKind.FalseLiteralExpression) | ||
{ | ||
// !(x ?? false) === (x != true) | ||
return BinaryExpression(SyntaxKind.NotEqualsExpression, binaryExpression.Left, LiteralExpression(SyntaxKind.TrueLiteralExpression)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CSharpFactory
provides some helper methods to make these calls shorter: CSharpFactory.NotEqualsExpression
and CSharpFactory.TrueLiteralExpression
.
👍 |
…orresponding tests (dotnet#1069)
Add explicit handling of CoalesceExpressions in the SyntaxLogicalInverter class. Currently, in Debug mode the Debug.Fail on L213 will be evaluated which is a less-than-ideal experience.
This PR also adds intelligent support for the case where the RHS of the coalesce operator is either true or false.
x??true
is inverted tox==false
x??false
is inverted tox!=true