Skip to content

Commit

Permalink
Merge pull request #425 from mrmonday/422-constructor-visibility
Browse files Browse the repository at this point in the history
Ensure class constructors are marked as public [#422]
  • Loading branch information
GrahamTheCoder authored Nov 17, 2019
2 parents 0904218 + 5290f63 commit 45fa282
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to the code converter will be documented here.
* Correctly handle type promoted module symbols (#375)
* Prefer renamed imports for name resolution (#401)
* Correctly convert ambiguous names (#332)
* Ensure correct visibility for constructors (#422)

### C# -> VB
* Convert property accessors with visiblity modifiers (#92)
Expand Down
4 changes: 2 additions & 2 deletions ICSharpCode.CodeConverter/CSharp/CommonConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ private static SyntaxToken CsEscapedIdentifier(string text)
}

public SyntaxTokenList ConvertModifiers(SyntaxNode node, IEnumerable<SyntaxToken> modifiers,
TokenContext context = TokenContext.Global, bool isVariableOrConst = false, bool isConstructor = false, params CSSyntaxKind[] extraCsModifierKinds)
TokenContext context = TokenContext.Global, bool isVariableOrConst = false, params CSSyntaxKind[] extraCsModifierKinds)
{
ISymbol declaredSymbol = _semanticModel.GetDeclaredSymbol(node);
var declaredAccessibility = declaredSymbol?.DeclaredAccessibility ?? Accessibility.NotApplicable;

var contextsWithIdenticalDefaults = new[] { TokenContext.Global, TokenContext.Local, TokenContext.InterfaceOrModule, TokenContext.MemberInInterface };
bool isPartial = declaredSymbol.IsPartialClassDefinition() || declaredSymbol.IsPartialMethodDefinition() || declaredSymbol.IsPartialMethodImplementation();
bool implicitVisibility = contextsWithIdenticalDefaults.Contains(context) || isVariableOrConst || isConstructor;
bool implicitVisibility = contextsWithIdenticalDefaults.Contains(context) || isVariableOrConst || declaredSymbol.IsStaticConstructor();
if (implicitVisibility && !isPartial) declaredAccessibility = Accessibility.NotApplicable;
var modifierSyntaxs = ConvertModifiersCore(declaredAccessibility, modifiers, context)
.Concat(extraCsModifierKinds.Select(SyntaxFactory.Token))
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.CodeConverter/CSharp/DeclarationNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ public override async Task<CSharpSyntaxNode> VisitConstructorBlock(VBSyntax.Cons
{
var block = node.BlockStatement;
var attributes = await block.AttributeLists.SelectManyAsync(_expressionNodeVisitor.ConvertAttribute);
var modifiers = CommonConversions.ConvertModifiers(block, block.Modifiers, GetMemberContext(node), isConstructor: true);
var modifiers = CommonConversions.ConvertModifiers(block, block.Modifiers, GetMemberContext(node));

var ctor = (node.Statements.FirstOrDefault() as VBSyntax.ExpressionStatementSyntax)?.Expression as VBSyntax.InvocationExpressionSyntax;
var ctorExpression = ctor?.Expression as VBSyntax.MemberAccessExpressionSyntax;
Expand Down
2 changes: 1 addition & 1 deletion Tests/CSharp/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ End Get
End Property
End Class", @"public partial class Class1
{
Class1()
public Class1()
{
var needsInitialization = default(int);
int notUsed;
Expand Down
16 changes: 15 additions & 1 deletion Tests/CSharp/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ await TestConversionVisualBasicToCSharp(
}");
}

[Fact]
public async Task TestConstructorVisibility()
{
await TestConversionVisualBasicToCSharpWithoutComments(@"Class Class1
Sub New(x As Boolean)
End Sub
End Class", @"internal partial class Class1
{
public Class1(bool x)
{
}
}");
}

[Fact]
public async Task TestModuleConstructor()
{
Expand Down Expand Up @@ -779,7 +793,7 @@ public async Task TestConstructorWithImplicitPublicAccessibility()
{
await TestConversionVisualBasicToCSharp(
@"Sub New()
End Sub", @"SurroundingClass()
End Sub", @"public SurroundingClass()
{
}");
}
Expand Down

0 comments on commit 45fa282

Please sign in to comment.