Skip to content
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,13 @@ 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 &&
potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
{
potentialNamespaceParent = potentialNamespaceParent.Parent;
}
if (potentialNamespaceParent is NamespaceDeclarationSyntax namespaceParent)
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
{
nspace = namespaceParent.Name.ToString();
while (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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 baseline = await File.ReadAllTextAsync(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