Skip to content

Commit

Permalink
Merge pull request #1026 from CommunityToolkit/dev/fix-body-expressions
Browse files Browse the repository at this point in the history
Handle semi-auto properties with expression bodies
  • Loading branch information
Sergio0694 authored Dec 12, 2024
2 parents ef36a6d + 3c720dd commit 657c697
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ public override void Initialize(AnalysisContext context)
}

// Check that either of them is a semicolon token 'get;' accessor (it can be in either position)
if (firstAccessor.IsKind(SyntaxKind.GetAccessorDeclaration) && firstAccessor.SemicolonToken.IsKind(SyntaxKind.SemicolonToken) ||
secondAccessor.IsKind(SyntaxKind.GetAccessorDeclaration) && secondAccessor.SemicolonToken.IsKind(SyntaxKind.SemicolonToken))
if (firstAccessor.IsKind(SyntaxKind.GetAccessorDeclaration) &&
firstAccessor.SemicolonToken.IsKind(SyntaxKind.SemicolonToken) &&
firstAccessor.ExpressionBody is null ||
secondAccessor.IsKind(SyntaxKind.GetAccessorDeclaration) &&
secondAccessor.SemicolonToken.IsKind(SyntaxKind.SemicolonToken) &&
secondAccessor.ExpressionBody is null)
{
validFlags[0] = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,27 @@ public string Name
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
}

[TestMethod]
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_GetAccessorWithExpressionBody_DoesNotWarn()
{
const string source = """
using CommunityToolkit.Mvvm.ComponentModel;
namespace MyApp;
public partial class SampleViewModel : ObservableObject
{
public string Name
{
get => "Hello world";
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 657c697

Please sign in to comment.