Skip to content

Commit ef36a6d

Browse files
authored
Merge pull request #1024 from CommunityToolkit/dev/better-analyzer-filtering
Improve filtering in semi-auto property analyzer
2 parents bcc6caf + 6cf850f commit ef36a6d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/UseObservablePropertyOnSemiAutoPropertyAnalyzer.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ public override void Initialize(AnalysisContext context)
110110
continue;
111111
}
112112

113-
// We can safely ignore properties that already have [ObservableProperty]
114-
if (typeSymbol.HasAttributeWithType(observablePropertySymbol))
113+
// We can safely ignore properties that already have [ObservableProperty].
114+
// This is because in that case, the other analyzer will already emit an error.
115+
if (propertySymbol.HasAttributeWithType(observablePropertySymbol))
115116
{
116117
continue;
117118
}

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/Test_SourceGeneratorsDiagnostics.cs

+22
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,28 @@ public string Name
13811381
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
13821382
}
13831383

1384+
[TestMethod]
1385+
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithObservableProperty_DoesNotWarn()
1386+
{
1387+
const string source = """
1388+
using CommunityToolkit.Mvvm.ComponentModel;
1389+
1390+
namespace MyApp;
1391+
1392+
public partial class SampleViewModel : ObservableObject
1393+
{
1394+
[ObservableProperty]
1395+
public string Name
1396+
{
1397+
get => field;
1398+
set => SetProperty(ref field, value);
1399+
}
1400+
}
1401+
""";
1402+
1403+
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
1404+
}
1405+
13841406
[TestMethod]
13851407
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_Warns()
13861408
{

0 commit comments

Comments
 (0)