Skip to content

Commit

Permalink
Merge pull request #1024 from CommunityToolkit/dev/better-analyzer-fi…
Browse files Browse the repository at this point in the history
…ltering

Improve filtering in semi-auto property analyzer
  • Loading branch information
Sergio0694 authored Dec 12, 2024
2 parents bcc6caf + 6cf850f commit ef36a6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ public override void Initialize(AnalysisContext context)
continue;
}

// We can safely ignore properties that already have [ObservableProperty]
if (typeSymbol.HasAttributeWithType(observablePropertySymbol))
// We can safely ignore properties that already have [ObservableProperty].
// This is because in that case, the other analyzer will already emit an error.
if (propertySymbol.HasAttributeWithType(observablePropertySymbol))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,28 @@ public string Name
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
}

[TestMethod]
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithObservableProperty_DoesNotWarn()
{
const string source = """
using CommunityToolkit.Mvvm.ComponentModel;
namespace MyApp;
public partial class SampleViewModel : ObservableObject
{
[ObservableProperty]
public string Name
{
get => field;
set => SetProperty(ref field, value);
}
}
""";

await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
}

[TestMethod]
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_Warns()
{
Expand Down

0 comments on commit ef36a6d

Please sign in to comment.