Refactor analyzers to reduce some duplication - #205
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the diagnostic analyzers and code fix providers to reduce code duplication by organizing analyzers into logical namespaces (UsageAnalyzers and DefinitionAnalyzers) and extracting common code fix logic into a shared base class. The refactoring also improves type resolution by introducing a GetBestTypeByMetadataName helper method.
Key changes:
- Extracted common code fix provider logic into
CodeFixProviderBase.FixAllAsync()method, eliminating duplicated code across four code fix providers - Organized analyzers into
UsageAnalyzers(ToString, Parse, IsDefined, HasFlag) andDefinitionAnalyzers(EnumInGenericType, DuplicateExtensionClass, DuplicateEnumValue) namespaces - Added
CompilationExtensionsclass withGetBestTypeByMetadataName()for improved type resolution
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/NetEscapades.EnumGenerators.Tests/ToStringAnalyzerTests.cs | Updated using alias to reference analyzer in new UsageAnalyzers namespace |
| tests/NetEscapades.EnumGenerators.Tests/ParseAnalyzerTests.cs | Updated using aliases to reference analyzer in new UsageAnalyzers namespace |
| tests/NetEscapades.EnumGenerators.Tests/IsDefinedAnalyzerTests.cs | Updated using aliases to reference analyzer in new UsageAnalyzers namespace |
| tests/NetEscapades.EnumGenerators.Tests/HasFlagAnalyzerTests.cs | Updated using alias to reference analyzer in new UsageAnalyzers namespace |
| tests/NetEscapades.EnumGenerators.Tests/EnumInGenericTypeAnalyzerTests.cs | Added using directive and updated using alias to reference analyzer in new DefinitionAnalyzers namespace |
| tests/NetEscapades.EnumGenerators.Tests/DuplicateExtensionClassAnalyzerTests.cs | Added using directive and updated using alias to reference analyzer in new DefinitionAnalyzers namespace |
| tests/NetEscapades.EnumGenerators.Tests/DuplicateEnumValueAnalyzerTests.cs | Added using directive and updated using alias to reference analyzer in new DefinitionAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/ToStringCodeFixProvider.cs | Moved to UsageAnalyzers namespace and refactored to use CodeFixProviderBase.FixWithEditor |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/ToStringAnalyzer.cs | Moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/ParseCodeFixProvider.cs | Moved to UsageAnalyzers namespace and refactored to use CodeFixProviderBase.FixWithEditor |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/ParseAnalyzer.cs | Moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/IsDefinedCodeFixProvider.cs | Moved to UsageAnalyzers namespace and refactored to use CodeFixProviderBase.FixWithEditor |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/IsDefinedAnalyzer.cs | Moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/HasFlagCodeFixProvider.cs | Moved to UsageAnalyzers namespace and refactored to use CodeFixProviderBase.FixWithEditor |
| src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/HasFlagAnalyzer.cs | Moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/ToStringCodeFixProvider.cs | Deleted - moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/ParseCodeFixProvider.cs | Deleted - moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/IsDefinedCodeFixProvider.cs | Deleted - moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/HasFlagCodeFixProvider.cs | Deleted - moved to UsageAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/DefinitionAnalyzers/EnumInGenericTypeAnalyzer.cs | Moved to DefinitionAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/DefinitionAnalyzers/DuplicateExtensionClassAnalyzer.cs | Moved to DefinitionAnalyzers namespace |
| src/NetEscapades.EnumGenerators/Diagnostics/DefinitionAnalyzers/DuplicateEnumValueAnalyzer.cs | Moved to DefinitionAnalyzers namespace and removed unused System.Linq import |
| src/NetEscapades.EnumGenerators/Diagnostics/CompilationExtensions.cs | New file with GetBestTypeByMetadataName helper method copied from Roslyn source |
| src/NetEscapades.EnumGenerators/Diagnostics/CodeFixProviderBase.cs | Added FixAllAsync method with shared logic and changed FixAllAsync from abstract to concrete method calling FixWithEditor |
| src/NetEscapades.EnumGenerators/Diagnostics/AnalyzerHelpers.cs | Updated to use GetBestTypeByMetadataName instead of GetTypeByMetadataName |
Comments suppressed due to low confidence (1)
src/NetEscapades.EnumGenerators/Diagnostics/DefinitionAnalyzers/DuplicateEnumValueAnalyzer.cs:1
- The
using System.Linq;directive has been removed, but the code at lines 80 and 94 uses LINQ extension methods (.OfType<IFieldSymbol>()and.FirstOrDefault()). This will cause compilation errors. The using directive should be retained.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.