committed by
GitHub
49 changed files with 212 additions and 80 deletions
@ -0,0 +1,22 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<IncludeBuildOutput>false</IncludeBuildOutput> |
|||
<PackageId>Avalonia.Analyzers</PackageId> |
|||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> |
|||
<IsPackable>true</IsPackable> |
|||
<IsRoslynComponent>true</IsRoslynComponent> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all"/> |
|||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" PrivateAssets="all" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> |
|||
</ItemGroup> |
|||
|
|||
<Import Project="..\..\..\build\TrimmingEnable.props" /> |
|||
<Import Project="..\..\..\build\NullableEnable.props" /> |
|||
</Project> |
|||
@ -0,0 +1,8 @@ |
|||
// This file is used by Code Analysis to maintain SuppressMessage
|
|||
// attributes that are applied to this project.
|
|||
// Project-level suppressions either have no target or are given
|
|||
// a specific target and scoped to a namespace, type, member, etc.
|
|||
|
|||
using System.Diagnostics.CodeAnalysis; |
|||
|
|||
[assembly: SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008:Enable analyzer release tracking")] |
|||
@ -0,0 +1,59 @@ |
|||
using System.Collections.Immutable; |
|||
using System.Linq; |
|||
using Microsoft.CodeAnalysis; |
|||
using Microsoft.CodeAnalysis.CSharp; |
|||
using Microsoft.CodeAnalysis.CSharp.Syntax; |
|||
using Microsoft.CodeAnalysis.Diagnostics; |
|||
|
|||
namespace Avalonia.Analyzers; |
|||
|
|||
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
|||
public class OnPropertyChangedOverrideAnalyzer : DiagnosticAnalyzer |
|||
{ |
|||
public const string DiagnosticId = "AVA2001"; |
|||
|
|||
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
|||
DiagnosticId, |
|||
"Missing invoke base.OnPropertyChanged", |
|||
"Method '{0}' do not invoke base.{0}", |
|||
"Potential issue", |
|||
DiagnosticSeverity.Warning, |
|||
isEnabledByDefault: true, |
|||
description: "The OnPropertyChanged of the base class was not invoked in the override method declaration, which could lead to unwanted behavior."); |
|||
|
|||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
|||
|
|||
public override void Initialize(AnalysisContext context) |
|||
{ |
|||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
|||
context.EnableConcurrentExecution(); |
|||
context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.MethodDeclaration); |
|||
} |
|||
|
|||
private static void AnalyzeMethod(SyntaxNodeAnalysisContext context) |
|||
{ |
|||
var method = (MethodDeclarationSyntax)context.Node; |
|||
if (context.SemanticModel.GetDeclaredSymbol(method, context.CancellationToken) is IMethodSymbol currentMethod |
|||
&& currentMethod.Name == "OnPropertyChanged" |
|||
&& currentMethod.OverriddenMethod is IMethodSymbol originalMethod) |
|||
{ |
|||
var baseInvocations = method.Body?.DescendantNodes().OfType<BaseExpressionSyntax>(); |
|||
if (baseInvocations?.Any() == true) |
|||
{ |
|||
foreach (var baseInvocation in baseInvocations) |
|||
{ |
|||
if (baseInvocation.Parent is SyntaxNode parent) |
|||
{ |
|||
var targetSymbol = context.SemanticModel.GetSymbolInfo(parent, context.CancellationToken); |
|||
if (SymbolEqualityComparer.Default.Equals(targetSymbol.Symbol, originalMethod)) |
|||
{ |
|||
return; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
context.ReportDiagnostic(Diagnostic.Create(Rule, currentMethod.Locations[0], currentMethod.Name)); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<Nullable>enable</Nullable> |
|||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> |
|||
<PrivateAssets>all</PrivateAssets> |
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
|||
</PackageReference> |
|||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
Loading…
Reference in new issue