Skip to content

Commit

Permalink
Handle file-scoped namespaces
Browse files Browse the repository at this point in the history
Handle namespace not being emitted when file-scoped
namespaces are used for a log class.
Relates to dotnet#57880.
  • Loading branch information
martincostello committed Aug 22, 2021
1 parent 454ac57 commit 4ba0da9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
</ItemGroup>
<PropertyGroup>
<!-- For source generator support we need to target a pinned version in order to be able to run on older versions of Roslyn -->
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>3.9.0</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisVersion>3.9.0</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.0.0-3.final</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisVersion>4.0.0-3.final</MicrosoftCodeAnalysisVersion>
</PropertyGroup>
<PropertyGroup>
<!-- Code analysis dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,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 @@ -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

0 comments on commit 4ba0da9

Please sign in to comment.