Skip to content

Commit 501b7c0

Browse files
committed
Fix spacing of closing parenthesis before range operator
Fixes #3064
1 parent b582f7f commit 501b7c0

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class SA1008CSharp8UnitTests : SA1008CSharp7UnitTests
2828
/// </remarks>
2929
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
3030
[Fact]
31-
public async Task TestBeforeRangeExpressionAsync()
31+
public async Task TestAfterRangeExpressionAsync()
3232
{
3333
var testCode = SpecialTypeDefinitions.IndexAndRange + @"
3434
namespace TestNamespace

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
}

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)