Skip to content

Commit e63edce

Browse files
committed
Merge branch 'NewRule' into BugFixes
2 parents 178f484 + 1f523c6 commit e63edce

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#UseShouldProcessForStateChangingFunctions
2+
**Severity Level: Warning**
3+
4+
##Description
5+
6+
Functions that have verbs like New, Start, Stop, Set, Reset and Restart that change system state should support 'ShouldProcess'
7+
8+
##How to Fix
9+
10+
To fix a violation of this rule, please add attribute SupportsShouldProcess. eg: [CmdletBinding(SupportsShouldProcess = $true)] to the function.
11+
12+
##Example
13+
14+
Wrong:
15+
```
16+
function Set-ServiceObject
17+
{
18+
[CmdletBinding()]
19+
param ([string]$c)
20+
}
21+
```
22+
23+
Correct:
24+
```
25+
function Set-ServiceObject
26+
{
27+
[CmdletBinding(SupportsShouldProcess = $true)]
28+
param ([string]$c)
29+
}
30+
```

Rules/Strings.Designer.cs

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@
658658
<value>Use ShouldProcess For State Changing Functions</value>
659659
</data>
660660
<data name="UseShouldProcessForStateChangingFunctionsDescrption" xml:space="preserve">
661-
<value>Functions that have verbs like New, Start, Stop, Set, Reset that change system state should support 'ShouldProcess'.</value>
661+
<value>Functions that have verbs like New, Start, Stop, Set, Reset, Restart that change system state should support 'ShouldProcess'.</value>
662662
</data>
663663
<data name="UseShouldProcessForStateChangingFunctionsError" xml:space="preserve">
664664
<value>Function ’{0}’ has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.</value>

Rules/UseShouldProcessForStateChangingFunctions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4444
string funcName = funcDefAst.Name;
4545
bool hasShouldProcessAttribute = false;
4646

47-
if (funcName.StartsWith("Stop-", StringComparison.OrdinalIgnoreCase)||
47+
if (funcName.StartsWith("Restart-", StringComparison.OrdinalIgnoreCase) ||
48+
funcName.StartsWith("Stop-", StringComparison.OrdinalIgnoreCase)||
4849
funcName.StartsWith("New-", StringComparison.OrdinalIgnoreCase) ||
4950
funcName.StartsWith("Set-", StringComparison.OrdinalIgnoreCase) ||
5051
funcName.StartsWith("Update-", StringComparison.OrdinalIgnoreCase) ||
@@ -75,7 +76,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7576
if (!hasShouldProcessAttribute)
7677
{
7778
yield return
78-
new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture,Strings.UseShouldProcessForStateChangingFunctionsError, funcName),funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
79+
new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture,Strings.UseShouldProcessForStateChangingFunctionsError, funcName),funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
7980
}
8081
}
8182
}

0 commit comments

Comments
 (0)