Skip to content

[release/6.0] Fix Logging Source Generator to handle file-scoped namespaces (#57894) #59100

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

Merged
merged 2 commits into from
Sep 17, 2021
Merged
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 @@ -438,11 +438,21 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
{
// determine the namespace the class is declared in, if any
SyntaxNode? potentialNamespaceParent = classDec.Parent;
while (potentialNamespaceParent != null && potentialNamespaceParent is not NamespaceDeclarationSyntax)
while (potentialNamespaceParent != null &&
potentialNamespaceParent is not NamespaceDeclarationSyntax
#if ROSLYN4_0_OR_GREATER
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax
#endif
)
{
potentialNamespaceParent = potentialNamespaceParent.Parent;
}

#if ROSLYN4_0_OR_GREATER
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
#else
if (potentialNamespaceParent is NamespaceDeclarationSyntax namespaceParent)
#endif
{
nspace = namespaceParent.Name.ToString();
while (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ internal class Class2 { }
await VerifyAgainstBaselineUsingFile("TestWithNestedClass.generated.txt", testSourceCode);
}

[Fact]
public async Task TestBaseline_TestWithFileScopedNamespace_Success()
{
string testSourceCode = @"
namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses;

internal static partial class TestWithDefaultValues
{
[LoggerMessage]
public static partial void M0(ILogger logger, LogLevel level);
}";
await VerifyAgainstBaselineUsingFile("TestWithDefaultValues.generated.txt", testSourceCode);
}

private async Task VerifyAgainstBaselineUsingFile(string filename, string testSourceCode)
{
string[] expectedLines = await File.ReadAllLinesAsync(Path.Combine("Baselines", filename)).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,24 @@ public partial class Nested
Assert.Empty(diagnostics);
}

[Fact]
public async Task FileScopedNamespaceOK()
{
IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@"
using Microsoft.Extensions.Logging;

namespace MyLibrary;

internal partial class Logger
{
[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = ""Hello {Name}!"")]
public static partial void Greeting(ILogger logger, string name);
}
");

Assert.Empty(diagnostics);
}

[Theory]
[InlineData("false")]
[InlineData("true")]
Expand Down