From e4a317b743af471913a3e3c69586c521742aec50 Mon Sep 17 00:00:00 2001 From: GwWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 28 Dec 2025 10:50:41 +0800 Subject: [PATCH] =?UTF-8?q?style(generator):=20=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E6=BA=90=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 ILogger.cs 中多余的逗号 - 统一 ContextAwareDiagnostic.cs 中的注释缩进格式 - 统一 ContextAwareGenerator.cs 中的注释缩进格式 - 统一 ContextAwareAttribute.cs 中的注释缩进格式 - 统一 CommonDiagnostics.cs 中的注释缩进格式 - 简化 AttributeClassGeneratorBase.cs 中的 isEnabledByDefault 参数 - 统一 GeneratorTest.cs 中的注释缩进格式 - 优化 ContextAwareGeneratorTests.cs 中的代码结构 - 调整 AnalyzerReleases.Unshipped.md 中的表格格式 --- .../logging/ILogger.cs | 2 +- .../rule/ContextAwareAttribute.cs | 2 +- .../diagnostics/CommonDiagnostics.cs | 16 +++---- .../generator/AttributeClassGeneratorBase.cs | 42 +++++++++++-------- .../core/GeneratorTest.cs | 6 +-- .../rule/ContextAwareGeneratorTests.cs | 6 +-- .../AnalyzerReleases.Unshipped.md | 8 ++-- .../diagnostics/ContextAwareDiagnostic.cs | 14 +++---- .../rule/ContextAwareGenerator.cs | 14 ++++--- 9 files changed, 58 insertions(+), 52 deletions(-) diff --git a/GFramework.Core.Abstractions/logging/ILogger.cs b/GFramework.Core.Abstractions/logging/ILogger.cs index fcb2db9..ee0944d 100644 --- a/GFramework.Core.Abstractions/logging/ILogger.cs +++ b/GFramework.Core.Abstractions/logging/ILogger.cs @@ -72,7 +72,7 @@ public interface ILogger LogLevel.Warning => IsWarnEnabled(), LogLevel.Error => IsErrorEnabled(), LogLevel.Fatal => IsFatalEnabled(), - _ => throw new ArgumentException($"Level [{level}] not recognized.", nameof(level)), + _ => throw new ArgumentException($"Level [{level}] not recognized.", nameof(level)) }; } diff --git a/GFramework.SourceGenerators.Abstractions/rule/ContextAwareAttribute.cs b/GFramework.SourceGenerators.Abstractions/rule/ContextAwareAttribute.cs index dae79be..7ebbb93 100644 --- a/GFramework.SourceGenerators.Abstractions/rule/ContextAwareAttribute.cs +++ b/GFramework.SourceGenerators.Abstractions/rule/ContextAwareAttribute.cs @@ -3,7 +3,7 @@ namespace GFramework.SourceGenerators.Abstractions.rule; /// -/// 标记该类需要自动实现 IContextAware +/// 标记该类需要自动实现 IContextAware /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] public sealed class ContextAwareAttribute : Attribute diff --git a/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs b/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs index 2bea910..e55ebac 100644 --- a/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs +++ b/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs @@ -3,19 +3,19 @@ namespace GFramework.SourceGenerators.Common.diagnostics; /// -/// 提供通用诊断描述符的静态类 +/// 提供通用诊断描述符的静态类 /// public static class CommonDiagnostics { /// - /// 定义类必须为partial的诊断描述符 + /// 定义类必须为partial的诊断描述符 /// /// - /// 诊断ID: GF001 - /// 诊断消息: "Class '{0}' must be declared partial for code generation" - /// 分类: GFramework.Common - /// 严重性: Error - /// 是否启用: true + /// 诊断ID: GF001 + /// 诊断消息: "Class '{0}' must be declared partial for code generation" + /// 分类: GFramework.Common + /// 严重性: Error + /// 是否启用: true /// public static readonly DiagnosticDescriptor ClassMustBePartial = new( @@ -24,6 +24,6 @@ public static class CommonDiagnostics "Class '{0}' must be declared partial for code generation", "GFramework.Common", DiagnosticSeverity.Error, - isEnabledByDefault: true + true ); } \ No newline at end of file diff --git a/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs b/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs index 366a885..6170e45 100644 --- a/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs +++ b/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs @@ -8,38 +8,38 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; namespace GFramework.SourceGenerators.Common.generator; /// -/// 属性类生成器基类,用于处理带有特定属性的类并生成相应的源代码 +/// 属性类生成器基类,用于处理带有特定属性的类并生成相应的源代码 /// public abstract class AttributeClassGeneratorBase : IIncrementalGenerator { /// - /// 获取属性的元数据名称 + /// 获取属性的元数据名称 /// protected abstract Type AttributeType { get; } /// - /// Attribute 的短名称(不含 Attribute 后缀) - /// 仅用于 Syntax 层宽松匹配 + /// Attribute 的短名称(不含 Attribute 后缀) + /// 仅用于 Syntax 层宽松匹配 /// protected abstract string AttributeShortNameWithoutSuffix { get; } /// - /// 初始化增量生成器 + /// 初始化增量生成器 /// /// 增量生成器初始化上下文 public void Initialize(IncrementalGeneratorInitializationContext context) { var targets = context.SyntaxProvider.CreateSyntaxProvider( - predicate: (node, _) => + (node, _) => node is ClassDeclarationSyntax cls && cls.AttributeLists .SelectMany(a => a.Attributes) .Any(a => a.Name.ToString() .Contains(AttributeShortNameWithoutSuffix)), - transform: static (ctx, t) => + static (ctx, t) => { var cls = (ClassDeclarationSyntax)ctx.Node; - var symbol = ctx.SemanticModel.GetDeclaredSymbol(cls, cancellationToken: t); + var symbol = ctx.SemanticModel.GetDeclaredSymbol(cls, t); return (ClassDecl: cls, Symbol: symbol); } ) @@ -59,7 +59,7 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator } /// - /// 执行源代码生成的主要逻辑 + /// 执行源代码生成的主要逻辑 /// /// 源生产上下文 /// 类声明语法节点 @@ -90,7 +90,7 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator #region 可覆写点 /// - /// 验证符号的有效性 + /// 验证符号的有效性 /// /// 源生产上下文 /// 类声明语法节点 @@ -102,10 +102,12 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator ClassDeclarationSyntax syntax, INamedTypeSymbol symbol, AttributeData attr) - => true; + { + return true; + } /// - /// 生成源代码 + /// 生成源代码 /// /// 命名类型符号 /// 属性数据 @@ -115,28 +117,32 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator AttributeData attr); /// - /// 获取生成文件的提示名称 + /// 获取生成文件的提示名称 /// /// 命名类型符号 /// 生成文件的提示名称 protected virtual string GetHintName(INamedTypeSymbol symbol) - => $"{symbol.Name}.g.cs"; + { + return $"{symbol.Name}.g.cs"; + } #endregion #region Attribute / Diagnostic /// - /// 获取指定符号的属性数据 + /// 获取指定符号的属性数据 /// /// 命名类型符号 /// 属性数据,如果未找到则返回null protected virtual AttributeData? GetAttribute(INamedTypeSymbol symbol) - => symbol.GetAttributes().FirstOrDefault(a => + { + return symbol.GetAttributes().FirstOrDefault(a => string.Equals(a.AttributeClass?.ToDisplayString(), AttributeType.FullName, StringComparison.Ordinal)); + } /// - /// 报告类必须是partial的诊断信息 + /// 报告类必须是partial的诊断信息 /// /// 源生产上下文 /// 类声明语法节点 @@ -153,7 +159,7 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator } /// - /// 发出错误信息 + /// 发出错误信息 /// /// 源生产上下文 /// 命名类型符号 diff --git a/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs b/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs index 7798649..4dfbc09 100644 --- a/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs +++ b/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs @@ -4,14 +4,14 @@ using Microsoft.CodeAnalysis.Testing; namespace GFramework.SourceGenerators.Tests.core; /// -/// 提供源代码生成器测试的通用功能 +/// 提供源代码生成器测试的通用功能 /// /// 要测试的源代码生成器类型,必须具有无参构造函数 public static class GeneratorTest where TGenerator : new() { /// - /// 运行源代码生成器测试 + /// 运行源代码生成器测试 /// /// 输入的源代码 /// 期望生成的源文件集合,包含文件名和内容的元组 @@ -30,10 +30,8 @@ public static class GeneratorTest // 添加期望的生成源文件到测试状态中 foreach (var (filename, content) in generatedSources) - { test.TestState.GeneratedSources.Add( (typeof(TGenerator), filename, content)); - } await test.RunAsync(); } diff --git a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs index dd23b7e..6bf322e 100644 --- a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs +++ b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs @@ -5,14 +5,14 @@ using NUnit.Framework; namespace GFramework.SourceGenerators.Tests.rule; /// -/// 测试ContextAwareGenerator源代码生成器的功能 +/// 测试ContextAwareGenerator源代码生成器的功能 /// [TestFixture] public class ContextAwareGeneratorTests { /// - /// 测试ContextAware代码生成功能 - /// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码 + /// 测试ContextAware代码生成功能 + /// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码 /// /// 异步任务 [Test] diff --git a/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md b/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md index 325ddf4..db19379 100644 --- a/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -3,7 +3,7 @@ ### New Rules - Rule ID | Category | Severity | Notes ----------------------|----------------------------------|----------|------------------------ - GF_Logging_001 | GFramework.Godot.Logging | Warning | LoggerDiagnostics - GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic \ No newline at end of file + Rule ID | Category | Severity | Notes +----------------|----------------------------------|----------|------------------------ + GF_Logging_001 | GFramework.Godot.Logging | Warning | LoggerDiagnostics + GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic \ No newline at end of file diff --git a/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs b/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs index ba4a00d..80f50d7 100644 --- a/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs +++ b/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs @@ -3,19 +3,19 @@ namespace GFramework.SourceGenerators.diagnostics; /// -/// 提供与上下文感知相关的诊断规则定义 +/// 提供与上下文感知相关的诊断规则定义 /// public static class ContextAwareDiagnostic { /// - /// 定义类必须实现IContextAware接口的诊断规则 + /// 定义类必须实现IContextAware接口的诊断规则 /// /// - /// 诊断ID: GF_Rule_001 - /// 诊断类别: GFramework.SourceGenerators.rule - /// 严重级别: 错误 - /// 启用状态: true - /// 消息格式: "Class '{0}' must implement IContextAware" + /// 诊断ID: GF_Rule_001 + /// 诊断类别: GFramework.SourceGenerators.rule + /// 严重级别: 错误 + /// 启用状态: true + /// 消息格式: "Class '{0}' must implement IContextAware" /// public static readonly DiagnosticDescriptor ClassMustImplementIContextAware = new( "GF_Rule_001", diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs index 0d0ed06..0e8a671 100644 --- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs +++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs @@ -14,17 +14,17 @@ namespace GFramework.SourceGenerators.rule; public sealed class ContextAwareGenerator : AttributeClassGeneratorBase { /// - /// 使用强类型 Attribute,替代字符串 + /// 使用强类型 Attribute,替代字符串 /// protected override Type AttributeType => typeof(ContextAwareAttribute); /// - /// 仅用于 Syntax 粗筛选 + /// 仅用于 Syntax 粗筛选 /// protected override string AttributeShortNameWithoutSuffix => "ContextAware"; /// - /// 额外语义校验:必须实现 IContextAware + /// 额外语义校验:必须实现 IContextAware /// protected override bool ValidateSymbol( SourceProductionContext context, @@ -46,7 +46,7 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase } /// - /// 生成源码 + /// 生成源码 /// protected override string Generate( INamedTypeSymbol symbol, @@ -81,8 +81,10 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase } /// - /// 自定义生成文件名 + /// 自定义生成文件名 /// protected override string GetHintName(INamedTypeSymbol symbol) - => $"{symbol.Name}.ContextAware.g.cs"; + { + return $"{symbol.Name}.ContextAware.g.cs"; + } } \ No newline at end of file