{
// 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)
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);
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")]