Deduplicate String/Collection assert analyzer scaffolding via shared base class#8846
Merged
Merged
Conversation
7 tasks
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor duplicate code in StringAssertToAssertAnalyzer and CollectionAssertToAssertAnalyzer
Deduplicate String/Collection assert analyzer scaffolding via shared base class
Jun 5, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors MSTest analyzer infrastructure by extracting shared scaffolding from the StringAssertToAssertAnalyzer and CollectionAssertToAssertAnalyzer into a common base class, while keeping each analyzer’s rule-specific mapping logic intact.
Changes:
- Introduced
AssertToAssertAnalyzerBaseto centralize common analyzer setup (supported diagnostics, initialization, and invocation filtering). - Refactored
StringAssertToAssertAnalyzerandCollectionAssertToAssertAnalyzerto inherit from the new base and only implement rule-specific behavior. - Updated both code-fix providers to read the diagnostic property key from the shared base instead of each analyzer type.
Show a summary per file
| File | Description |
|---|---|
| src/Analyzers/MSTest.Analyzers/AssertToAssertAnalyzerBase.cs | Adds shared analyzer scaffolding for “legacy assert type → Assert” migration analyzers. |
| src/Analyzers/MSTest.Analyzers/StringAssertToAssertAnalyzer.cs | Refactors analyzer to inherit from shared base and provide only string-assert-specific mapping. |
| src/Analyzers/MSTest.Analyzers/CollectionAssertToAssertAnalyzer.cs | Refactors analyzer to inherit from shared base and provide only collection-assert-specific mapping. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/StringAssertToAssertFixer.cs | Switches property-key lookup to use the shared base constant. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/CollectionAssertToAssertFixer.cs | Switches property-key lookup to use the shared base constant. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 1
JanKrivanek
approved these changes
Jun 5, 2026
Replace the new public abstract AssertToAssertAnalyzerBase with an internal static AssertToAssertAnalyzerHelpers class. The concrete analyzers now inherit DiagnosticAnalyzer directly and call into the helper, avoiding adding new public API surface area. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
CollectionAssertToAssertAnalyzerandStringAssertToAssertAnalyzerhad near-identical structural boilerplate (diagnostic property key,SupportedDiagnostics,Initialize, and invocation preamble). This change extracts that shared analyzer scaffolding while preserving each analyzer’s rule-specific mapping logic.Shared analyzer infrastructure
AssertToAssertAnalyzerBaseto centralize:ProperAssertMethodNameKeySupportedDiagnosticsimplementationInitializeregistration flow (CompilationStart+OperationKind.Invocation)TryGetTargetMethod)Analyzer refactor
StringAssertToAssertAnalyzerCollectionAssertToAssertAnalyzerDiagnosticRule, source assert type metadata name, and analyzer-specific invocation mapping).Code-fix alignment
ProperAssertMethodNameKeyfrom the shared base class, removing duplicated key ownership assumptions.