Skip to content

Commit 9b31ca6

Browse files
committed
Escaping variable names in block variable declarations
1 parent f1209e0 commit 9b31ca6

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

ReadableExpressions.UnitTests/WhenTranslatingLanguageKeywords.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace AgileObjects.ReadableExpressions.UnitTests
22
{
3+
using System;
34
using System.Linq.Expressions;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56

@@ -56,5 +57,20 @@ private static void VerifyIsEscaped(string keyword)
5657

5758
Assert.AreEqual("@" + keyword, translated);
5859
}
60+
61+
[TestMethod]
62+
public void ShouldTranslateADeclaredBlockVariableKeyword()
63+
{
64+
var variable = Expression.Variable(typeof(string), "string");
65+
Expression<Action> writeLine = () => Console.WriteLine("La la la");
66+
var block = Expression.Block(new[] { variable }, writeLine.Body);
67+
var translated = block.ToReadableString();
68+
69+
const string EXPECTED = @"
70+
string @string;
71+
Console.WriteLine(""La la la"");";
72+
73+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
74+
}
5975
}
6076
}

ReadableExpressions/ExpressionTranslatorRegistry.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ internal class ExpressionTranslatorRegistry
1111

1212
public ExpressionTranslatorRegistry()
1313
{
14-
var negationTranslator = new NegationExpressionTranslator();
15-
var memberAccessTranslator = new MemberAccessExpressionTranslator();
1614
var defaultTranslator = new DefaultExpressionTranslator();
1715
var assignmentTranslator = new AssignmentExpressionTranslator(defaultTranslator);
16+
var negationTranslator = new NegationExpressionTranslator();
17+
var parameterTranslator = new ParameterExpressionTranslator();
18+
var memberAccessTranslator = new MemberAccessExpressionTranslator();
1819
var indexAccessTranslator = new IndexAccessExpressionTranslator();
1920
var methodCallTranslator = new MethodCallExpressionTranslator(indexAccessTranslator);
2021

@@ -23,7 +24,7 @@ public ExpressionTranslatorRegistry()
2324
new ArrayLengthExpressionTranslator(),
2425
assignmentTranslator,
2526
new BinaryExpressionTranslator(negationTranslator),
26-
new BlockExpressionTranslator(),
27+
new BlockExpressionTranslator(parameterTranslator),
2728
new CastExpressionTranslator(),
2829
new ConditionalExpressionTranslator(),
2930
new ConstantExpressionTranslator(),
@@ -42,7 +43,7 @@ public ExpressionTranslatorRegistry()
4243
negationTranslator,
4344
new NewArrayExpressionTranslator(),
4445
new NewExpressionTranslator(),
45-
new ParameterExpressionTranslator(),
46+
parameterTranslator,
4647
new QuotedLambdaExpressionTranslator(),
4748
new RuntimeVariablesExpressionTranslator(),
4849
new SwitchExpressionTranslator(),

ReadableExpressions/Translators/BlockExpressionTranslator.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace AgileObjects.ReadableExpressions.Translators
22
{
33
using System;
4-
using System.CodeDom;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Linq.Expressions;
@@ -10,9 +9,12 @@ namespace AgileObjects.ReadableExpressions.Translators
109

1110
internal class BlockExpressionTranslator : ExpressionTranslatorBase
1211
{
13-
public BlockExpressionTranslator()
12+
private readonly ParameterExpressionTranslator _variableNameTranslator;
13+
14+
public BlockExpressionTranslator(ParameterExpressionTranslator variableNameTranslator)
1415
: base(ExpressionType.Block)
1516
{
17+
_variableNameTranslator = variableNameTranslator;
1618
}
1719

1820
public override string Translate(Expression expression, TranslationContext context)
@@ -29,15 +31,20 @@ public override string Translate(Expression expression, TranslationContext conte
2931
return string.Join(Environment.NewLine, blockContents);
3032
}
3133

32-
private static IEnumerable<string> GetVariableDeclarations(
34+
private IEnumerable<string> GetVariableDeclarations(
3335
BlockExpression block,
3436
TranslationContext context)
3537
{
3638
return block
3739
.Variables
3840
.Except(context.JoinedAssignmentVariables)
3941
.GroupBy(v => v.Type)
40-
.Select(vGrp => $"{vGrp.Key.GetFriendlyName()} {string.Join(", ", vGrp)};");
42+
.Select(vGrp => new
43+
{
44+
TypeName = vGrp.Key.GetFriendlyName(),
45+
VariableNames = vGrp.Select(varName => _variableNameTranslator.Translate(varName))
46+
})
47+
.Select(varData => $"{varData.TypeName} {string.Join(", ", varData.VariableNames)};");
4148
}
4249

4350
private static IEnumerable<string> GetBlockLines(BlockExpression block, TranslationContext context)

ReadableExpressions/Translators/ParameterExpressionTranslator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ internal ParameterExpressionTranslator()
4141

4242
public override string Translate(Expression expression, TranslationContext context)
4343
{
44-
var parameterName = ((ParameterExpression)expression).Name;
44+
return Translate((ParameterExpression)expression);
45+
}
4546

46-
return _keywords.Contains(parameterName) ? "@" + parameterName : parameterName;
47+
public string Translate(ParameterExpression parameter)
48+
{
49+
return _keywords.Contains(parameter.Name) ? "@" + parameter.Name : parameter.Name;
4750
}
4851
}
4952
}

0 commit comments

Comments
 (0)