Skip to content

Commit d1b050e

Browse files
committed
Removing parenthesis wrapping from negated method calls
1 parent 9b31ca6 commit d1b050e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

ReadableExpressions.UnitTests/WhenTranslatingMemberAccesses.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ public void ShouldTranslateAnOutParameterMethodCallWithoutGenericArgumentInclude
291291
Assert.AreEqual("new ValueConverter().TryConvert(i, out result)", translated);
292292
}
293293

294+
[TestMethod]
295+
public void ShouldTranslateANegatedMethodCall()
296+
{
297+
Expression<Func<List<int>, bool>> listDoesNotContainZero =
298+
l => !l.Contains(0, EqualityComparer<int>.Default);
299+
300+
var translated = listDoesNotContainZero.Body.ToReadableString();
301+
302+
Assert.AreEqual("!l.Contains(0, EqualityComparer<int>.Default)", translated);
303+
}
304+
294305
[TestMethod]
295306
public void ShouldNotIncludeCapturedInstanceNames()
296307
{

ReadableExpressions/Translators/NegationExpressionTranslator.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,22 @@ private static string Translate(ExpressionType negationType, Expression expressi
3434
{
3535
var valueToNegate = context.Translate(expression);
3636

37-
if (valueToNegate.Contains(" "))
37+
if (WrapNegatedValue(valueToNegate, expression))
3838
{
3939
valueToNegate = valueToNegate.WithSurroundingParentheses(checkExisting: true);
4040
}
4141

4242
return _negationsByNodeType[negationType] + valueToNegate;
4343
}
44+
45+
private static bool WrapNegatedValue(string value, Expression expression)
46+
{
47+
if (expression.NodeType == ExpressionType.Call)
48+
{
49+
return false;
50+
}
51+
52+
return value.Contains(" ");
53+
}
4454
}
4555
}

0 commit comments

Comments
 (0)