Skip to content

Commit 0d90d66

Browse files
authored
Merge pull request #3936 from FantasyTeddy/relaxed-sa1516
Do not report SA1516 for two consecutive single line properties
2 parents 04fa43a + e3ba459 commit 0d90d66

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1516UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class Bar
3232
public string Test3;
3333
3434
public string TestProperty1 { get; set; }
35-
3635
public string TestProperty2 { get; set; }
3736
/// <summary>
3837
/// A summary.
@@ -149,7 +148,6 @@ class Foo { }
149148
Diagnostic().WithLocation(2, 1),
150149
Diagnostic().WithLocation(5, 1),
151150
Diagnostic().WithLocation(12, 1),
152-
Diagnostic().WithLocation(13, 1),
153151
Diagnostic().WithLocation(18, 1),
154152
Diagnostic().WithLocation(24, 1),
155153
Diagnostic().WithLocation(29, 1),

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1516ElementsMustBeSeparatedByBlankLine.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,33 +346,53 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, SyntaxLi
346346
if (!members[i - 1].ContainsDiagnostics && !members[i].ContainsDiagnostics)
347347
{
348348
// Report if
349-
// the current declaration is not a field declaration
349+
// the current declaration is not a field declaration or property declaration
350350
// or the previous declaration is of different type
351351
// or the previous declaration spans across multiple lines
352352
//
353-
// Note that the order of checking is important, as the call to IsMultiLine requires a FieldDeclarationSyntax,
354-
// something that can only be guaranteed if the first two checks fail.
355-
if (!members[i].IsKind(SyntaxKind.FieldDeclaration)
356-
|| !members[i - 1].IsKind(members[i].Kind())
357-
|| IsMultiline((FieldDeclarationSyntax)members[i - 1]))
353+
// Note that the order of checking is important, as the call to IsMultiLine requires a
354+
// FieldDeclarationSyntax or PropertyDeclarationSyntax, something that can only be guaranteed if the
355+
// first two checks fail.
356+
if (members[i].IsKind(SyntaxKind.FieldDeclaration)
357+
&& members[i - 1].IsKind(members[i].Kind())
358+
&& !IsMultiline((FieldDeclarationSyntax)members[i - 1]))
358359
{
359-
ReportIfThereIsNoBlankLine(context, members[i - 1], members[i]);
360+
continue;
361+
}
362+
363+
if (members[i].IsKind(SyntaxKind.PropertyDeclaration)
364+
&& members[i - 1].IsKind(members[i].Kind())
365+
&& !IsMultiline((PropertyDeclarationSyntax)members[i])
366+
&& !IsMultiline((PropertyDeclarationSyntax)members[i - 1]))
367+
{
368+
continue;
360369
}
370+
371+
ReportIfThereIsNoBlankLine(context, members[i - 1], members[i]);
361372
}
362373
}
363374
}
364375

365376
private static bool IsMultiline(FieldDeclarationSyntax fieldDeclaration)
366377
{
367-
var lineSpan = fieldDeclaration.GetLineSpan();
368-
var attributeLists = fieldDeclaration.AttributeLists;
378+
return IsMultiline(fieldDeclaration, fieldDeclaration.AttributeLists);
379+
}
380+
381+
private static bool IsMultiline(PropertyDeclarationSyntax propertyDeclaration)
382+
{
383+
return IsMultiline(propertyDeclaration, propertyDeclaration.AttributeLists);
384+
}
385+
386+
private static bool IsMultiline(MemberDeclarationSyntax memberDeclaration, SyntaxList<AttributeListSyntax> attributeLists)
387+
{
388+
var lineSpan = memberDeclaration.GetLineSpan();
369389

370390
int startLine;
371391

372392
// Exclude attributes when determining if a field declaration spans multiple lines
373393
if (attributeLists.Count > 0)
374394
{
375-
var lastAttributeSpan = fieldDeclaration.SyntaxTree.GetLineSpan(attributeLists.Last().FullSpan);
395+
var lastAttributeSpan = memberDeclaration.SyntaxTree.GetLineSpan(attributeLists.Last().FullSpan);
376396
startLine = lastAttributeSpan.EndLinePosition.Line;
377397
}
378398
else

0 commit comments

Comments
 (0)