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

VB -> C#: Null coalesce missing brackets #626

Closed
jmeijrink opened this issue Sep 10, 2020 · 7 comments · Fixed by #628
Closed

VB -> C#: Null coalesce missing brackets #626

jmeijrink opened this issue Sep 10, 2020 · 7 comments · Fixed by #628
Labels
good first issue VB -> C# Specific to VB -> C# conversion

Comments

@jmeijrink
Copy link

Input code

Public Class VisualBasicClass
   Public Sub New()
       Dim y As String = If(("xxx")?.InnerText, String.Empty).ToUpper()
   End Sub
End Class

Erroneous output

using System;

public partial class VisualBasicClass
{
    public VisualBasicClass()
    {
        string y = "xxx"?.InnerText ?? string.Empty.ToUpper();
    }
}

Expected output

using System;

public partial class VisualBasicClass
{
    public VisualBasicClass()
    {
        string y = ("xxx"?.InnerText ?? string.Empty).ToUpper();
    }
}

Details

  • Because the parentheses are missing in the Erroneous output the value of .InnerText is not uppercased. But left in lowercase.
  • Current online code converter at https://codeconverter.icsharpcode.net/ can be used to reproduce this issue.
@jmeijrink jmeijrink added the VB -> C# Specific to VB -> C# conversion label Sep 10, 2020
@GrahamTheCoder GrahamTheCoder changed the title VB -> C#: _Add a short description_ VB -> C#: ternary missing brackets Sep 11, 2020
@GrahamTheCoder
Copy link
Member

Should be a very straightforward fix. Let me know if you're interested in having a go and want some hints.

@SuperRembo
Copy link
Contributor

Actually for the ternary operator parenthesis are added when needed, but not for the null-coalescing operator.

I've added a test to describe the issue, but don't have a clue how to fix it. It would be nice to get some hints.

@GrahamTheCoder GrahamTheCoder changed the title VB -> C#: ternary missing brackets VB -> C#: Null coalesce missing brackets Sep 13, 2020
@GrahamTheCoder
Copy link
Member

Nice, always good to start with the test. OK, so the first thing is to figure out which syntax type the expression is. SharpLab has a nice syntax tree viewer:

image

Then you'll usually find there's a "Visit" method for that syntax construct (in this case within the CSharp namespace since that's the target language)

public override async Task<CSharpSyntaxNode> VisitBinaryConditionalExpression(VBasic.Syntax.BinaryConditionalExpressionSyntax node)
{
return SyntaxFactory.BinaryExpression(
SyntaxKind.CoalesceExpression,
await node.FirstExpression.AcceptAsync<ExpressionSyntax>(TriviaConvertingExpressionVisitor),
await node.SecondExpression.AcceptAsync<ExpressionSyntax>(TriviaConvertingExpressionVisitor)
);
}

So if you breakpoint that, your test should hit it

You may notice that right below that method is the ternary case which you mentioned works, so see if you can make this one work using a similar mechanism.

@SuperRembo
Copy link
Contributor

I applied the same mechanism as used for the ternary operator. That makes the test pass.

I'm not sure what the purpose of node.Parent.IsKind(VBasic.SyntaxKind.Interpolation) is. It seems to be intended for when the operator is used in string interpolation, but the tests pass without this check.

@SuperRembo
Copy link
Contributor

In other words: would node.ParenthesizeIfPrecedenceCouldChange(expr) be sufficient?

@GrahamTheCoder
Copy link
Member

Yeah sounds right. I think it's a special case: within string interpolation, ternary statements need parentheses in c# to avoid a compile error. Should have really had a comment.

Could write a test to check this works within string interpolation to be sure.

@GrahamTheCoder
Copy link
Member

Ah yes, just saw you did a test for it, nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants