refactor(analyzer): 优化上下文注册分析器中的方法解析逻辑#173
Conversation
- 移除对已具备源码方法的特殊处理逻辑 - 统一使用 ResolveHierarchyMethodImplementation 进行方法解析 - 优先解析到当前具体架构类型上的 override 方法 - 为模块安装路径添加一致的 override 解析逻辑 - 添加完整的单元测试验证分析器行为
审阅者指南重构 ContextRegistrationAnalyzer 的方法解析逻辑,使其在架构和模块辅助方法上都一致地使用基于继承层级的实现查找机制,优先选择具体类型上的重写实现;并新增测试,用于验证继承的初始化和模块安装路径在跟随被重写的虚拟辅助方法时能够正确工作且不会报告诊断信息。 ContextRegistrationAnalyzer 中架构辅助方法解析的时序图sequenceDiagram
participant Analyzer as ContextRegistrationAnalyzer
participant OnInit as Architecture_OnInitialize
participant SymbolHelpers as SymbolHelpers
participant ArchitectureBase as ArchitectureBase
participant GameArchitecture as GameArchitecture
participant TargetMethod as RegisterComponents_base
participant OverrideMethod as RegisterComponents_override
OnInit->>Analyzer: Analyze invocation of RegisterComponents
Analyzer->>Analyzer: TryResolveArchitectureHelperMethod(targetMethod, architectureType)
Analyzer->>SymbolHelpers: ResolveHierarchyMethodImplementation(targetMethod, architectureType)
SymbolHelpers->>ArchitectureBase: Search implementation in hierarchy
ArchitectureBase-->>SymbolHelpers: No concrete override on ArchitectureBase
SymbolHelpers->>GameArchitecture: Check for override of RegisterComponents
GameArchitecture-->>SymbolHelpers: Return OverrideMethod
SymbolHelpers-->>Analyzer: OverrideMethod
Analyzer->>Analyzer: helperMethod = OverrideMethod
Analyzer->>OverrideMethod: Check DeclaringSyntaxReferences
OverrideMethod-->>Analyzer: Has syntax references
Analyzer-->>OnInit: Use OverrideMethod as helper, no diagnostic reported
ContextRegistrationAnalyzer 中模块辅助方法解析的时序图sequenceDiagram
participant Analyzer as ContextRegistrationAnalyzer
participant Install as Module_Install
participant SymbolHelpers as SymbolHelpers
participant ModuleBase as ModuleBase
participant DerivedModule as DerivedInventoryModule
participant TargetMethod as RegisterComponents_base
participant OverrideMethod as RegisterComponents_override
Install->>Analyzer: Analyze invocation of RegisterComponents(architecture)
Analyzer->>Analyzer: TryResolveModuleHelperMethod(targetMethod, moduleType)
Analyzer->>SymbolHelpers: ResolveHierarchyMethodImplementation(targetMethod, moduleType)
SymbolHelpers->>ModuleBase: Search implementation in hierarchy
ModuleBase-->>SymbolHelpers: No concrete override on ModuleBase
SymbolHelpers->>DerivedModule: Check for override of RegisterComponents
DerivedModule-->>SymbolHelpers: Return OverrideMethod
SymbolHelpers-->>Analyzer: OverrideMethod
Analyzer->>Analyzer: helperMethod = OverrideMethod
Analyzer->>OverrideMethod: Check DeclaringSyntaxReferences
OverrideMethod-->>Analyzer: Has syntax references
Analyzer-->>Install: Use OverrideMethod as helper, no diagnostic reported
ContextRegistrationAnalyzer 中更新后方法解析逻辑的类图classDiagram
class ContextRegistrationAnalyzer {
+bool TryResolveArchitectureHelperMethod(IMethodSymbol targetMethod, INamedTypeSymbol architectureType, out IMethodSymbol helperMethod)
+bool TryResolveModuleHelperMethod(IMethodSymbol targetMethod, INamedTypeSymbol moduleType, out IMethodSymbol helperMethod)
}
class SymbolHelpers {
+bool IsWithinTypeHierarchy(INamedTypeSymbol candidateType, INamedTypeSymbol rootType)
+IMethodSymbol ResolveHierarchyMethodImplementation(IMethodSymbol targetMethod, INamedTypeSymbol concreteType)
}
class Architecture {
+void OnInitialize()
+void RegisterModel(IModel model)
+void RegisterSystem(ISystem system)
}
class ArchitectureBase {
+void OnInitialize()
+void RegisterComponents()
}
class GameArchitecture {
+void OnInitialize()
+void RegisterComponents()
}
class IArchitectureModule {
+void Install(IArchitecture architecture)
}
class ModuleBase {
+void Install(IArchitecture architecture)
+void RegisterComponents(IArchitecture architecture)
}
class DerivedInventoryModule {
+void RegisterComponents(IArchitecture architecture)
}
class IArchitecture {
+void RegisterModel(IModel model)
+void RegisterSystem(ISystem system)
+void InstallModule(IArchitectureModule module)
}
class IModel
class ISystem
ContextRegistrationAnalyzer ..> SymbolHelpers : uses
ContextRegistrationAnalyzer ..> Architecture : analyzes
ContextRegistrationAnalyzer ..> IArchitectureModule : analyzes
ArchitectureBase --|> Architecture
GameArchitecture --|> ArchitectureBase
ModuleBase ..|> IArchitectureModule
DerivedInventoryModule --|> ModuleBase
Architecture ..|> IArchitecture
文件级变更
技巧与命令与 Sourcery 交互
自定义你的体验打开你的 控制面板 以:
获取帮助Original review guide in EnglishReviewer's GuideRefactors the ContextRegistrationAnalyzer’s method resolution logic to consistently use hierarchy-based implementation lookup for both architecture and module helper methods, preferring overrides on the concrete type, and adds tests to validate that inherited initialization and module installation paths correctly follow overridden virtual helper methods without reporting diagnostics. Sequence diagram for architecture helper method resolution in ContextRegistrationAnalyzersequenceDiagram
participant Analyzer as ContextRegistrationAnalyzer
participant OnInit as Architecture_OnInitialize
participant SymbolHelpers as SymbolHelpers
participant ArchitectureBase as ArchitectureBase
participant GameArchitecture as GameArchitecture
participant TargetMethod as RegisterComponents_base
participant OverrideMethod as RegisterComponents_override
OnInit->>Analyzer: Analyze invocation of RegisterComponents
Analyzer->>Analyzer: TryResolveArchitectureHelperMethod(targetMethod, architectureType)
Analyzer->>SymbolHelpers: ResolveHierarchyMethodImplementation(targetMethod, architectureType)
SymbolHelpers->>ArchitectureBase: Search implementation in hierarchy
ArchitectureBase-->>SymbolHelpers: No concrete override on ArchitectureBase
SymbolHelpers->>GameArchitecture: Check for override of RegisterComponents
GameArchitecture-->>SymbolHelpers: Return OverrideMethod
SymbolHelpers-->>Analyzer: OverrideMethod
Analyzer->>Analyzer: helperMethod = OverrideMethod
Analyzer->>OverrideMethod: Check DeclaringSyntaxReferences
OverrideMethod-->>Analyzer: Has syntax references
Analyzer-->>OnInit: Use OverrideMethod as helper, no diagnostic reported
Sequence diagram for module helper method resolution in ContextRegistrationAnalyzersequenceDiagram
participant Analyzer as ContextRegistrationAnalyzer
participant Install as Module_Install
participant SymbolHelpers as SymbolHelpers
participant ModuleBase as ModuleBase
participant DerivedModule as DerivedInventoryModule
participant TargetMethod as RegisterComponents_base
participant OverrideMethod as RegisterComponents_override
Install->>Analyzer: Analyze invocation of RegisterComponents(architecture)
Analyzer->>Analyzer: TryResolveModuleHelperMethod(targetMethod, moduleType)
Analyzer->>SymbolHelpers: ResolveHierarchyMethodImplementation(targetMethod, moduleType)
SymbolHelpers->>ModuleBase: Search implementation in hierarchy
ModuleBase-->>SymbolHelpers: No concrete override on ModuleBase
SymbolHelpers->>DerivedModule: Check for override of RegisterComponents
DerivedModule-->>SymbolHelpers: Return OverrideMethod
SymbolHelpers-->>Analyzer: OverrideMethod
Analyzer->>Analyzer: helperMethod = OverrideMethod
Analyzer->>OverrideMethod: Check DeclaringSyntaxReferences
OverrideMethod-->>Analyzer: Has syntax references
Analyzer-->>Install: Use OverrideMethod as helper, no diagnostic reported
Class diagram for updated method resolution logic in ContextRegistrationAnalyzerclassDiagram
class ContextRegistrationAnalyzer {
+bool TryResolveArchitectureHelperMethod(IMethodSymbol targetMethod, INamedTypeSymbol architectureType, out IMethodSymbol helperMethod)
+bool TryResolveModuleHelperMethod(IMethodSymbol targetMethod, INamedTypeSymbol moduleType, out IMethodSymbol helperMethod)
}
class SymbolHelpers {
+bool IsWithinTypeHierarchy(INamedTypeSymbol candidateType, INamedTypeSymbol rootType)
+IMethodSymbol ResolveHierarchyMethodImplementation(IMethodSymbol targetMethod, INamedTypeSymbol concreteType)
}
class Architecture {
+void OnInitialize()
+void RegisterModel(IModel model)
+void RegisterSystem(ISystem system)
}
class ArchitectureBase {
+void OnInitialize()
+void RegisterComponents()
}
class GameArchitecture {
+void OnInitialize()
+void RegisterComponents()
}
class IArchitectureModule {
+void Install(IArchitecture architecture)
}
class ModuleBase {
+void Install(IArchitecture architecture)
+void RegisterComponents(IArchitecture architecture)
}
class DerivedInventoryModule {
+void RegisterComponents(IArchitecture architecture)
}
class IArchitecture {
+void RegisterModel(IModel model)
+void RegisterSystem(ISystem system)
+void InstallModule(IArchitectureModule module)
}
class IModel
class ISystem
ContextRegistrationAnalyzer ..> SymbolHelpers : uses
ContextRegistrationAnalyzer ..> Architecture : analyzes
ContextRegistrationAnalyzer ..> IArchitectureModule : analyzes
ArchitectureBase --|> Architecture
GameArchitecture --|> ArchitectureBase
ModuleBase ..|> IArchitectureModule
DerivedInventoryModule --|> ModuleBase
Architecture ..|> IArchitecture
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# | Apr 5, 2026 1:14a.m. | Review ↗ | |
| Secrets | Apr 5, 2026 1:14a.m. | Review ↗ |
There was a problem hiding this comment.
Hey - 我在这里给出了一些总体反馈:
- 将行为更改为始终优先使用
ResolveHierarchyMethodImplementation会移除之前对“源代码中定义的非抽象方法”的保护机制;请考虑一下:原本希望通过显式调用base.Xxx()保持在基类实现上的调用,现在是否可能会被错误地解析为architectureType/moduleType上的重写实现?如果可能,需要考虑是否应该通过在ResolveHierarchyMethodImplementation中增加额外检查或选项来保留这一行为。 - 由于
TryResolveArchitectureHelperMethod和TryResolveModuleHelperMethod现在基本共享相同的解析模式,可以考虑抽取一个共享的辅助方法,通过传入目标方法和具体类型来减少重复,并使 architecture/module 两条路径的行为保持高度一致。
面向 AI Agent 的提示
Please address the comments from this code review:
## Overall Comments
- The change to always prefer `ResolveHierarchyMethodImplementation` removes the previous safeguard for source-defined non-abstract methods; consider whether explicit `base.Xxx()` calls that are meant to remain on the base implementation could now incorrectly resolve to overrides on `architectureType/moduleType`, and if so, whether that should be preserved via an additional check or option in `ResolveHierarchyMethodImplementation`.
- Now that `TryResolveArchitectureHelperMethod` and `TryResolveModuleHelperMethod` share essentially the same resolution pattern, consider extracting a shared helper that takes the target method and concrete type to reduce duplication and keep the architecture/module paths behavior-tightly aligned.帮我变得更有用!请对每条评论点 👍 或 👎 ,我会根据这些反馈改进后续的评审质量。
Original comment in English
Hey - I've left some high level feedback:
- The change to always prefer
ResolveHierarchyMethodImplementationremoves the previous safeguard for source-defined non-abstract methods; consider whether explicitbase.Xxx()calls that are meant to remain on the base implementation could now incorrectly resolve to overrides onarchitectureType/moduleType, and if so, whether that should be preserved via an additional check or option inResolveHierarchyMethodImplementation. - Now that
TryResolveArchitectureHelperMethodandTryResolveModuleHelperMethodshare essentially the same resolution pattern, consider extracting a shared helper that takes the target method and concrete type to reduce duplication and keep the architecture/module paths behavior-tightly aligned.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The change to always prefer `ResolveHierarchyMethodImplementation` removes the previous safeguard for source-defined non-abstract methods; consider whether explicit `base.Xxx()` calls that are meant to remain on the base implementation could now incorrectly resolve to overrides on `architectureType/moduleType`, and if so, whether that should be preserved via an additional check or option in `ResolveHierarchyMethodImplementation`.
- Now that `TryResolveArchitectureHelperMethod` and `TryResolveModuleHelperMethod` share essentially the same resolution pattern, consider extracting a shared helper that takes the target method and concrete type to reduce duplication and keep the architecture/module paths behavior-tightly aligned.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- 修改 TryResolveArchitectureHelperMethod 和 TryResolveModuleHelperMethod 方法参数 - 添加新的 TryResolveHelperMethod 通用方法处理辅助方法调用解析 - 实现对显式 base 调用的特殊处理以保留基类语义 - 添加 IsExplicitBaseInvocation 方法检测显式基础调用 - 更新测试文件验证分析器核心行为
Summary by Sourcery
在上下文注册分析器中优化方法解析逻辑,使其在任何情况下都一致地优先选择具体的重写实现,并使模块辅助方法的解析与架构行为保持一致。
Bug 修复:
增强功能:
测试:
Original summary in English
Summary by Sourcery
Refine method resolution in the context registration analyzer to consistently prefer concrete override implementations and align module helper resolution with architecture behavior.
Bug Fixes:
Enhancements:
Tests: