Skip to content

Commit 5f0a586

Browse files
authored
Fix get expression text method (#85)
1 parent cf868bb commit 5f0a586

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs

+27-22
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,6 @@ public static string TryGetTemplateText(this ICSharpArgument argument)
118118
return argument.Value.GetExpressionText();
119119
}
120120

121-
public static string GetExpressionText(this ICSharpExpression expression)
122-
{
123-
var stringLiteral = StringLiteralAltererUtil.TryCreateStringLiteralByExpression(expression);
124-
if (stringLiteral == null)
125-
{
126-
return null;
127-
}
128-
129-
var expressionText = stringLiteral.Expression.GetText();
130-
if (expressionText.StartsWith("@"))
131-
{
132-
expressionText = expressionText.Substring(1);
133-
}
134-
135-
return StringUtil.Unquote(expressionText);
136-
}
137-
138121
[CanBeNull]
139122
public static IStringLiteralAlterer TryCreateLastTemplateFragmentExpression(this ICSharpArgument argument)
140123
{
@@ -191,11 +174,6 @@ public static bool IsSerilogContextPushPropertyMethod(this IInvocationExpression
191174
return LogContextFqn.Equals(containingType.GetClrName()) && typeMember.ShortName == "PushProperty";
192175
}
193176

194-
public static bool IsVerbatimString([CanBeNull]this IExpression expression)
195-
{
196-
return expression?.FirstChild?.NodeType == CSharpTokenType.STRING_LITERAL_VERBATIM;
197-
}
198-
199177
[CanBeNull]
200178
public static IType GetFirstGenericArgumentType([NotNull]this IDeclaredType declared)
201179
{
@@ -209,6 +187,33 @@ public static IType GetFirstGenericArgumentType([NotNull]this IDeclaredType decl
209187
return substitution.Apply(typeParameter);
210188
}
211189

190+
private static bool IsVerbatimString([CanBeNull]this IExpression expression)
191+
{
192+
return expression?.FirstChild?.NodeType == CSharpTokenType.STRING_LITERAL_VERBATIM;
193+
}
194+
195+
private static string GetExpressionText(this ICSharpExpression expression)
196+
{
197+
if (expression == null)
198+
{
199+
return null;
200+
}
201+
202+
var stringLiteral = StringLiteralAltererUtil.TryCreateStringLiteralByExpression(expression);
203+
if (stringLiteral == null)
204+
{
205+
return null;
206+
}
207+
208+
var expressionText = stringLiteral.Expression.GetText();
209+
if (expressionText.StartsWith("@"))
210+
{
211+
expressionText = expressionText.Substring(1);
212+
}
213+
214+
return StringUtil.Unquote(expressionText);
215+
}
216+
212217
private static void FlattenAdditiveExpression(IAdditiveExpression additiveExpression, LinkedList<ExpressionArgumentInfo> list)
213218
{
214219
foreach (var argumentInfo in additiveExpression.Arguments)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Serilog;
2+
3+
namespace ConsoleApp
4+
{
5+
public static class Program
6+
{
7+
public static void Main()
8+
{
9+
Log.Logger.Information(%"{MyProperty}", 1);
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Serilog;
2+
3+
namespace ConsoleApp
4+
{
5+
public static class Program
6+
{
7+
public static void Main()
8+
{
9+
Log.Logger.Information(%"{MyProperty}", 1);
10+
}
11+
}
12+
}
13+
14+
---------------------------------------------------------

test/src/Analyzer/PropertiesNamingAnalyzerTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class PropertiesNamingAnalyzerTests : MessageTemplateAnalyzerTestBase
1818

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

21+
[Test] public void TestSerilogInvalidSyntax() => DoNamedTest2();
22+
2123
[Test] public void TestSerilogInvalidNamedPropertyWithSpace() => DoNamedTest2();
2224
}
2325
}

0 commit comments

Comments
 (0)