Skip to content

Commit

Permalink
Merge pull request #1114 from TymurGubayev/fix/DeclareMethodVisibility
Browse files Browse the repository at this point in the history
use `TokenContext` when creating modifiers for a `DeclareStatementSyntax`
  • Loading branch information
GrahamTheCoder authored Jul 18, 2024
2 parents ccbd778 + 480ffe1 commit 10af52f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CodeConverter/CSharp/DeclarationNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,13 @@ public override async Task<CSharpSyntaxNode> VisitDeclareStatement(VBSyntax.Decl

var attributeLists = (await CommonConversions.ConvertAttributesAsync(node.AttributeLists)).Add(dllImportAttributeList);

var modifiers = CommonConversions.ConvertModifiers(node, node.Modifiers).Add(SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)).Add(SyntaxFactory.Token(CSSyntaxKind.ExternKeyword));
var tokenContext = GetMemberContext(node);
var modifiers = CommonConversions.ConvertModifiers(node, node.Modifiers, tokenContext);
if (!modifiers.Any(m => m.IsKind(CSSyntaxKind.StaticKeyword))) {
modifiers = modifiers.Add(SyntaxFactory.Token(CSSyntaxKind.StaticKeyword));
}
modifiers = modifiers.Add(SyntaxFactory.Token(CSSyntaxKind.ExternKeyword));

var returnType = await (node.AsClause?.Type).AcceptAsync<TypeSyntax>(_triviaConvertingExpressionVisitor) ?? SyntaxFactory.PredefinedType(SyntaxFactory.Token(CSSyntaxKind.VoidKeyword));
var parameterListSyntax = await (node.ParameterList).AcceptAsync<ParameterListSyntax>(_triviaConvertingExpressionVisitor) ??
SyntaxFactory.ParameterList();
Expand Down
28 changes: 28 additions & 0 deletions Tests/CSharp/MemberTests/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ static Module1()
}");
}

[Fact]
public async Task TestDeclareMethodVisibilityInModuleAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Module Module1
Declare Sub External Lib ""lib.dll"" ()
End Module", @"using System.Runtime.InteropServices;
internal static partial class Module1
{
[DllImport(""lib.dll"")]
public static extern void External();
}");
}

[Fact]
public async Task TestDeclareMethodVisibilityInClassAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class Class1
Declare Sub External Lib ""lib.dll"" ()
End Class", @"using System.Runtime.InteropServices;
internal partial class Class1
{
[DllImport(""lib.dll"")]
public static extern void External();
}");
}

[Fact]
public async Task TestTypeInferredConstAsync()
{
Expand Down

0 comments on commit 10af52f

Please sign in to comment.