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.Warning => IsWarnEnabled(),
LogLevel.Error => IsErrorEnabled(), LogLevel.Error => IsErrorEnabled(),
LogLevel.Fatal => IsFatalEnabled(), 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", "Class '{0}' must be declared partial for code generation",
"GFramework.Common", "GFramework.Common",
DiagnosticSeverity.Error, DiagnosticSeverity.Error,
isEnabledByDefault: true true
); );
} }

View File

@ -30,16 +30,16 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext context) public void Initialize(IncrementalGeneratorInitializationContext context)
{ {
var targets = context.SyntaxProvider.CreateSyntaxProvider( var targets = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (node, _) => (node, _) =>
node is ClassDeclarationSyntax cls && node is ClassDeclarationSyntax cls &&
cls.AttributeLists cls.AttributeLists
.SelectMany(a => a.Attributes) .SelectMany(a => a.Attributes)
.Any(a => a.Name.ToString() .Any(a => a.Name.ToString()
.Contains(AttributeShortNameWithoutSuffix)), .Contains(AttributeShortNameWithoutSuffix)),
transform: static (ctx, t) => static (ctx, t) =>
{ {
var cls = (ClassDeclarationSyntax)ctx.Node; 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); return (ClassDecl: cls, Symbol: symbol);
} }
) )
@ -102,7 +102,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
ClassDeclarationSyntax syntax, ClassDeclarationSyntax syntax,
INamedTypeSymbol symbol, INamedTypeSymbol symbol,
AttributeData attr) AttributeData attr)
=> true; {
return true;
}
/// <summary> /// <summary>
/// 生成源代码 /// 生成源代码
@ -120,7 +122,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
/// <param name="symbol">命名类型符号</param> /// <param name="symbol">命名类型符号</param>
/// <returns>生成文件的提示名称</returns> /// <returns>生成文件的提示名称</returns>
protected virtual string GetHintName(INamedTypeSymbol symbol) protected virtual string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.g.cs"; {
return $"{symbol.Name}.g.cs";
}
#endregion #endregion
@ -132,8 +136,10 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
/// <param name="symbol">命名类型符号</param> /// <param name="symbol">命名类型符号</param>
/// <returns>属性数据如果未找到则返回null</returns> /// <returns>属性数据如果未找到则返回null</returns>
protected virtual AttributeData? GetAttribute(INamedTypeSymbol symbol) protected virtual AttributeData? GetAttribute(INamedTypeSymbol symbol)
=> symbol.GetAttributes().FirstOrDefault(a => {
return symbol.GetAttributes().FirstOrDefault(a =>
string.Equals(a.AttributeClass?.ToDisplayString(), AttributeType.FullName, StringComparison.Ordinal)); string.Equals(a.AttributeClass?.ToDisplayString(), AttributeType.FullName, StringComparison.Ordinal));
}
/// <summary> /// <summary>
/// 报告类必须是partial的诊断信息 /// 报告类必须是partial的诊断信息

View File

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

View File

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

View File

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