CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

C# -> VB: Expected VB code is invalid in the test Conflict_DeclarationsAfterConvertionForToWhileAsync

Open RussellSmith2 opened this issue 3 years ago • 2 comments

Input code

class TestClass {
    double height;
    void TestMethod() {
        for(double y = 0d; y < height; y += 10d)
            Draw(y);
        for(double y = 0d; y < height; y += 20d)
            Draw(y);
    }
    void Draw(double height) {
    }
}

Erroneous output

Friend Class TestClass
    Private height As Double

    Private Sub TestMethod()
        Dim y As Double = 0R

        While y < height
            Me.Draw(y)
            y += 10R
        End While

        Dim y As Double = 0R

        While y < height
            Me.Draw(y)
            y += 20R
        End While
    End Sub

    Private Sub Draw(ByVal height As Double)
    End Sub
End Class

Expected output

Friend Class TestClass
    Private height As Double

    Private Sub TestMethod()
        For y As Double = 0R To height - 1 Step 10R
            Draw(y)
        Next
        For y As Double = 0R To height - 1 Step 20R
            Draw(y)
        Next
    End Sub

    Private Sub Draw(height As Double)
    End Sub
End Class

Details

In Conflict_DeclarationsAfterConvertionForToWhileAsync test method starting on line 896. https://github.com/icsharpcode/CodeConverter/blob/master/Tests/VB/StatementTests.cs

The error is the variable y is declared twice in the method.

Not sure why a C# for loop is being converted to a VB while loop.

RussellSmith2 avatar Dec 23 '22 19:12 RussellSmith2

Ah yes, in the general case (arbitrary expression or method call) a while loop is needed, so the duplicate name does need to be solved either with a surrounding block or renaming. Though obviously this could be special cased as shown (like adding 1 probably is already). Happy to support anyone developing PRs for any of those approaches

GrahamTheCoder avatar Dec 23 '22 22:12 GrahamTheCoder

Switching from a For loop to a While loop can create an error. I just found an error when converting this code from the UnicodeNewline.cs file.

https://github.com/icsharpcode/CodeConverter/blob/f1d1780c85f00c3db756518db10d70728f2ac008/CodeConverter/Util/UnicodeNewline.cs#L185

Input code

public static string WithoutNewLines(this string text, char SubstituteChar = default)
{
	Contract.Requires(text != null);
	var sb = new StringBuilder();
	int length = default(int);

	for (int i = 0, loopTo = text.Length - 1; i <= loopTo; i++)
	{
		char ch = text[i];
		// Do not delete the next line
		int j = i;
		if (TryGetDelimiterLengthAndType(ch, out length, () => j < text.Length - 1 ? text[j + 1] : SubstituteChar))
		{
			i += length - 1;
			continue;
		}
		sb.Append(ch);
	}
	return sb.ToString();
}

Erroneous output

<Extension()>
Public Function WithoutNewLines(ByVal text As String, ByVal Optional SubstituteChar As Char = Nothing) As String
    Contract.Requires(Not Equals(text, Nothing))
    Dim sb = New StringBuilder()
    Dim length As Integer = Nothing

    Dim i = 0, loopTo = text.Length - 1

    While i <= loopTo
	Dim ch = text(i)
	' Do not delete the next line
	Dim j = i
	If TryGetDelimiterLengthAndType(ch, length, Function() If(j < text.Length - 1, text(j + 1), SubstituteChar)) Then
	    i += length - 1
	    Continue While
	End If
	sb.Append(ch)
	i += 1
    End While
    Return sb.ToString()
End Function

The line 'i += length - 1;' is the problem. In the For loop the '- 1' is required because the For loop increases i each time. In the While loop '- 1' will cause an infinite loop.

RussellSmith2 avatar Dec 25 '22 06:12 RussellSmith2