Skip to content

Commit f8ee188

Browse files
committed
Merge pull request #1981 from vweijsters/fix-1921
SA1115 will now allow comments between parameters
2 parents bbd37ff + 21f7ecf commit f8ee188

2 files changed

Lines changed: 127 additions & 13 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1115UnitTests.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,96 @@ public void TestMethod(
11541154
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
11551155
}
11561156

1157+
/// <summary>
1158+
/// Verifies that comment lines will not reported a diagnostic.
1159+
/// This is a regression test for #1921.
1160+
/// </summary>
1161+
/// <param name="commentText">The comment test to use for the test.</param>
1162+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1163+
[Theory]
1164+
[InlineData("/* comment */")]
1165+
[InlineData("// comment")]
1166+
[InlineData("//// comment")]
1167+
public async Task TestWithCommentLinesAsync(string commentText)
1168+
{
1169+
var testCode = $@"
1170+
public class TestClass
1171+
{{
1172+
public void MyTest(int v1, int v2)
1173+
{{
1174+
}}
1175+
1176+
public void MyTest2(
1177+
int v1,
1178+
{commentText}
1179+
int v2)
1180+
{{
1181+
}}
1182+
1183+
public void TestMethod()
1184+
{{
1185+
this.MyTest(
1186+
1,
1187+
{commentText}
1188+
2);
1189+
}}
1190+
}}
1191+
";
1192+
1193+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1194+
}
1195+
1196+
/// <summary>
1197+
/// Verifies that multiple comment lines will not reported a diagnostic.
1198+
/// This is a regression test for #1921.
1199+
/// </summary>
1200+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1201+
[Fact]
1202+
public async Task TestWithMultipleCommentLinesAsync()
1203+
{
1204+
var testCode = @"
1205+
public class TestClass
1206+
{
1207+
public void MyTest1(
1208+
int v1,
1209+
//// bool b1,
1210+
//// bool b2,
1211+
int v2)
1212+
{
1213+
}
1214+
1215+
public void MyTest2(
1216+
int v1,
1217+
/*
1218+
bool b1,
1219+
bool b2,
1220+
*/
1221+
int v2)
1222+
{
1223+
}
1224+
1225+
public void TestMethod()
1226+
{
1227+
this.MyTest1(
1228+
1,
1229+
//// true,
1230+
//// false,
1231+
2);
1232+
1233+
this.MyTest2(
1234+
1,
1235+
/*
1236+
true,
1237+
false,
1238+
*/
1239+
2);
1240+
}
1241+
}
1242+
";
1243+
1244+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1245+
}
1246+
11571247
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
11581248
{
11591249
yield return new SA1115ParameterMustFollowComma();

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1115ParameterMustFollowComma.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private static void AnalyzeArgumentList(SyntaxNodeAnalysisContext context, BaseA
277277
int currentArgumentStartLine;
278278
int currentArgumentEndLine;
279279

280-
if (currentArgument.HasLeadingTrivia && currentArgument.GetLeadingTrivia().All(trivia => IsValidTrivia(trivia)))
280+
if (currentArgument.HasLeadingTrivia && IsValidTrivia(currentArgument.GetLeadingTrivia()))
281281
{
282282
var lineSpan = currentArgument.SyntaxTree.GetLineSpan(currentArgument.FullSpan);
283283
currentArgumentStartLine = lineSpan.StartLinePosition.Line;
@@ -313,7 +313,7 @@ private static void AnalyzeParameterList(SyntaxNodeAnalysisContext context, Base
313313
var currentParameter = parameterListSyntax.Parameters[i];
314314
int currentParameterLine;
315315

316-
if (currentParameter.HasLeadingTrivia && currentParameter.GetLeadingTrivia().All(trivia => IsValidTrivia(trivia)))
316+
if (currentParameter.HasLeadingTrivia && IsValidTrivia(currentParameter.GetLeadingTrivia()))
317317
{
318318
currentParameterLine = currentParameter.SyntaxTree.GetLineSpan(currentParameter.FullSpan).StartLinePosition.Line;
319319
}
@@ -331,22 +331,46 @@ private static void AnalyzeParameterList(SyntaxNodeAnalysisContext context, Base
331331
}
332332
}
333333

334-
private static bool IsValidTrivia(SyntaxTrivia trivia)
334+
private static bool IsValidTrivia(SyntaxTriviaList triviaList)
335335
{
336-
if (trivia.IsDirective)
337-
{
338-
return true;
339-
}
336+
var inBlankLine = true;
340337

341-
switch (trivia.Kind())
338+
for (var i = 0; i < triviaList.Count; i++)
342339
{
343-
case SyntaxKind.DisabledTextTrivia:
344-
case SyntaxKind.WhitespaceTrivia:
345-
return true;
340+
var trivia = triviaList[i];
341+
342+
if (trivia.IsDirective)
343+
{
344+
inBlankLine = false;
345+
continue;
346+
}
347+
348+
switch (trivia.Kind())
349+
{
350+
case SyntaxKind.WhitespaceTrivia:
351+
break;
346352

347-
default:
348-
return false;
353+
case SyntaxKind.EndOfLineTrivia:
354+
if (inBlankLine)
355+
{
356+
return false;
357+
}
358+
359+
inBlankLine = true;
360+
break;
361+
362+
case SyntaxKind.DisabledTextTrivia:
363+
case SyntaxKind.SingleLineCommentTrivia:
364+
case SyntaxKind.MultiLineCommentTrivia:
365+
inBlankLine = false;
366+
break;
367+
368+
default:
369+
return false;
370+
}
349371
}
372+
373+
return true;
350374
}
351375
}
352376
}

0 commit comments

Comments
 (0)