Skip to content

Commit b11bba5

Browse files
committed
Merge pull request #1887 from oatkins/SA1115-ParameterMustBeginOnNextLine-Fix
Fix for bug in SA1115 (Parameter must begin on next line fix) #1886
2 parents 35c0aff + 3dff1a2 commit b11bba5

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,31 @@ public void Baz()
179179
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
180180
}
181181

182+
[Fact]
183+
public async Task TestMethodCallPragmaDirectiveBetweenParametersAsync()
184+
{
185+
var testCode = @"
186+
class Foo
187+
{
188+
public void Bar(int i, int z)
189+
{
190+
}
191+
192+
public void Baz()
193+
{
194+
Bar(
195+
#pragma warning disable CS4014
196+
1,
197+
#pragma warning restore CS4014
198+
2);
199+
}
200+
}";
201+
202+
DiagnosticResult[] expected = EmptyDiagnosticResults;
203+
204+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
205+
}
206+
182207
[Fact]
183208
public async Task TestMethodCallSecondParameterOnTheNextLineAsync()
184209
{

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,32 @@ private static void AnalyzeArgumentList(SyntaxNodeAnalysisContext context, BaseA
270270
return;
271271
}
272272

273-
var previousLine = argumentListSyntax.Arguments[0].GetLineSpan().EndLinePosition.Line;
273+
var previousArgumentLine = argumentListSyntax.Arguments[0].GetLineSpan().EndLinePosition.Line;
274274
for (int i = 1; i < argumentListSyntax.Arguments.Count; i++)
275275
{
276276
var currentArgument = argumentListSyntax.Arguments[i];
277-
var lineSpan = currentArgument.GetLineSpan();
278-
var currentLine = lineSpan.StartLinePosition.Line;
279-
if (currentLine - previousLine > 1)
277+
int currentArgumentStartLine;
278+
int currentArgumentEndLine;
279+
280+
if (currentArgument.HasLeadingTrivia && currentArgument.GetLeadingTrivia().All(trivia => IsValidTrivia(trivia)))
281+
{
282+
var lineSpan = currentArgument.SyntaxTree.GetLineSpan(currentArgument.FullSpan);
283+
currentArgumentStartLine = lineSpan.StartLinePosition.Line;
284+
currentArgumentEndLine = lineSpan.EndLinePosition.Line;
285+
}
286+
else
287+
{
288+
var lineSpan = currentArgument.GetLineSpan();
289+
currentArgumentStartLine = lineSpan.StartLinePosition.Line;
290+
currentArgumentEndLine = lineSpan.EndLinePosition.Line;
291+
}
292+
293+
if (currentArgumentStartLine - previousArgumentLine > 1)
280294
{
281295
context.ReportDiagnostic(Diagnostic.Create(Descriptor, currentArgument.GetLocation()));
282296
}
283297

284-
previousLine = lineSpan.EndLinePosition.Line;
298+
previousArgumentLine = currentArgumentEndLine;
285299
}
286300
}
287301

@@ -319,12 +333,13 @@ private static void AnalyzeParameterList(SyntaxNodeAnalysisContext context, Base
319333

320334
private static bool IsValidTrivia(SyntaxTrivia trivia)
321335
{
336+
if (trivia.IsDirective)
337+
{
338+
return true;
339+
}
340+
322341
switch (trivia.Kind())
323342
{
324-
case SyntaxKind.IfDirectiveTrivia:
325-
case SyntaxKind.ElseDirectiveTrivia:
326-
case SyntaxKind.ElifDirectiveTrivia:
327-
case SyntaxKind.EndIfDirectiveTrivia:
328343
case SyntaxKind.DisabledTextTrivia:
329344
case SyntaxKind.WhitespaceTrivia:
330345
return true;

0 commit comments

Comments
 (0)