style(generator): 格式化源代码生成器相关文件

- 修复 ILogger.cs 中多余的逗号
- 统一 ContextAwareDiagnostic.cs 中的注释缩进格式
- 统一 ContextAwareGenerator.cs 中的注释缩进格式
- 统一 ContextAwareAttribute.cs 中的注释缩进格式
- 统一 CommonDiagnostics.cs 中的注释缩进格式
- 简化 AttributeClassGeneratorBase.cs 中的 isEnabledByDefault 参数
- 统一 GeneratorTest.cs 中的注释缩进格式
- 优化 ContextAwareGeneratorTests.cs 中的代码结构
- 调整 AnalyzerReleases.Unshipped.md 中的表格格式
This commit is contained in:
GwWuYou 2025-12-28 10:50:41 +08:00
parent 3e1531d7eb
commit e4a317b743
9 changed files with 58 additions and 52 deletions

View File

@ -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))
};
}

View File

@ -24,6 +24,6 @@ public static class CommonDiagnostics
"Class '{0}' must be declared partial for code generation",
"GFramework.Common",
DiagnosticSeverity.Error,
isEnabledByDefault: true
true
);
}

View File

@ -30,16 +30,16 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
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);
}
)
@ -102,7 +102,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
ClassDeclarationSyntax syntax,
INamedTypeSymbol symbol,
AttributeData attr)
=> true;
{
return true;
}
/// <summary>
/// 生成源代码
@ -120,7 +122,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
/// <param name="symbol">命名类型符号</param>
/// <returns>生成文件的提示名称</returns>
protected virtual string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.g.cs";
{
return $"{symbol.Name}.g.cs";
}
#endregion
@ -132,8 +136,10 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
/// <param name="symbol">命名类型符号</param>
/// <returns>属性数据如果未找到则返回null</returns>
protected virtual AttributeData? GetAttribute(INamedTypeSymbol symbol)
=> symbol.GetAttributes().FirstOrDefault(a =>
{
return symbol.GetAttributes().FirstOrDefault(a =>
string.Equals(a.AttributeClass?.ToDisplayString(), AttributeType.FullName, StringComparison.Ordinal));
}
/// <summary>
/// 报告类必须是partial的诊断信息

View File

@ -30,10 +30,8 @@ public static class GeneratorTest<TGenerator>
// 添加期望的生成源文件到测试状态中
foreach (var (filename, content) in generatedSources)
{
test.TestState.GeneratedSources.Add(
(typeof(TGenerator), filename, content));
}
await test.RunAsync();
}

View File

@ -4,6 +4,6 @@
### New Rules
Rule ID | Category | Severity | Notes
---------------------|----------------------------------|----------|------------------------
----------------|----------------------------------|----------|------------------------
GF_Logging_001 | GFramework.Godot.Logging | Warning | LoggerDiagnostics
GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic

View File

@ -84,5 +84,7 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
/// 自定义生成文件名
/// </summary>
protected override string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.ContextAware.g.cs";
{
return $"{symbol.Name}.ContextAware.g.cs";
}
}