Skip to content

Commit

Permalink
Merge pull request #3409 from bjornhellander/feature/Issue3386CastInR…
Browse files Browse the repository at this point in the history
…ange

Update SA1003 and SA1008 to not require space before a cast inside a range expression
  • Loading branch information
sharwell authored Dec 9, 2021
2 parents ca7ccd2 + d2a3d2b commit b5b91d7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,67 @@

namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
using Xunit;

using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>;

public class SA1003CSharp8UnitTests : SA1003CSharp7UnitTests
{
/// <summary>
/// Verifies that spacing around a range expression double dots isn't required.
/// </summary>
/// <remarks>
/// <para>Double dots of range expressions already provide enough spacing for readability so there is no
/// need to surround the range expression with spaces.</para>
/// </remarks>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3386, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3386")]
public async Task TestRangeExpressionAsync()
{
var testCode = @"
namespace TestNamespace
{
using System;
public class TestClass
{
public void TestMethod()
{
var test1 = .. {|#0:(|}int)1;
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
using System;
public class TestClass
{
public void TestMethod()
{
var test1 = ..(int)1;
}
}
}
";

await new CSharpTest(LanguageVersion.CSharp8)
{
ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31,
TestCode = testCode,
ExpectedDiagnostics = { Diagnostic(DescriptorNotPrecededByWhitespace).WithLocation(0).WithArguments("(int)") },
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;

using static StyleCop.Analyzers.SpacingRules.SA1008OpeningParenthesisMustBeSpacedCorrectly;
Expand All @@ -28,6 +27,7 @@ public class SA1008CSharp8UnitTests : SA1008CSharp7UnitTests
/// </remarks>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3386, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3386")]
public async Task TestAfterRangeExpressionAsync()
{
var testCode = @"
Expand All @@ -36,11 +36,12 @@ namespace TestNamespace
using System;
public class TestClass
{
public string TestMethod()
public void TestMethod()
{
string str = ""test"";
int finalLen = 4;
return str[.. {|#0:(|}finalLen - 1)];
var test1 = str[.. {|#0:(|}finalLen - 1)];
var test2 = .. {|#1:(|}int)finalLen;
}
}
}
Expand All @@ -52,11 +53,12 @@ namespace TestNamespace
using System;
public class TestClass
{
public string TestMethod()
public void TestMethod()
{
string str = ""test"";
int finalLen = 4;
return str[..(finalLen - 1)];
var test1 = str[..(finalLen - 1)];
var test2 = ..(int)finalLen;
}
}
}
Expand All @@ -66,7 +68,11 @@ public string TestMethod()
{
ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31,
TestCode = testCode,
ExpectedDiagnostics = { Diagnostic(DescriptorNotPreceded).WithLocation(0) },
ExpectedDiagnostics =
{
Diagnostic(DescriptorNotPreceded).WithLocation(0),
Diagnostic(DescriptorNotPreceded).WithLocation(1),
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// The spacing around an operator symbol is incorrect, within a C# code file.
Expand Down Expand Up @@ -344,7 +345,8 @@ private static void HandleCastExpression(SyntaxNodeAnalysisContext context)
&& !(castExpression.Parent is CastExpressionSyntax)
&& !precedingToken.IsKind(SyntaxKind.OpenParenToken)
&& !precedingToken.IsKind(SyntaxKind.OpenBracketToken)
&& !(precedingToken.IsKind(SyntaxKind.OpenBraceToken) && (precedingToken.Parent is InterpolationSyntax));
&& !(precedingToken.IsKind(SyntaxKind.OpenBraceToken) && (precedingToken.Parent is InterpolationSyntax))
&& !precedingToken.IsKind(SyntaxKindEx.DotDotToken);

var tokenString = castExpression.OpenParenToken.ToString() + castExpression.Type.ToString() + castExpression.CloseParenToken.ToString();
CheckToken(context, castExpression.OpenParenToken, mustHaveLeadingWhitespace, false, false, tokenString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken);
var consecutiveCast = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
var partOfInterpolation = prevToken.IsKind(SyntaxKind.OpenBraceToken) && prevToken.Parent.IsKind(SyntaxKind.Interpolation);
var partOfRange = prevToken.IsKind(SyntaxKindEx.DotDotToken);

haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !consecutiveCast && !partOfInterpolation;
haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !consecutiveCast && !partOfInterpolation && !partOfRange;
break;

case SyntaxKind.ParameterList:
Expand Down

0 comments on commit b5b91d7

Please sign in to comment.