Skip to content

Commit 704c2a8

Browse files
author
Maksim Volkau
committed
exposed enclosedIn
1 parent 89a1cbb commit 704c2a8

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9943,16 +9943,15 @@ public static string ToCSharpString(this Expression expr) =>
99439943
expr.ToCSharpString(new StringBuilder(1024), stripNamespace: true).Append(';').ToString();
99449944

99459945
/// <summary>Tries hard to convert the expression into the valid C# code</summary>
9946-
public static string ToCSharpString(this Expression expr, ObjectToCode notRecognizedToCode) =>
9946+
public static string ToCSharpString(this Expression expr, ObjectToCode notRecognizedToCode, EnclosedIn enclosedIn = EnclosedIn.AvoidParens) =>
99479947
expr.ToCSharpString(new StringBuilder(1024), stripNamespace: true, notRecognizedToCode: notRecognizedToCode).Append(';').ToString();
99489948

99499949
/// <summary>Tries hard to convert the expression into the valid C# code</summary>
9950-
public static StringBuilder ToCSharpString(this Expression e, StringBuilder sb,
9950+
public static StringBuilder ToCSharpString(this Expression e, StringBuilder sb, EnclosedIn enclosedIn = EnclosedIn.ParensByDefault,
99519951
int lineIndent = 0, bool stripNamespace = false, Func<Type, string, string> printType = null, int indentSpaces = 4, ObjectToCode notRecognizedToCode = null)
99529952
{
99539953
SmallList<NamedWithIndex, Stack4<NamedWithIndex>, NoArrayPool<NamedWithIndex>> named = default;
9954-
return e.ToCSharpString(sb, EnclosedIn.ParensByDefault, ref named,
9955-
lineIndent, stripNamespace, printType, indentSpaces, notRecognizedToCode);
9954+
return e.ToCSharpString(sb, enclosedIn, ref named, lineIndent, stripNamespace, printType, indentSpaces, notRecognizedToCode);
99569955
}
99579956

99589957
/// <summary>Indicates the expression container</summary>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#if NET8_0_OR_GREATER
2+
3+
using System;
4+
using System.Reflection.Emit;
5+
using System.Text.Json;
6+
7+
#if LIGHT_EXPRESSION
8+
using FastExpressionCompiler.LightExpression.ImTools;
9+
using static FastExpressionCompiler.LightExpression.Expression;
10+
namespace FastExpressionCompiler.LightExpression.IssueTests;
11+
#else
12+
using System.Linq.Expressions;
13+
using FastExpressionCompiler.ImTools;
14+
using static System.Linq.Expressions.Expression;
15+
namespace FastExpressionCompiler.IssueTests;
16+
#endif
17+
18+
public struct Issue487_Fix_ToCSharpString_output_for_boolean_equality_expressions : ITestX
19+
{
20+
public void Run(TestRun t)
21+
{
22+
Original_case(t);
23+
}
24+
25+
public class TestClass
26+
{
27+
public required bool MyTestBool { get; set; }
28+
}
29+
30+
public void Original_case(TestContext t)
31+
{
32+
var input = new TestClass() { MyTestBool = true };
33+
var parameter = Expression.Parameter(input.GetType(), "x");
34+
var property = Expression.Property(parameter, nameof(TestClass.MyTestBool));
35+
var expr = Expression.Equal(property, Expression.Constant(true));
36+
37+
var str = expr.ToString(); // => (x.MyTestBool == True)
38+
var cs = expr.ToCSharpString(); // => (x.MyTestBool;
39+
40+
expr.PrintCSharp();
41+
42+
// var fs = expr.CompileSys();
43+
// fs.PrintIL(format: ILDecoder.ILFormat.AssertOpCodes);
44+
45+
// var ff = expr.CompileFast(false);
46+
// ff.PrintIL(format: ILDecoder.ILFormat.AssertOpCodes);
47+
}
48+
}
49+
#endif

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public static void Main()
2525
// new Issue441_Fails_to_pass_Constant_as_call_parameter_by_reference().Run();
2626
// new Issue461_InvalidProgramException_when_null_checking_type_by_ref().Run();
2727

28+
29+
var st = new TestRun();
30+
#if NET8_0_OR_GREATER
31+
st.Run(new Issue487_Fix_ToCSharpString_output_for_boolean_equality_expressions());
32+
st.Run(new Issue475_Reuse_DynamicMethod_if_possible());
33+
#endif
34+
2835
var lt = new LightExpression.TestRun(LightExpression.TestFlags.RethrowException);
2936

3037
#if NET8_0_OR_GREATER
@@ -37,11 +44,6 @@ public static void Main()
3744
lt.Run(new LightExpression.IssueTests.Issue473_InvalidProgramException_when_using_Expression_Condition_with_converted_decimal_expression());
3845
lt.Run(new LightExpression.IssueTests.Issue476_System_ExecutionEngineException_with_nullables_on_repeated_calls_to_ConcurrentDictionary());
3946

40-
#if NET8_0_OR_GREATER
41-
var st = new TestRun();
42-
st.Run(new Issue475_Reuse_DynamicMethod_if_possible());
43-
#endif
44-
4547
RunAllTests();
4648
}
4749

0 commit comments

Comments
 (0)