Browse Source

Parenthesize interpolations containing global::

pull/3463/head
ds5678 2 months ago committed by Siegfried Pammer
parent
commit
b6e55eafc3
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 13
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 7
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs
  4. 20
      ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

1
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -137,6 +137,7 @@ @@ -137,6 +137,7 @@
<Compile Include="TestCases\ILPretty\Issue3442.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" />
<Compile Include="TestCases\Pretty\GloballyQualifiedTypeInStringInterpolation.cs" />
<Compile Include="TestCases\Pretty\Issue3406.cs" />
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
<Compile Include="TestCases\Pretty\Issue3439.cs" />

13
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -308,6 +308,19 @@ namespace ICSharpCode.Decompiler.Tests @@ -308,6 +308,19 @@ namespace ICSharpCode.Decompiler.Tests
await RunForLibrary(cscOptions: cscOptions);
}
[Test]
public async Task GloballyQualifiedTypeInStringInterpolation([ValueSource(nameof(roslynOnlyWithNet40Options))] CompilerOptions cscOptions)
{
// https://github.com/icsharpcode/ILSpy/issues/3447
await RunForLibrary(
cscOptions: cscOptions,
configureDecompiler: settings => {
settings.UsingDeclarations = false;
settings.AlwaysUseGlobal = true;
}
);
}
[Test]
public async Task LiftedOperators([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{

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

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public static class GloballyQualifiedTypeInStringInterpolation
{
public static string CurrentDateTime => $"Time: {(global::System.DateTime.Now)}";
}
}

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

@ -389,6 +389,26 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -389,6 +389,26 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
base.VisitAsExpression(asExpression);
}
public override void VisitInterpolation(Interpolation interpolation)
{
// If an interpolation contains global::, we need to parenthesize the expression.
if (IsThisOrChildMemberTypeWithDoubleColon(interpolation))
Parenthesize(interpolation.Expression);
base.VisitInterpolation(interpolation);
static bool IsThisOrChildMemberTypeWithDoubleColon(AstNode node)
{
if (node is MemberType { IsDoubleColon: true })
return true;
foreach (var child in node.Children)
{
if (IsThisOrChildMemberTypeWithDoubleColon(child))
return true;
}
return false;
}
}
// Conditional operator
public override void VisitConditionalExpression(ConditionalExpression conditionalExpression)
{

Loading…
Cancel
Save