Skip to content

Commit

Permalink
Update SA1008 to require a space before the opening parenthesis if it…
Browse files Browse the repository at this point in the history
…'s the left operand of a range expression. Also add test for SA1008 in collection expression.

DotNetAnalyzers#3894
  • Loading branch information
bjornhellander committed Oct 17, 2024
1 parent 2dc4507 commit 2d7e105
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp12.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp11.SpacingRules;
using Xunit;

Expand All @@ -28,5 +29,48 @@ public async Task TestTupleUsingAliasAsync()
var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3894")]
public async Task TestCollectionExpressionAsync()
{
var testCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod()
{
int[] x = [ {|#0:(|} 0 + 0)];
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod()
{
int[] x = [(0 + 0)];
}
}
}
";

DiagnosticResult[] expectedResults =
{
Diagnostic(DescriptorNotPreceded).WithLocation(0),
Diagnostic(DescriptorNotFollowed).WithLocation(0),
};

await VerifyCSharpFixAsync(
testCode,
expectedResults,
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,48 @@ await VerifyCSharpFixAsync(
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3894")]
public async Task TestLeftOperandInRangeExpressionAsync()
{
var testCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod()
{
var x ={|#0:(|} 0 + 0)..1;
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod()
{
var x = (0 + 0)..1;
}
}
}
";

DiagnosticResult[] expectedResults =
{
Diagnostic(DescriptorPreceded).WithLocation(0),
Diagnostic(DescriptorNotFollowed).WithLocation(0),
};

await VerifyCSharpFixAsync(
testCode,
expectedResults,
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
case SyntaxKind.ParenthesizedExpression:
case SyntaxKindEx.TupleExpression:
if (prevToken.Parent.IsKind(SyntaxKind.Interpolation)
|| token.Parent.Parent.IsKind(SyntaxKindEx.RangeExpression))
|| (token.Parent.Parent.IsKind(SyntaxKindEx.RangeExpression) && ((RangeExpressionSyntaxWrapper)token.Parent.Parent).RightOperand == token.Parent))
{
haveLeadingSpace = false;
break;
Expand Down

0 comments on commit 2d7e105

Please sign in to comment.