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

Commit 23e3dca

Browse files
committed
Fix casing of renamed fields
1 parent 18113de commit 23e3dca

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,36 @@ class C3
113113

114114
Verify(text, expected, runFormatter: true);
115115
}
116+
117+
/// <summary>
118+
/// If the name is pascal cased make it camel cased during the rewrite. If it is not
119+
/// pascal cased then do not change the casing.
120+
/// </summary>
121+
[Fact]
122+
public void NameCasingField()
123+
{
124+
var text = @"
125+
class C
126+
{
127+
int Field;
128+
static int Other;
129+
int GCField;
130+
static int GCOther;
131+
}
132+
";
133+
134+
var expected = @"
135+
class C
136+
{
137+
int _field;
138+
static int s_other;
139+
int _GCField;
140+
static int s_GCOther;
141+
}
142+
";
143+
144+
Verify(text, expected, runFormatter: false);
145+
146+
}
116147
}
117148
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ private static string GetNewFieldName(IFieldSymbol fieldSymbol)
215215
return fieldSymbol.Name;
216216
}
217217

218+
if (name.Length > 2 && char.IsUpper(name[0]) && char.IsLower(name[1]))
219+
{
220+
name = char.ToLower(name[0]) + name.Substring(1);
221+
}
222+
218223
if (fieldSymbol.IsStatic)
219224
{
220225
// Check for ThreadStatic private fields.

0 commit comments

Comments
 (0)