Skip to content

Commit

Permalink
avoidUsingContentDialogShowAsyncDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Sep 6, 2024
1 parent 85bce14 commit c5789f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<PropertyGroup>
<PackageId>Snap.Hutao.SourceGeneration</PackageId>
<Version>1.1.15</Version>
<Version>1.1.16</Version>
<Authors>DGP Studio</Authors>
<IncludeBuildOutput>false</IncludeBuildOutput>
<DevelopmentDependency>true</DevelopmentDependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace Snap.Hutao.SourceGeneration;

/// <summary>
/// 通用分析器
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
{
Expand All @@ -23,6 +20,8 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor useIsPatternRecursiveMatchingDescriptor = new("SH006", "Use \"is { } obj\" whenever possible", "Use \"is {{ }} {0}\"", "Quality", DiagnosticSeverity.Info, true);
private static readonly DiagnosticDescriptor useArgumentNullExceptionThrowIfNullDescriptor = new("SH007", "Use \"ArgumentNullException.ThrowIfNull()\" instead of \"!\"", "Use \"ArgumentNullException.ThrowIfNull()\"", "Quality", DiagnosticSeverity.Info, true);

private static readonly DiagnosticDescriptor avoidUsingContentDialogShowAsyncDescriptor = new("SH100", "Use \"IContentDialogFactory.EnqueueAndShowAsync\" instead", "Use \"IContentDialogFactory.EnqueueAndShowAsync\" instead", "Quality", DiagnosticSeverity.Warning, true);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
Expand All @@ -34,7 +33,8 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
useIsNotNullPatternMatchingDescriptor,
useIsNullPatternMatchingDescriptor,
useIsPatternRecursiveMatchingDescriptor,
useArgumentNullExceptionThrowIfNullDescriptor
useArgumentNullExceptionThrowIfNullDescriptor,
avoidUsingContentDialogShowAsyncDescriptor
}.ToImmutableArray();
}
}
Expand All @@ -49,14 +49,13 @@ public override void Initialize(AnalysisContext context)

private static void CompilationStart(CompilationStartAnalysisContext context)
{
SyntaxKind[] commonTypes = [SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.EnumDeclaration,];
context.RegisterSyntaxNodeAction(HandleTypeShouldBeInternal, commonTypes);
context.RegisterSyntaxNodeAction(HandleTypeShouldBeInternal, SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.EnumDeclaration);
context.RegisterSyntaxNodeAction(HandleMethodReturnTypeShouldUseValueTaskInsteadOfTask, SyntaxKind.MethodDeclaration);

SyntaxKind[] expressions = [SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression,];
context.RegisterSyntaxNodeAction(HandleEqualsAndNotEqualsExpressionShouldUsePatternMatching, expressions);
context.RegisterSyntaxNodeAction(HandleEqualsAndNotEqualsExpressionShouldUsePatternMatching, SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression);
context.RegisterSyntaxNodeAction(HandleIsPatternShouldUseRecursivePattern, SyntaxKind.IsPatternExpression);
context.RegisterSyntaxNodeAction(HandleArgumentNullExceptionThrowIfNull, SyntaxKind.SuppressNullableWarningExpression);

context.RegisterSyntaxNodeAction(HandleContentDialogShowAsyncAccess, SyntaxKind.SimpleMemberAccessExpression);
}

private static void HandleTypeShouldBeInternal(SyntaxNodeAnalysisContext context)
Expand Down Expand Up @@ -118,7 +117,7 @@ private static void HandleMethodReturnTypeShouldUseValueTaskInsteadOfTask(Syntax
}
}

public static void HandleEqualsAndNotEqualsExpressionShouldUsePatternMatching(SyntaxNodeAnalysisContext context)
private static void HandleEqualsAndNotEqualsExpressionShouldUsePatternMatching(SyntaxNodeAnalysisContext context)
{
BinaryExpressionSyntax syntax = (BinaryExpressionSyntax)context.Node;
if (syntax.IsKind(SyntaxKind.NotEqualsExpression) && syntax.Right.IsKind(SyntaxKind.NullLiteralExpression))
Expand Down Expand Up @@ -173,4 +172,19 @@ private static void HandleArgumentNullExceptionThrowIfNull(SyntaxNodeAnalysisCon
Diagnostic diagnostic = Diagnostic.Create(useArgumentNullExceptionThrowIfNullDescriptor, location);
context.ReportDiagnostic(diagnostic);
}

private static void HandleContentDialogShowAsyncAccess(SyntaxNodeAnalysisContext context)
{
MemberAccessExpressionSyntax syntax = (MemberAccessExpressionSyntax)context.Node;
if (syntax.Name.Identifier.Text == "ShowAsync")
{
ITypeSymbol? type = context.SemanticModel.GetTypeInfo(syntax.Expression).Type;
if (type?.ToDisplayString() == "Microsoft.UI.Xaml.Controls.ContentDialog")
{
Location location = syntax.GetLocation();
Diagnostic diagnostic = Diagnostic.Create(avoidUsingContentDialogShowAsyncDescriptor, location);
context.ReportDiagnostic(diagnostic);
}
}
}
}

0 comments on commit c5789f9

Please sign in to comment.