Skip to content

Commit 28ef7f4

Browse files
authored
Merge pull request #3078 from sharwell/ranges
Fix spacing of closing parenthesis before range operator
2 parents f63619d + 501b7c0 commit 28ef7f4

4 files changed

Lines changed: 81 additions & 22 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.Testing;
1010
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
11+
using StyleCop.Analyzers.Test.Verifiers;
1112
using Xunit;
1213

1314
using static StyleCop.Analyzers.SpacingRules.SA1008OpeningParenthesisMustBeSpacedCorrectly;
@@ -18,25 +19,6 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
1819

1920
public class SA1008CSharp8UnitTests : SA1008CSharp7UnitTests
2021
{
21-
private const string RangePrologue = @"namespace System
22-
{
23-
public struct Range
24-
{
25-
public Range(Index a, Index b)
26-
{
27-
}
28-
public Index Start { get; }
29-
public Index End { get; }
30-
}
31-
32-
public struct Index
33-
{
34-
public static implicit operator Index(int value) => throw null;
35-
public int GetOffset(int length) => throw null;
36-
}
37-
}
38-
";
39-
4022
/// <summary>
4123
/// Verifies that spacing after a range expression double dots isn't required.
4224
/// </summary>
@@ -46,9 +28,9 @@ public struct Index
4628
/// </remarks>
4729
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
4830
[Fact]
49-
public async Task TestBeforeRangeExpressionAsync()
31+
public async Task TestAfterRangeExpressionAsync()
5032
{
51-
var testCode = RangePrologue + @"
33+
var testCode = SpecialTypeDefinitions.IndexAndRange + @"
5234
namespace TestNamespace
5335
{
5436
using System;
@@ -64,7 +46,7 @@ public string TestMethod()
6446
}
6547
";
6648

67-
var fixedCode = RangePrologue + @"
49+
var fixedCode = SpecialTypeDefinitions.IndexAndRange + @"
6850
namespace TestNamespace
6951
{
7052
using System;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1009CSharp8UnitTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.Testing;
10+
using StyleCop.Analyzers.SpacingRules;
1011
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
12+
using StyleCop.Analyzers.Test.Verifiers;
1113
using Xunit;
1214
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1315
StyleCop.Analyzers.SpacingRules.SA1009ClosingParenthesisMustBeSpacedCorrectly,
@@ -96,5 +98,52 @@ public IDisposable Service()
9698

9799
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
98100
}
101+
102+
/// <summary>
103+
/// Verifies that spacing before a range expression double dots isn't required.
104+
/// </summary>
105+
/// <remarks>
106+
/// <para>Double dots of range expressions already provide enough spacing for readability so there is no
107+
/// need to suffix the closing parenthesis with a space.</para>
108+
/// </remarks>
109+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
110+
[Fact]
111+
[WorkItem(3064, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3064")]
112+
public async Task TestBeforeRangeExpressionAsync()
113+
{
114+
var testCode = SpecialTypeDefinitions.IndexAndRange + @"
115+
namespace TestNamespace
116+
{
117+
using System;
118+
public class TestClass
119+
{
120+
public string TestMethod()
121+
{
122+
string str = ""test"";
123+
int startLen = 4;
124+
return str[(startLen - 1) ..];
125+
}
126+
}
127+
}
128+
";
129+
130+
var fixedCode = SpecialTypeDefinitions.IndexAndRange + @"
131+
namespace TestNamespace
132+
{
133+
using System;
134+
public class TestClass
135+
{
136+
public string TestMethod()
137+
{
138+
string str = ""test"";
139+
int startLen = 4;
140+
return str[(startLen - 1)..];
141+
}
142+
}
143+
}
144+
";
145+
DiagnosticResult expected = Diagnostic().WithSpan(28, 37, 28, 38).WithArguments(" not", "followed");
146+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
147+
}
99148
}
100149
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.Verifiers
5+
{
6+
internal static class SpecialTypeDefinitions
7+
{
8+
public const string IndexAndRange = @"namespace System
9+
{
10+
public struct Range
11+
{
12+
public Range(Index a, Index b)
13+
{
14+
}
15+
public Index Start { get; }
16+
public Index End { get; }
17+
}
18+
19+
public struct Index
20+
{
21+
public static implicit operator Index(int value) => throw null;
22+
public int GetOffset(int length) => throw null;
23+
}
24+
}
25+
";
26+
}
27+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
105105
case SyntaxKind.SemicolonToken:
106106
case SyntaxKind.CommaToken:
107107
case SyntaxKind.DoubleQuoteToken:
108+
case SyntaxKindEx.DotDotToken:
108109
precedesStickyCharacter = true;
109110
break;
110111

0 commit comments

Comments
 (0)