Skip to content

Commit 6673e09

Browse files
committed
Added new logic to skip SA1514 when documenting types declared in the global namespace
1 parent 51c772e commit 6673e09

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,5 +1151,23 @@ public enum TestEnum
11511151

11521152
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
11531153
}
1154+
1155+
[Fact]
1156+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1157+
public async Task TestClassInGlobalNamespaceAsync()
1158+
{
1159+
var testCode = @"
1160+
/// <summary>
1161+
/// X.
1162+
/// </summary>
1163+
public class TestClass
1164+
{
1165+
}
1166+
";
1167+
1168+
var expected = DiagnosticResult.EmptyDiagnosticResults;
1169+
1170+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
1171+
}
11541172
}
11551173
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ private static void HandleDeclaration(SyntaxNodeAnalysisContext context)
170170
// no leading blank line necessary at start of scope.
171171
return;
172172
}
173+
174+
// Logic to handle global namespace case
175+
if (prevToken.IsKind(SyntaxKind.None))
176+
{
177+
// Check if the node is the first non-trivia element in the file
178+
var firstToken = context.Node.SyntaxTree.GetRoot().GetFirstToken();
179+
if (firstToken == context.Node.GetFirstToken())
180+
{
181+
// Node is the first element in the global namespace
182+
return;
183+
}
184+
}
173185
}
174186

175187
context.ReportDiagnostic(Diagnostic.Create(Descriptor, GetDiagnosticLocation(documentationHeader)));

0 commit comments

Comments
 (0)