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

Fix refactoring 'Change accessibility' (RR0186) #1599

Merged
merged 5 commits into from
Dec 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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix refactoring 'Change accessibility' ([RR0186](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0186)) ([PR](https://github.com/dotnet/roslynator/pull/1599))

### Changed

- Move `DiagnosticRules` and `DiagnosticIdentifiers` to `Roslynator.Common` ([PR](https://github.com/dotnet/roslynator/pull/1597))
Expand Down
17 changes: 13 additions & 4 deletions src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -39,9 +40,17 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy

if (syntaxReferences.Length > 1)
{
ImmutableArray<MemberDeclarationSyntax> memberDeclarations = ImmutableArray.CreateRange(
syntaxReferences,
f => (MemberDeclarationSyntax)f.GetSyntax(context.CancellationToken));
ImmutableArray<MemberDeclarationSyntax>.Builder memberDeclarations = ImmutableArray.CreateBuilder<MemberDeclarationSyntax>();

foreach (SyntaxReference syntaxReference in syntaxReferences)
{
SyntaxNode declaration = await syntaxReference.GetSyntaxAsync(context.CancellationToken).ConfigureAwait(false);

if (node.RawKind != declaration.RawKind)
return;

memberDeclarations.Add((MemberDeclarationSyntax)declaration);
}

ImmutableArray<CodeAction>.Builder typeDeclarationActions = ImmutableArray.CreateBuilder<CodeAction>();

Expand All @@ -52,7 +61,7 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy
{
typeDeclarationActions.Add(CodeActionFactory.Create(
SyntaxFacts.GetText(accessibility),
ct => RefactorAsync(context.Solution, memberDeclarations, accessibility, ct),
ct => RefactorAsync(context.Solution, memberDeclarations.ToImmutable(), accessibility, ct),
RefactoringDescriptors.ChangeAccessibility,
accessibility.ToString()));
}
Expand Down
13 changes: 13 additions & 0 deletions src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,17 @@ class C : B
[||]public override string M() => null;
}", equivalenceKey: EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Private)));
}

[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ChangeAccessibility)]
public async Task Test_InvalidCode()
{
await VerifyNoRefactoringAsync("""
[||]internal class Program;
{
static void Main(string[] args)
{
}
}
""", equivalenceKey: EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Internal)), options: Options.AddAllowedCompilerDiagnosticIds(["CS8803", "CS0260", "CS7022"]));
}
}