Skip to content

Commit 9e15313

Browse files
committed
Fixing unnamed root ParameterExpression translation / Try+Catching visualizer translation exceptions, re: #100
1 parent 1ac7952 commit 9e15313

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

ReadableExpressions.UnitTests/WhenTranslatingVariableNames.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public void ShouldNameAnUnnamedVariable()
2424
translated.ShouldBe("@int = default(int)");
2525
}
2626

27+
// See https://github.com/agileobjects/ReadableExpressions/issues/100
28+
[Fact]
29+
public void ShouldNameARootUnnamedVariable()
30+
{
31+
var intVariable = Variable(typeof(int));
32+
33+
var translated = intVariable.ToReadableString();
34+
35+
translated.ShouldBe("@int");
36+
}
37+
2738
[Fact]
2839
public void ShouldNameAnUnnamedParameter()
2940
{

ReadableExpressions.Visualizers.ObjectSource/ExpressionVisualizerObjectSource.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,34 @@ public static void GetData(
1717
Stream outgoingData,
1818
Action<Stream, string> serializer)
1919
{
20-
var translated = GetTranslationFor(target) ?? "default(void)";
20+
string translated;
21+
22+
try
23+
{
24+
translated = GetTranslationFor(target) ?? "default(void)";
25+
}
26+
catch (Exception ex)
27+
{
28+
translated = $@"</pre>
29+
<h1>Ut-oh</h1>
30+
<p>
31+
An exception occurred translating that
32+
<span class=""tn"">{target?.GetType().Name ?? "target"}</span>.
33+
</p>
34+
<p>
35+
Please report this error with the stack trace below using
36+
<a href=""https://github.com/agileobjects/ReadableExpressions/issues/new"" target=""_blank"">
37+
https://github.com/agileobjects/ReadableExpressions/issues/new
38+
</a>.
39+
</p>
40+
41+
<p>Thanks! (and sorry about that)<br />Steve</p>
42+
43+
<hr />
44+
45+
<p>{ex}</p>
46+
<pre>".TrimStart();
47+
}
2148

2249
serializer.Invoke(outgoingData, translated);
2350
}
@@ -54,7 +81,7 @@ internal static string GetTranslationFor(object target)
5481
private static string Translate(Type type)
5582
=> type.ToReadableString(ApplyDialogSettings);
5683

57-
private static ITranslationSettings ApplyDialogSettings(ITranslationSettings settings)
84+
private static ITranslationSettings ApplyDialogSettings(ITranslationSettings settings)
5885
=> GetDialogSettings().Update(settings.FormatUsing(_htmlFormatter));
5986

6087
private static VisualizerDialogSettings GetDialogSettings()

ReadableExpressions/ExpressionAnalysis.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected virtual void Analyse(Expression expression)
8080
{
8181
case DebugInfo:
8282
case Default:
83-
case Parameter:
83+
case Parameter when ((ParameterExpression)expression).IsNamed():
8484
case RuntimeVariables:
8585
ResultExpression = expression;
8686
break;
@@ -234,8 +234,8 @@ public bool IsPartOfMethodCallChain(MethodCallExpression methodCall)
234234
/// </summary>
235235
/// <param name="variable">The variable for which to get the 1-based index.</param>
236236
/// <returns>
237-
/// The 1-based index of the given <paramref name="variable"/>, or null if only variable of
238-
/// this Type is declared.
237+
/// The 1-based index of the given <paramref name="variable"/>, or null if only one variable
238+
/// of this Type is declared.
239239
/// </returns>
240240
public int? GetUnnamedVariableNumberOrNull(ParameterExpression variable)
241241
{

ReadableExpressions/Extensions/InternalExpressionExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
internal static class InternalExpressionExtensions
1818
{
19+
public static bool IsNamed(this ParameterExpression parameter)
20+
=> !parameter.Name.IsNullOrWhiteSpace();
21+
1922
public static bool HasReturnType(this Expression expression)
2023
=> expression.Type != typeof(void);
2124

ReadableExpressions/Translations/ParameterTranslation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal static class ParameterTranslation
1414
{
1515
public static ITranslation For(ParameterExpression parameter, ITranslationContext context)
1616
{
17-
if (parameter.Name.IsNullOrWhiteSpace())
17+
if (!parameter.IsNamed())
1818
{
1919
return new UnnamedParameterTranslation(parameter, context);
2020
}

0 commit comments

Comments
 (0)