Skip to content

Commit c3b1f93

Browse files
committed
opt: 优化插值字符串的处理
1 parent 4a05df4 commit c3b1f93

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<PropertyGroup>
13-
<Version>1.0.1</Version>
13+
<Version>1.1.0</Version>
1414

1515
<Description>Code fix provider for `C#` code log diagnosis `CA1727`, `CA2253`, `CA2254`. 针对 `C#` 代码日志诊断 `CA1727`, `CA2253`, `CA2254` 的代码修复提供程序</Description>
1616

src/Cuture.CodeAnalysis.LoggingCodeFixes/LoggingInvocationFixer.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ private static InterpolatedStringTextSyntax CreateHolderInterpolatedStringTextSy
184184

185185
private static IEnumerable<InterpolatedStringContentSyntaxDescriptor> EnumerateProcessedInterpolatedStringContentSyntaxes(IEnumerable<InterpolatedStringContentSyntax> contentSyntaxes)
186186
{
187+
var argumentCount = 0;
188+
187189
foreach (var contentSyntax in contentSyntaxes)
188190
{
189191
if (contentSyntax is not InterpolationSyntax interpolationSyntax)
@@ -239,6 +241,37 @@ private static IEnumerable<InterpolatedStringContentSyntaxDescriptor> EnumerateP
239241
}
240242
break;
241243

244+
case ConditionalAccessExpressionSyntax conditionalAccessExpressionSyntax: //eg: xxx?.Xxx()
245+
{
246+
//TODO 递归展开处理多级 ToString()
247+
if (conditionalAccessExpressionSyntax.WhenNotNull is not InvocationExpressionSyntax invocationExpressionSyntax
248+
|| invocationExpressionSyntax.Expression is not MemberBindingExpressionSyntax memberBindingExpressionSyntax)
249+
{
250+
yield return (CreateHolderInterpolatedStringTextSyntax(interpolationSyntax.ToString()), CreateArgumentSyntax(interpolationSyntax.Expression));
251+
break;
252+
}
253+
if (memberBindingExpressionSyntax.Name.Identifier.ValueText == "ToString") //ToString
254+
{
255+
if (invocationExpressionSyntax.ArgumentList.Arguments.Count == 0) //无参ToString()
256+
{
257+
yield return (CreateHolderInterpolatedStringTextSyntax(conditionalAccessExpressionSyntax.Expression.ToString()), CreateArgumentSyntax(conditionalAccessExpressionSyntax.Expression), conditionalAccessExpressionSyntax);
258+
}
259+
else
260+
{
261+
yield return (CreateHolderInterpolatedStringTextSyntax(conditionalAccessExpressionSyntax.Expression.ToString()), CreateArgumentSyntax(conditionalAccessExpressionSyntax), conditionalAccessExpressionSyntax);
262+
}
263+
}
264+
else
265+
{
266+
yield return (CreateHolderInterpolatedStringTextSyntax(interpolationSyntax.ToString()), CreateArgumentSyntax(interpolationSyntax.Expression));
267+
}
268+
}
269+
break;
270+
271+
case InterpolatedStringExpressionSyntax interpolatedStringExpressionSyntax: //插值字符串 $"xxxx: {1}"
272+
yield return (CreateHolderInterpolatedStringTextSyntax($"Value_{argumentCount++}"), CreateArgumentSyntax(interpolationSyntax.Expression));
273+
break;
274+
242275
default:
243276
yield return (CreateHolderInterpolatedStringTextSyntax(interpolationSyntax.ToString()), CreateArgumentSyntax(interpolationSyntax.Expression));
244277
break;

test/Cuture.CodeAnalysis.LoggingCodeFixes.Test/StructuredLoggingFixTest.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,65 @@ public async Task Should_Success()
135135
await VerifyCS.VerifyCodeFixAsync(test, expected, fixtest);
136136
}
137137

138+
[TestMethod]
139+
public async Task Should_Success_For_Escape_Interpolation_String()
140+
{
141+
LoggingCodeTemplate test =
142+
"""
143+
var type = _logger.GetType();
144+
_logger.LogInformation({|#0:$"Value: \"{$"Score: {50}"}\" - \"{nameof(type)}\" - \"{$"Today: {DateTime.Now}"}\""|});
145+
""";
146+
147+
LoggingCodeTemplate fixtest =
148+
"""
149+
var type = _logger.GetType();
150+
_logger.LogInformation($"Value: \"{{Value_0}}\" - \"{nameof(type)}\" - \"{{Value_1}}\"", $"Score: {50}", $"Today: {DateTime.Now}");
151+
""";
152+
153+
var expected = GetExpected();
154+
await VerifyCS.VerifyCodeFixAsync(test, expected, fixtest);
155+
}
156+
157+
[TestMethod]
158+
public async Task Should_Success_For_Escape_String()
159+
{
160+
LoggingCodeTemplate test =
161+
"""
162+
var type = _logger.GetType();
163+
_logger.LogInformation({|#0:$"Value: \"{type.Name}\""|});
164+
""";
165+
166+
LoggingCodeTemplate fixtest =
167+
"""
168+
var type = _logger.GetType();
169+
_logger.LogInformation("Value: \"{Name}\"", type.Name);
170+
""";
171+
172+
var expected = GetExpected();
173+
await VerifyCS.VerifyCodeFixAsync(test, expected, fixtest);
174+
}
175+
176+
[TestMethod]
177+
public async Task Should_Success_For_NullCheck_ToString()
178+
{
179+
LoggingCodeTemplate test =
180+
"""
181+
var type = _logger.GetType();
182+
float? value = 1f;
183+
_logger.LogInformation({|#0:$"Value: \"{type?.ToString()}\" - {value?.ToString("D2")} - {type?.Name?.ToString()}"|});
184+
""";
185+
186+
LoggingCodeTemplate fixtest =
187+
"""
188+
var type = _logger.GetType();
189+
float? value = 1f;
190+
_logger.LogInformation("Value: \"{Type}\" - {Value} - {Name}", type, value?.ToString("D2"), type?.Name?.ToString());
191+
""";
192+
193+
var expected = GetExpected();
194+
await VerifyCS.VerifyCodeFixAsync(test, expected, fixtest);
195+
}
196+
138197
[TestMethod]
139198
public async Task Should_Success_With_Log_Exception()
140199
{

0 commit comments

Comments
 (0)