Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code fix for TestCleanupShouldBeValidAnalyzer #2887

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (fixesToApply != FixtureMethodSignatureChanges.None)
{
// Ensure that the method will be static.
fixesToApply |= FixtureMethodSignatureChanges.MakeStatic;

context.RegisterCodeFix(
CodeAction.Create(
CodeFixResources.AssemblyCleanupShouldBeValidCodeFix,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ internal enum FixtureMethodSignatureChanges
FixAsyncVoid = 32,
FixReturnType = 64,
RemoveGeneric = 128,
RemoveAbstract = 256,
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ private static IEnumerable<SyntaxNode> GetStatements(SyntaxNode node, SyntaxGene

private static DeclarationModifiers GetModifiers(FixtureMethodSignatureChanges fixesToApply, IMethodSymbol methodSymbol)
{
var currentModifiers = DeclarationModifiers.From(methodSymbol);
DeclarationModifiers newModifiers = methodSymbol.IsAsync
? DeclarationModifiers.Async
: DeclarationModifiers.None;

return fixesToApply.HasFlag(FixtureMethodSignatureChanges.MakeStatic)
? currentModifiers.WithIsStatic(true)
? newModifiers.WithIsStatic(true)
: fixesToApply.HasFlag(FixtureMethodSignatureChanges.RemoveStatic)
? currentModifiers.WithIsStatic(false)
: currentModifiers;
? newModifiers.WithIsStatic(false)
: newModifiers;
}

private static Accessibility GetAccessibility(FixtureMethodSignatureChanges fixesToApply, IMethodSymbol methodSymbol)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Composition;

using Analyzer.Utilities;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;

using MSTest.Analyzers.Helpers;

namespace MSTest.Analyzers;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(TestCleanupShouldBeValidFixer))]
[Shared]
public sealed class TestCleanupShouldBeValidFixer : CodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(DiagnosticIds.TestCleanupShouldBeValidRuleId);

public override FixAllProvider GetFixAllProvider()
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
=> WellKnownFixAllProviders.BatchFixer;

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
SyntaxNode node = root.FindNode(context.Span);
if (node == null)
{
return;
}

FixtureMethodSignatureChanges fixesToApply = context.Diagnostics.Aggregate(FixtureMethodSignatureChanges.None, (acc, diagnostic) =>
{
if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.NotStaticRule)
{
return acc | FixtureMethodSignatureChanges.RemoveStatic;
}

if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.PublicRule)
{
return acc | FixtureMethodSignatureChanges.MakePublic;
}

if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.ReturnTypeRule)
{
return acc | FixtureMethodSignatureChanges.FixReturnType;
}

if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.NotAsyncVoidRule)
{
return acc | FixtureMethodSignatureChanges.FixAsyncVoid;
}

if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.NoParametersRule)
{
return acc | FixtureMethodSignatureChanges.RemoveParameters;
}

if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.NotGenericRule)
{
return acc | FixtureMethodSignatureChanges.RemoveGeneric;
}

if (diagnostic.Descriptor == TestCleanupShouldBeValidAnalyzer.NotAbstractRule)
{
return acc | FixtureMethodSignatureChanges.RemoveAbstract;
}

// return accumulator unchanged, either the action cannot be fixed or it will be fixed by default.
return acc;
});

if (fixesToApply != FixtureMethodSignatureChanges.None)
{
context.RegisterCodeFix(
CodeAction.Create(
CodeFixResources.AssemblyCleanupShouldBeValidCodeFix,
ct => FixtureMethodFixer.FixSignatureAsync(context.Document, root, node, fixesToApply, ct),
nameof(TestCleanupShouldBeValidFixer)),
context.Diagnostics);
}
}
}
Loading