Skip to content

Commit 78fea87

Browse files
committed
Added contructor initializer support to SA1113
1 parent 855ab0a commit 78fea87

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1113UnitTests.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,11 +1128,102 @@ public void TestMethod2()
11281128
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
11291129
}
11301130

1131+
/// <summary>
1132+
/// Verifies that a base constructor call with the comma on the wrong line is handled properly.
1133+
/// This is a regression test for #1654.
1134+
/// </summary>
1135+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1136+
[Fact]
1137+
public async Task VerifyBaseConstructorCallAsync()
1138+
{
1139+
var testCode = @"
1140+
abstract class BaseClass
1141+
{
1142+
protected BaseClass(int x, int y) { }
1143+
}
1144+
1145+
class ClassName : BaseClass
1146+
{
1147+
protected ClassName(int x, int y)
1148+
: base(
1149+
x
1150+
, y)
1151+
{
1152+
}
1153+
}
1154+
";
1155+
var fixedCode = @"
1156+
abstract class BaseClass
1157+
{
1158+
protected BaseClass(int x, int y) { }
1159+
}
1160+
1161+
class ClassName : BaseClass
1162+
{
1163+
protected ClassName(int x, int y)
1164+
: base(
1165+
x,
1166+
y)
1167+
{
1168+
}
1169+
}
1170+
";
1171+
1172+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(12, 13);
1173+
1174+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
1175+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1176+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
1177+
}
1178+
1179+
/// <summary>
1180+
/// Verifies that a this constructor call with the comma on the wrong line is handled properly.
1181+
/// </summary>
1182+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1183+
[Fact]
1184+
public async Task VerifyThisConstructorCallAsync()
1185+
{
1186+
var testCode = @"
1187+
class ClassName
1188+
{
1189+
private ClassName(int x, int y) { }
1190+
1191+
protected ClassName(int x, int y, int z)
1192+
: this(
1193+
x
1194+
, y)
1195+
{
1196+
}
1197+
}
1198+
";
1199+
var fixedCode = @"
1200+
class ClassName
1201+
{
1202+
private ClassName(int x, int y) { }
1203+
1204+
protected ClassName(int x, int y, int z)
1205+
: this(
1206+
x,
1207+
y)
1208+
{
1209+
}
1210+
}
1211+
";
1212+
1213+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(9, 13);
1214+
1215+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
1216+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1217+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
1218+
}
1219+
1220+
/// <inheritdoc/>
11311221
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
11321222
{
11331223
yield return new SA1113CommaMustBeOnSameLineAsPreviousParameter();
11341224
}
11351225

1226+
/// <inheritdoc/>
11361227
protected override CodeFixProvider GetCSharpCodeFixProvider()
11371228
{
11381229
return new TokenSpacingCodeFixProvider();

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1113CommaMustBeOnSameLineAsPreviousParameter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ internal class SA1113CommaMustBeOnSameLineAsPreviousParameter : DiagnosticAnalyz
5050
private static readonly DiagnosticDescriptor Descriptor =
5151
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
5252

53+
private static readonly ImmutableArray<SyntaxKind> ConstructorInitializerKinds =
54+
ImmutableArray.Create(SyntaxKind.BaseConstructorInitializer, SyntaxKind.ThisConstructorInitializer);
55+
5356
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
5457
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
5558
private static readonly Action<SyntaxNodeAnalysisContext> ConstructorDeclarationAction = HandleConstructorDeclaration;
@@ -64,6 +67,7 @@ internal class SA1113CommaMustBeOnSameLineAsPreviousParameter : DiagnosticAnalyz
6467
private static readonly Action<SyntaxNodeAnalysisContext> AttributeListAction = HandleAttributeList;
6568
private static readonly Action<SyntaxNodeAnalysisContext> OperatorDeclarationAction = HandleOperatorDeclaration;
6669
private static readonly Action<SyntaxNodeAnalysisContext> ArrayCreationExpressionAction = HandleArrayCreationExpression;
70+
private static readonly Action<SyntaxNodeAnalysisContext> ConstructorInitializerExpressionAction = HandleConstructorInitializerExpression;
6771

6872
/// <inheritdoc/>
6973
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -90,6 +94,7 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
9094
context.RegisterSyntaxNodeActionHonorExclusions(AttributeListAction, SyntaxKind.AttributeList);
9195
context.RegisterSyntaxNodeActionHonorExclusions(OperatorDeclarationAction, SyntaxKind.OperatorDeclaration);
9296
context.RegisterSyntaxNodeActionHonorExclusions(ArrayCreationExpressionAction, SyntaxKind.ArrayCreationExpression);
97+
context.RegisterSyntaxNodeActionHonorExclusions(ConstructorInitializerExpressionAction, ConstructorInitializerKinds);
9398
}
9499

95100
private static void HandleArrayCreationExpression(SyntaxNodeAnalysisContext context)
@@ -205,6 +210,12 @@ private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
205210
HandleBaseParameterListSyntax(context, methodDeclaration.ParameterList);
206211
}
207212

213+
private static void HandleConstructorInitializerExpression(SyntaxNodeAnalysisContext context)
214+
{
215+
var constructorInitializer = (ConstructorInitializerSyntax)context.Node;
216+
HandleBaseArgumentListSyntax(context, constructorInitializer.ArgumentList);
217+
}
218+
208219
private static void HandleBaseArgumentListSyntax(SyntaxNodeAnalysisContext context, BaseArgumentListSyntax argumentList)
209220
{
210221
if (argumentList != null && !argumentList.IsMissing)

0 commit comments

Comments
 (0)