Skip to content

Commit

Permalink
Adds support to @ signed prefixed parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamariyan committed Feb 2, 2022
1 parent 6d70f1b commit 94ac7e1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void GenFieldAssignments(LoggerMethod lm, string nestedIndentation)
{
foreach (LoggerParameter p in lm.TemplateParameters)
{
_builder.AppendLine($" {nestedIndentation}this._{p.Name} = {p.Name};");
_builder.AppendLine($" {nestedIndentation}this._{p.Name} = {(p.NeedsAtSign ? "@" : string.Empty)}{p.Name};");
}
}

Expand Down Expand Up @@ -265,7 +265,7 @@ private void GenCallbackArguments(LoggerMethod lm)
{
foreach (LoggerParameter p in lm.TemplateParameters)
{
_builder.Append($"{p.Name}, ");
_builder.Append($"{(p.NeedsAtSign ? "@" : string.Empty)}{p.Name}, ");
}
}

Expand Down Expand Up @@ -323,7 +323,7 @@ private void GenParameters(LoggerMethod lm)
{
_builder.Append($"{p.Qualifier} ");
}
_builder.Append($"{p.Type} {p.Name}");
_builder.Append($"{p.Type} {(p.NeedsAtSign ? "@" : string.Empty)}{p.Name}");
}
}

Expand All @@ -341,7 +341,7 @@ private void GenArguments(LoggerMethod lm)
_builder.Append(", ");
}

_builder.Append($"{p.Type} {p.Name}");
_builder.Append($"{p.Type} {(p.NeedsAtSign ? "@" : string.Empty)}{p.Name}");
}
}

Expand All @@ -357,7 +357,7 @@ private void GenHolder(LoggerMethod lm)
_builder.Append(", ");
}

_builder.Append(p.Name);
_builder.Append((p.NeedsAtSign ? "@" : string.Empty) + p.Name);
}

_builder.Append(')');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
foreach (IParameterSymbol paramSymbol in methodSymbol.Parameters)
{
string paramName = paramSymbol.Name;
bool needsAtSign = false;
if (paramSymbol.DeclaringSyntaxReferences.Length > 0)
{
ParameterSyntax paramSyntax = paramSymbol.DeclaringSyntaxReferences[0].GetSyntax(_cancellationToken) as ParameterSyntax;
if (paramSyntax != null && !string.IsNullOrEmpty(paramSyntax.Identifier.Text))
{
needsAtSign = paramSyntax.Identifier.Text[0] == '@';
}
}
if (string.IsNullOrWhiteSpace(paramName))
{
// semantic problem, just bail quietly
Expand Down Expand Up @@ -339,6 +348,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
Name = paramName,
Type = typeName,
Qualifier = qualifier,
NeedsAtSign = needsAtSign,
IsLogger = !foundLogger && IsBaseOrIdentity(paramTypeSymbol!, loggerSymbol),
IsException = !foundException && IsBaseOrIdentity(paramTypeSymbol!, exceptionSymbol),
IsLogLevel = !foundLogLevel && IsBaseOrIdentity(paramTypeSymbol!, logLevelSymbol),
Expand Down Expand Up @@ -740,6 +750,7 @@ internal class LoggerParameter
public string Name = string.Empty;
public string Type = string.Empty;
public string? Qualifier;
public bool NeedsAtSign;
public bool IsLogger;
public bool IsException;
public bool IsLogLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
internal static partial class AtSymbolTestExtensions
{
[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "M0 {event}")]
internal static partial void M0(ILogger logger, string @event);
}
}

0 comments on commit 94ac7e1

Please sign in to comment.