Skip to content

Commit 245f725

Browse files
committed
Update SA1003 to support additional expression-bodied members added in C# 7
1 parent b9427d9 commit 245f725

3 files changed

Lines changed: 78 additions & 46 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1003CSharp7UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using Test.SpacingRules;
9+
using TestHelper;
10+
using Xunit;
11+
12+
using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
713

814
public class SA1003CSharp7UnitTests : SA1003UnitTests
915
{
16+
/// <summary>
17+
/// Verifies that the additional expression-bodied members supported in C# 7 trigger diagnostics as expected.
18+
/// </summary>
19+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
20+
[Fact]
21+
public async Task TestCSharp7ExpressionBodiedMembersAsync()
22+
{
23+
var testCode = @"using System;
24+
namespace N1
25+
{
26+
public class C1
27+
{
28+
private int x;
29+
private EventHandler e;
30+
31+
public C1()=>x = 1; // Constructors
32+
~C1()=>x = 0; // Finalizers
33+
public event EventHandler E { add=>e += value; remove=>e -= value; } // Event accessors
34+
public int Answer { get=>42; set=>x = 2; } // Property accessors
35+
public int this[int index] { get=>42; set=>x = value; } // Indexer accessors
36+
}
37+
}
38+
";
39+
var fixedTestCode = @"using System;
40+
namespace N1
41+
{
42+
public class C1
43+
{
44+
private int x;
45+
private EventHandler e;
46+
47+
public C1() => x = 1; // Constructors
48+
~C1() => x = 0; // Finalizers
49+
public event EventHandler E { add => e += value; remove => e -= value; } // Event accessors
50+
public int Answer { get => 42; set => x = 2; } // Property accessors
51+
public int this[int index] { get => 42; set => x = value; } // Indexer accessors
52+
}
53+
}
54+
";
55+
DiagnosticResult[] expected =
56+
{
57+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(9, 20).WithArguments("=>"),
58+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(9, 20).WithArguments("=>"),
59+
60+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(10, 14).WithArguments("=>"),
61+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(10, 14).WithArguments("=>"),
62+
63+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(11, 42).WithArguments("=>"),
64+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(11, 42).WithArguments("=>"),
65+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(11, 62).WithArguments("=>"),
66+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(11, 62).WithArguments("=>"),
67+
68+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(12, 32).WithArguments("=>"),
69+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(12, 32).WithArguments("=>"),
70+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(12, 41).WithArguments("=>"),
71+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(12, 41).WithArguments("=>"),
72+
73+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(13, 41).WithArguments("=>"),
74+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(13, 41).WithArguments("=>"),
75+
this.CSharpDiagnostic(DescriptorPrecededByWhitespace).WithLocation(13, 50).WithArguments("=>"),
76+
this.CSharpDiagnostic(DescriptorFollowedByWhitespace).WithLocation(13, 50).WithArguments("=>"),
77+
};
78+
79+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
80+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
81+
}
1082
}
1183
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1003UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ public void TestMethod()
801801
}
802802

803803
/// <summary>
804-
/// Verifies that unary plus expression will not trigger any diagnostics.
804+
/// Verifies that the <c>=&gt;</c> operator for expression-bodied members triggers diagnostics as expected.
805805
/// </summary>
806806
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
807807
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
128128
private static readonly Action<SyntaxNodeAnalysisContext> CastExpressionAction = HandleCastExpression;
129129
private static readonly Action<SyntaxNodeAnalysisContext> EqualsValueClauseAction = HandleEqualsValueClause;
130130
private static readonly Action<SyntaxNodeAnalysisContext> LambdaExpressionAction = HandleLambdaExpression;
131-
private static readonly Action<SyntaxNodeAnalysisContext> PropertyDeclarationAction = HandlePropertyDeclaration;
132-
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
133-
private static readonly Action<SyntaxNodeAnalysisContext> OperatorDeclarationAction = HandleOperatorDeclaration;
134-
private static readonly Action<SyntaxNodeAnalysisContext> ConversionOperatorDeclarationAction = HandleConversionOperatorDeclaration;
135-
private static readonly Action<SyntaxNodeAnalysisContext> IndexerDeclarationAction = HandleIndexerDeclaration;
131+
private static readonly Action<SyntaxNodeAnalysisContext> ArrowExpressionClauseAction = HandleArrowExpressionClause;
136132

137133
/// <summary>
138134
/// Gets the descriptor for prefix unary expression that may not be followed by a comment.
@@ -208,11 +204,7 @@ public override void Initialize(AnalysisContext context)
208204
context.RegisterSyntaxNodeAction(CastExpressionAction, SyntaxKind.CastExpression);
209205
context.RegisterSyntaxNodeAction(EqualsValueClauseAction, SyntaxKind.EqualsValueClause);
210206
context.RegisterSyntaxNodeAction(LambdaExpressionAction, SyntaxKinds.LambdaExpression);
211-
context.RegisterSyntaxNodeAction(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration);
212-
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
213-
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
214-
context.RegisterSyntaxNodeAction(OperatorDeclarationAction, SyntaxKind.OperatorDeclaration);
215-
context.RegisterSyntaxNodeAction(ConversionOperatorDeclarationAction, SyntaxKind.ConversionOperatorDeclaration);
207+
context.RegisterSyntaxNodeAction(ArrowExpressionClauseAction, SyntaxKind.ArrowExpressionClause);
216208
}
217209

218210
private static void HandleConstructorDeclaration(SyntaxNodeAnalysisContext context)
@@ -350,42 +342,10 @@ private static void HandleLambdaExpression(SyntaxNodeAnalysisContext context)
350342
CheckToken(context, lambdaExpression.ArrowToken, true, true, true);
351343
}
352344

353-
private static void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context)
345+
private static void HandleArrowExpressionClause(SyntaxNodeAnalysisContext context)
354346
{
355-
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;
356-
HandleArrowExpressionClause(context, propertyDeclaration.ExpressionBody);
357-
}
358-
359-
private static void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context)
360-
{
361-
var indexerDeclaration = (IndexerDeclarationSyntax)context.Node;
362-
HandleArrowExpressionClause(context, indexerDeclaration.ExpressionBody);
363-
}
364-
365-
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
366-
{
367-
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
368-
HandleArrowExpressionClause(context, methodDeclaration.ExpressionBody);
369-
}
370-
371-
private static void HandleOperatorDeclaration(SyntaxNodeAnalysisContext context)
372-
{
373-
var operatorDeclaration = (OperatorDeclarationSyntax)context.Node;
374-
HandleArrowExpressionClause(context, operatorDeclaration.ExpressionBody);
375-
}
376-
377-
private static void HandleConversionOperatorDeclaration(SyntaxNodeAnalysisContext context)
378-
{
379-
var conversionOperatorDeclaration = (ConversionOperatorDeclarationSyntax)context.Node;
380-
HandleArrowExpressionClause(context, conversionOperatorDeclaration.ExpressionBody);
381-
}
382-
383-
private static void HandleArrowExpressionClause(SyntaxNodeAnalysisContext context, ArrowExpressionClauseSyntax arrowExpressionClause)
384-
{
385-
if (arrowExpressionClause != null)
386-
{
387-
CheckToken(context, arrowExpressionClause.ArrowToken, true, true, true);
388-
}
347+
ArrowExpressionClauseSyntax arrowExpressionClause = (ArrowExpressionClauseSyntax)context.Node;
348+
CheckToken(context, arrowExpressionClause.ArrowToken, true, true, true);
389349
}
390350

391351
private static void CheckToken(SyntaxNodeAnalysisContext context, SyntaxToken token, bool withLeadingWhitespace, bool allowAtEndOfLine, bool withTrailingWhitespace, string tokenText = null)

0 commit comments

Comments
 (0)