paul1956 / CSharpToVB

New version of CSharpToVB converter
MIT License
25 stars 9 forks source link

Add Public to Sub Main when declared in non static class in C# #52

Closed elGuille-info closed 3 years ago

elGuille-info commented 3 years ago

Your code:

Dim modifiers As List(Of SyntaxToken)
If Me.IsModule AndAlso
    methodNameToken.ValueText = "Main" AndAlso
    node.Modifiers.Count = 1 AndAlso
    node.Modifiers(0).ValueText = "static" Then
    modifiers = PublicModifier.ToList
Else
    modifiers = ConvertModifiers(node.Modifiers, Me.IsModule, If(containingType?.IsInterfaceType() = True, TokenContext.Local, TokenContext.Member)).ToList
End If

I changed to this and works in all the test I made: Declaring the class static or not.

The only thing is that Remove seems not to work on the List(Of SyntaxToken) and I use a simple for/netxt (and works!)

Dim modifiers As List(Of SyntaxToken)
If methodNameToken.ValueText = "Main" AndAlso
    node.Modifiers.Count = 1 AndAlso
    node.Modifiers(0).ValueText = "static" Then
    If Me.IsModule Then
        modifiers = PublicModifier.ToList
    Else
        modifiers = PublicModifier.ToList
        modifiers.AddRange(ConvertModifiers(node.Modifiers, Me.IsModule, If(containingType?.IsInterfaceType() = True, TokenContext.Local, TokenContext.Member)).ToList)
        ' Esto no funciona, no quita el privado
        'modifiers.Remove(PrivateKeyword)
        Dim index As Integer = -1
        For i As Integer = modifiers.Count - 1 To 0 Step -1
            If modifiers(i).Text = PrivateKeyword.Text Then
                index = i
                Exit For
            End If
        Next
        If index > -1 Then
            modifiers.RemoveAt(index)
        End If
    End If
Else
    modifiers = ConvertModifiers(node.Modifiers, Me.IsModule, If(containingType?.IsInterfaceType() = True, TokenContext.Local, TokenContext.Member)).ToList
End If

File: CSharpToVB\CodeConverter\CSharpToVBVisitors\DeclarationVisitor.vb

Guillermo

elGuille-info commented 3 years ago

I "forked" your project and make this change.

paul1956 commented 3 years ago

@elGuille-info do a PR and I will merge. I don't use List I use SyntaxList. Also if you do a PR add your tests as well.

paul1956 commented 3 years ago

@elGuille-info I simplified it and merged thanks

Dim modifiers As List(Of SyntaxToken)
If methodNameToken.ValueText = "Main" AndAlso
    node.Modifiers.Count = 1 AndAlso
    node.Modifiers(0).IsKind(CS.SyntaxKind.StaticKeyword) Then
    If Me.IsModule Then
        modifiers = PublicModifier.ToList
    Else
        modifiers = PublicModifier.ToList
        modifiers.AddRange(ConvertModifiers(node.Modifiers, Me.IsModule, If(containingType?.IsInterfaceType() = True, TokenContext.Local, TokenContext.Member)).ToList)
        'modifiers.Remove(PrivateKeyword)
        Dim index As Integer = modifiers.IndexOf(VB.SyntaxKind.PrivateKeyword)
        If index > -1 Then
            modifiers.RemoveAt(index)
        End If
    End If
Else
    modifiers = ConvertModifiers(node.Modifiers, Me.IsModule, If(containingType?.IsInterfaceType() = True, TokenContext.Local, TokenContext.Member)).ToList
End If
paul1956 commented 3 years ago

Closed in New in 5.0.0.17/5.0.0.8 Thanks @elGuille-info