Skip to content

Commit

Permalink
Fix SA1205 support for 'private protected'
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Dec 5, 2020
1 parent 139c3c7 commit eceae28
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class SA1205CodeFixProvider : CodeFixProvider
private static readonly ImmutableArray<SyntaxKind> InternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.InternalKeyword);
private static readonly ImmutableArray<SyntaxKind> ProtectedAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.ProtectedKeyword);
private static readonly ImmutableArray<SyntaxKind> ProtectedOrInternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.ProtectedKeyword, SyntaxKind.InternalKeyword);
private static readonly ImmutableArray<SyntaxKind> ProtectedAndInternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.PrivateKeyword, SyntaxKind.ProtectedKeyword);
private static readonly ImmutableArray<SyntaxKind> PrivateAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.PrivateKeyword);
private static readonly ImmutableArray<SyntaxKind> UnexpectedAccessibilityKeywords = ImmutableArray.Create<SyntaxKind>();

Expand Down Expand Up @@ -89,6 +90,8 @@ private static ImmutableArray<SyntaxKind> GetMissingAccessModifiers(Accessibilit
return ProtectedAccessibilityKeywords;
case Accessibility.ProtectedOrInternal:
return ProtectedOrInternalAccessibilityKeywords;
case Accessibility.ProtectedAndInternal:
return ProtectedAndInternalAccessibilityKeywords;
case Accessibility.Private:
return PrivateAccessibilityKeywords;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.OrderingRules;
Expand Down Expand Up @@ -96,13 +97,21 @@ public static IEnumerable<object[]> ValidNestedDeclarations
yield return new object[] { "protected internal", "interface" };
yield return new object[] { "private", "interface" };

if (LightupHelpers.SupportsCSharp72)
{
yield return new object[] { "private protected", "class" };
yield return new object[] { "private protected", "struct" };
yield return new object[] { "private protected", "interface" };
}

if (LightupHelpers.SupportsCSharp9)
{
yield return new object[] { "public", "record" };
yield return new object[] { "protected", "record" };
yield return new object[] { "internal", "record" };
yield return new object[] { "protected internal", "record" };
yield return new object[] { "private", "record" };
yield return new object[] { "private protected", "record" };
}
}
}
Expand Down Expand Up @@ -225,7 +234,14 @@ internal static partial class TestPartial
}}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
var languageVersion = (LightupHelpers.SupportsCSharp8, LightupHelpers.SupportsCSharp72) switch
{
// Make sure to use C# 7.2 if supported, unless we are going to default to something greater
(false, true) => LanguageVersionEx.CSharp7_2,
_ => (LanguageVersion?)null,
};

await VerifyCSharpDiagnosticAsync(languageVersion, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -294,7 +310,14 @@ public class Foo
}}
";

await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(8, 14 + typeKeyword.Length), fixedTestCode, CancellationToken.None).ConfigureAwait(false);
var languageVersion = (LightupHelpers.SupportsCSharp8, LightupHelpers.SupportsCSharp72) switch
{
// Make sure to use C# 7.2 if supported, unless we are going to default to something greater
(false, true) => LanguageVersionEx.CSharp7_2,
_ => (LanguageVersion?)null,
};

await VerifyCSharpFixAsync(languageVersion, testCode, Diagnostic().WithLocation(8, 14 + typeKeyword.Length), fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}
}
}

0 comments on commit eceae28

Please sign in to comment.