Skip to content

Commit 1dc1973

Browse files
Merge pull request #111 from thomasclaudiushuber/issue-105-fix2
Issue 105 fix2
2 parents 8f50393 + 7c7affe commit 1dc1973

File tree

6 files changed

+70
-5
lines changed

6 files changed

+70
-5
lines changed

src/MvvmGen.SourceGenerators.Tests/MvvmGen.SourceGenerators.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<IsPackable>false</IsPackable>
66
<RootNamespace>MvvmGen</RootNamespace>
77
<Configurations>Debug;Release;MvvmGen_PureCodeGeneration</Configurations>
8+
<LangVersion>13.0</LangVersion>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/MvvmGen.SourceGenerators.Tests/ViewModelGeneratorTests/Base/ViewModelGeneratorTestBase.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ protected static void ShouldGenerateExpectedCode(string inputCode, params string
6464
}
6565
}
6666

67-
Assert.True(found, "Expected code must be in generated sources");
67+
var output = $"""
68+
Expected code must be in generated sources
69+
70+
Generated code:
71+
{generatorResult.GeneratedSources.FirstOrDefault().SourceText.ToString()}
72+
73+
Expected code:
74+
{expectedCode}
75+
""";
76+
77+
Assert.True(found, output);
6878
}
6979
}
7080

src/MvvmGen.SourceGenerators.Tests/ViewModelGeneratorTests/PropertyAttributeTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public string FirstName
6060
[InlineData("public")]
6161
[InlineData("private")]
6262
[InlineData("protected")]
63+
[InlineData("protected internal")]
64+
[InlineData("internal")]
6365
[Theory]
6466
public void GeneratePartialProperty(string accessModifier)
6567
{

src/MvvmGen.SourceGenerators.Tests/ViewModelGeneratorTests/ViewModelGenerateInterfaceAttributeTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public partial class EmployeeViewModel
2424
{{
2525
[Property] string _firstName;
2626
27+
[Property] public partial string MiddleName {{ get; set; }}
28+
29+
[Property] internal partial string InternalNotForInterface {{ get; set; }}
30+
2731
public string LastName {{ get; set; }}
2832
2933
public string FullName => FirstName + "" "" + LastName;
@@ -58,11 +62,42 @@ public string FirstName
5862
}}
5963
}}
6064
}}
65+
66+
private string _middleName;
67+
68+
public partial string MiddleName
69+
{{
70+
get => _middleName;
71+
set
72+
{{
73+
if (_middleName != value)
74+
{{
75+
_middleName = value;
76+
OnPropertyChanged(""MiddleName"");
77+
}}
78+
}}
79+
}}
80+
81+
private string _internalNotForInterface;
82+
83+
internal partial string InternalNotForInterface
84+
{{
85+
get => _internalNotForInterface;
86+
set
87+
{{
88+
if (_internalNotForInterface != value)
89+
{{
90+
_internalNotForInterface = value;
91+
OnPropertyChanged(""InternalNotForInterface"");
92+
}}
93+
}}
94+
}}
6195
}}
6296
6397
public interface IEmployeeViewModel : System.ComponentModel.INotifyPropertyChanged
6498
{{
6599
string FirstName {{ get; set; }}
100+
string MiddleName {{ get; set; }}
66101
string LastName {{ get; set; }}
67102
string FullName {{ get; }}
68103
void CustomMethod();

src/MvvmGen.SourceGenerators/Inspectors/ViewModelGenerateInterfaceAttributeInspector.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ internal static class ViewModelGenerateInterfaceAttributeInspector
4747
{
4848
foreach (var propertyToGenerate in propertiesToGenerate)
4949
{
50-
properties ??= new();
51-
properties.Add(new InterfaceProperty(propertyToGenerate.PropertyName,
52-
propertyToGenerate.PropertyType, propertyToGenerate.IsReadOnly));
50+
if (propertyToGenerate.AccessModifier == "public") // partial properties can have other access modifiers than public, for example internal
51+
{
52+
properties ??= new();
53+
properties.Add(new InterfaceProperty(propertyToGenerate.PropertyName,
54+
propertyToGenerate.PropertyType, propertyToGenerate.IsReadOnly));
55+
}
5356
}
5457
}
5558

@@ -59,6 +62,11 @@ internal static class ViewModelGenerateInterfaceAttributeInspector
5962
{
6063
if (memberSymbol is IPropertySymbol propertySymbol)
6164
{
65+
if (propertySymbol.IsPartialDefinition)
66+
{
67+
continue; // Partial properties are already part of propertiesToGenerate list and so added already to the properties list
68+
}
69+
6270
properties ??= new();
6371
properties.Add(new InterfaceProperty(propertySymbol.Name,
6472
propertySymbol.Type.ToDisplayString(), propertySymbol.IsReadOnly));

src/MvvmGen.SourceGenerators/Inspectors/ViewModelMemberInspector.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,18 @@ private static void FindPropertiesToGenerate(ISymbol symbol, List<PropertyToGene
216216
}
217217

218218
var generateBackingField = symbol.Kind == SymbolKind.Property;
219+
var accessModifier = "public";
220+
if(symbol.Kind== SymbolKind.Property)
221+
{
222+
accessModifier = propertySymbol!.DeclaredAccessibility switch
223+
{
224+
Accessibility.ProtectedOrInternal => "protected internal",
225+
_ => propertySymbol!.DeclaredAccessibility.ToString().ToLower()
226+
};
227+
}
219228

220229
propertiesToGenerate.Add(new PropertyToGenerate(propertyName, propertyType, fieldName,
221-
generateBackingField, isReadOnly: false, accessModifier: (symbol.Kind == SymbolKind.Field ? "public" : propertySymbol!.DeclaredAccessibility.ToString().ToLower()))
230+
generateBackingField, isReadOnly: false, accessModifier)
222231
{
223232
EventsToPublish = eventsToPublish,
224233
MethodsToCall = methodsToCall

0 commit comments

Comments
 (0)