From 480ffe1c3ed46c578fb2c826098b4e64c58d9c9d Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Thu, 18 Jul 2024 15:24:18 +0200 Subject: [PATCH] use TokenContext when creating modifiers for a DeclareStatementSyntax, same as MethodStatementSyntax --- .../CSharp/DeclarationNodeVisitor.cs | 8 +++++- Tests/CSharp/MemberTests/MemberTests.cs | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CodeConverter/CSharp/DeclarationNodeVisitor.cs b/CodeConverter/CSharp/DeclarationNodeVisitor.cs index 0030f8806..698190667 100644 --- a/CodeConverter/CSharp/DeclarationNodeVisitor.cs +++ b/CodeConverter/CSharp/DeclarationNodeVisitor.cs @@ -1504,7 +1504,13 @@ public override async Task 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(_triviaConvertingExpressionVisitor) ?? SyntaxFactory.PredefinedType(SyntaxFactory.Token(CSSyntaxKind.VoidKeyword)); var parameterListSyntax = await (node.ParameterList).AcceptAsync(_triviaConvertingExpressionVisitor) ?? SyntaxFactory.ParameterList(); diff --git a/Tests/CSharp/MemberTests/MemberTests.cs b/Tests/CSharp/MemberTests/MemberTests.cs index af4fb5428..148aa6a58 100644 --- a/Tests/CSharp/MemberTests/MemberTests.cs +++ b/Tests/CSharp/MemberTests/MemberTests.cs @@ -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() {