diff --git a/src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs b/src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs index 5e87ef5..d96e977 100644 --- a/src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs +++ b/src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs @@ -118,23 +118,6 @@ public static string TryGetTemplateText(this ICSharpArgument argument) return argument.Value.GetExpressionText(); } - public static string GetExpressionText(this ICSharpExpression expression) - { - var stringLiteral = StringLiteralAltererUtil.TryCreateStringLiteralByExpression(expression); - if (stringLiteral == null) - { - return null; - } - - var expressionText = stringLiteral.Expression.GetText(); - if (expressionText.StartsWith("@")) - { - expressionText = expressionText.Substring(1); - } - - return StringUtil.Unquote(expressionText); - } - [CanBeNull] public static IStringLiteralAlterer TryCreateLastTemplateFragmentExpression(this ICSharpArgument argument) { @@ -191,11 +174,6 @@ public static bool IsSerilogContextPushPropertyMethod(this IInvocationExpression return LogContextFqn.Equals(containingType.GetClrName()) && typeMember.ShortName == "PushProperty"; } - public static bool IsVerbatimString([CanBeNull]this IExpression expression) - { - return expression?.FirstChild?.NodeType == CSharpTokenType.STRING_LITERAL_VERBATIM; - } - [CanBeNull] public static IType GetFirstGenericArgumentType([NotNull]this IDeclaredType declared) { @@ -209,6 +187,33 @@ public static IType GetFirstGenericArgumentType([NotNull]this IDeclaredType decl return substitution.Apply(typeParameter); } + private static bool IsVerbatimString([CanBeNull]this IExpression expression) + { + return expression?.FirstChild?.NodeType == CSharpTokenType.STRING_LITERAL_VERBATIM; + } + + private static string GetExpressionText(this ICSharpExpression expression) + { + if (expression == null) + { + return null; + } + + var stringLiteral = StringLiteralAltererUtil.TryCreateStringLiteralByExpression(expression); + if (stringLiteral == null) + { + return null; + } + + var expressionText = stringLiteral.Expression.GetText(); + if (expressionText.StartsWith("@")) + { + expressionText = expressionText.Substring(1); + } + + return StringUtil.Unquote(expressionText); + } + private static void FlattenAdditiveExpression(IAdditiveExpression additiveExpression, LinkedList list) { foreach (var argumentInfo in additiveExpression.Arguments) diff --git a/test/data/Analyzers/PropertiesNamingAnalyzer/SerilogInvalidSyntax.cs b/test/data/Analyzers/PropertiesNamingAnalyzer/SerilogInvalidSyntax.cs new file mode 100644 index 0000000..2e6b418 --- /dev/null +++ b/test/data/Analyzers/PropertiesNamingAnalyzer/SerilogInvalidSyntax.cs @@ -0,0 +1,12 @@ +using Serilog; + +namespace ConsoleApp +{ + public static class Program + { + public static void Main() + { + Log.Logger.Information(%"{MyProperty}", 1); + } + } +} diff --git a/test/data/Analyzers/PropertiesNamingAnalyzer/SerilogInvalidSyntax.cs.gold b/test/data/Analyzers/PropertiesNamingAnalyzer/SerilogInvalidSyntax.cs.gold new file mode 100644 index 0000000..8dd41dd --- /dev/null +++ b/test/data/Analyzers/PropertiesNamingAnalyzer/SerilogInvalidSyntax.cs.gold @@ -0,0 +1,14 @@ +using Serilog; + +namespace ConsoleApp +{ + public static class Program + { + public static void Main() + { + Log.Logger.Information(%"{MyProperty}", 1); + } + } +} + +--------------------------------------------------------- diff --git a/test/src/Analyzer/PropertiesNamingAnalyzerTests.cs b/test/src/Analyzer/PropertiesNamingAnalyzerTests.cs index 446f3ec..cd7f7d6 100644 --- a/test/src/Analyzer/PropertiesNamingAnalyzerTests.cs +++ b/test/src/Analyzer/PropertiesNamingAnalyzerTests.cs @@ -18,6 +18,8 @@ public class PropertiesNamingAnalyzerTests : MessageTemplateAnalyzerTestBase [Test] public void TestSerilogInvalidNamedPropertyWithDot() => DoNamedTest2(); + [Test] public void TestSerilogInvalidSyntax() => DoNamedTest2(); + [Test] public void TestSerilogInvalidNamedPropertyWithSpace() => DoNamedTest2(); } }