From 1a18528d13228f0eef727ff49767726b2c8c50dd Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Wed, 28 Apr 2021 22:20:01 -0400 Subject: [PATCH] Logging Generator Parser - Code cleanup PR feedback (#52018) --- .../gen/LoggerMessageGenerator.Parser.cs | 169 ++++++------------ .../LoggerMessageGeneratorParserTests.cs | 63 ------- 2 files changed, 58 insertions(+), 174 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging/gen/LoggerMessageGenerator.Parser.cs b/src/libraries/Microsoft.Extensions.Logging/gen/LoggerMessageGenerator.Parser.cs index 3b909af90cc6a..b7a7beea720ed 100644 --- a/src/libraries/Microsoft.Extensions.Logging/gen/LoggerMessageGenerator.Parser.cs +++ b/src/libraries/Microsoft.Extensions.Logging/gen/LoggerMessageGenerator.Parser.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -61,19 +62,8 @@ public IReadOnlyList GetLogClasses(IEnumerable(); } - INamedTypeSymbol enumerableSymbol = _compilation.GetTypeByMetadataName("System.Collections.IEnumerable"); - if (enumerableSymbol == null) - { - Diag(DiagnosticDescriptors.MissingRequiredType, null, "System.Collections.IEnumerable"); - return Array.Empty(); - } - - INamedTypeSymbol stringSymbol = _compilation.GetTypeByMetadataName("System.String"); - if (stringSymbol == null) - { - Diag(DiagnosticDescriptors.MissingRequiredType, null, "System.String"); - return Array.Empty(); - } + INamedTypeSymbol enumerableSymbol = _compilation.GetSpecialType(SpecialType.System_Collections_IEnumerable); + INamedTypeSymbol stringSymbol = _compilation.GetSpecialType(SpecialType.System_String); var results = new List(); var ids = new HashSet(); @@ -102,12 +92,12 @@ public IReadOnlyList GetLogClasses(IEnumerable GetLogClasses(IEnumerable GetLogClasses(IEnumerable GetLogClasses(IEnumerable GetLogClasses(IEnumerable GetLogClasses(IEnumerable GetLogClasses(IEnumerable GetLogClasses(IEnumerable /// Finds the template arguments contained in the message string. /// - private static void ExtractTemplates(string? message, IDictionary templateMap, IList templateList) + private static void ExtractTemplates(string? message, IDictionary templateMap, ICollection templateList) { if (string.IsNullOrEmpty(message)) { @@ -613,6 +545,21 @@ private static int FindIndexOfAny(string message, char[] chars, int startIndex, int findIndex = message.IndexOfAny(chars, startIndex, endIndex - startIndex); return findIndex == -1 ? endIndex : findIndex; } + + private string GetStringExpression(SemanticModel sm, SyntaxNode expr) + { + Optional optional = sm.GetConstantValue(expr, _cancellationToken); + if (optional.HasValue) + { + object o = optional.Value; + if (o != null) + { + return o.ToString(); + } + } + + return string.Empty; + } } /// @@ -642,7 +589,7 @@ internal class LoggerMethod public string? EventName; public bool IsExtensionMethod; public string Modifiers = string.Empty; - public string LoggerField; + public string LoggerField = string.Empty; } /// diff --git a/src/libraries/Microsoft.Extensions.Logging/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs b/src/libraries/Microsoft.Extensions.Logging/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs index 1aabf53997b2a..f8942e11b6140 100644 --- a/src/libraries/Microsoft.Extensions.Logging/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs +++ b/src/libraries/Microsoft.Extensions.Logging/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs @@ -298,69 +298,6 @@ partial class C Assert.Equal(DiagnosticDescriptors.MissingRequiredType.Id, diagnostics[0].Id); } - [Fact] - public async Task MissingStringType() - { - IReadOnlyList diagnostics = await RunGenerator(@" - namespace System - { - public class Object {} - public class Void {} - public class Exception {} - public struct DateTime {} - } - namespace System.Collections - { - public interface IEnumerable {} - } - namespace Microsoft.Extensions.Logging - { - public enum LogLevel {} - public interface ILogger {} - } - namespace Microsoft.Extensions.Logging - { - public class LoggerMessageAttribute {} - } - partial class C - { - } - ", false, includeBaseReferences: false, includeLoggingReferences: false); - - Assert.Single(diagnostics); - Assert.Equal(DiagnosticDescriptors.MissingRequiredType.Id, diagnostics[0].Id); - } - - [Fact] - public async Task MissingEnumerableType() - { - IReadOnlyList diagnostics = await RunGenerator(@" - namespace System - { - public class Object {} - public class Void {} - public class Exception {} - public struct DateTime {} - public class String {} - } - namespace Microsoft.Extensions.Logging - { - public enum LogLevel {} - public interface ILogger {} - } - namespace Microsoft.Extensions.Logging - { - public class LoggerMessageAttribute {} - } - partial class C - { - } - ", false, includeBaseReferences: false, includeLoggingReferences: false); - - Assert.Single(diagnostics); - Assert.Equal(DiagnosticDescriptors.MissingRequiredType.Id, diagnostics[0].Id); - } - [Fact] public async Task MissingLoggerMessageAttributeType() {