Skip to content

Commit e2a09f2

Browse files
committed
Fix SA1009 handling of record inheritance
Fixes #3248
1 parent 363a36c commit e2a09f2

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1009CSharp9UnitTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,54 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.SpacingRules.SA1009ClosingParenthesisMustBeSpacedCorrectly;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.SpacingRules.SA1009ClosingParenthesisMustBeSpacedCorrectly,
15+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
716

817
public class SA1009CSharp9UnitTests : SA1009CSharp8UnitTests
918
{
19+
[Fact]
20+
[WorkItem(3248, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3248")]
21+
public async Task TestRecordInheritanceAsync()
22+
{
23+
const string testCode = @"
24+
public abstract record BaseQuery<T>;
25+
public record MyQuery1( {|#0:)|}: BaseQuery<object>;
26+
public record MyQuery2( {|#1:)|} : BaseQuery<object>;
27+
public record MyQuery3({|#2:)|}: BaseQuery<object>;";
28+
const string fixedCode = @"
29+
public abstract record BaseQuery<T>;
30+
public record MyQuery1() : BaseQuery<object>;
31+
public record MyQuery2() : BaseQuery<object>;
32+
public record MyQuery3() : BaseQuery<object>;";
33+
34+
await new CSharpTest(LanguageVersion.CSharp9)
35+
{
36+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
37+
ExpectedDiagnostics =
38+
{
39+
// /0/Test0.cs(3,25): warning SA1009: Closing parenthesis should not be preceded by a space
40+
Diagnostic(DescriptorNotPreceded).WithLocation(0),
41+
42+
// /0/Test0.cs(3,25): warning SA1009: Closing parenthesis should be followed by a space
43+
Diagnostic(DescriptorFollowed).WithLocation(0),
44+
45+
// /0/Test0.cs(4,25): warning SA1009: Closing parenthesis should not be preceded by a space
46+
Diagnostic(DescriptorNotPreceded).WithLocation(1),
47+
48+
// /0/Test0.cs(5,24): warning SA1009: Closing parenthesis should be followed by a space
49+
Diagnostic(DescriptorFollowed).WithLocation(2),
50+
},
51+
TestCode = testCode,
52+
FixedCode = fixedCode,
53+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
54+
}
1055
}
1156
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1024CSharp9UnitTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,54 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.SpacingRules.SA1024ColonsMustBeSpacedCorrectly;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.SpacingRules.SA1024ColonsMustBeSpacedCorrectly,
15+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
716

817
public class SA1024CSharp9UnitTests : SA1024CSharp8UnitTests
918
{
19+
[Fact]
20+
[WorkItem(3248, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3248")]
21+
public async Task TestRecordInheritanceAsync()
22+
{
23+
const string testCode = @"
24+
public abstract record BaseQuery<T>;
25+
public record MyQuery1(){|#0::|}BaseQuery<object>;
26+
public record MyQuery2(){|#1::|} BaseQuery<object>;
27+
public record MyQuery3() {|#2::|}BaseQuery<object>;";
28+
const string fixedCode = @"
29+
public abstract record BaseQuery<T>;
30+
public record MyQuery1() : BaseQuery<object>;
31+
public record MyQuery2() : BaseQuery<object>;
32+
public record MyQuery3() : BaseQuery<object>;";
33+
34+
await new CSharpTest(LanguageVersion.CSharp9)
35+
{
36+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
37+
ExpectedDiagnostics =
38+
{
39+
// /0/Test0.cs(3,25): warning SA1024: Colon should be preceded by a space
40+
Diagnostic(DescriptorPreceded).WithLocation(0),
41+
42+
// /0/Test0.cs(3,25): warning SA1024: Colon should be followed by a space
43+
Diagnostic(DescriptorFollowed).WithLocation(0),
44+
45+
// /0/Test0.cs(4,25): warning SA1024: Colon should be preceded by a space
46+
Diagnostic(DescriptorPreceded).WithLocation(1),
47+
48+
// /0/Test0.cs(5,26): warning SA1024: Colon should be followed by a space
49+
Diagnostic(DescriptorFollowed).WithLocation(2),
50+
},
51+
TestCode = testCode,
52+
FixedCode = fixedCode,
53+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
54+
}
1055
}
1156
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1009ClosingParenthesisMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
171171
bool requireSpace =
172172
nextToken.Parent.IsKind(SyntaxKind.ConditionalExpression)
173173
|| nextToken.Parent.IsKind(SyntaxKind.BaseConstructorInitializer)
174-
|| nextToken.Parent.IsKind(SyntaxKind.ThisConstructorInitializer);
174+
|| nextToken.Parent.IsKind(SyntaxKind.ThisConstructorInitializer)
175+
|| nextToken.Parent.IsKind(SyntaxKind.BaseList);
175176
precedesStickyCharacter = !requireSpace;
176177
break;
177178

0 commit comments

Comments
 (0)