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

Commit 7df8bd6

Browse files
committed
Merge pull request #218 from davkean/PrivateFieldNamingRule
No longer crash when PrivateFieldNamingRule encounters WithEvents
2 parents 849fcf6 + f174ac4 commit 7df8bd6

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal override IGlobalSemanticFormattingRule Rule
1414
get { return new Rules.PrivateFieldNamingRule(); }
1515
}
1616

17-
private sealed class CSharpFields : PrivateFieldNamingRuleTests
17+
public sealed class CSharpFields : PrivateFieldNamingRuleTests
1818
{
1919
[Fact]
2020
public void TestUnderScoreInPrivateFields()
@@ -215,7 +215,7 @@ int M(C p)
215215
}
216216
}
217217

218-
private sealed class VisualBasicFields : PrivateFieldNamingRuleTests
218+
public sealed class VisualBasicFields : PrivateFieldNamingRuleTests
219219
{
220220
[Fact]
221221
public void Simple()
@@ -314,6 +314,23 @@ End Function
314314

315315
Verify(text, expected, languageName: LanguageNames.VisualBasic);
316316
}
317+
318+
[Fact]
319+
public void FieldMarkedWithEvents()
320+
{ // See: https://github.com/dotnet/codeformatter/issues/216
321+
322+
var text = @"
323+
Class C1
324+
Private Field WithEvents As Integer
325+
End Class";
326+
327+
var expected = @"
328+
Class C1
329+
Private _field WithEvents As Integer
330+
End Class";
331+
332+
Verify(text, expected, languageName: LanguageNames.VisualBasic);
333+
}
317334
}
318335
}
319336
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
6363
var semanticModel = await solution.GetDocument(documentId).GetSemanticModelAsync(cancellationToken);
6464
var root = await semanticModel.SyntaxTree.GetRootAsync(cancellationToken);
6565
var declaration = root.GetAnnotatedNodes(s_markerAnnotation).ElementAt(i);
66-
var fieldSymbol = (IFieldSymbol)semanticModel.GetDeclaredSymbol(declaration, cancellationToken);
66+
67+
// Make note, VB represents "fields" marked as "WithEvents" as properties, so don't be
68+
// tempted to treat this as a IFieldSymbol. We only need the name, so ISymbol is enough.
69+
var fieldSymbol = semanticModel.GetDeclaredSymbol(declaration, cancellationToken);
6770
var newName = GetNewFieldName(fieldSymbol);
6871

6972
// Can happen with pathologically bad field names like _
@@ -79,7 +82,7 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
7982
return solution;
8083
}
8184

82-
private static string GetNewFieldName(IFieldSymbol fieldSymbol)
85+
private static string GetNewFieldName(ISymbol fieldSymbol)
8386
{
8487
var name = fieldSymbol.Name.Trim('_');
8588
if (name.Length > 2 && char.IsLetter(name[0]) && name[1] == '_')

0 commit comments

Comments
 (0)