NetEscapades.EnumGenerators icon indicating copy to clipboard operation
NetEscapades.EnumGenerators copied to clipboard

Add unit tests demonstrating code fix failures in method argument contexts

Open Copilot opened this issue 2 months ago • 1 comments

Usage analyzer code fix providers fail when static method invocations appear as method arguments rather than variable assignments. The issue affects GetNames, GetValues, Parse, IsDefined, and similar analyzers but not instance method analyzers like ToString and HasFlag.

Changes

  • Added whitespace preservation tests for all usage analyzers covering:

    • Method argument contexts: SomeMethod(Enum.GetNames(typeof(MyEnum)))
    • Extra indentation scenarios
    • Non-assignment expressions (if statements, foreach loops)
  • Test Results:

    • ✅ Instance method analyzers (ToString, HasFlag): All tests pass
    • ❌ Static method analyzers (GetNames, GetValues, Parse, IsDefined, TryParse): Method argument tests fail
    • ✅ Static method analyzers in assignment contexts: Tests pass
  • Attempted Fix in GetNamesCodeFixProvider:

    • Changed from generator.InvocationExpression() with .WithTriviaFrom()
    • To SyntaxFactory.InvocationExpression() with explicit .WithLeadingTrivia() and .WithTrailingTrivia()
    • Issue persists, indicating deeper DocumentEditor node replacement problem

Root Cause

Code fix providers fail when:

  1. Replacing static method calls with different parameter signatures
  2. Target node is embedded in argument list context
  3. Node structure changes significantly (e.g., Enum.GetNames(typeof(T))TExtensions.GetNames())

The tests successfully reproduce the issue and will serve as regression tests once the underlying Roslyn DocumentEditor behavior is properly addressed.

Example Failure

// Test expects this transformation:
SomeMethod(Enum.GetNames(typeof(MyEnum)));
// →
SomeMethod(MyEnumExtensions.GetNames());

// But code fix is not applied at all in this context
// Works correctly when: var names = Enum.GetNames(typeof(MyEnum));
Original prompt

This section details on the original issue you should resolve

<issue_title>Fix whitespace issue in usage analyzers</issue_title> <issue_description>When working on the usage analyzers for switching to the extension methods, we noticed that in unit tests, where the call is not assigning to a variable, we are sometimes generating the incorrect leading trivia.

We should create unit tests demonstrating the issue for all the analyzers first, and try to understand what's going on and resolve the issue creating incorrect spacing.</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes andrewlock/NetEscapades.EnumGenerators#210

💡 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 avatar Dec 29 '25 22:12 Copilot