feat(generator): 添加对 [GetAll] 特性的静态和只读字段跳过支持#151
Conversation
- 添加了新的诊断规则 GF_ContextGet_007 和 GF_ContextGet_08 - 实现了对静态字段和只读字段的跳过逻辑 - 为 [GetAll] 特性添加了跳过字段的警告提示 - 更新了测试用例验证跳过逻辑的正确性 - 修改了代码生成顺序以确保正确的绑定推断 - 在 README 中添加了关于字段跳过的文档说明
- 将GFramework源代码生成器抽象层命名空间注释更正为GFramework源代码生成器根命名空间
Reviewer's Guide在 ContextGet 源生成器中实现了 [GetAll] 支持,用于跳过 static 和 readonly 字段;为这些跳过场景引入了专门的诊断信息;通过共享常量统一诊断类别命名,并扩展了测试和文档以覆盖新行为。 更新后的 ContextGet 诊断与生成器绑定逻辑类图classDiagram
class PathContests {
<<static>>
+string BaseNamespace
+string SourceGeneratorsPath
+string GameNamespace
}
class ContextGetDiagnostics {
<<static>>
-string SourceGeneratorsRuleCategory
+DiagnosticDescriptor NestedClassNotSupported
+DiagnosticDescriptor StaticFieldNotSupported
+DiagnosticDescriptor ReadOnlyFieldNotSupported
+DiagnosticDescriptor GetAllStaticFieldSkipped
+DiagnosticDescriptor GetAllReadOnlyFieldSkipped
+DiagnosticDescriptor FieldTypeNotValid
+DiagnosticDescriptor ContextAwareTypeRequired
+DiagnosticDescriptor MultipleContextGetAttributesNotSupported
}
class ContextGetGenerator {
<<static>>
-void AddInferredBindings(SourceProductionContext context, InferredSymbols symbols, ImmutableArray~IFieldSymbol~ fields, ImmutableArray~IFieldSymbol~ explicitFields, List~ContextBinding~ bindings)
-bool TryCreateInferredBinding(IFieldSymbol field, InferredSymbols symbols, out ContextBinding binding)
-bool CanApplyInferredBinding(SourceProductionContext context, IFieldSymbol field)
-void ReportFieldDiagnostic(SourceProductionContext context, DiagnosticDescriptor descriptor, IFieldSymbol field)
}
PathContests <.. ContextGetDiagnostics : uses
ContextGetDiagnostics <.. ContextGetGenerator : uses
[GetAll] 推断绑定与跳过诊断的流程图flowchart TD
A[Start AddInferredBindings] --> B[Iterate each field]
B --> C{Field in explicitFields?}
C -- Yes --> B
C -- No --> D[TryCreateInferredBinding]
D --> E{TryCreateInferredBinding success?}
E -- No --> B
E -- Yes --> F[CanApplyInferredBinding]
F --> G{field.IsStatic?}
G -- Yes --> H[Report GF_ContextGet_007 via GetAllStaticFieldSkipped]
H --> B
G -- No --> I{field.IsReadOnly?}
I -- Yes --> J[Report GF_ContextGet_008 via GetAllReadOnlyFieldSkipped]
J --> B
I -- No --> K[Add binding to bindings]
K --> B
B --> L{More fields?}
L -- Yes --> B
L -- No --> M[End AddInferredBindings]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your Experience打开你的 dashboard 以:
Getting HelpOriginal review guide in EnglishReviewer's GuideImplements [GetAll] support for skipping static and readonly fields in the ContextGet source generator, introduces dedicated diagnostics for these skip cases, aligns diagnostic category naming via a shared constant, and expands tests and documentation to cover the new behavior. Class diagram for updated ContextGet diagnostics and generator binding logicclassDiagram
class PathContests {
<<static>>
+string BaseNamespace
+string SourceGeneratorsPath
+string GameNamespace
}
class ContextGetDiagnostics {
<<static>>
-string SourceGeneratorsRuleCategory
+DiagnosticDescriptor NestedClassNotSupported
+DiagnosticDescriptor StaticFieldNotSupported
+DiagnosticDescriptor ReadOnlyFieldNotSupported
+DiagnosticDescriptor GetAllStaticFieldSkipped
+DiagnosticDescriptor GetAllReadOnlyFieldSkipped
+DiagnosticDescriptor FieldTypeNotValid
+DiagnosticDescriptor ContextAwareTypeRequired
+DiagnosticDescriptor MultipleContextGetAttributesNotSupported
}
class ContextGetGenerator {
<<static>>
-void AddInferredBindings(SourceProductionContext context, InferredSymbols symbols, ImmutableArray~IFieldSymbol~ fields, ImmutableArray~IFieldSymbol~ explicitFields, List~ContextBinding~ bindings)
-bool TryCreateInferredBinding(IFieldSymbol field, InferredSymbols symbols, out ContextBinding binding)
-bool CanApplyInferredBinding(SourceProductionContext context, IFieldSymbol field)
-void ReportFieldDiagnostic(SourceProductionContext context, DiagnosticDescriptor descriptor, IFieldSymbol field)
}
PathContests <.. ContextGetDiagnostics : uses
ContextGetDiagnostics <.. ContextGetGenerator : uses
Flow diagram for [GetAll] inferred binding and skip diagnosticsflowchart TD
A[Start AddInferredBindings] --> B[Iterate each field]
B --> C{Field in explicitFields?}
C -- Yes --> B
C -- No --> D[TryCreateInferredBinding]
D --> E{TryCreateInferredBinding success?}
E -- No --> B
E -- Yes --> F[CanApplyInferredBinding]
F --> G{field.IsStatic?}
G -- Yes --> H[Report GF_ContextGet_007 via GetAllStaticFieldSkipped]
H --> B
G -- No --> I{field.IsReadOnly?}
I -- Yes --> J[Report GF_ContextGet_008 via GetAllReadOnlyFieldSkipped]
J --> B
I -- No --> K[Add binding to bindings]
K --> B
B --> L{More fields?}
L -- Yes --> B
L -- No --> M[End AddInferredBindings]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
|
Overall Grade |
Security Reliability Complexity Hygiene |
Code Review Summary
| Analyzer | Status | Updated (UTC) | Details |
|---|---|---|---|
| C# | Mar 30, 2026 1:23a.m. | Review ↗ | |
| Secrets | Mar 30, 2026 1:23a.m. | Review ↗ |
There was a problem hiding this comment.
Hey - 我在这里给出了一些高层次的反馈:
- 分析器测试目前是通过硬编码的行/列范围来断言诊断结果(例如
.WithSpan(52, 42, 52, 48)),这种方式相当脆弱;建议改为在源字符串中使用标记(markup)来定位,这样当测试代码布局发生变化时,测试仍能保持稳定。 - README 现在说明
[GetAll]会跳过const字段,但生成器目前只对 static/readonly 字段做了显式处理和诊断;建议在生成器中也对 const 字段的行为进行显式处理,并添加一个专门的测试来覆盖本应可推断的 const 字段,以确保实际行为与文档描述相一致。
给 AI 代理的提示
Please address the comments from this code review:
## Overall Comments
- The analyzer tests that assert diagnostics via hard-coded line/column spans (e.g., `.WithSpan(52, 42, 52, 48)`) are quite brittle; consider switching to markup-based locations in the source strings so the tests remain stable if the test code layout changes.
- The README now documents that `[GetAll]` skips `const` fields, but the generator only has explicit handling and diagnostics for static/readonly fields; consider making the const-field behavior explicit in the generator and adding a focused test that covers a const field that would otherwise be inferable to ensure behavior matches the documentation.帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English
Hey - I've left some high level feedback:
- The analyzer tests that assert diagnostics via hard-coded line/column spans (e.g.,
.WithSpan(52, 42, 52, 48)) are quite brittle; consider switching to markup-based locations in the source strings so the tests remain stable if the test code layout changes. - The README now documents that
[GetAll]skipsconstfields, but the generator only has explicit handling and diagnostics for static/readonly fields; consider making the const-field behavior explicit in the generator and adding a focused test that covers a const field that would otherwise be inferable to ensure behavior matches the documentation.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The analyzer tests that assert diagnostics via hard-coded line/column spans (e.g., `.WithSpan(52, 42, 52, 48)`) are quite brittle; consider switching to markup-based locations in the source strings so the tests remain stable if the test code layout changes.
- The README now documents that `[GetAll]` skips `const` fields, but the generator only has explicit handling and diagnostics for static/readonly fields; consider making the const-field behavior explicit in the generator and adding a focused test that covers a const field that would otherwise be inferable to ensure behavior matches the documentation.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- 在 ContextGetGenerator 中添加对 const 字段的显式检查和跳过逻辑 - 更新文档说明 const、static 和 readonly 字段的处理方式 - 重构测试代码使用 MarkupTestSource 解析器进行更精确的诊断测试 - 添加新的 MarkupTestSource 类用于源码标记解析和诊断定位
- 在 ContextGetGenerator 中添加 System.Text 和 Extensions 引入 - 在测试文件中添加 Microsoft.CodeAnalysis.Text 引入
由 Sourcery 提供的摘要
为 [GetAll] 上下文注入添加对跳过不可赋值字段的支持,并提供相应的诊断信息和文档。
新功能:
改进:
SourceGeneratorsPath常量集中管理源生成器诊断类别,并更新全局 using。文档:
测试:
GetModel字段添加诊断测试。Original summary in English
Summary by Sourcery
Add support for skipping non-assignable fields in [GetAll] context injection and surface corresponding diagnostics and documentation.
New Features:
Enhancements:
Documentation:
Tests: