Skip to content

Commit c4c86e5

Browse files
committed
Improving detection of statement existing surrounding parentheses
1 parent d33fab6 commit c4c86e5

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

ReadableExpressions.UnitTests/WhenTranslatingConditionals.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,27 @@ public void ShouldBreakLongMultipleConditionsOntoMultipleLines()
456456

457457
Assert.Equal(EXPECTED.TrimStart(), translated);
458458
}
459+
460+
[Fact]
461+
public void ShouldTranslateAssignmentOutcomeTests()
462+
{
463+
var intVariable = Expression.Variable(typeof(int), "i");
464+
var intValue = Expression.Constant(123);
465+
var intAssignment = Expression.Assign(intVariable, intValue);
466+
var intDefault = Expression.Default(typeof(int));
467+
var assignmentResultNotDefault = Expression.NotEqual(intAssignment, intDefault);
468+
var doNothing = Expression.Default(typeof(void));
469+
var ifNotdefaultDoNothing = Expression.IfThen(assignmentResultNotDefault, doNothing);
470+
471+
var translated = ifNotdefaultDoNothing.ToReadableString();
472+
473+
const string EXPECTED = @"
474+
if ((i = 123) != default(int))
475+
{
476+
}";
477+
478+
Assert.Equal(EXPECTED.TrimStart(), translated);
479+
}
459480
}
460481

461482
#region Helpers

ReadableExpressions/StringExtensions.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,36 @@ public static string WithoutUnindents(this string code)
120120
return code;
121121
}
122122

123-
public static string WithSurroundingParentheses(this string value, bool checkExisting = false)
123+
public static string WithSurroundingParentheses(this string value)
124124
{
125-
if (checkExisting && value.HasSurroundingParentheses())
125+
if (!value.HasSurroundingParentheses())
126126
{
127-
return value;
127+
return "(" + value + ")";
128+
}
129+
130+
var openParenthesesCount = 0;
131+
132+
for (var i = 1; i < value.Length - 1; ++i)
133+
{
134+
switch (value[i])
135+
{
136+
case '(':
137+
++openParenthesesCount;
138+
continue;
139+
140+
case ')':
141+
--openParenthesesCount;
142+
143+
if (openParenthesesCount < 0)
144+
{
145+
return "(" + value + ")";
146+
}
147+
148+
continue;
149+
}
128150
}
129151

130-
return $"({value})";
152+
return value;
131153
}
132154

133155
public static string WithoutSurroundingParentheses(this string value, Expression expression)

ReadableExpressions/Translators/BinaryExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static string TranslateOperand(Expression expression, TranslationContext
9292
var translation = context.Translate(expression);
9393

9494
return expression.IsAssignment()
95-
? translation.WithSurroundingParentheses(checkExisting: true)
95+
? translation.WithSurroundingParentheses()
9696
: translation;
9797
}
9898

ReadableExpressions/Translators/Formatting/FormattedCondition.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ public FormattedCondition(
2828
test = test.TrimStart();
2929
}
3030

31-
_singleLineTest = test.WithSurroundingParentheses(CheckExistingParentheses());
31+
_singleLineTest = FinaliseCondition(test.WithSurroundingParentheses());
3232

3333
MultipleLineTranslationFactory = GetMultipleLineTranslation;
3434
}
3535

36-
private bool CheckExistingParentheses() => IsNotRelevantBinary(_condition, out var _);
37-
3836
protected override Func<string> SingleLineTranslationFactory => () => _singleLineTest;
3937

4038
protected override Func<string> MultipleLineTranslationFactory { get; }
@@ -54,7 +52,7 @@ private string GetMultipleLineTranslation()
5452
{conditionLeft} {conditionOperator}
5553
{conditionRight.ToString().Indented()}".TrimStart().WithSurroundingParentheses();
5654

57-
return condition;
55+
return FinaliseCondition(condition);
5856
}
5957

6058
private static bool IsNotRelevantBinary(Expression condition, out BinaryExpression binary)
@@ -72,5 +70,10 @@ private static bool IsNotRelevantBinary(Expression condition, out BinaryExpressi
7270
binary = null;
7371
return true;
7472
}
73+
74+
private static string FinaliseCondition(string condition)
75+
{
76+
return condition;
77+
}
7578
}
7679
}

ReadableExpressions/Translators/Formatting/FormattedTernary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static string GetTest(Expression condition, TranslationContext context)
3535
return test.WithoutSurroundingParentheses(condition);
3636
}
3737

38-
return test.WithSurroundingParentheses(checkExisting: true);
38+
return test.WithSurroundingParentheses();
3939
}
4040

4141
private static string GetBranch(CodeBlock codeBlock)

ReadableExpressions/Translators/NegationExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static string Translate(ExpressionType negationType, Expression expressi
3636

3737
if (WrapNegatedValue(valueToNegate, expression))
3838
{
39-
valueToNegate = valueToNegate.WithSurroundingParentheses(checkExisting: true);
39+
valueToNegate = valueToNegate.WithSurroundingParentheses();
4040
}
4141

4242
return _negationsByNodeType[negationType] + valueToNegate;

0 commit comments

Comments
 (0)