mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 新增 PriorityGenerator 源生成器,自动生成 IPrioritized 接口实现 - 添加 PriorityAttribute 特性,用于标记类的优先级值 - 实现 PriorityUsageAnalyzer 分析器,检测优先级使用建议 - 添加预定义的 PriorityGroup 常量,提供标准优先级分组 - 在 AnalyzerReleases.Unshipped.md 中注册新的诊断规则 - 更新项目依赖,升级 Meziantou.Analyzer 和 Polyfill 版本 - 为测试项目添加源生成器项目引用 - 添加 PriorityGenerator 的快照测试用例
129 lines
4.4 KiB
C#
129 lines
4.4 KiB
C#
using System.Collections.Immutable;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
using Microsoft.CodeAnalysis.Operations;
|
|
|
|
namespace GFramework.SourceGenerators.analyzers;
|
|
|
|
/// <summary>
|
|
/// 优先级使用分析器,检测应该使用 GetAllByPriority 而非 GetAll 的场景
|
|
/// </summary>
|
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
|
public sealed class PriorityUsageAnalyzer : DiagnosticAnalyzer
|
|
{
|
|
/// <summary>
|
|
/// 诊断 ID
|
|
/// </summary>
|
|
private const string DiagnosticId = "GF_Priority_Usage_001";
|
|
|
|
/// <summary>
|
|
/// 诊断规则
|
|
/// </summary>
|
|
private static readonly DiagnosticDescriptor Rule = new(
|
|
id: DiagnosticId,
|
|
title: "建议使用 GetAllByPriority",
|
|
messageFormat: "类型 '{0}' 实现了 IPrioritized 接口,建议使用 GetAllByPriority<{0}>() 而非 GetAll<{0}>()",
|
|
category: "GFramework.Usage",
|
|
defaultSeverity: DiagnosticSeverity.Info,
|
|
isEnabledByDefault: true,
|
|
description: "当获取实现了 IPrioritized 接口的服务时,应使用 GetAllByPriority 方法以确保按优先级排序。");
|
|
|
|
/// <summary>
|
|
/// 支持的诊断规则
|
|
/// </summary>
|
|
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
|
|
|
|
/// <summary>
|
|
/// 初始化分析器
|
|
/// </summary>
|
|
public override void Initialize(AnalysisContext context)
|
|
{
|
|
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
|
context.EnableConcurrentExecution();
|
|
|
|
context.RegisterCompilationStartAction(compilationContext =>
|
|
{
|
|
// 缓存符号查找
|
|
var iPrioritized = compilationContext.Compilation.GetTypeByMetadataName(
|
|
"GFramework.Core.Abstractions.bases.IPrioritized");
|
|
|
|
if (iPrioritized == null)
|
|
return;
|
|
|
|
var iocContainer = compilationContext.Compilation.GetTypeByMetadataName(
|
|
"GFramework.Core.Abstractions.ioc.IIocContainer");
|
|
|
|
var architectureContext = compilationContext.Compilation.GetTypeByMetadataName(
|
|
"GFramework.Core.Abstractions.architecture.IArchitectureContext");
|
|
|
|
compilationContext.RegisterOperationAction(
|
|
operationContext => AnalyzeInvocation(
|
|
operationContext,
|
|
iPrioritized,
|
|
iocContainer,
|
|
architectureContext),
|
|
OperationKind.Invocation);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分析方法调用
|
|
/// </summary>
|
|
private static void AnalyzeInvocation(
|
|
OperationAnalysisContext context,
|
|
INamedTypeSymbol iPrioritized,
|
|
INamedTypeSymbol? iocContainer,
|
|
INamedTypeSymbol? architectureContext)
|
|
{
|
|
var invocation = (IInvocationOperation)context.Operation;
|
|
var method = invocation.TargetMethod;
|
|
|
|
// 检查方法名是否为 GetAll
|
|
if (method.Name != "GetAll")
|
|
return;
|
|
|
|
// 检查是否为泛型方法
|
|
if (!method.IsGenericMethod || method.TypeArguments.Length != 1)
|
|
return;
|
|
|
|
// 检查方法来源
|
|
var containingType = method.ContainingType;
|
|
if (iocContainer != null && SymbolEqualityComparer.Default.Equals(containingType, iocContainer))
|
|
{
|
|
// 来自 IIocContainer
|
|
}
|
|
else if (architectureContext != null &&
|
|
SymbolEqualityComparer.Default.Equals(containingType, architectureContext))
|
|
{
|
|
// 来自 IArchitectureContext
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 检查泛型参数是否实现了 IPrioritized
|
|
var typeArgument = method.TypeArguments[0];
|
|
if (typeArgument is not INamedTypeSymbol namedType)
|
|
return;
|
|
|
|
if (!ImplementsInterface(namedType, iPrioritized))
|
|
return;
|
|
|
|
// 报告诊断
|
|
var diagnostic = Diagnostic.Create(
|
|
Rule,
|
|
invocation.Syntax.GetLocation(),
|
|
typeArgument.ToDisplayString());
|
|
|
|
context.ReportDiagnostic(diagnostic);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查类型是否实现了指定接口
|
|
/// </summary>
|
|
private static bool ImplementsInterface(INamedTypeSymbol type, INamedTypeSymbol interfaceType)
|
|
{
|
|
return type.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, interfaceType));
|
|
}
|
|
} |