Skip to content

Commit f11c3fd

Browse files
committed
Fixing params array translation for methods with multiple parameters
1 parent 316a247 commit f11c3fd

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

ReadableExpressions.UnitTests/WhenTranslatingMemberAccesses.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@ public void ShouldTranslateAnEmptyParamsArrayArgument()
159159
translated.ShouldBe("str => ParamsHelper.OptionalParams(str)");
160160
}
161161

162+
[Fact]
163+
public void ShouldTranslateMultiParameterCallParamsArrayArgument()
164+
{
165+
var stringJoinMethod = typeof(string)
166+
.GetPublicStaticMethods("Join")
167+
.First(m =>
168+
(m.GetParameters().Length == 2) &&
169+
(m.GetParameters()[0].ParameterType == typeof(string)) &&
170+
(m.GetParameters()[1].ParameterType == typeof(string[])));
171+
172+
var stringEmpty = Expression.Field(null, typeof(string).GetPublicStaticField("Empty"));
173+
var expressions = new Expression[] { Expression.Constant("Value["), Expression.Constant("0"), Expression.Constant("]") };
174+
var newStringArray = Expression.NewArrayInit(typeof(string), expressions);
175+
176+
var stringJoinCall = Expression.Call(stringJoinMethod, stringEmpty, newStringArray);
177+
178+
var translated = stringJoinCall.ToReadableString();
179+
180+
// string.Join(string, string[]) in .NET 3.5 doesn't take a params array:
181+
#if NET35
182+
const string EXPECTED = @"
183+
string.Join(string.Empty, new[] { ""Value["", ""0"", ""]"" })";
184+
#else
185+
const string EXPECTED = @"
186+
string.Join(
187+
string.Empty,
188+
""Value["",
189+
""0"",
190+
""]"")";
191+
#endif
192+
193+
translated.ShouldBe(EXPECTED.TrimStart());
194+
195+
}
196+
162197
[Fact]
163198
public void ShouldTranslateAnIndexedPropertyAccessExpression()
164199
{

ReadableExpressions/Translations/ParameterSetTranslation.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,14 @@ private ParameterSetTranslation(
119119

120120
if (methodProvided)
121121
{
122-
// If a parameter is a params array then index will increase
123-
// past parameterCount, so adjust here:
124-
var parameterIndex = (ParameterCount != parameterCount)
125-
? ParameterCount - parameterCount - index
126-
: index;
122+
var parameterIndex = index;
123+
124+
if (ParameterCount != parameterCount)
125+
{
126+
// If a parameter is a params array then index will increase
127+
// past parameterCount, so adjust here:
128+
parameterIndex -= ParameterCount - parameterCount;
129+
}
127130

128131
// ReSharper disable once PossibleNullReferenceException
129132
translation = GetParameterTranslation(p, methodParameters[parameterIndex], context);

0 commit comments

Comments
 (0)