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

Add code fix for AssemblyInitializeShouldBeValidAnalyzer #2882

Merged
merged 2 commits into from
May 21, 2024
Merged
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
@@ -0,0 +1,86 @@
// 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(AssemblyInitializeShouldBeValidFixer))]
[Shared]
public sealed class AssemblyInitializeShouldBeValidFixer : CodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(DiagnosticIds.AssemblyInitializeShouldBeValidRuleId);

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 == AssemblyInitializeShouldBeValidAnalyzer.StaticRule)
{
return acc | FixtureMethodSignatureChanges.MakeStatic;
}

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

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

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

if (diagnostic.Descriptor == AssemblyInitializeShouldBeValidAnalyzer.SingleContextParameterRule)
{
return acc | FixtureMethodSignatureChanges.AddTestContextParameter;
}

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

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

if (fixesToApply != FixtureMethodSignatureChanges.None)
{
// If we have some fixes to apply, we want to ensure the new method signature will have the TestContext parameter.
fixesToApply |= FixtureMethodSignatureChanges.AddTestContextParameter;

context.RegisterCodeFix(
CodeAction.Create(
CodeFixResources.AssemblyInitializeShouldBeValidCodeFix,
ct => FixtureMethodFixer.FixSignatureAsync(context.Document, root, node, fixesToApply, ct),
nameof(CodeFixResources.AssemblyInitializeShouldBeValidCodeFix)),
context.Diagnostics);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -120,4 +120,7 @@
<data name="AssemblyCleanupShouldBeValidCodeFix" xml:space="preserve">
<value>Fix signature</value>
</data>
<data name="AssemblyInitializeShouldBeValidCodeFix" xml:space="preserve">
<value>Fix signature</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
<target state="new">Fix signature</target>
<note />
</trans-unit>
<trans-unit id="AssemblyInitializeShouldBeValidCodeFix">
<source>Fix signature</source>
<target state="new">Fix signature</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

Large diffs are not rendered by default.