Skip to content

Commit d999a4a

Browse files
authored
Merge pull request #3570 from bjornhellander/feature/sa1402-record
Update SA1402 to handle records and record structs
2 parents 11dc1e9 + 566cd37 commit d999a4a

File tree

10 files changed

+99
-1
lines changed

10 files changed

+99
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ private static async Task<Solution> GetTransformedSolutionAsync(Document documen
9090
case SyntaxKind.InterfaceDeclaration:
9191
case SyntaxKind.EnumDeclaration:
9292
case SyntaxKind.DelegateDeclaration:
93+
case SyntaxKindEx.RecordDeclaration:
94+
case SyntaxKindEx.RecordStructDeclaration:
9395
nodesToRemoveFromExtracted.Add(child);
9496
break;
9597

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.CSharp10.MaintainabilityRules
5+
{
6+
using StyleCop.Analyzers.Test.MaintainabilityRules;
7+
8+
public class SA1402CSharp10ForRecordClassUnitTests : SA1402ForBlockDeclarationUnitTestsBase
9+
{
10+
public override string Keyword => "record class";
11+
12+
protected override string SettingKeyword => "class";
13+
14+
protected override bool IsConfiguredAsTopLevelTypeByDefault => true;
15+
}
16+
}
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.CSharp10.MaintainabilityRules
5+
{
6+
using StyleCop.Analyzers.Test.MaintainabilityRules;
7+
8+
public class SA1402CSharp10ForRecordStructUnitTests : SA1402ForBlockDeclarationUnitTestsBase
9+
{
10+
public override string Keyword => "record struct";
11+
12+
protected override string SettingKeyword => "struct";
13+
14+
protected override bool IsConfiguredAsTopLevelTypeByDefault => false;
15+
}
16+
}
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.CSharp10.MaintainabilityRules
5+
{
6+
using StyleCop.Analyzers.Test.CSharp9.MaintainabilityRules;
7+
8+
public class SA1402CSharp10ForRecordUnitTests : SA1402CSharp9ForRecordUnitTests
9+
{
10+
}
11+
}
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 SA1402CSharp11ForRecordClassUnitTests : SA1402CSharp10ForRecordClassUnitTests
9+
{
10+
}
11+
}
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 SA1402CSharp11ForRecordStructUnitTests : SA1402CSharp10ForRecordStructUnitTests
9+
{
10+
}
11+
}
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+
}
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ 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:
119120
isRelevant = topLevelTypes.Contains(TopLevelType.Interface);
120121
break;
121122
case SyntaxKind.StructDeclaration:
123+
case SyntaxKindEx.RecordStructDeclaration:
122124
isRelevant = topLevelTypes.Contains(TopLevelType.Struct);
123125
break;
124126
case SyntaxKind.EnumDeclaration:

0 commit comments

Comments
 (0)