Skip to content

Commit 0d73af7

Browse files
author
msftbot[bot]
authored
Fixed generated properties within regions (#4227)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 👉 It is imperative to resolve ONE ISSUE PER PR and avoid making multiple changes unless the changes interrelate with each other --> <!-- 📝 Please always keep the "☑️ Allow edits by maintainers" button checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Fixes #4225 <!-- Add the relevant issue number after the word "Fixes" mentioned above (for ex: "## Fixes #1234") which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more options below that apply to this PR. --> - Bugfix <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? Compilation fails If the Microsoft MVVM Toolkit source generator attribute `[ObservableProperty]` is in a `#region` block. <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> ## What is the new behavior? The generated code is now correct when regions are used. <!-- Describe how was this issue resolved or changed? --> ## PR Checklist Please check if your PR fulfills the following requirements: <!-- and remove the ones that are not applicable to the current PR --> - [X] Tested code with current [supported SDKs](../#supported) - [X] New component - [X] Pull Request has been submitted to the documentation repository [instructions](../blob/main/Contributing.md#docs). Link: <!-- docs PR link --> - [X] Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that) - [X] If control, added to Visual Studio Design project - [X] Sample in sample app has been added / updated (for bug fixes / features) - [X] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/CommunityToolkit/WindowsCommunityToolkit-design-assets) - [X] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/CommunityToolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run _build/UpdateHeaders.bat_) - [X] Contains **NO** breaking changes
2 parents f18604f + 28bcbaf commit 0d73af7

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
443443
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
444444
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
445445
.AddAttributeLists(validationAttributes.Select(static a => AttributeList(SingletonSeparatedList(a))).ToArray())
446-
.WithLeadingTrivia(leadingTrivia)
446+
.WithLeadingTrivia(leadingTrivia.Where(static trivia => !trivia.IsKind(SyntaxKind.RegionDirectiveTrivia) && !trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia)))
447447
.AddModifiers(Token(SyntaxKind.PublicKeyword))
448448
.AddAccessorListAccessors(
449449
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)

UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.Toolkit.Mvvm.Input;
99
using Microsoft.VisualStudio.TestTools.UnitTesting;
1010

11+
#pragma warning disable SA1124
12+
1113
namespace UnitTests.Mvvm
1214
{
1315
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1601", Justification = "Type only used for testing")]
@@ -78,6 +80,8 @@ private async Task DelayAndIncrementCounterAsync()
7880
Counter += 1;
7981
}
8082

83+
#region Test region
84+
8185
/// <summary>
8286
/// This is multi line with also other stuff below
8387
/// </summary>
@@ -100,6 +104,8 @@ private async Task DelayAndIncrementCounterWithValueAsync(int count)
100104
Counter += count;
101105
}
102106

107+
#endregion
108+
103109
[ICommand]
104110
private async Task DelayAndIncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
105111
{

UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.Toolkit.Mvvm.ComponentModel;
1212
using Microsoft.VisualStudio.TestTools.UnitTesting;
1313

14+
#pragma warning disable SA1124
15+
1416
#nullable enable
1517

1618
namespace UnitTests.Mvvm
@@ -58,6 +60,86 @@ public void Test_ObservablePropertyAttribute_Events()
5860
Assert.AreEqual(changed.Item2, 42);
5961
}
6062

63+
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225
64+
[TestCategory("Mvvm")]
65+
[TestMethod]
66+
public void Test_ObservablePropertyAttributeWithinRegion_Events()
67+
{
68+
var model = new SampleModel();
69+
70+
(PropertyChangingEventArgs, int) changing = default;
71+
(PropertyChangedEventArgs, int) changed = default;
72+
73+
model.PropertyChanging += (s, e) =>
74+
{
75+
Assert.IsNull(changing.Item1);
76+
Assert.IsNull(changed.Item1);
77+
Assert.AreSame(model, s);
78+
Assert.IsNotNull(s);
79+
Assert.IsNotNull(e);
80+
81+
changing = (e, model.Counter);
82+
};
83+
84+
model.PropertyChanged += (s, e) =>
85+
{
86+
Assert.IsNotNull(changing.Item1);
87+
Assert.IsNull(changed.Item1);
88+
Assert.AreSame(model, s);
89+
Assert.IsNotNull(s);
90+
Assert.IsNotNull(e);
91+
92+
changed = (e, model.Counter);
93+
};
94+
95+
model.Counter = 42;
96+
97+
Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Counter));
98+
Assert.AreEqual(changing.Item2, 0);
99+
Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Counter));
100+
Assert.AreEqual(changed.Item2, 42);
101+
}
102+
103+
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225
104+
[TestCategory("Mvvm")]
105+
[TestMethod]
106+
public void Test_ObservablePropertyAttributeRightBelowRegion_Events()
107+
{
108+
var model = new SampleModel();
109+
110+
(PropertyChangingEventArgs, string?) changing = default;
111+
(PropertyChangedEventArgs, string?) changed = default;
112+
113+
model.PropertyChanging += (s, e) =>
114+
{
115+
Assert.IsNull(changing.Item1);
116+
Assert.IsNull(changed.Item1);
117+
Assert.AreSame(model, s);
118+
Assert.IsNotNull(s);
119+
Assert.IsNotNull(e);
120+
121+
changing = (e, model.Name);
122+
};
123+
124+
model.PropertyChanged += (s, e) =>
125+
{
126+
Assert.IsNotNull(changing.Item1);
127+
Assert.IsNull(changed.Item1);
128+
Assert.AreSame(model, s);
129+
Assert.IsNotNull(s);
130+
Assert.IsNotNull(e);
131+
132+
changed = (e, model.Name);
133+
};
134+
135+
model.Name = "Bob";
136+
137+
Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Name));
138+
Assert.AreEqual(changing.Item2, null);
139+
Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Name));
140+
Assert.AreEqual(changed.Item2, "Bob");
141+
}
142+
61143
[TestCategory("Mvvm")]
62144
[TestMethod]
63145
public void Test_AlsoNotifyChangeForAttribute_Events()
@@ -162,6 +244,16 @@ public partial class SampleModel : ObservableObject
162244
/// </summary>
163245
[ObservableProperty]
164246
private int data;
247+
248+
#region More properties
249+
250+
[ObservableProperty]
251+
private int counter;
252+
253+
#endregion
254+
255+
[ObservableProperty]
256+
private string? name;
165257
}
166258

167259
[INotifyPropertyChanged]

0 commit comments

Comments
 (0)