Skip to content

Commit 86e580c

Browse files
author
Kapil Borle
committed
Suggest setting SupportsShouldProcess to true
1 parent 4fbdb3a commit 86e580c

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Tests/Rules/UseSupportsShouldProcess.tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,27 @@ function foo {
268268
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
269269
}
270270

271+
It "Suggests setting SupportsShouldProcess to `$true" {
272+
$def = @'
273+
function foo {
274+
[CmdletBinding(SupportsShouldProcess=$false)]
275+
param($whatif)
276+
}
277+
'@
278+
$s = " "
279+
$expectedCorrection = @'
280+
function foo {
281+
[CmdletBinding(SupportsShouldProcess=$true)]
282+
param()
283+
}
284+
'@
285+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
286+
$violations.Count | Should Be 1
287+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
288+
}
289+
290+
291+
271292

272293
}
273294

rules/UseSupportsShouldProcess.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,22 @@ private List<CorrectionExtent> GetCorrections(
132132
correctionExtents.Add(GetCorrectionToRemoveParam(confirmIndex, parameterAsts));
133133
}
134134

135-
AttributeAst attributeAst;
135+
AttributeAst attributeAst = paramBlockAst.GetCmdletBindingAttributeAst();
136136

137137
// check if it has cmdletbinding attribute
138-
if (TryGetCmdletBindingAttribute(paramBlockAst, out attributeAst))
138+
if (attributeAst != null)
139139
{
140140
NamedAttributeArgumentAst shouldProcessAst = attributeAst.GetSupportsShouldProcessAst();
141141
if (shouldProcessAst != null)
142142
{
143-
143+
ExpressionAst argAst;
144+
if (!shouldProcessAst.IsTrue(out argAst)
145+
&& argAst != null)
146+
{
147+
// SupportsShouldProcess is set to something other than $true.
148+
// Set it to $true
149+
correctionExtents.Add(GetCorrectionsToSetShouldProcessToTrue(argAst));
150+
}
144151
}
145152
else
146153
{
@@ -198,6 +205,17 @@ private List<CorrectionExtent> GetCorrections(
198205
return result;
199206
}
200207

208+
private CorrectionExtent GetCorrectionsToSetShouldProcessToTrue(ExpressionAst argAst)
209+
{
210+
return new CorrectionExtent(
211+
argAst.Extent.StartLineNumber,
212+
argAst.Extent.EndLineNumber,
213+
argAst.Extent.StartColumnNumber,
214+
argAst.Extent.EndColumnNumber,
215+
"$true",
216+
argAst.Extent.File);
217+
}
218+
201219
private CorrectionExtent GetCorrectionToAddParamBlock(
202220
FunctionDefinitionAst funcDefnAst,
203221
ParameterAst[] parameterAsts)
@@ -281,14 +299,6 @@ private CorrectionExtent Normalize(
281299
correctionExtent.Description);
282300
}
283301

284-
private static bool TryGetCmdletBindingAttribute(
285-
ParamBlockAst paramBlockAst,
286-
out AttributeAst attributeAst)
287-
{
288-
attributeAst = paramBlockAst.GetCmdletBindingAttributeAst();
289-
return attributeAst != null;
290-
}
291-
292302
private static CorrectionExtent GetCorrectionToAddAttribute(ParamBlockAst paramBlockAst)
293303
{
294304
return new CorrectionExtent(

0 commit comments

Comments
 (0)