refactor: eliminate duplicated code in source generators#300
refactor: eliminate duplicated code in source generators#300
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the source generator code to eliminate extensive duplication in XML documentation generation, generic type parameter formatting, and interface descriptions by extracting common patterns into reusable helper methods.
- Introduced 7 new helper methods in
Sources.csto handle repetitive code generation tasks - Replaced duplicated XML documentation and type parameter generation logic across multiple source generator files
- Simplified code generation for Mock.Create methods, method setups, and indexer setups
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Source/Mockolate.SourceGenerators/Sources/Sources.cs | Added 7 helper methods for XML documentation and type parameter generation; refactored Mock.Create method generation to use these helpers |
| Source/Mockolate.SourceGenerators/Sources/Sources.MethodSetups.cs | Replaced duplicated XML documentation and type parameter logic with calls to new helper methods |
| Source/Mockolate.SourceGenerators/Sources/Sources.IndexerSetups.cs | Replaced duplicated XML documentation and type parameter logic with calls to new helper methods |
Comments suppressed due to low confidence (1)
Source/Mockolate.SourceGenerators/Sources/Sources.cs:1
- The indent parameter is inconsistent - AppendXmlSummary is missing the explicit indent parameter while AppendXmlRemarks uses the default. Both should consistently use '\t\t' indent to match the context, or both should use the default '\t' if that's the intended behavior.
using System.Text;
| sb.AppendXmlSummary("Flag indicating if the base class implementation should be called, and its return values used as default values."); | ||
| sb.AppendXmlRemarks("If not specified, use <see cref=\"MockBehavior.CallBaseClass\" />."); |
There was a problem hiding this comment.
The indent parameter is inconsistent - AppendXmlSummary uses the default '\t' indent while AppendXmlRemarks uses '\t\t'. Both should consistently use '\t\t' to match the context where these methods are defined within an interface body.
| sb.AppendXmlSummary("Flag indicating if the base class implementation should be called, and its return values used as default values."); | |
| sb.AppendXmlRemarks("If not specified, use <see cref=\"MockBehavior.CallBaseClass\" />."); | |
| sb.AppendXmlSummary("Flag indicating if the base class implementation should be called, and its return values used as default values.", "\t\t"); | |
| sb.AppendXmlRemarks("If not specified, use <see cref=\"MockBehavior.CallBaseClass\" />.", "\t\t"); |
There was a problem hiding this comment.
Fixed in commit 65eb57c. All AppendXmlSummary and AppendXmlRemarks calls inside interface bodies now consistently use "\t\t" indentation. Also completed the refactoring by replacing all remaining old-style XML comments with helper method calls.
🚀 Benchmark ResultsDetails
|
vbreuss
left a comment
There was a problem hiding this comment.
The changes are incomplete and partly have incorrect indentation
All indentation issues have been fixed in commit 65eb57c. The refactoring is now complete:
|
Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
Fixed inconsistent indentation where AppendXmlSummary and AppendXmlRemarks were using default '\t' indent inside interface bodies that require '\t\t' indent. Also completed refactoring by replacing all remaining old-style XML comments with helper method calls. Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
Fixed missing word 'indexer' in documentation: "Initializes the indexer according to the given..." Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
7671bf1 to
4721cc0
Compare
|
|
See #307 |



Refactor Source Generator Code to Reduce Duplication ✅
Summary
Successfully refactored source generator code to eliminate ~200+ lines of duplicated code by extracting common patterns into 7 reusable helper methods. All issues addressed.
Completed Work
Helper Methods Added
Files Refactored
Sources.cs- MockClass and Factory methodsSources.MethodSetups.cs- Method setup generation (100% refactored)Sources.IndexerSetups.cs- Indexer setup generation (100% refactored)Benefits
Testing Results
✅ All 87 source generator tests passing
✅ Build successful with no warnings
✅ All indentation issues resolved
✅ All documentation typos fixed
✅ Code review feedback addressed
✅ Security scan clean (0 alerts)
Impact
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.