Skip to content

Commit b03c98f

Browse files
Update SA1402 to treat records the same ways as classes
#3542
1 parent ed67842 commit b03c98f

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ private static async Task<Solution> GetTransformedSolutionAsync(Document documen
9090
case SyntaxKind.InterfaceDeclaration:
9191
case SyntaxKind.EnumDeclaration:
9292
case SyntaxKind.DelegateDeclaration:
93+
case SyntaxKindEx.RecordDeclaration:
9394
nodesToRemoveFromExtracted.Add(child);
9495
break;
9596

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
#nullable disable
5+
6+
namespace StyleCop.Analyzers.Test.CSharp10.MaintainabilityRules
7+
{
8+
using StyleCop.Analyzers.Test.CSharp9.MaintainabilityRules;
9+
10+
public class SA1402CSharp10ForRecordUnitTests : SA1402CSharp9ForRecordUnitTests
11+
{
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp11.MaintainabilityRules
5+
{
6+
using StyleCop.Analyzers.Test.CSharp10.MaintainabilityRules;
7+
8+
public class SA1402CSharp11ForRecordUnitTests : SA1402CSharp10ForRecordUnitTests
9+
{
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp9.MaintainabilityRules
5+
{
6+
using StyleCop.Analyzers.Test.MaintainabilityRules;
7+
8+
public class SA1402CSharp9ForRecordUnitTests : SA1402ForBlockDeclarationUnitTestsBase
9+
{
10+
public override string Keyword => "record";
11+
12+
protected override string SettingKeyword => "class";
13+
14+
protected override bool IsConfiguredAsTopLevelTypeByDefault => true;
15+
}
16+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForBlockDeclarationUnitTestsBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public abstract class SA1402ForBlockDeclarationUnitTestsBase : FileMayOnlyContai
2424

2525
protected SA1402SettingsConfiguration SettingsConfiguration { get; set; } = SA1402SettingsConfiguration.ConfigureAsTopLevelType;
2626

27+
protected virtual string SettingKeyword => this.Keyword;
28+
2729
protected abstract bool IsConfiguredAsTopLevelTypeByDefault { get; }
2830

2931
[Fact]
@@ -190,7 +192,7 @@ public async Task TestNestedTypesAsync()
190192

191193
protected override string GetSettings()
192194
{
193-
return this.SettingsConfiguration.GetSettings(this.Keyword);
195+
return this.SettingsConfiguration.GetSettings(this.SettingKeyword);
194196
}
195197
}
196198
}

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1402FileMayOnlyContainASingleType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private static bool IsRelevantType(SyntaxNode node, StyleCopSettings settings)
113113
switch (node.Kind())
114114
{
115115
case SyntaxKind.ClassDeclaration:
116+
case SyntaxKindEx.RecordDeclaration:
116117
isRelevant = topLevelTypes.Contains(TopLevelType.Class);
117118
break;
118119
case SyntaxKind.InterfaceDeclaration:

0 commit comments

Comments
 (0)