Browse Source

Implement revisions

* Update Lambda1 to be invariant
* Visit descendents before deciding whether or not to parenthesize an interpolation expression
* Rename local function
* Remove branch for conditional expressions
* Handle Lambda expressions without a block body
* Check for parenthesized expressions
pull/3463/head
ds5678 2 months ago
parent
commit
2f00eef4ee
  1. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs
  2. 20
      ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs

@ -4,7 +4,11 @@ @@ -4,7 +4,11 @@
{
public static string Root => $"Prefix {(global::System.DateTime.Now)} suffix";
public static string Cast => $"Prefix {((int)global::System.DateTime.Now.Ticks)} suffix";
#if CS100 && !NET40
public static string Lambda1 => $"Prefix {(() => global::System.DateTime.Now)} suffix";
#else
public static string Lambda1 => $"Prefix {(global::System.Func<global::System.DateTime>)(() => global::System.DateTime.Now)} suffix";
#endif
public static string Lambda2 => $"Prefix {((global::System.Func<global::System.DateTime>)(() => global::System.DateTime.Now))()} suffix";
public static string Method1 => $"Prefix {M(global::System.DateTime.Now)} suffix";
public static string Method2 => $"Prefix {(global::System.DateTime.Now.Ticks)} suffix";

20
ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

@ -392,28 +392,30 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -392,28 +392,30 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
public override void VisitInterpolation(Interpolation interpolation)
{
// Need to do this first, in case the descendents parenthesize themselves.
base.VisitInterpolation(interpolation);
// If an interpolation contains global::, we need to parenthesize the expression.
if (InterpolationNeedsParenthesized(interpolation))
if (InterpolationNeedsParenthesis(interpolation))
Parenthesize(interpolation.Expression);
base.VisitInterpolation(interpolation);
static bool InterpolationNeedsParenthesized(AstNode node)
static bool InterpolationNeedsParenthesis(AstNode node)
{
if (node is MemberType { IsDoubleColon: true })
return true;
if (node is ConditionalExpression)
return false; // Conditional expressions are parenthesized in their own visit method.
if (node is AnonymousMethodExpression or LambdaExpression)
if (node is ParenthesizedExpression)
return false;
if (node is AnonymousMethodExpression or LambdaExpression { Body: BlockStatement })
return false;
if (node is InvocationExpression invocation)
return InterpolationNeedsParenthesized(invocation.Target);
return InterpolationNeedsParenthesis(invocation.Target);
if (node is CastExpression cast)
return InterpolationNeedsParenthesized(cast.Expression);
return InterpolationNeedsParenthesis(cast.Expression);
foreach (var child in node.Children)
{
if (InterpolationNeedsParenthesized(child))
if (InterpolationNeedsParenthesis(child))
return true;
}
return false;

Loading…
Cancel
Save