Skip to content

Commit

Permalink
Fix get expression text method (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
olsh authored Mar 2, 2023
1 parent cf868bb commit 5f0a586
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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<ExpressionArgumentInfo> list)
{
foreach (var argumentInfo in additiveExpression.Arguments)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Serilog;

namespace ConsoleApp
{
public static class Program
{
public static void Main()
{
Log.Logger.Information(%"{MyProperty}", 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Serilog;

namespace ConsoleApp
{
public static class Program
{
public static void Main()
{
Log.Logger.Information(%"{MyProperty}", 1);
}
}
}

---------------------------------------------------------
2 changes: 2 additions & 0 deletions test/src/Analyzer/PropertiesNamingAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class PropertiesNamingAnalyzerTests : MessageTemplateAnalyzerTestBase

[Test] public void TestSerilogInvalidNamedPropertyWithDot() => DoNamedTest2();

[Test] public void TestSerilogInvalidSyntax() => DoNamedTest2();

[Test] public void TestSerilogInvalidNamedPropertyWithSpace() => DoNamedTest2();
}
}

0 comments on commit 5f0a586

Please sign in to comment.