Skip to content

Refactor String/Collection Assert code-fix scaffolding into shared base#8848

Merged
Evangelink merged 4 commits into
mainfrom
copilot/duplicate-code-fix
Jun 5, 2026
Merged

Refactor String/Collection Assert code-fix scaffolding into shared base#8848
Evangelink merged 4 commits into
mainfrom
copilot/duplicate-code-fix

Conversation

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

StringAssertToAssertFixer and CollectionAssertToAssertFixer had duplicated code-fix scaffolding (GetFixAllProvider, diagnostic-property extraction, invocation-node guards, and root replacement flow). This change consolidates that shared pipeline while preserving each fixer’s rule-specific rewrite behavior.

  • Shared code-fix pipeline

    • Added AssertToAssertFixerBase with:
      • common GetFixAllProvider (BatchFixer)
      • unified RegisterCodeFixesAsync scaffold
      • optional FixKind property handling
      • shared invocation replacement helper
  • String assert fixer migration

    • StringAssertToAssertFixer now derives from AssertToAssertFixerBase
    • Keeps only StringAssert-specific wiring and argument/member rewrite logic
  • Collection assert fixer migration

    • CollectionAssertToAssertFixer now derives from AssertToAssertFixerBase
    • Keeps only CollectionAssert-specific FixKind routing and rewrite logic
public abstract class AssertToAssertFixerBase : CodeFixProvider
{
    public sealed override FixAllProvider GetFixAllProvider()
        => WellKnownFixAllProviders.BatchFixer;

    public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        // shared diagnostic/property extraction + invocation guards + CodeAction registration
    }

    protected abstract Task<Document> FixAssertAsync(
        Document document,
        InvocationExpressionSyntax invocationExpr,
        string properAssertMethodName,
        string? fixKind,
        CancellationToken cancellationToken);
}

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 5, 2026 06:13
Copilot AI changed the title [WIP] Fix duplicate code in StringAssertToAssertFixer and CollectionAssertToAssertFixer Refactor String/Collection Assert code-fix scaffolding into shared base Jun 5, 2026
Copilot AI requested a review from Evangelink June 5, 2026 06:14
@Evangelink Evangelink marked this pull request as ready for review June 5, 2026 07:26
Copilot AI review requested due to automatic review settings June 5, 2026 07:26
@Evangelink Evangelink enabled auto-merge (squash) June 5, 2026 07:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 duplicated Roslyn code-fix scaffolding from the MSTest analyzer code fixers into a shared base class, keeping the StringAssert/CollectionAssert-specific rewrite logic in their respective fixers.

Changes:

  • Introduces AssertToAssertFixerBase to centralize common CodeFixProvider plumbing (FixAll provider, diagnostic property extraction, invocation guards, and CodeAction registration).
  • Updates StringAssertToAssertFixer to derive from the new base and delegate into its existing StringAssert rewrite.
  • Updates CollectionAssertToAssertFixer to derive from the new base, including optional FixKind handling, and delegate into its existing CollectionAssert rewrite.
Show a summary per file
File Description
src/Analyzers/MSTest.Analyzers.CodeFixes/StringAssertToAssertFixer.cs Migrates to the shared base and keeps only StringAssert-specific rewrite behavior.
src/Analyzers/MSTest.Analyzers.CodeFixes/CollectionAssertToAssertFixer.cs Migrates to the shared base and retains fix-kind routing + CollectionAssert-specific rewrites.
src/Analyzers/MSTest.Analyzers.CodeFixes/AssertToAssertFixerBase.cs Adds a shared code-fix pipeline used by both fixers.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment thread src/Analyzers/MSTest.Analyzers.CodeFixes/AssertToAssertFixerBase.cs Outdated
Replace the new public abstract AssertToAssertFixerBase with an internal static AssertToAssertFixerHelpers class. The concrete code fix providers now inherit CodeFixProvider directly and call into the helper, avoiding adding new public API surface area.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Evangelink

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

… to AssertToAssertAnalyzerHelpers

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 5, 2026 12:20

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Resolved. The two conflicts were in CollectionAssertToAssertFixer.cs and StringAssertToAssertFixer.cs — both caused by main's commit #8846 moving ProperAssertMethodNameKey into AssertToAssertAnalyzerHelpers. I kept our refactored RegisterCodeFixesAsync delegation to the helper class and updated both key references to AssertToAssertAnalyzerHelpers.ProperAssertMethodNameKey. Build verified clean (0 errors, 0 warnings).

@Evangelink Evangelink merged commit 716747c into main Jun 5, 2026
33 checks passed
@Evangelink Evangelink deleted the copilot/duplicate-code-fix branch June 5, 2026 13:39
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.

[duplicate-code] Duplicate Code: Scaffolding Pattern in StringAssertToAssertFixer and CollectionAssertToAssertFixer

5 participants