Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1151,5 +1151,23 @@ public enum TestEnum

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
public async Task TestClassInGlobalNamespaceAsync()
{
var testCode = @"
/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var expected = DiagnosticResult.EmptyDiagnosticResults;

await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ private static void HandleDeclaration(SyntaxNodeAnalysisContext context)
// no leading blank line necessary at start of scope.
return;
}

// Logic to handle global namespace case
if (prevToken.IsKind(SyntaxKind.None))
{
// Check if the node is the first non-trivia element in the file
var firstToken = context.Node.SyntaxTree.GetRoot().GetFirstToken();
if (firstToken == context.Node.GetFirstToken())
{
// Node is the first element in the global namespace
return;
}
}
}

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