Skip to content

Commit

Permalink
Fix refactoring 'Change accessibility' (RR0186) (#1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Dec 21, 2024
1 parent c2b1f46 commit 498bb29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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"]));
}
}

0 comments on commit 498bb29

Please sign in to comment.