Skip to content

Commit 0d5b12f

Browse files
author
Kapil Borle
committed
Remove checking for ShouldContinue
If a cmdlet declares SupportsShouldProcess attribute, it must call ShouldProcess but calling ShouldContinue is optional.
1 parent bdb3ef4 commit 0d5b12f

File tree

3 files changed

+30
-58
lines changed

3 files changed

+30
-58
lines changed

RuleDocumentation/UseShouldProcessCorrectly.md

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
**Severity Level: Warning**
33

44
##Description
5-
Checks that if the `SupportsShouldProcess` is present, i.e, `[CmdletBinding(SupportsShouldProcess = $true)]`, and then tests if the function or cmdlet calls
6-
ShouldProcess or ShouldContinue; i.e `$PSCmdlet.ShouldProcess` or `$PSCmdlet.ShouldContinue`.
5+
If a cmdlet declares the `SupportsShouldProcess` attribute, then it should also call `ShouldProcess`. A violation is any function which either declares `SupportsShouldProcess` attribute but makes no calls to `ShouldProcess` or it calls `ShouldProcess` but does not does not declare `SupportsShouldProcess`
76

8-
A violation is any function where `SupportsShouldProcess` that makes no calls to `ShouldProcess` or `ShouldContinue`.
9-
10-
Scripts with one or the other but not both will generally run into an error or unexpected behavior.
7+
For more information, please refer to `about_Functions_Advanced_Methods` and `about_Functions_CmdletBindingAttribute`
118

129
##How to Fix
13-
To fix a violation of this rule, please call `ShouldProcess` method in advanced functions when `SupportsShouldProcess` argument is present.
14-
Or please add `SupportsShouldProcess` argument when calling `ShouldProcess`.
15-
You can get more details by running `Get-Help about_Functions_CmdletBindingAttribute` and `Get-Help about_Functions_Advanced_Methods` command in Windows PowerShell.
10+
To fix a violation of this rule, please call `ShouldProcess` method when a cmdlet declares `SupportsShouldProcess` attribute. Or please add `SupportsShouldProcess` attribute argument when calling `ShouldProcess`.
1611

1712
##Example
1813
###Wrong:
@@ -26,17 +21,7 @@ You can get more details by running `Get-Help about_Functions_CmdletBindingAttri
2621
[Parameter(Mandatory=$true)]
2722
$Path
2823
)
29-
30-
Begin
31-
{
32-
}
33-
Process
34-
{
35-
"String" | Out-File -FilePath $FilePath
36-
}
37-
End
38-
{
39-
}
24+
"String" | Out-File -FilePath $FilePath
4025
}
4126
```
4227

@@ -52,22 +37,13 @@ You can get more details by running `Get-Help about_Functions_CmdletBindingAttri
5237
$Path
5338
)
5439
55-
Begin
56-
{
57-
}
58-
Process
59-
{
60-
if ($PSCmdlet.ShouldProcess("Target", "Operation"))
61-
{
62-
"String" | Out-File -FilePath $FilePath
63-
}
64-
}
65-
End
66-
{
67-
if ($pscmdlet.ShouldContinue("Yes", "No"))
68-
{
69-
...
70-
}
71-
}
40+
if ($PSCmdlet.ShouldProcess("Target", "Operation"))
41+
{
42+
"String" | Out-File -FilePath $FilePath
43+
}
44+
else
45+
{
46+
Write-Host ('Write "String" to file {0}' -f $FilePath)
47+
}
7248
}
7349
```

Rules/UseShouldProcessCorrectly.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ public class UseShouldProcessCorrectly : IScriptRule
3737
private List<DiagnosticRecord> diagnosticRecords;
3838

3939
private readonly Vertex shouldProcessVertex;
40-
private readonly Vertex shouldContinueVertex;
41-
4240

4341
public UseShouldProcessCorrectly()
4442
{
4543
diagnosticRecords = new List<DiagnosticRecord>();
46-
shouldContinueVertex = new Vertex {name="ShouldContinue", ast=null};
4744
shouldProcessVertex = new Vertex {name="ShouldProcess", ast=null};
4845
}
4946

@@ -143,8 +140,7 @@ private void FindViolations()
143140
/// <returns>An instance of DiagnosticRecord if it find violation, otherwise null</returns>
144141
private DiagnosticRecord GetViolation(Vertex v)
145142
{
146-
bool callsShouldProcess = funcDigraph.IsConnected(v, shouldContinueVertex)
147-
|| funcDigraph.IsConnected(v, shouldProcessVertex);
143+
bool callsShouldProcess = funcDigraph.IsConnected(v, shouldProcessVertex);
148144
FunctionDefinitionAst fast = v.ast as FunctionDefinitionAst;
149145
if (fast == null)
150146
{

Tests/Rules/GoodCmdlet.ps1

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#>
2323
function Get-File
2424
{
25-
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
26-
SupportsShouldProcess=$true,
25+
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
26+
SupportsShouldProcess=$true,
2727
PositionalBinding=$false,
2828
HelpUri = 'http://www.microsoft.com/',
2929
ConfirmImpact='Medium')]
@@ -34,17 +34,17 @@ function Get-File
3434
Param
3535
(
3636
# Param1 help description
37-
[Parameter(Mandatory=$true,
37+
[Parameter(Mandatory=$true,
3838
ValueFromPipeline=$true,
39-
ValueFromPipelineByPropertyName=$true,
40-
ValueFromRemainingArguments=$false,
39+
ValueFromPipelineByPropertyName=$true,
40+
ValueFromRemainingArguments=$false,
4141
Position=0,
4242
ParameterSetName='Parameter Set 1')]
4343
[ValidateNotNull()]
4444
[ValidateNotNullOrEmpty()]
4545
[ValidateCount(0,5)]
4646
[ValidateSet("sun", "moon", "earth")]
47-
[Alias("p1")]
47+
[Alias("p1")]
4848
$Param1,
4949

5050
# Param2 help description
@@ -131,8 +131,8 @@ function Get-File
131131
#>
132132
function Get-Folder
133133
{
134-
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
135-
SupportsShouldProcess,
134+
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
135+
SupportsShouldProcess,
136136
PositionalBinding=$false,
137137
HelpUri = 'http://www.microsoft.com/',
138138
ConfirmImpact='Medium')]
@@ -143,17 +143,17 @@ function Get-Folder
143143
Param
144144
(
145145
# Param1 help description
146-
[Parameter(Mandatory=$true,
146+
[Parameter(Mandatory=$true,
147147
ValueFromPipeline=$true,
148-
ValueFromPipelineByPropertyName=$true,
149-
ValueFromRemainingArguments=$false,
148+
ValueFromPipelineByPropertyName=$true,
149+
ValueFromRemainingArguments=$false,
150150
Position=0,
151151
ParameterSetName='Parameter Set 1')]
152152
[ValidateNotNull()]
153153
[ValidateNotNullOrEmpty()]
154154
[ValidateCount(0,5)]
155155
[ValidateSet("sun", "moon", "earth")]
156-
[Alias("p1")]
156+
[Alias("p1")]
157157
$Param1,
158158

159159
# Param2 help description
@@ -279,13 +279,13 @@ function Get-Reserved*
279279

280280
<#
281281
.Synopsis
282-
function that has a noun that is singular
282+
function that has a noun that is singular
283283
.DESCRIPTION
284-
284+
285285
.EXAMPLE
286-
286+
287287
.EXAMPLE
288-
288+
289289
#>
290290
function Get-MyWidgetStatus
291291
{
@@ -299,7 +299,7 @@ function Get-MyFood
299299

300300
process
301301
{
302-
if ($PSCmdlet.ShouldContinue("Are you sure?"))
302+
if ($PSCmdlet.ShouldProcess(("Are you sure?")))
303303
{
304304
}
305305
}

0 commit comments

Comments
 (0)