Skip to content

Commit aa324dc

Browse files
author
Kapil Borle
committed
Add correction for writing param block
1 parent ce345cc commit aa324dc

3 files changed

Lines changed: 89 additions & 5 deletions

File tree

Engine/TextEdit.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ public TextEdit(
5151
string newText)
5252
: base(startLineNumber, startColumnNumber, endLineNumber, endColumnNumber)
5353
{
54+
// Instead of outputting a text object TextEdit should output an array of strings
55+
// such that each string represents a line. Also for each each line, there should
56+
// be some information to encode the indentation level. Let the client decide the
57+
// new line characters to insert between each line and the indentation type (space or tab)
5458
Text = newText;
5559
}
56-
57-
58-
5960
}
6061
}

Tests/Rules/UseSupportsShouldProcess.tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,27 @@ $s$s$s$s$s$s$s$s
207207
$violations.Count | Should Be 1
208208
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
209209
}
210+
211+
It "Suggests replacing function parameter declaration with a param block" {
212+
$def = @'
213+
function foo ($param1, $whatif, $param2) {
214+
}
215+
'@
216+
$s = " "
217+
$expectedCorrection = @'
218+
function foo {
219+
[CmdletBinding(SupportsShouldProcess)]
220+
param(
221+
$param1,
222+
$param2
223+
)
224+
}
225+
'@
226+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
227+
$violations.Count | Should Be 1
228+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
229+
}
230+
210231
}
232+
211233
}

rules/UseSupportsShouldProcess.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
using System;
1212
using System.Linq;
13+
using System.Text;
1314
using System.Collections.Generic;
1415
#if !CORECLR
1516
using System.ComponentModel.Composition;
1617
#endif
1718
using System.Globalization;
1819
using System.Management.Automation.Language;
1920
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
21+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;
2022

2123
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2224
{
@@ -188,7 +190,8 @@ private List<CorrectionExtent> GetCorrections(
188190
// remove the parameter list
189191
// and create an equivalent param block
190192
// add cmdletbinding attribute and add supportsshouldprocess to it.
191-
correctionExtents.Add(GetCorrectionsExtentRemoveParams(funcDefnAst, ast, tokens));
193+
correctionExtents.Add(GetCorrectionExtentRemoveParams(funcDefnAst, ast, tokens));
194+
correctionExtents.Add(GetCorrectionExtentAddParamBlock(funcDefnAst, parameterAsts));
192195
}
193196

194197
// sort in descending order of start position
@@ -222,7 +225,65 @@ private List<CorrectionExtent> GetCorrections(
222225
// and then give the corrected extent as suggested correction.
223226
}
224227

225-
private static CorrectionExtent GetCorrectionsExtentRemoveParams(
228+
private CorrectionExtent GetCorrectionExtentAddParamBlock(
229+
FunctionDefinitionAst funcDefnAst,
230+
ParameterAst[] parameterAsts)
231+
{
232+
var funcStartScriptPos = funcDefnAst.Extent.StartScriptPosition;
233+
var paramBlockText = WriteParamBlock(parameterAsts);
234+
// TODO replace this hard coding
235+
var indentation = new string(' ', funcStartScriptPos.ColumnNumber + 3);
236+
var sb = new StringBuilder();
237+
sb.Append("{");
238+
sb.AppendLine();
239+
sb.Append(String.Join(
240+
Environment.NewLine,
241+
paramBlockText.GetLines().Select(line => indentation + line)));
242+
243+
return new CorrectionExtent(
244+
funcDefnAst.Body.Extent.StartLineNumber,
245+
funcDefnAst.Body.Extent.StartLineNumber,
246+
funcDefnAst.Body.Extent.StartColumnNumber,
247+
funcDefnAst.Body.Extent.StartColumnNumber + 1,
248+
sb.ToString(),
249+
funcDefnAst.Extent.File);
250+
}
251+
252+
private string WriteParamBlock(ParameterAst[] parameterAsts)
253+
{
254+
var sb = new StringBuilder();
255+
sb.AppendLine("[CmdletBinding(SupportsShouldProcess)]");
256+
sb.AppendLine("param(");
257+
// TODO replace this hard coding
258+
string indentation = new string(' ', 4);
259+
int count = 0;
260+
foreach (var paramAst in parameterAsts)
261+
{
262+
count++;
263+
if (IsWhatIf(paramAst) || IsConfirm(paramAst))
264+
{
265+
continue;
266+
}
267+
268+
foreach (var line in paramAst.Extent.Text.GetLines())
269+
{
270+
sb.Append(indentation);
271+
sb.Append(line);
272+
}
273+
274+
if (count != parameterAsts.Length)
275+
{
276+
sb.AppendLine(",");
277+
}
278+
}
279+
280+
sb.AppendLine();
281+
sb.AppendLine(")");
282+
return sb.ToString();
283+
}
284+
285+
286+
private static CorrectionExtent GetCorrectionExtentRemoveParams(
226287
FunctionDefinitionAst funcDefnAst,
227288
Ast ast,
228289
Token[] tokens)

0 commit comments

Comments
 (0)