Skip to content

Commit 7f8d943

Browse files
committed
Update SA1516 to handle assembly attributes
Fixes #1923
1 parent b1a92b6 commit 7f8d943

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1516CodeFixProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ private static SyntaxNode GetRelevantNode(SyntaxNode innerNode)
142142
return currentNode;
143143
}
144144

145+
if (currentNode is AttributeListSyntax)
146+
{
147+
return currentNode;
148+
}
149+
145150
currentNode = currentNode.Parent;
146151
}
147152

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,5 +713,33 @@ private set
713713

714714
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
715715
}
716+
717+
[Theory]
718+
[InlineData("namespace")]
719+
[InlineData("public class")]
720+
[WorkItem(1923, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1923")]
721+
public async Task TestBlankLinesAroundAssemblyAttributesAsync(string followingElementKind)
722+
{
723+
string testCode = $@"using System.Runtime.CompilerServices;
724+
[assembly: InternalsVisibleTo(""AnotherAssembly"")]
725+
{followingElementKind} Foo
726+
{{
727+
}}";
728+
string fixedTestCode = $@"using System.Runtime.CompilerServices;
729+
730+
[assembly: InternalsVisibleTo(""AnotherAssembly"")]
731+
732+
{followingElementKind} Foo
733+
{{
734+
}}";
735+
736+
var expected = new[]
737+
{
738+
Diagnostic().WithLocation(2, 1),
739+
Diagnostic().WithLocation(3, 1),
740+
};
741+
742+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
743+
}
716744
}
717745
}

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,43 @@ private static void HandleCompilationUnit(SyntaxNodeAnalysisContext context, Sty
169169
{
170170
var compilationUnit = (CompilationUnitSyntax)context.Node;
171171

172+
var externs = compilationUnit.Externs;
172173
var usings = compilationUnit.Usings;
174+
var attributeLists = compilationUnit.AttributeLists;
173175
var members = compilationUnit.Members;
174176

175177
HandleUsings(context, usings, settings);
176178
HandleMemberList(context, members);
177179

178-
if (members.Count > 0 && compilationUnit.Usings.Count > 0)
180+
SyntaxNode previousItem = externs.LastOrDefault();
181+
if (usings.Any())
179182
{
180-
ReportIfThereIsNoBlankLine(context, usings[usings.Count - 1], members[0]);
183+
if (previousItem != null)
184+
{
185+
ReportIfThereIsNoBlankLine(context, previousItem, usings[0]);
186+
}
187+
188+
previousItem = usings.Last();
181189
}
182190

183-
if (compilationUnit.Usings.Count > 0 && compilationUnit.Externs.Count > 0)
191+
if (attributeLists.Any())
184192
{
185-
ReportIfThereIsNoBlankLine(context, compilationUnit.Externs[compilationUnit.Externs.Count - 1], compilationUnit.Usings[0]);
193+
if (previousItem != null)
194+
{
195+
ReportIfThereIsNoBlankLine(context, previousItem, attributeLists[0]);
196+
}
197+
198+
previousItem = attributeLists.Last();
199+
}
200+
201+
if (members.Any())
202+
{
203+
if (previousItem != null)
204+
{
205+
ReportIfThereIsNoBlankLine(context, previousItem, members[0]);
206+
}
207+
208+
previousItem = members.Last();
186209
}
187210
}
188211

0 commit comments

Comments
 (0)