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

Implement SA1315 #2015

Closed
wants to merge 1 commit into from
Closed
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
@@ -0,0 +1,82 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.NamingRules
{
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;

/// <summary>
/// Implements a code fix for diagnostics which are fixed by renaming a symbol to a set of given new names.
/// </summary>
/// <remarks>
/// <para>To fix a violation of this rule, change the name of the symbol so that it is one of the set of new names.</para>
/// </remarks>
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(RenameToAnyCodeFixProvider))]
[Shared]
internal class RenameToAnyCodeFixProvider : CodeFixProvider
{
private static char[] comma = { ',' };

/// <inheritdoc/>
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(
SA1315ParametersShouldMatchInheritedNames.DiagnosticId);

/// <inheritdoc/>
public override FixAllProvider GetFixAllProvider()
{
return CustomFixAllProviders.BatchFixer;
}

/// <inheritdoc/>
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
var root = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

foreach (var diagnostic in context.Diagnostics)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
if (string.IsNullOrEmpty(token.ValueText))
{
continue;
}

var originalName = token.ValueText;

var memberSyntax = RenameHelper.GetParentDeclaration(token);

SemanticModel semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

var declaredSymbol = semanticModel.GetDeclaredSymbol(memberSyntax);
if (declaredSymbol == null)
{
continue;
}

string[] newNames = diagnostic.Properties[SA1315ParametersShouldMatchInheritedNames.PropertyName].Split(comma);
foreach (var newName in newNames)
{
if (!await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
{
continue;
}

context.RegisterCodeFix(
CodeAction.Create(
string.Format(NamingResources.RenameToCodeFix, newName),
cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
nameof(RenameToAnyCodeFixProvider) + newName),
diagnostic);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="MaintainabilityRules\SA1410SA1411CodeFixProvider.cs" />
<Compile Include="MaintainabilityRules\SA1412CodeFixProvider.cs" />
<Compile Include="MaintainabilityRules\SA1412FixAllProvider.cs" />
<Compile Include="NamingRules\RenameToAnyCodeFixProvider.cs" />
<Compile Include="NamingRules\RenameToLowerCaseCodeFixProvider.cs" />
<Compile Include="NamingRules\RenameToUpperCaseCodeFixProvider.cs" />
<Compile Include="NamingRules\SA1302CodeFixProvider.cs" />
Expand Down
Loading