Skip to content

Commit

Permalink
Update SA1206 to handle c# 11 modifier "required"
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Mar 26, 2023
1 parent 84e2324 commit 6b80473
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
Expand Up @@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.CSharp11.OrderingRules
using System.Threading;
using System.Threading.Tasks;
using StyleCop.Analyzers.Test.CSharp10.OrderingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.OrderingRules.SA1206DeclarationKeywordsMustFollowOrder,
Expand All @@ -23,5 +24,36 @@ public async Task VerifyFileKeywordReorderingInClassDeclarationAsync()
var expected = Diagnostic().WithLocation(0).WithArguments("file", "unsafe");
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3527, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3527")]
public async Task VerifyRequiredKeywordReorderingInPropertiesAndFieldsAsync()
{
var testCode = @"
internal struct SomeStruct
{
required {|#0:public|} int Prop { get; set; }
required {|#1:public|} int Field;
}";

var fixedCode = @"
internal struct SomeStruct
{
public required int Prop { get; set; }
public required int Field;
}";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet70,
TestCode = testCode,
FixedCode = fixedCode,
ExpectedDiagnostics =
{
Diagnostic().WithLocation(0).WithArguments("public", "required"),
Diagnostic().WithLocation(1).WithArguments("public", "required"),
},
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Expand Up @@ -7,7 +7,6 @@ namespace StyleCop.Analyzers.Test.Verifiers
{
using System;
using System.Collections.Immutable;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -23,6 +22,8 @@ internal static class GenericAnalyzerTest

internal static readonly ReferenceAssemblies ReferenceAssembliesNet60;

internal static readonly ReferenceAssemblies ReferenceAssembliesNet70;

private static readonly Lazy<IExportProviderFactory> ExportProviderFactory;

static GenericAnalyzerTest()
Expand All @@ -44,15 +45,11 @@ static GenericAnalyzerTest()
ReferenceAssembliesNet50 = ReferenceAssemblies.Net.Net50.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));

ReferenceAssembliesNet60 =
new ReferenceAssemblies(
"net6.0",
new PackageIdentity(
"Microsoft.NETCore.App.Ref",
"6.0.0"),
Path.Combine("ref", "net6.0"))
.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
ReferenceAssembliesNet60 = ReferenceAssemblies.Net.Net60.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));

ReferenceAssembliesNet70 = ReferenceAssemblies.Net.Net70.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));

ExportProviderFactory = new Lazy<IExportProviderFactory>(
() =>
Expand Down
Expand Up @@ -69,6 +69,7 @@ internal static ModifierType GetModifierType(SyntaxToken modifier)
case SyntaxKind.AsyncKeyword:
case SyntaxKind.PartialKeyword:
case SyntaxKind.RefKeyword:
case SyntaxKindEx.RequiredKeyword:
result = ModifierType.Other;
break;
}
Expand Down
Expand Up @@ -14,6 +14,7 @@ internal static class SyntaxKindEx
public const SyntaxKind NotKeyword = (SyntaxKind)8440;
public const SyntaxKind ManagedKeyword = (SyntaxKind)8445;
public const SyntaxKind UnmanagedKeyword = (SyntaxKind)8446;
public const SyntaxKind RequiredKeyword = (SyntaxKind)8447;
public const SyntaxKind FileKeyword = (SyntaxKind)8449;
public const SyntaxKind NullableKeyword = (SyntaxKind)8486;
public const SyntaxKind EnableKeyword = (SyntaxKind)8487;
Expand Down

0 comments on commit 6b80473

Please sign in to comment.