Skip to content

Commit 094adbe

Browse files
committed
Transating checked assignments using checked {} code blocks
1 parent 9dbdc5d commit 094adbe

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

ReadableExpressions.UnitTests/WhenTranslatingAssignments.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ShouldTranslateACheckedAdditionAssignment()
3939

4040
var translated = addTenAndAssign.ToReadableString();
4141

42-
Assert.AreEqual("i += 10", translated);
42+
Assert.AreEqual("checked { i += 10 }", translated);
4343
}
4444

4545
[TestMethod]
@@ -61,7 +61,7 @@ public void ShouldTranslateACheckedSubtractionAssignment()
6161

6262
var translated = substractOneAndAssign.ToReadableString();
6363

64-
Assert.AreEqual("i -= 1", translated);
64+
Assert.AreEqual("checked { i -= 1 }", translated);
6565
}
6666

6767
[TestMethod]
@@ -83,7 +83,7 @@ public void ShouldTranslateACheckedMultiplicationAssignment()
8383

8484
var translated = doubleAndAssign.ToReadableString();
8585

86-
Assert.AreEqual("i *= 2", translated);
86+
Assert.AreEqual("checked { i *= 2 }", translated);
8787
}
8888

8989
[TestMethod]

ReadableExpressions/StringExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static string Indent(this string line)
3838
return string.Empty;
3939
}
4040

41-
if (line.Contains(Environment.NewLine))
41+
if (line.IsMultiLine())
4242
{
4343
return string.Join(
4444
Environment.NewLine,
@@ -48,6 +48,8 @@ public static string Indent(this string line)
4848
return IndentSpaces + line;
4949
}
5050

51+
public static bool IsMultiLine(this string value) => value.Contains(Environment.NewLine);
52+
5153
private const string UnindentPlaceholder = "*unindent*";
5254

5355
public static string Unindented(this string line)

ReadableExpressions/Translators/AssignmentExpressionTranslator.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace AgileObjects.ReadableExpressions.Translators
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56
using System.Linq.Expressions;
@@ -26,6 +27,11 @@ internal class AssignmentExpressionTranslator : ExpressionTranslatorBase
2627
[ExpressionType.SubtractAssignChecked] = "-="
2728
};
2829

30+
private static readonly ExpressionType[] _checkedAssignmentTypes = _symbolsByNodeType
31+
.Keys
32+
.Where(nt => nt.ToString().EndsWith("Checked", StringComparison.Ordinal))
33+
.ToArray();
34+
2935
private readonly DefaultExpressionTranslator _defaultTranslator;
3036

3137
internal AssignmentExpressionTranslator(DefaultExpressionTranslator defaultTranslator)
@@ -54,7 +60,11 @@ internal string GetAssignment(
5460
? _defaultTranslator.Translate((DefaultExpression)value)
5561
: GetValueTranslation(value, context);
5662

57-
return $"{target} {symbol} {valueString}";
63+
var assignment = $"{target} {symbol} {valueString}";
64+
65+
assignment = AdjustForCheckedAssignmentIfAppropriate(assignment, assignmentType);
66+
67+
return assignment;
5868
}
5969

6070
private string GetValueTranslation(Expression value, TranslationContext context)
@@ -73,5 +83,26 @@ private string GetValueTranslation(Expression value, TranslationContext context)
7383

7484
return valueBlock.WithReturn().WithParentheses();
7585
}
86+
87+
private static string AdjustForCheckedAssignmentIfAppropriate(
88+
string assignment,
89+
ExpressionType assignmentType)
90+
{
91+
if (!_checkedAssignmentTypes.Contains(assignmentType))
92+
{
93+
return assignment;
94+
}
95+
96+
if (assignment.IsMultiLine())
97+
{
98+
return $@"
99+
checked
100+
{{
101+
{assignment.Indent()}
102+
}}".TrimStart();
103+
}
104+
105+
return $"checked {{ {assignment} }}";
106+
}
76107
}
77108
}

ReadableExpressions/Translators/Formatting/FormattableExpressionBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected string GetFormattedTranslation()
1717
{
1818
var translation = SingleLineTranslationFactory.Invoke();
1919

20-
return (translation.Length > 100) || translation.Contains(Environment.NewLine)
20+
return (translation.Length > 100) || translation.IsMultiLine()
2121
? MultipleLineTranslationFactory.Invoke()
2222
: translation;
2323
}

0 commit comments

Comments
 (0)