Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SA1514: Do not report when documenting types declared in the global namespace #3863

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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,199 @@ 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>
MattFromRVA marked this conversation as resolved.
Show resolved Hide resolved
/// X.
/// </summary>
public class TestClass
{
}
";

var expected = DiagnosticResult.EmptyDiagnosticResults;

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

[Fact]
public async Task TestClassInGlobalNamespaceWithoutNewlineAsync()
{
var testCode = @"/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var expected = DiagnosticResult.EmptyDiagnosticResults;

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

[Fact]
public async Task TestClassInNamespaceAsync()
{
var testCode = @"
namespace TestNamespace
{
/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
}
";

var expected = DiagnosticResult.EmptyDiagnosticResults;

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

[Fact]
public async Task TestClassInNamespaceWithCommentAsync()
{
var testCode = @"
namespace TestNamespace
{
// Normal comment
{|#0:///|} <summary>
/// X.
/// </summary>
public class TestClass
{
}
}
";

var fixedCode = @"
namespace TestNamespace
{
// Normal comment

/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
}
";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

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

[Fact]
public async Task TestClassInGlobalNamespaceWithCommentAsync()
{
var testCode = @"
// Normal comment
{|#0:///|} <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var fixedCode = @"
// Normal comment

/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

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

[Fact]
public async Task TestClassInGlobalNamespaceWithPreprocessorDirectiveAsync()
{
var testCode = @"
#if DEBUG
#endif
{|#0:///|} <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var fixedCode = @"
#if DEBUG
#endif

/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

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

[Fact]
public async Task TestClassInGlobalNamespaceWithMultilineCommentAsync()
{
var testCode = @"
/* Normal
* multiline
* comment */
{|#0:///|} <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var fixedCode = @"
/* Normal
* multiline
* comment */

/// <summary>
/// X.
/// </summary>
public class TestClass
{
}
";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ 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))
{
// Node is the first element in the global namespace
return;
}
}

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