Skip to content

Commit 016f0d4

Browse files
author
Kapil Borle
committed
Fix processing Range parameter
1 parent a101343 commit 016f0d4

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using System;
1414
using System.Globalization;
15+
using System.Linq;
1516
using System.Management.Automation;
1617

1718
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
@@ -77,20 +78,17 @@ protected override void BeginProcessing()
7778
}
7879
#endif
7980

80-
if (Range != null)
81+
try
8182
{
82-
try
83-
{
84-
range = GetRange(Range);
85-
}
86-
catch (Exception e)
87-
{
88-
this.ThrowTerminatingError(new ErrorRecord(
89-
e,
90-
"RANGE_ERROR",
91-
ErrorCategory.InvalidArgument,
92-
Range));
93-
}
83+
range = GetRange();
84+
}
85+
catch (Exception e)
86+
{
87+
this.ThrowTerminatingError(new ErrorRecord(
88+
e,
89+
"RANGE_ERROR",
90+
ErrorCategory.InvalidArgument,
91+
Range));
9492
}
9593

9694
try
@@ -126,26 +124,50 @@ protected override void ProcessRecord()
126124
this.WriteObject(formattedScriptDefinition);
127125
}
128126

129-
private static Range GetRange(object rangeObj)
127+
private Range GetRange()
130128
{
131-
var range = rangeObj as Range;
132-
if (range == null)
129+
if (Range == null)
130+
{
131+
return null;
132+
}
133+
134+
var range = Range as Range;
135+
if (range != null)
136+
{
137+
return range;
138+
}
139+
140+
var objArr = Range as object[];
141+
int[] intArr;
142+
if (objArr == null)
133143
{
134-
var intArr = rangeObj as int[];
144+
// todo check passing int[] casted parameter
145+
intArr = Range as int[];
135146
if (intArr == null)
136147
{
137-
throw new ArgumentException("Argument should be of type Range or int[].");
148+
throw new ArgumentException(
149+
"Argument should be of type Range or object[] or int[].",
150+
nameof(Range));
138151
}
152+
}
139153

140-
if (intArr.Length != 4)
141-
{
142-
throw new ArgumentException("Integer array should be of length 4.");
143-
}
154+
if (objArr.Length != 4)
155+
{
156+
throw new ArgumentException(
157+
"Array should be of length 4.",
158+
nameof(Range));
159+
}
144160

145-
range = new Range(intArr[0], intArr[1], intArr[2], intArr[3]);
161+
if (!objArr.All(x => x is int))
162+
{
163+
throw new ArgumentException(
164+
"Array should contain integer elements.",
165+
nameof(Range));
146166
}
147167

148-
return range;
168+
intArr = new int[objArr.Length];
169+
objArr.CopyTo(intArr, 0);
170+
return new Range(intArr[0], intArr[1], intArr[2], intArr[3]);
149171
}
150172

151173
private void ValidateInputSettings()

0 commit comments

Comments
 (0)