Skip to content

Commit 856c724

Browse files
committed
Do not generate property if field name consists of " _"
1 parent 40107c3 commit 856c724

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

DevExpress.Mvvm.CodeGenerators.Tests/TestSource/GenerationTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ partial class Example {
175175
StringAssert.Contains("OnParentViewModelChanged", generatedWithOnChanged);
176176
StringAssert.DoesNotContain("OnParentViewModelChanged", generatedWithOutOnChanged);
177177
}
178+
[Test]
179+
public void GeneratePropertyName_() {
180+
var source = @"using DevExpress.Mvvm.CodeGenerators;
181+
182+
namespace Test {
183+
[GenerateViewModel]
184+
partial class Example {
185+
[GenerateProperty]
186+
int _;
187+
}
188+
}
189+
";
190+
Assert.DoesNotThrow(() => GenerateCode(source));
191+
}
178192

179193
static string GenerateCode(string source, bool addMVVM = true) {
180194
var references = new[] {
@@ -235,6 +249,9 @@ void OnDateTimeChanging() { }
235249
[GenerateProperty(IsVirtual = true, SetterAccessModifier = AccessModifier.Protected)]
236250
double _Double;
237251
252+
[GenerateProperty]
253+
int _;
254+
238255
/// <summary>
239256
/// Test command comment
240257
/// </summary>

DevExpress.Mvvm.CodeGenerators/ExtensionMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static NullableFlowState ToNullableFlowState(NullableAnnotation nullableAnnotati
2020

2121
#region String
2222
public static string ConcatToString(this IEnumerable<string> source, string separator) => string.Join(separator, source);
23-
public static string FirstToUpperCase(this string str) => $"{str.Substring(0, 1).ToUpper()}{str.Substring(1)}";
23+
public static string FirstToUpperCase(this string str) => string.IsNullOrEmpty(str) ? str : $"{str.Substring(0, 1).ToUpper()}{str.Substring(1)}";
2424
#endregion
2525
public static string TypeToString(this TypeKind type) => type == TypeKind.Structure ? "struct" : type.ToString().ToLower();
2626

DevExpress.Mvvm.CodeGenerators/Generators/PropertyGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ static class PropertyGenerator {
1010
if(propertyName == fieldSymbol.Name)
1111
info.Context.ReportInvalidPropertyName(fieldSymbol, propertyName);
1212

13-
CSharpSyntaxNode fieldSyntaxNode = (CSharpSyntaxNode)fieldSymbol.DeclaringSyntaxReferences[0].GetSyntax().Parent?.Parent!;
14-
XMLCommentHelper.AppendComment(source, fieldSyntaxNode);
15-
1613
string? changedMethod = PropertyHelper.GetChangedMethod(info, classSymbol, fieldSymbol, propertyName, fieldSymbol.Type);
1714
string? changingMethod = PropertyHelper.GetChangingMethod(info, classSymbol, fieldSymbol, propertyName, fieldSymbol.Type);
1815

19-
if(propertyName == fieldSymbol.Name || changedMethod == null || changingMethod == null)
16+
if(propertyName == fieldSymbol.Name || string.IsNullOrEmpty(propertyName) || changedMethod == null || changingMethod == null)
2017
return null;
2118

19+
CSharpSyntaxNode fieldSyntaxNode = (CSharpSyntaxNode)fieldSymbol.DeclaringSyntaxReferences[0].GetSyntax().Parent?.Parent!;
20+
XMLCommentHelper.AppendComment(source, fieldSyntaxNode);
21+
2222
PropertyHelper.AppendAttributesList(source, fieldSymbol);
2323

2424
bool isVirtual = PropertyHelper.GetIsVirtualValue(fieldSymbol, info.PropertyAttributeSymbol);

0 commit comments

Comments
 (0)