Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit bfcc391

Browse files
committed
More VB support for private field name rules
1 parent 4c6cc9d commit bfcc391

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/PrivateFieldNamingRuleTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,62 @@ Private _field As Integer
165165

166166
Verify(text, expected, runFormatter: false, languageName: LanguageNames.VisualBasic);
167167
}
168+
169+
[Fact]
170+
public void ModuleFieldsAreShared()
171+
{
172+
var text = @"
173+
Module C
174+
Private Field As Integer
175+
End Module";
176+
177+
var expected = @"
178+
Module C
179+
Private s_field As Integer
180+
End Module";
181+
182+
Verify(text, expected, runFormatter: false, languageName: LanguageNames.VisualBasic);
183+
}
184+
185+
[Fact]
186+
public void MultipleDeclarations()
187+
{
188+
var text = @"
189+
Class C
190+
Private Field1, Field2 As Integer
191+
End Class";
192+
193+
var expected = @"
194+
Class C
195+
Private _field1,_field2 As Integer
196+
End Class";
197+
198+
Verify(text, expected, runFormatter: false, languageName: LanguageNames.VisualBasic);
199+
}
200+
201+
[Fact]
202+
public void FieldAndUse()
203+
{
204+
var text = @"
205+
Class C
206+
Private Field As Integer
207+
208+
Sub M()
209+
Console.WriteLine(Field)
210+
End Sub
211+
End Class";
212+
213+
var expected = @"
214+
Class C
215+
Private _field As Integer
216+
217+
Sub M()
218+
Console.WriteLine(_field)
219+
End Sub
220+
End Class";
221+
222+
Verify(text, expected, runFormatter: false, languageName: LanguageNames.VisualBasic);
223+
}
168224
}
169225
}
170226
}

src/Microsoft.DotNet.CodeFormatting/Rules/PrivateFieldNamingRule.VisualBasic.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected override SyntaxNode RemoveRenameAnnotations(SyntaxNode syntaxNode)
3636
private sealed class VisualBasicPrivateFieldAnnotationRewriter : VisualBasicSyntaxRewriter
3737
{
3838
private int _count;
39+
private bool _inModule;
3940

4041
internal static SyntaxNode AddAnnotations(SyntaxNode node, out int count)
4142
{
@@ -45,6 +46,20 @@ internal static SyntaxNode AddAnnotations(SyntaxNode node, out int count)
4546
return newNode;
4647
}
4748

49+
public override SyntaxNode VisitModuleBlock(ModuleBlockSyntax node)
50+
{
51+
var savedInModule = _inModule;
52+
try
53+
{
54+
_inModule = true;
55+
return base.VisitModuleBlock(node);
56+
}
57+
finally
58+
{
59+
_inModule = savedInModule;
60+
}
61+
}
62+
4863
public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
4964
{
5065
bool isInstance;
@@ -75,7 +90,7 @@ public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
7590
return node.WithDeclarators(SyntaxFactory.SeparatedList(declarators));
7691
}
7792

78-
private static bool NeedsRewrite(FieldDeclarationSyntax fieldSyntax, out bool isInstance)
93+
private bool NeedsRewrite(FieldDeclarationSyntax fieldSyntax, out bool isInstance)
7994
{
8095
if (!IsPrivateField(fieldSyntax, out isInstance))
8196
{
@@ -96,10 +111,10 @@ private static bool NeedsRewrite(FieldDeclarationSyntax fieldSyntax, out bool is
96111
return false;
97112
}
98113

99-
private static bool IsPrivateField(FieldDeclarationSyntax node, out bool isInstance)
114+
private bool IsPrivateField(FieldDeclarationSyntax node, out bool isInstance)
100115
{
101116
var isPrivate = true;
102-
isInstance = true;
117+
isInstance = !_inModule;
103118

104119
foreach (var modifier in node.Modifiers)
105120
{

0 commit comments

Comments
 (0)