Skip to content

Commit

Permalink
Improve refactoring ChangeAccessibility (RR0186)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Apr 5, 2020
1 parent d35bbfe commit 6e7b27b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Common/CSharp/Analysis/ChangeAccessibilityAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static AccessibilityFilter GetValidAccessibilityFilter(

ImmutableArray<Accessibility> avaiableAccessibilities = AvailableAccessibilities;

bool isAllExplicit = true;
var all = AccessibilityFilter.None;

AccessibilityFilter valid = AccessibilityFilter.Public
Expand All @@ -51,6 +52,8 @@ public static AccessibilityFilter GetValidAccessibilityFilter(

if (accessibility == Accessibility.NotApplicable)
{
isAllExplicit = false;

accessibility = SyntaxAccessibility.GetDefaultExplicitAccessibility(member);

if (accessibility == Accessibility.NotApplicable)
Expand Down Expand Up @@ -124,16 +127,19 @@ public static AccessibilityFilter GetValidAccessibilityFilter(
}
}

switch (all)
if (isAllExplicit)
{
case AccessibilityFilter.Private:
case AccessibilityFilter.Protected:
case AccessibilityFilter.Internal:
case AccessibilityFilter.Public:
{
valid &= ~all;
break;
}
switch (all)
{
case AccessibilityFilter.Private:
case AccessibilityFilter.Protected:
case AccessibilityFilter.Internal:
case AccessibilityFilter.Public:
{
valid &= ~all;
break;
}
}
}

return valid;
Expand Down
36 changes: 36 additions & 0 deletions src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,42 @@ class C
", equivalenceKey: EquivalenceKey.Join(RefactoringId, nameof(Accessibility.Public)));
}

[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ChangeAccessibility)]
public async Task Test_MultipleDeclarations_AllImplicit()
{
await VerifyRefactoringAsync(@"
class C
{
[| object M1() => null;
object M2() => null;|]
}
", @"
class C
{
private object M1() => null;
private object M2() => null;
}
", equivalenceKey: EquivalenceKey.Join(RefactoringId, nameof(Accessibility.Private)));
}

[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ChangeAccessibility)]
public async Task Test_MultipleDeclarations_AnyImplicit()
{
await VerifyRefactoringAsync(@"
class C
{
[| private object M1() => null;
object M2() => null;|]
}
", @"
class C
{
private object M1() => null;
private object M2() => null;
}
", equivalenceKey: EquivalenceKey.Join(RefactoringId, nameof(Accessibility.Private)));
}

[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ChangeAccessibility)]
public async Task TestNoRefactoring_OverrideDeclarationWithoutBaseSource()
{
Expand Down

0 comments on commit 6e7b27b

Please sign in to comment.